Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Prolog do if you X = f(X)?

What does it mean in Prolog comparing a predicate to an unbound variable?

like image 985
Pedro Montoto García Avatar asked Apr 17 '15 21:04

Pedro Montoto García


People also ask

What does is do in Prolog?

is. is , evaluation The is built-in predicate is used in Prolog to force the evaluation of arithmetic expressions. If you just write something like X = 2 + 4 , the result is to bind X to the unevaluated term 2 + 4 , not to 6 . Example: ?- X = 2 + 4. X = 2+4.

What are rules in Prolog give example?

A rule in Prolog is a clause, normally with one or more variables in it. Normally, rules have a head, neck and body, as in: eats(Person, Thing) :- likes(Person, Thing), food(Thing). This says that a Person eats a Thing if the Person likes the Thing , and the Thing is food .

How do you do or in Prolog?

Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon: registered(X, Y) :- X = ct101; X = ct102; X = ct103. Save this answer.

How do you query in Prolog?

Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact that matches the goal, then the query succeeds and the listener responds with 'yes. ' If there is no matching fact, then the query fails and the listener responds with 'no.


2 Answers

In Prolog, (=)/2 it not a comparison, but a fundamental operation, called unification.

The expression you show in your question title, if called when X is a free variable, will create a cyclic term. In SWI-Prolog

?- X=f(X),write(X).
@(S_1,[S_1=f(S_1)])
X = f(X).

Cyclic terms are problematic to handle, usually are created by programming errors: the behaviour of SWI-Prolog (and others) can be controlled using a global flag, see occurs_check.

like image 147
CapelliC Avatar answered Sep 20 '22 11:09

CapelliC


The result of the X = f(X) goal depends on the Prolog implementation, In some systems, as Carlo pointed out in his answer, the result can be controlled by a user settable flag. The unification predicate, (=)/2, may be implemented with or without what's called the occurs check. This check verifies if a variable in one operand occurs in a sub-term in the other operand. When the unification predicate implements this check, the goal X = f(X) fails. But, for performance reasons, the unification predicate is often implemented without this check. The ISO Prolog standard specifies an alternative unification predicate, aptly named unify_with_occurs_check/2, that can be used when goals such as this one can lead to trouble.

Nowadays, several implementations support the cyclic terms, also known as rational terms, that are created by goals such as X = f(X). These include CxProlog, ECLiPSe, SICStus Prolog, SWI-Prolog, and YAP. Note, however, that the level of support for rational terms varies across system. The minimal support will be (1) to be able to create rational terms (without a stack overflow!), (2) to be able to unify two rational terms, and (3) to be able to print query bindings that include rational terms in a non-ambiguous way. With these three features you can e.g. implement coinductive logic programming, which is useful for several classes of problems.

like image 31
Paulo Moura Avatar answered Sep 23 '22 11:09

Paulo Moura