Introduction

History Relationship to other languages The simplicity of Prolog relative to other languages The principles of running Prolog


The Core of the Language

Terms

In Prolog all data objects are called terms

Atomic terms

Function terms

Variables

Predicates

A predicate (called an atom in logic) has the form <predicate symbol>(<term>{,<term>}).

Prolog programs

Programs are made up of Horn clauses, which in turn are made up of predicates. The closest imperative structure is a 'Boolean function'. Propositional facts Facts with no variable arguments. Propositional rules Rules with no variables

Queries

Queries, in the form of a clause body, are entered at the ?- prompt, and end with a .

Backtracking

There may be more than one version of a clause. The first version (in source code order) is used when the clause is queried. If the first version fails, then the second is tried, and so on. The query fails if none of the versions succeed. This may lead to alternatives further back being tried, and so on. This process is called backtracking. As well as finding a solution, backtracking can also find alternative solutions. After a solution is given, the user can ask for alternatives to be found. The next alternative of the last call that succeeded, is used. If it does not have any alternatives, then calls further back are tried successively. A ; is used to get alternative answers.

Variables

The only way to introduce variables is in a formal or actual argument list. Variables are local to their clause, and each time a clause is used a new set of variables are allocated. The new variables are typically appended with a unique number to make them distinct and local. Clauses with variables Variables can be in one of two states - bound (have value) or unbound (no value). Variables are initially unbound. Bound variables cannot have their values reset. However bound variables can become unbound. The binding of variables occurs in a proccess called unification. Two values/variables unify iff When a clause is queried, the actual and formal arguments must pairwise unify.

Query with unification

As well as answering yes or no, the bindings of any variables in the original query are printed by the interpreter. When another version of a clause is used in backtracking, any variable bindings done in the use of the previous version are undone.

Query with variables and backtracking on failure

Query with variables and backtracking on success Consider what happens if the tail is reordered Consider what happens if the tail is re-ordered and the has_studied clauses are swapped! Notice the tree like nature of the search for alternative solutions.

Collecting Successes

There are meta-predicates for collecting all solutions, the simplest of which is findall. findall takes three arguments: the variable or term to collect, the query, and a variable in which a list of the collected results are placed.

Output

A side effect that always succeeds

Input

A side effect that always succeeds

Comments

/*----This is a comment */
%----This is a comment

Example to Run

%------------------------------------------------------------------------------
make_tree(Tree):-
    read(Word),
    insert(Word,Tree).    %----Note recursion for looping
%------------------------------------------------------------------------------
insert(exit,_).

insert(Word,Tree):-
    insert_word(Word,Tree),
    make_tree(Tree).
%------------------------------------------------------------------------------
insert_word(Word,node(Word,_,_)).

insert_word(Word,node(NodeWord,Smaller,_)):-
    ordered(Word,NodeWord),
    insert_word(Word,Smaller).

insert_word(Word,node(NodeWord,_,Larger)):-
    ordered(NodeWord,Word),
    insert_word(Word,Larger).
%------------------------------------------------------------------------------
ordered(Big,Small):-
    Big @> Small.
%------------------------------------------------------------------------------

Tutorial

Write a program that stores information about your family, and will answer queries about relationships. Information about individual people must be stored as facts containing the persons name, sex and parents' names, e.g.
person(geoff,male,bob,margaret).
Here are the queries you should be able to answer ... Answer


Programming Technique

Programming style

Layout Commenting Clauses Predicates

Debugging

CALL, EXIT, REDO, FAIL

Tutorial

Use the debugging facilities to follow the execution of your programs.