What is the Prolog operator `^` (“caret”)?

前端 未结 4 514
[愿得一人]
[愿得一人] 2020-12-10 13:26

What is the Prolog operator ^ ?

Looking at The Prolog Built-in Directive op gives a list of the built-in operators.

I see

  • **
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 14:06

    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.

提交回复
热议问题