User Tools

Site Tools


prolog:syntax

Syntax of Prolog

The syntax of Prolog is based on First-Order Predicate Calculus.

Constants - individual names

To denote individual objects their names are used. An individual name is a string starting with a lower-case letter. They can be composed of letters and digits only. If you need/prefer to start the name with an upper-case letter, or use some special characters use quotes.

Here are some example individual names: alpha, bravo, delta,

john_doe, john_Doe, johnDoe, 'John Doe', 'John_Doe', 'John-Doe'.

These examples shows also the way of composing more complex names from several components.

Variables

Variables in Prolog are different from variables in other popular languages. They play the following three roles:

  • they represent individual objects not specified for some purpose,
  • they represent sets of objects in universal statements since in fact they are universally quantified,
  • they represent coreference constraints; if an occurence of a variable is substituted with some term, all other occurrences of this variable (within a clause) must be substituted with the same term,
  • they book place for arguments in terms and atomic formulae.

Variable names start with an upper-case letter or the underscore. Here are some examples:

  • X, Y, Z,
  • Alpha, Bravo, Delta,
  • FirstVariable, SecondVariable, ThirdVariable.

A variable can be substituted (unified) with an expression (a term). This means that it is bound to the specific value of it. If so, it cannot be assigned any other value unless the assignment is annulled; this happens during backtracking.

Two variables can be bound. This means that if one of them is substituted with a term, the other one is substituted with the same term as well.

Terms

Terms are special expressions for representing complex objects. All terms have internal structure of a tree.

A legal term is:

  • any constant,
  • any variable,
  • if f is a functional symbol of arity n and t1, t2, … tn are terms, then f(t1,t2,…,tn) is a (compound) term.

Note that the definition of terms is recursive.

Some examples of terms are as follows:

  • a, b, X, Y, f(a), f(b), f(X), f(f(a), f(f(X)),
  • g(a,a), g(a,b), g(a,X), g(a,f(b)), g(f(a),g(a,f(X))),
  • book(author,title), person(first_name,last_name,date_of_birth,address(zip_code,city,street,number)).

All terms have the internal structure of a tree, where the functional symbol corresponds to the root, and its arguments correspond to its subtrees.

Terms are used to represent and manipulate complex data structures, such as record-like structures, lists, trees, etc.

A more detailed presentation on variables and terms can be found here Prolog variables and terms.

Facts

Facts or atoms represent simple statements in Prolog. Any fact is of the form

p(t1,t2,…,tn).

where p is a relational (or predicate) symbol and t1, t2, … tn are terms. The meaning of p(t1,t2,…,tn). is that relation (property) p holds for arguments t1, t2, … tn. All facts are followed by the full stop.

Here are some examples of facts:

run.
man(peter).
brother(peter,john).
lives(john,address(zip_code,city,street,number)).
equal(X,X).

Facts are structurally equivalent to terms. However, they have different meaning, application and position in Prolog programs. While terms are used as arguments of facts (atoms) and represent objects, facts are independent statements of chunks of knowledge. All facts specified within Prolog code are assumed to be true.

Facts sharing the same functional symbol must be place together within a program.

Clauses

Clauses are used to represent inference rules. Every clause has a head (h) and a body (b). The body can be empty; such a clause is a fact. Clause are written as h:- b. More precisely, every clause has the following form:

h:- p1,p2,...,pn.

where h is its head, and p1, p2, …, pn are components of its body. Both h and p1, p2, …, pn must be legal atoms.

A clause in Prolog is interpreted as: h if p1 and p2 and … and pn.

Clauses sharing the same head predicate symbol must be place together within a program.

As an example of a clause defining the relation of being a brother consider the following code excerpt:

parent(john,tom).
parent(john,ted).
parent(john,eva).
man(tom).
man(ted).
woman(eva).

brother(B,X):-
   parent(P,B),
   parent(P,X),
   man(B),
   B \= X.

After compilation Prolog produces the following results for the goal brother(B,X):

?- [brother].
% brother compiled 0.00 sec, 1,436 bytes

Yes
?- brother(B,X).

B = tom
X = ted ;

B = tom
X = eva ;

B = ted
X = tom ;

B = ted
X = eva ;

No
prolog/syntax.txt · Last modified: 2022/01/17 18:29 (external edit)