Prolog Operator =:=

2020-02-17 05:29发布

There are some special operators in Prolog, one of them is "is", however, recently I came across the =:= operators, and I have no idea how it works.

Can someone explain what the operator does, and also where can I find a predefined list of such special operators and what they do?

Thanks.

标签: prolog
7条回答
虎瘦雄心在
2楼-- · 2020-02-17 05:44

Its an ISO core standard predicate operator, which cannot be bootstrapped from unification (=)/2 or syntactic equality (==)/2. It is defined in section 8.7 Arithmetic Comparison. And it basically behaves as follows:

E =:= F :- 
    X is E, 
    Y is F, 
    arithmetic_compare(=, X, Y).

So both the left hand side (LHS) and right hand side (RHS) must be arithmetic expressions that are evaluted before they are compared. Arithmetic comparison can compare across numeric types. So we have:

   GNU Prolog 1.4.5 (64 bits)

   ?- 0 = 0.0.
   no

   ?- 0 == 0.0
   no

   ?- 0 =:= 0.0.
   yes
查看更多
▲ chillily
4楼-- · 2020-02-17 05:59

First operator =:= is check equal ? for example enter image description here

it"s return true. but this returns false enter image description here

查看更多
该账号已被封号
5楼-- · 2020-02-17 06:02
?- 2+3 =:= 6-1.
true.

?- 2+3 is 6-1.
false.

Also please see docs http://www.swi-prolog.org/pldoc/man?predicate=is/2

查看更多
forever°为你锁心
6楼-- · 2020-02-17 06:04

I think the above answer deserves a few words of explanation here nevertheless.

A short note in advance: Arithmetic expressions in Prolog are just terms ("Everything is a term in Prolog"), which are not evaluated automatically. (If you have a Lisp background, think of quoted lists). So 3 + 4 is just the same as +(3,4), which does nothing on its own. It is the responsibility of individual predicates to evaluate those terms.

Several built-in predicates do implicit evaluation, among them the arithmetic comparsion operators like =:= and is. While =:= evaluates both arguments and compares the result, is accepts and evaluates only its right argument as an arithmetic expression.

The left argument has to be an atom, either a numeric constant (which is then compared to the result of the evaluation of the right operand), or a variable. If it is a bound variable, its value has to be numeric and is compared to the right operand as in the former case. If it is an unbound variable, the result of the evaluation of the right operand is bound to that variable. is is often used in this latter case, to bind variables.

To pick up on an example from the above linked Prolog Dictionary: To test if a number N is even, you could use both operators:

0 is N mod 2  % true if N is even
0 =:= N mod 2 % dito

But if you want to capture the result of the operation you can only use the first variant. If X is unbound, then:

X is N mod 2   % X will be 0 if N is even
X =:= N mod 2  % !will bomb with argument/instantiation error!

Rule of thumb: If you just need arithmetic comparison, use =:=. If you want to capture the result of an evaluation, use is.

查看更多
Bombasti
7楼-- · 2020-02-17 06:05

=:= is a comparison operator.A1 =:= A2 succeeds if values of expressions A1 and A2 are equal. A1 == A2 succeeds if terms A1 and A2 are identical;

查看更多
登录 后发表回答