Fundamentals of Prolog Prof. Geraint A. Wiggins Centre for Cognition, Computation and Culture Goldsmiths College, University of London Contents Summary of Lecture 1 What makes a good Prolog program? What makes a bad Prolog program? More about Prolog clause syntax More about Prolog guery syntax Proof strategy
Summary of Lecture 1 Course structure Examinations, practicals, books Prolog in English Using the Prolog system Fundamental Prolog syntax Predicates Clauses Heads & Bodies Atoms & Numbers Variables Full-stops (periods) Goldsmiths College, University of London 2
What makes a good Prolog program? In order of decreasing importance: correctness & careful design clarity & comments speed (at least for our purposes here) Because we understand the logical meaning of logic programs we can rely on the computer to transform elegant but inefficient programs into ugly (but invisible) efficient ones Goldsmiths College, University of London 3
What makes a bad Prolog program? In no particular order: hacking undesigned code at the terminal using obscure or meaningless variable and predicate names not commenting code abusing the procedural aspects of Prolog to produce logically meaningless code Goldsmiths College, University of London 4
More about Prolog clause syntax (1) Recall that a Prolog program is made up of predicates... and a predicate is made up of clauses. A clause has a head and maybe a body. The head of a clause always takes one of two forms: predname predname( argument1, argument2,...) If the predicate is always true, there is no body, and we finish with a full-stop (period). Goldsmiths College, University of London 5
More about Prolog clause syntax (2) If the predicate is conditional, we connect it to a body of subgoals with the if operator, :- Each subgoal is a Prolog literal... which is either like a head, as before; or a negated literal, written \+ predname \+ predname( argument1,...) Note the space after the \+ operator We will return to \+ later Goldsmiths College, University of London 6
More about Prolog clause syntax (3) We can combine literals in Prolog clauses with the operators, (comma) meaning and conjunction ; (semicolon) meaning or disjunction, (and) binds more strongly than ;, so a, b ; c means that either both a and b are true, or c is true You should never need to use ; in a program because you can express disjunction via multiple clauses. It makes Prolog compilation less effective and your program less clear Goldsmiths College, University of London 7
More about Prolog clause syntax (4) There is also -> (minus, greater than) supposedly meaning implies You should never need to use ->, and it is best avoided because it does not mean the same as logical implies and can therefore beconfusing We can make complex expressions using brackets ( l1( a1, a2 ); l2( a3 )), l3( a1 ) l1( a1, a2 ); ( l2( a3 ), l3( a1 )) l1( a1, a2 ); l2( a3 ), l3( a1 ) Goldsmiths College, University of London 8
More about Prolog query syntax We run a Prolog program by posing a query Prolog query syntax is the same as the syntax of the clause body Literals in queries can be combined with and, or, not and implies, just as in programs Goldsmiths College, University of London 9
Proof strategy Prolog answers queries by attempting to prove them true Suppose we have consulted the ancestor/2 program: ancestor/2 is defined as ancestor( A, B ) :- parent( A, B ). ancestor( A, B ) :- parent( A, C ), ancestor( C, B ). Suppose parent/2 is defined as parent( alan, clive ). parent( clive, dave ). and we ask the question ancestor( alan, dave ). Goldsmiths College, University of London 10
Proof strategy (2) To prove this, prolog starts at the top of the database, and tries to find a predicate called ancestor; Then it looks at each clause in turn, and tries to unify its head with the goal; Once unification is complete, it attempts to prove the literals in the body, in order of appearance Goldsmiths College, University of London 11
Unification Unification works by comparing the structure of terms and literals: First compare the predicate/functor name; Then, for each argument; If both of the two unificands are variables, then make them the same variable, and unification succeeds; Otherwise, if one unificand is a variable, then set it to the value of the other, and unification succeeds; Otherwise, if both unificands are atomic and are the identical, unification succeeds; Otherwise, unification succeeds if the two arguments can be unified Otherwise, unification fails Goldsmiths College, University of London 12
Proof Strategy (3) Prove: ancestor( alan, dave ) Find: ancestor clause 1 Unify: A = alan, B = dave Prove: parent( alan, dave ) FAIL Try again: Find: ancestor clause 2 Unify: A = alan, B = dave Prove: parent( alan, C ) Find: parent clause 1 Unify: C = clive SUCCEED Next goal: Prove: ancestor( clive, dave ) Find: ancestor clause 1 Unify: A = clive, B = dave Prove: parent( clive, dave ) SUCCEED Answer: yes. Goldsmiths College, University of London 13
Summary In this lecture, we have covered: Prolog style More Prolog syntax Prolog standard proof (search) strategy Goldsmiths College, University of London 14