The following notes aim just at giving a rough (and somewhat not formal) idea of how and why the programming language Prolog works. Prolog is a programming language based on a fragment of first-order predicate logic.
Formulas of first-order logic are formed using basic predicates (and terms),
logical connectives and quantifiers. In the following a generic formula will be denoted by A,B, etc. We write A(X) to represent that the formula A contains a predicate which uses a variable X. In first-order logic, formulas can be universally or existentially quantified:
Given a formula A it is not possible, in general, to decide whether there is a proof for A (i.e. A is derivable) in first-order logic. In other words, there is no algorithm whatsoever which, given a formula A, terminates telling us if A is derivable or not (or, equivalently, if A is valid or not). The following are some of the rules of first-order logic that can be used to prove formulas: Modus Ponens:
A A → B
∀-Elimination:
------------------- B
∀X.A(X)
∃-Introduction:
-------------- (where t can be any term) A(t)
A(t)
∧-Introduction :
--------------- ( where t can be any term) ∃X.A(X)
A B
------------------- A ∧ B Even if first-order logic is undecidable, there are however fragments of it that are decidable. Other ones, even if not decidable, allow for semi-decision algorithms that are extremely efficient. One of these is the fragment where we consider only formulas with the following shape
(A1 ∧ A2 ∧ .... ∧ An) → B
or with the shape
C
where
Notice that instead of considering the above quantifications, we can consider all the variables in C and (A1 ∧ A2 ∧ .... ∧ An) → B to be universally quantified (since in first order logic, when Y is not a free variable in Q, ∀X.( (∃Y.P)→Q ) is logically equivalent to ∀X,Y.( P→Q), see (5) of Proposizione 2 in [AU] ). In Prolog, a formula (A1 ∧ A2 ∧ .... ∧ An) → B is represented as B :- A1, A2,..., AnAn example of Horn Clause in Prolog is c(X,f(Y)) :- a(X,Y), b(g(Z),s), d(Z,Z).(Horn Clauses in Prolog always terminate with a dot '.') In the above formula all the variables are considered to be quantified as explained before. So the corresponding formula in logic notation would be
∀X,Y.( ∃Z. (a(X,Y) ∧ b(g(Z),s) ∧ d(Z,Z) ) → c(X,f(Y)) )
If one takes a set of Horn clauses H1,..,Hn, and a formula of the form
∃X1,..,Xk.B
where B is an atomic formula there exists an algorithm which, in case this formula is derivable from the hypotheses H1,..,Hn, finds a proof for it in an efficient way (such algorithm is called Resolution Algorithm). Even more, it can find out all the possible proofs if there is more than one. In Prolog the formula ∃X1,..,Xk.B is represented simply by B and it is usually written after the prompt of the interpreter '?-', as follows ?- B.B is called Goal. Let us consider a few clauses, like in [ACa,], in particular the facts animal(lion). animal(dog). animal(sparrow). animal(seagull). has_feathers(sparrow). has_feathers(seagull).and the clause bird(X) :- animal(X), has_feathers(X).If we make the following query to the Prolog interpreter ?- bird(Z)is like asking the Prolog interpreter for a proof of
Γ,∀X.(has_feathers(X) ∧ animal(X) → bird(X) ) |- ∃Y. bird(Y)
where Γ is the set of the above facts.The interpreter finds a proof of it, something like THIS. and returns us what the possible bird is, namely sparrow. Actually the Prolog interpreter finds all the possible proofs for
Γ,∀X.(has_feathers(X) ∧ animal(X) → bird(X) ) |- ∃Y. bird(Y)
so it also finds
THIS and tell us that another possible bird is seagull.
Notice that we can also compute functions in Prolog, since we can look at a function as a relation. For instance fact can be used to represent the relation such that fact(X,Y) holds whenever X is the factorial of Y. The Prolog program for the factorial consists in the fact fact(0,1) and in the clause stating that fact(X,Y) holds if Y is the multiplication of X with the factorial of X-1. |