Operators For A Controlled Natural Language
First we’ll need a code base; let’s start with some classic family tree facts and rules. We’ll keep this here for later comparison.
?-
op(+Precedence, +Type, :Name)
Let’s declare our operators! Precedence determines the order of evaluation; SWI-Prolog has the precedence for built-in operators in their docs. Type is how the operator is evaluated: xf
, yf
, xfx
, xfy
, yfx
, fy
, or fx
, where f
is a functor, x
is an argument, and y
is an argument or lower-precedence functor.
Let’s turn our predicates into operators and enjoy querying in a somewhat more natural way. Precedence for and
as well as or
is taken to be the same as that of ,
and ;
. Note how we can then use the operators in our declarations of facts and rules.
?-
The Good, the Bad, and the Ugly
Just because we can do something doesn’t mean we should! Personally, I appreciate this representation, but I understand that some people prefer the standard syntax. Declaring operators is especially useful when working within a domain that already uses them, such as logic — especially if your implementation of Prolog supports Unicode. However, the most common use seems to be to develop a structure for passing data around.