Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog. Structure(complex term) vs predicate, i dont really get the difference

Tags:

prolog

I'm new to prolog and i cant seem to understand the difference between a structure and a predicate. Is there really any difference?

While digging around i found that some people consider f(X):-a(X) to be a predicate and some consider jealous(X,Y):-loves(X,Z), loves(Y,Z) to be a structure (or a complex term). They look pretty much the same to me.

Somebody care to explain?

like image 481
K.Tsanov Avatar asked Mar 10 '15 19:03

K.Tsanov


1 Answers

In Prolog, a term is a constant, atom, variable, or compound term.

A compound term consists of a functor with 1 or more arguments. The following are terms:

a.          % a term with functor 'a' and 0 arguments
a(b,c).     % a term with functor 'a' and 2 arguments, b and c

The empty list [] is term and, more specifically, an atom. A list [H|T] is represented intrinsically as '.'(H, T) and is, therefore a compound term and, therefore, also a term.

You can also have more complex compound terms:

a(b(c,d), e(f,g(h)))

Here, a is a functor with two arguments: b(c,d), and e(f,g(h)), and so on.

A compound term can also be called a structure as they give you a way to structure facts:

customer(name(john,doe), address(street('123 Main St'), city('Framusville'), ...), ...).

A predicate clause is a specific structure or term. In Prolog, everything is a structure or term of the form: functor(arg1, arg2, ...).

Let's look at the predicate clause:

f(X) :- a(X).

It is itself a structure which internally is represented as this term: :-(f(X), (a(X))). The period (.) is a terminator. What makes it a predicate, as @false indicates, is:

  • It's at the "top level" (not an argument in a higher level term)

  • It's functor is :-

A predicate clause is also referred to as a rule since the the term, :-(A, B) defines the relation: A is true if B is true. The term f(X) is referred to as the head of the predicate clause.

A collection of one or more predicate clauses which all have the same functor and arity (number of arguments) for their heads is referred to as a predicate.

Looking at your second example:

jealous(X,Y) :- loves(X,Z), loves(Y,Z).

This is also one predicate clause for the predicate jealous/2 (the predicate whose functor is jealous and has arity of 2). It would internally be expressed as this compound term: :-(jealous(X,Y), ','(loves(X,Z), loves(Y,Z))). This means that the above expression is also a compound term.

You can see how Prolog views an expression in its canonical form by using write_canonical/1:

| ?- write_canonical((jealous(X,Y) :- loves(X,Z), loves(Y,Z))).
:-(jealous(_17,_18),','(loves(_17,_22),loves(_18,_22)))

The SWI Prolog site has a very good glossary of Prolog terms.

like image 77
lurker Avatar answered Nov 07 '22 22:11

lurker