What is the Prolog operator ^?

≡放荡痞女 提交于 2019-12-05 06:16:54

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.

false

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.

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 "^"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!