Upload
natalie-harmon
View
212
Download
0
Embed Size (px)
Citation preview
Logic Programming and Prolog
• Goal: use formalism of first-order logic• Output described by logical formula (theorem)• Input described by set of formulae (axioms)• Computation is verification that output can be formally derived
from input• Computation engine is theorem prover• Side-effect of computation is a set of bindings of logical
variables to terms, which describes desired result.
Biblical Prolog
father (adam, abel). % an assertion (a fact) father (adam, cain). brother (X, Y) :- father (Z, X), father (Z, Y). % a rule ? :- father (adam, abel) % a queryyes % response ? :- father (X, abel) X = adam ? :- father (cain, abel)no ? :- brother (abel, Z)Z = cain ? :- mother (eve, abel)no % no is non-provability, not falsehood
The syntax of logical formulae
• Terminals: literals, variables, function symbols, punctuation
• Productions:• term ::= literal | variable | function_symbol ( terms )• terms ::= term | term terms• simple_clause ::= term | term , simple_clause• clause ::= simple_clause | term :- simple_clause• program ::= clauses• comma is conjunction: and• :- is reverse implication: iff
Substitutions and unification
• Computation perfoms unification between terms, by generating substitutions on variables. Computation halts when no further unifications are possible
• Result is final set of substitutions, mapping variables to terms• Unify father (adam, cain) with father (X, cain) by unifying X with
adam.– Result is { X/adam }
• Unify father (adam, cain) with father (X, Y)– Result is { X/adam, Y/cain }
• Unify father ( adam, cain ) with father ( X, X )– Result is { } : failure
Unification algorithm
• To unify two terms t1 and t2: t1 = t2
f (s1, s2,..) = f (t1, t2..)
Replace equation with s1 = t1, s2 = t2…
f (s1, s2..) = g(t1, t2..) where f /= g
fail
v = v
delete equation
v = t where t is a term not containing v
Apply the substitution v / t everywhere
v = t where t is a term that contains v
fail
The meaning of a clause
• P (x1, x2…) :- q1 (s1, s2..), q2 (t1, t2..) ..
• Logical (declarative) interpretation: the predicate P is true (satisfiable) on arguments x1, x2.. If each predicate qi can be shown to be satisfiable
• Procedural interpretation: a call to procedure P is executed by calling in turn q1, q2… (subgoals)
• Several unifications may be possible at a given time: selection is non-deterministic
• If unification fails, computation backtracks
Lists
• Terms are sufficient for everything, but need special syntax for simple recursive structures
• [X | Y] designates list with head X and tail Y – (special term for function symbol cons)
• [X| _ ] indicates that tail of list is irrelevant:– _ is variable that needs no binding
Backtracking and brute force search
• color (C):- member (C, [green, red, blue, yellow])• member (X, [X | _ ]).• member X [ _ |Y]) :- member (X, Y).• next (R1, R2) :- color (R1), color (R2), R1 \= R2.
• Graph to be colored is described by adjacency terms:
• ?:- next(R1, R2), next (R1, R3), next (R1, R4), next (R2, R3), next (R2, R4), next (R3, R4)
1234
reversibility
• Predicate can be used as generator: member (X, [X | _ ]). member X [ _ |Y]) :- member (X, Y). color (C):- member (C, [green, red, blue, yellow]) ?:- color (C)C = green; % semicolon forces backtrackingC = read; % second choiceC = blueC = yellow;no % no further backtracking possible
arithmetic
• Ideal: symmetric handling of queries
factor (X, Y, Z) :- X = Y * Z
?:- factor (10, 2, 5)
Yes
?:- factor (15234512, Y, Z) % you wish!
In practice: cannot do arithmetic on uninstantiated variables
arithmetic and binding
• Z is X + Y succeeds if X and Y are instantiated and Z is uninstantiated
• Z is Z + 1 always fails
fact (0, 1).
fact (N, M) :- N1 is N -1, fact (N1, M1), M is N * M1