Applied Logic in Engineering Logic Programming Technische Universität München Institut für Informatik Software & Systems Engineering Dr. Maria Spichkova M. Spichkova WS 2012/13: Applied Logic in Engineering 1
Logic Programs Logic programs: set of (definite Horn) clauses Programming language ProLog: based on Horn clauses Restriction to the Horn form: Why? Efficiency of the algorithms Simpler answer situations Most of the mathematical theories seem to be axiomatizable in terms of Horn formulas (i.e. this does not seem to be a real restriction in practice) M. Spichkova WS 2012/13: Applied Logic in Engineering 2
Clausal Form CNF: (L 11 L 1n1 ) (L m1 L mnm ) Clausal form: set notation of formulas in CNF {{L 11,..., L 1n1 },..., {L m1,..., L mnm }} clauses Empty clause: Halting clause Unit clause: clause which contains only one literal. Horn clause: clause which contain only one (at most one) positive literal M. Spichkova WS 2012/13: Applied Logic in Engineering 3
Clausal Form (2) Definite clause: horn clause which contains a positive literal Fact: definite unit clause Fact: just one positive literal {L} Procedure clause: clause which contain only one positive literal ProLog: L. L 1 L n L (L 1 L n ) L (L 1 L n ) L L (L 1 L n ) {L, L 1,..., L n } ProLog: Procedure head Procedure body L : L 1,..., L n. Procedure call M. Spichkova WS 2012/13: Applied Logic in Engineering 4
Clausal Form (3) Goal clause (query clause): horn clause which contains only negative literals (L 1 L n ) L 1 L n ProLog:? L 1,..., L n. L 1 L n 0 (L 1 L n ) 0 { L 1,..., L n } M. Spichkova WS 2012/13: Applied Logic in Engineering 5
Horn Clause Programs Configuration: a pair (G, sub) where G is a goal clause and sub is a substitution Transition relation for configurations: G 1 has a form there is a clause C in the logic program F after that its variables are renamed so that G 1 and C do not have variables in common B and A i are unifiable (G 1, sub 1 ) F (G 2, sub 2 ) G 1 = { A 1, A 2,..., A k } (k 1) i {1,..., k} C = {B, C 1, C 2,..., C n } (n 0) Then the G 2 has the form (s is here a most general unifier) G 2 = { A 1,..., A i 1, C 1, C 2,..., C n, A i+1,..., A k }s sub 2 = sub 1 s M. Spichkova WS 2012/13: Applied Logic in Engineering 6
Horn Clause Programs (2) A computation of the logical program F on input is a (finite or infinite) sequence of the form If the sequence is final, and the last configuration of it has the form then this computation is called successful, and the formula G = { A 1, A 2,..., A k } (k 1) (G, []) F (G 1, sub 1 ) F (G 2, sub 2 ) F... (A 1 A k )sub (, sub) is called the result of the computation Logic programs are nondeterministic: Each configuration can have more than one successor configuration M. Spichkova WS 2012/13: Applied Logic in Engineering 7
Example from [Schöning1989] Logic program: ProLog notation: F = { {P (x, z), Q(x, y), P (y, z)}, {P (u, u)}, {Q(a, b)} } p(x, Z) :- q(x, Y), p(y, Z). p(u, U). q(a, b). Find the computations for the following goal clauses: G = { P (v, b)}? - p(v, b). M. Spichkova WS 2012/13: Applied Logic in Engineering 8
ProLog: Programm direct_flight(munich, berlin). direct_flight(munich, dresden). direct_flight(berlin, toronto). direct_flight(toronto, montreal). Facts connection(x, Y) :- direct_flight(x, Y). connection(x, Y) :- direct_flight(x, Z), connection(z, Y). Rules M. Spichkova WS 2012/13: Applied Logic in Engineering 9
ProLog: Queries direct_flight(munich, berlin). direct_flight(munich, dresden). direct_flight(berlin, toronto). direct_flight(toronto, montreal). connection(x, Y) :- direct_flight(x, Y). connection(x, Y) :- direct_flight(x, Z), connection(z, Y).?- connection(montreal, dresden). no M. Spichkova WS 2012/13: Applied Logic in Engineering 10
ProLog: Queries (2) direct_flight(munich, berlin). direct_flight(munich, dresden). direct_flight(berlin, toronto). direct_flight(toronto, montreal). connection(x, Y) :- direct_flight(x, Y). connection(x, Y) :- direct_flight(x, Z), connection(z, Y).?- connection(munich, dresden). yes M. Spichkova WS 2012/13: Applied Logic in Engineering 11
ProLog: Queries direct_flight(munich, berlin). direct_flight(munich, dresden). direct_flight(berlin, toronto). direct_flight(toronto, montreal). connection(x, Y) :- direct_flight(x, Y). connection(x, Y) :- direct_flight(x, Z), connection(z, Y).?- connection(munich, X) berlin dresden toronto montreal no M. Spichkova WS 2012/13: Applied Logic in Engineering 12
ProLog: Syntax Correct? Animal(baer, mammal) animal(tiger, mammal)). Animal(sparrow) animal(bird, _flamingo). animal(salmon. fish); Plant(rose) plant[palm]. plant(tulip). is flower (tulip). Creature(x) = animal(x), plant(x) Correct! animal(baer, mammal). animal(tiger, mammal). animal(sparrow). animal(sparrow, bird). animal(_flamingo, bird). animal(flamingo, bird). animal(salmon, fish). plant(rose). plant(palm). Without warnings plant(tulip). creature(x) :- animal(x, _). creature(x) :- plant(x). is_flower(tulip). creature(x) :- animal(x), plant(x). M. Spichkova WS 2012/13: Applied Logic in Engineering 13
ProLog: Queries (4) animal(baer, mammal). animal(tiger, mammal). animal(sparrow, bird). animal(flamingo, bird). animal(salmon, fish). plant(rose). plant(palm). plant(tulip). is_flower(tulip). creature(x) :- animal(x, _). creature(x) :- plant(x).?- animal(animal, X). Animal = baer X = mammal ; Animal = tiger X = mammal ; Animal = sparrow X = bird ; Animal = flamingo X = bird ; Animal = salmon X = fish ; M. Spichkova WS 2012/13: Applied Logic in Engineering 14
ProLog: Queries (5) animal(baer, mammal). animal(tiger, mammal). animal(sparrow, bird). animal(flamingo, bird). animal(salmon, fish). plant(rose). plant(palm). plant(tulip). is_flower(tulip). creature(x) :- animal(x, _). creature(x) :- plant(x).?- animal(_, X). X = mammal ; X = mammal ; X = bird ; X = bird ; X = fish ; M. Spichkova WS 2012/13: Applied Logic in Engineering 15
ProLog: Queries (6) animal(baer, mammal). animal(tiger, mammal). animal(sparrow, bird). animal(flamingo, bird). animal(salmon, fish). plant(rose). plant(palm). plant(tulip). is_flower(tulip). creature(x) :- animal(x, _). creature(x) :- plant(x).?- animal(tiger, X). X = mammal ; no?- animal. WARNING: undefined predicate: animal/0 However there are definitions for: animal/2 M. Spichkova WS 2012/13: Applied Logic in Engineering 16
ProLog: Order p(x, Z) :- q(x,y), p(y, Z). p(x, X). q(a,b). p(x, Z) :- p(y, Z), q(x,y). p(x, X). q(a,b).?- p(t,b).?- p(t,b). M. Spichkova WS 2012/13: Applied Logic in Engineering 17