Logic Programming

Prolog (2)

The Fundamentals of Prolog

Prolog is made up of facts and rules. Facts determine the nature of an object. Rules are properties that can be derived from the relations.

Facts:
The object determined by a fact, can belong to a certain category. For example "John is a male", can be coded as male(john). Facts also represent relationships which are true in a given domain. These facts can involve more that one object. For example the statement parent(john, tom). This means that there exists a relation between Tom and John called parent. The order of the clauses (objects) in the relation matters. This statement means that John is a parent of Tom, but the reverse is not true.

Once several facts have been established, questions can be asked about the relations. For example a question ?- parent(john,tom) would generate yes, but ?- parent(jane,tom) would generate no, since no fact states anything about Jane with respect to Tom. The questions can also be combined where the answer depends on several things being true.

Rules:
Rules are properties that can be derived from the relations. A new relation can be concluded from the relations that already exist. For example a new relation John is Tom's father must imply that John is a parent of Tom and John is a male, provided that the relations "parent" and "male" are already defined.

This can be coded as follows; father(john,tom) :- male(john), parent(john,tom). The symbol ":-" means "if". In this context if the RHS is true the LHS is also true.
The comma in between represents the conjunction of computations. It means that both relations male and parent have to be true in order to conclude relation father.

Variables:
A variable represents an object in the circumstances where name is irrelevant. What matters, is whether the object that meets the criteria exists or not. This means that questions such as Who are John's children? can be asked. For example the following code: ?- parent(john,X) would mean is there an X such that John is a parent of X. If X exists, Prolog will return one of the possible values of X, otherwise the answer will be no. If asked, Prolog can also return all other possible answers, if they exist. Prolog can distinguish between variables and the names of particular objects, since any name beginning with a capital letter is taken to be a variable.