1.38k likes | 1.4k Views
Programming Languages (CSCI 4430/6430) Part 3: Logic Programming: Summary. Carlos Varela Rensselaer Polytechnic Institute December 8, 2017. An Early (1971) “ Conversation ”. USER: Cats kill mice. Tom is a cat who does not like mice who eat cheese. Jerry is a mouse who eats cheese.
E N D
Programming Languages (CSCI 4430/6430)Part 3: Logic Programming: Summary Carlos Varela Rensselaer Polytechnic Institute December 8, 2017 C. Varela
An Early (1971) “Conversation” USER: Cats kill mice. Tom is a cat who does not like mice who eat cheese. Jerry is a mouse who eats cheese. Max is not a mouse. What does Tom do? COMPUTER: Tom does not like mice who eat cheese. Tom kills mice. USER: Who is a cat? COMPUTER: Tom. USER: What does Jerry eat? COMPUTER: Cheese. USER: Who does not like mice who eat cheese? COMPUTER: Tom. USER: What does Tom eat? COMPUTER: What cats who do not like mice who eat cheese eat. C. Varela
Logic programming • A program is a collection of axioms, from which theorems can be proven. • A goal states the theorem to be proved. • A logic programming language implementation attempts to satisfy the goal given the axioms and built-in inference mechanism. C. Varela
First Order Predicate Calculus • Adds variables, terms, and (first-order) quantification of variables. • Predicate syntax: a ::= p(v1, v2,…,vn) predicate f ::= a atom | v = p(v1, v2,…,vn) equality | v1= v2 | ff | ff | ff | ff |f | v.funiversal quantifier | v.fexistential quantifier C. Varela
Predicate Calculus • In mathematical logic, a predicate is a function that maps constants or variables to true and false. • Predicate calculus enables reasoning about propositions. • For example: C [rainy(C) cold(C) snowy(C)] Quantifier (, ) Operators (,,,,) C. Varela
Structural Congruence Laws P1 P2P1 P2 X [P(X)] X [P(X)] X [P(X)] X [P(X)] (P1 P2) P1P2 (P1 P2) P1P2 P P (P1 P2) (P1 P2) (P2 P1) P1 (P2 P3) (P1 P2) (P1 P3) P1 (P2 P3) (P1 P2) (P1 P3) P1 P2 P2 P1 C. Varela
Clausal Form • Looking for a minimal kernel appropriate for theorem proving. • Propositions are transformed into normal form by using structural congruence relationship. • One popular normal form candidate is clausal form. • Clocksin and Melish (1994) introduce a 5-step procedure to convert first-order logic propositions into clausal form. C. Varela
Clocksin and Melish Procedure • Eliminate implication () and equivalence (). • Move negation () inwards to individual terms. • Skolemization: eliminate existential quantifiers (). • Move universal quantifiers () to top-level and make implicit , i.e., all variables are universally quantified. • Use distributive, associative and commutative rules of , , and , to move into conjuctive normal form, i.e., a conjuction of disjunctions (or clauses.) C. Varela
Example A [student(A) (dorm_resident(A) B [takes(A,B) class(B)])] • Eliminate implication () and equivalence (). A [student(A) (dorm_resident(A) B [takes(A,B) class(B)])] • Move negation () inwards to individual terms. A [student(A) (dorm_resident(A) B [(takes(A,B) class(B))])] A [student(A) (dorm_resident(A) B [takes(A,B) class(B)])] C. Varela
Example Continued A [student(A) (dorm_resident(A) B [takes(A,B) class(B)])] • Skolemization: eliminate existential quantifiers (). • Move universal quantifiers () to top-level and make implicit , i.e., all variables are universally quantified. student(A) (dorm_resident(A) (takes(A,B) class(B))) • Use distributive, associative and commutative rules of , , and , to move into conjuctive normal form, i.e., a conjuction of disjunctions (or clauses.) (student(A) dorm_resident(A)) (student(A) takes(A,B) class(B)) C. Varela
Horn clauses • A standard form for writing axioms, e.g.: father(X,Y) parent(X,Y), male(X). • The Horn clause consists of: • A head or consequent term H, and • A body consisting of terms Bi H B0 , B1 , …, Bn • The semantics is: « If B0 , B1 , …, and Bn, then H » C. Varela
Clausal Form to Prolog (student(A) dorm_resident(A)) (student(A) takes(A,B) class(B)) • Use commutativity of to move negated terms to the right of each clause. • Use P1P2 P2 P1 P1 P2 (student(A) dorm_resident(A)) (student(A) (takes(A,B) class(B))) • Move Horn clauses to Prolog: student(A) :- dorm_resident(A). student(A) :- takes(A,B),class(B). C. Varela
Skolemization X [takes(X,cs101) class_year(X,2)] introduce a Skolem constant to get rid of existential quantifier (): takes(x,cs101) class_year(x,2) X [dorm_resident(X) A [campus_address_of(X,A)]] introduce a Skolem function to get rid of existential quantifier (): X [dorm_resident(X) campus_address_of(X,f(X))] C. Varela
Limitations • If more than one non-negated (positive) term in a clause, then it cannot be moved to a Horn clause (which restricts clauses to only one head term). • If zero non-negated (positive) terms, the same problem arises (Prolog’s inability to prove logical negations). • For example: • « every living thing is an animal or a plant » animal(X) plant(X) living(X) animal(X) plant(X) living(X) C. Varela
Prolog Terms • Constants rpi troy • Variables University City • Predicates located_at(rpi,troy) pair(a, pair(b,c)) Can be nested. C. Varela
Resolution • To derive new statements, Robinson’s resolution principle says that if two Horn clauses: H1 B11 , B12 , …, B1m H2 B21 , B22 , …, B2n are such that H1matches B2i, then we can replace B2iwith B11 , B12 , …, B1m : H2 B21 , B22 , …, B2(i-1), B11 , B12 , …, B1m , B2(i+1) …, B2n • For example: C A,B E C,D E A,B,D C. Varela
Resolution Example father(X,Y) :- parent(X,Y), male(X). grandfather(X,Y) :- father(X,Z), parent(Z,Y). grandfather(X,Y) :- parent(X,Z), male(X), parent(Z,Y). :-is Prolog’s notation (syntax) for . C. Varela
Unification • During resolution, free variables acquire values through unification with expressions in matching terms. • For example: male(carlos). parent(carlos, tatiana). parent(carlos, catalina). father(X,Y) :- parent(X,Y), male(X). father(carlos, tatiana). father(carlos, catalina). C. Varela
Unification Process • A constant unifies only with itself. • Two predicates unify if and only if they have • the same functor, • the same number of arguments, and • the corresponding arguments unify. • A variable unifies with anything. • If the other thing has a value, then the variable is instantiated. • If it is an uninstantiatedvariable, then the two variables are associated. C. Varela
Backtracking • Forward chaining goes from axioms forward into goals. • Backward chaining starts from goals and works backwards to prove them with existing axioms. C. Varela
Backtracking example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). C. Varela
Backtracking example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(C) _C = _X success snowy(X) cold(seattle) fails; backtrack. AND rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela
Relational computation model (Oz) The following defines the syntax of a statement, sdenotes a statement s::= skip empty statement | x = y variable-variable binding | x = v variable-value binding | s1 s2 sequential composition | local x in s1 end declaration| proc {x y1 … yn } s1 end procedure introduction | if x then s1 else s2 end conditional | { x y1 … yn } procedure application | case x of pattern then s1 else s2 end pattern matching | choice s1 [] … [] sn end choice | fail failure C. Varela
Relational Computation Model • Declarative model (purely functional) is extended with relations. • The choice statement groups a set of alternatives. • Execution of choice statement chooses one alternative. • Semantics is to rollback and try other alternatives if a failure is subsequently encountered. • The fail statement indicates that the current alternative is wrong. • A fail is implicit upon trying to bind incompatible values, e.g., 3=4. This is in contrast to raising an exception (as in the declarative model). C. Varela
Search tree and procedure • The search tree is produced by creating a new branch at each choice point. • When fail is executed, execution « backs up » or backtracks to the most recent choice statement, which picks the next alternative (left to right). • Each path in the tree can correspond to no solution (« fail »), or to a solution (« succeed »). • A search procedure returns a lazy list of all solutions, ordered according to a depth-first search strategy. C. Varela
Rainy/Snowy Example fun {Rainy} choice seattle [] rochester end end fun {Cold} rochester end proc {Snowy X} {Rainy X} {Cold X} end {Browse {Search.base.all proc {$ C} {Rainy C} end}} {Browse {Search.base.all Snowy}} C. Varela; Adapted with permission from S. Haridi and P. Van Roy
Imperative Control Flow • Programmer has explicit control on backtracking process. Cut (!) • As a goal it succeeds, but with a side effect: • Commits interpreter to choices made since unifying parent goal with left-hand side of current rule. Choices include variable unifications and rule to satisfy the parent goal. C. Varela
Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). C. Varela
Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). cold(seattle) fails; no backtracking to rainy(X). GOAL FAILS. snowy(C) _C = _X snowy(X) AND rainy(X) ! cold(X) X = seattle OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela
Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy). C. Varela
Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy). C = troy FAILS snowy(X) is committed to bindings (X = seattle). GOAL FAILS. snowy(C) OR C = troy _C = _X snowy(X) snowy(troy) AND rainy(X) ! cold(X) X = seattle OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela
Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy). C. Varela
Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy). C = troy SUCCEEDS Only rainy(X) is committed to bindings (X = seattle). snowy(C) OR C = troy _C = _X snowy(X) snowy(troy) AND rainy(X) cold(X) X = seattle OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela !
Cut (!) Example 4 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X). C. Varela
Cut (!) Example 4 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X). snowy(C) _C = _X success snowy(X) cold(seattle) fails; backtrack. AND ! rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela
Cut (!) Example 5 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !. C. Varela
Cut (!) Example 5 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !. snowy(C) _C = _X success snowy(X) AND ! rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela
First-Class Terms C. Varela
not P is not P • In Prolog, the database of facts and rules includes a list of things assumed to be true. • It does not include anything assumed to be false. • Unless our database contains everything that is true (the closed-world assumption), the goal not P (or \+ P in some Prolog implementations) can succeed simply because our current knowledge is insufficient to prove P. C. Varela
More not vs ?- snowy(X). X = rochester ?- not(snowy(X)). no Prolog does not reply: X = seattle. The meaning of not(snowy(X)) is: X [snowy(X)] rather than: X [snowy(X)] C. Varela
Fail, true, repeat • repeat. • repeat :- repeat. C. Varela
not Semantics not(P) :- call(P), !, fail. not(P). Definition of not in terms of failure (fail) means that variable bindings are lost whenever not succeeds, e.g.: ?- not(not(snowy(X))). X=_G147 C. Varela
Conditionals and Loops • statement :- condition, !, then. • statement :- else. • natural(1). • natural(N) :- natural(M), N is M+1. • my_loop(N) :- N>0, • natural(I), • write(I), nl, • I=N, • !, fail. Also called generate-and-test. C. Varela
Prolog lists • [a,b,c] is syntactic sugar for: .(a,.(b,.(c, []))) where [] is the empty list, and . is a built-in cons-like functor. • [a,b,c] can also be expressed as: [a | [b,c]] , or [a, b | [c]] , or [a,b,c | []] C. Varela
Prolog lists append example append([],L,L). append([H|T], A, [H|L]) :- append(T,A,L). C. Varela
Oz lists (Review) • [a b c] is syntactic sugar for: ’|’(a ’|’(b ’|’(c nil))) where nil is the empty list, and ’|’ is the tuple’s functor. • A list has two components: a head, and a tail declare L = [6 7 8] L.1 gives 6 L.2 give [7 8] ‘|’ 6 ‘|’ ‘|’ 7 8 nil C. Varela; Adapted w. permission from S. Haridi and P. Van Roy
Oz lists append example proc {Append Xs Ys Zs} choice Xs = nil Zs = Ys [] X Xr Zr in Xs=X|Xr Zs=X|Zr {Append Xr Ys Zr} end end % new search query proc {P S} X Y in {Append X Y [1 2 3]} S=X#Y end % new search engine E = {New Search.object script(P)} % calculate and display one at a time {Browse {E next($)}} % calculate all {Browse {Search.base.all P}} C. Varela; Adapted with permission from S. Haridi and P. Van Roy
Arithmetic Goals N>M N<M N=<M N>=M • N and M must be bound to numbers for these tests to succeed or fail. • X is 1+2 is used to assign numeric value of right-hand-side to variable in left-hand-side. C. Varela
Loop Revisited • natural(1). • natural(N) :- natural(M), N is M+1. • my_loop(N) :- N>0, • natural(I), • write(I), nl, • I=N, • !. • my_loop(_). Also called generate-and-test. C. Varela
= is not equal to == or =:= X=Y X\=Y test whether X and Y canbe or cannotbeunified. X==Y X\==Y test whether X and Y are currently co-bound, i.e., have been bound to, or share the same value. X=:=Y X=\=Y test arithmetic equality and inequality. C. Varela