Download presentation
Presentation is loading. Please wait.
Published byNatalie Harmon Modified over 9 years ago
1
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.
2
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 query yes % 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
3
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
4
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
5
Unification algorithm To unify two terms t1 and t2: t1 = t2 f (s 1, s 2,..) = f (t 1, t 2..) Replace equation with s 1 = t 1, s 2 = t 2 … f (s 1, s 2..) = g(t 1, t 2..) 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
6
The meaning of a clause P (x 1, x 2 …) :- q 1 (s 1, s 2..), q 2 (t 1, t 2..).. Logical (declarative) interpretation: the predicate P is true (satisfiable) on arguments x 1, x 2.. If each predicate q i can be shown to be satisfiable Procedural interpretation: a call to procedure P is executed by calling in turn q 1, q 2 … (subgoals) Several unifications may be possible at a given time: selection is non-deterministic If unification fails, computation backtracks
7
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
8
Backtracking and brute force search color (C):- member (C, [green, red, blue, yellow]) member (X, [X | _ ]). member X [ _ |Y]) :- member (X, Y). next (R 1, R 2 ) :- color (R 1 ), color (R 2 ), R 1 \= R 2. Graph to be colored is described by adjacency terms: ?:- next(R 1, R 2 ), next (R 1, R 3 ), next (R 1, R 4 ), next (R 2, R 3 ), next (R 2, R 4 ), next (R 3, R 4 ) 12 3 4
9
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 backtracking C = read; % second choice C = blue C = yellow; no % no further backtracking possible
10
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
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.