问题
What is the Prolog operator ^ ?
Looking at The Prolog Built-in Directive op gives a list of the built-in operators.
I see
** is exponentiation
/\ is or
but what is ^ ?
Each of the three current answers are of value and I learned something:
- Roy for the book
- false for the examples
- I accepted the answer by CapelliC because it made clear that ^/2 has multiple meanings
depending on context which instantly cleared up my confusion.
回答1:
In Prolog, most symbols can be used 'uninterpreted', at syntactic level, in particular after a op/3 declaration, any atom can be used as operator. Then you can use, for instance, ^/2, as a function constructor for a domain specific language (a DSL), with a semantic specified from your rules.
Is SWI-Prolog (or more generally in ISO Prolog), current_op/3 gives you information about declared operators:
?- current_op(X,Y,^).
X = 200,
Y = xfy.
That said, any Prolog implementing setof/3, is expected to interpret ^/2 as a quantification specifier, when put to decorate the 2° argument. As well, any Prolog implementing is/2, is expected to interpret ^/2 as exponentiation, when occurring is the right side of the is/2 expression.
回答2:
The operator (^)/2
serves several purposes:
setof/3
, bagof/3
Here it is used to denote the existential variables (set) of a term. Like in
setof(Ch, P^child_of(Ch,P), Chs)
where P
is declared as an existential variable.
As a non-standard side effect to this, many systems have defined it as predicate with the following definition:
_^Goal :- Goal
But then, others do not have such a definition. It is in any case a good idea to avoid to define a predicate (^)/2
.
(^)/2
- power
This is an evaluable functor accessible via (is)/2
and arithmetic comparison like (=:=)/2
and (>)/2
. Also library(clpfd) uses it with this meaning. In contrast to (**)/2
which always results in a float, 2^2
is an integer - thereby permitting arithmetics with bigints. Just try ?- X is 7^7^7.
to see if your system supports them.
Finally, there are user defined uses for (^)/2
that do not collide with above uses like lambda expressions via library(lambda) (source).
There are a few general remarks about its use. (^)/2
associates to the right which means that:
(7^7^7) = (7^(7^7))
. It has a very low priority which means that you have to use brackets for arguments with standard operators.
回答3:
In math expressions, ^ is exponentiation, it's just different notation for **.
In lambda expressions, it is a parameter-passing operator.
As in Pereira and Shieber's book:
Thus the lambda expression λ x. x + 1 would be encoded in Prolog as X^(X+1). Similarly, the lambda expression λ x. λ y.wrote(y, x) would be encoded as the Prolog term X^Y^wrote(Y,X), assuming right associativity of "^"
来源:https://stackoverflow.com/questions/19931801/what-is-the-prolog-operator