bfbad.blogg.se

Prolog write
Prolog write






prolog write

So far, so good! This has all been a purely syntactical feature. You can use write_canonical/1 to verify this: In both cases, the functor of the term is =:=, and the arity of the term is 2. This means that you can, if you want, write a term like =:=(X, Y) equivalently as X =:= Y. This means that it is a binary infix operator. This means that the operator =:= has precedence 700 and is of type xfx. For example: ?- current_op(Prec, Type, =:=). In Prolog, we can use the predicate current_op/3 to learn more about operators. If you want to capture the result of an evaluation, use is.Ĭomplementing the existing answers, I would like to state a few additional points: An operator is an operatorįirst of all, the operator =:= is, as the name indicates, an operator. Rule of thumb: If you just need arithmetic comparison, use =:=. X =:= N mod 2 % !will bomb with argument/instantiation error! If X is unbound, then: X is N mod 2 % X will be 0 if N is even 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īut if you want to capture the result of the operation you can only use the first variant. is is often used in this latter case, to bind variables. If it is an unbound variable, the result of the evaluation of the right operand is bound to that variable.

prolog write

If it is a bound variable, its value has to be numeric and is compared to the right operand as in the former case.

prolog write

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. While =:= evaluates both arguments and compares the result, is accepts and evaluates only its right argument as an arithmetic expression. Several built-in predicates do implicit evaluation, among them the arithmetic comparsion operators like =:= and is. It is the responsibility of individual predicates to evaluate those terms. So 3 + 4 is just the same as +(3,4), which does nothing on its own. (If you have a Lisp background, think of quoted lists). I think the above answer deserves a few words of explanation here nevertheless.Ī short note in advance: Arithmetic expressions in Prolog are just terms ("Everything is a term in Prolog"), which are not evaluated automatically.








Prolog write