Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

lambda - What is the Prolog operator `^` ("caret")?

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...