Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-

Similar presentations


Presentation on theme: "Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-"— Presentation transcript:

1 Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?- ['myfile.pl']. 2. Install SWIProlog (See “Useful links” in the course web page).Useful links You can work with the editor provided with SWIProlog: Use “File->edit” to open the editor. Use “File->Reload modified files” to load the changes to the interpreter. Use “File->Navigator” to view files and procedure definitions. 1

2 Logic Programming: Introduction o A logic program is a set of procedures, defining relations in the problem domain. o A procedure is a set of axioms (rules and facts) with identical predicate symbol and arity. o The prolog interpreter loads a program; then operates in a read-eval-print loop. Signature: parent (Parent, Child) /2 Purpose: Parent is a parent of Child 1. parent (erik, jonas). 3. parent (lena, jonas). Signature: male(Person) /1 Purpose: Person is a male 1. male (erik). Signature: father (Dad, Child) /2 Purpose: Dad is father of Child 1. father (Dad, Child) :- parent(Dad, Child), male (Dad). program fact rule axioms arity procedure The relation father holds between Dad and Child predicate if parent holds andDad is male 2

3 Logic Programming: Introduction % Signature: parent (Parent, Child) /2 % Purpose: Parent is a parent of Child parent (erik, jonas). parent (lena, jonas). % Signature: male(Person) /1 % Purpose: Person is a male male (erik). % Signature: father (Dad, Child) /2 % Purpose: Dad is father of Child father (Dad, Child) :- parent(Dad, Child), male (Dad). Given a query, the interpreter attempts to prove it. The answer is either: No (false) – If the query isn’t provable under the program axioms, or Yes (true) Otherwise. All possible query variable instantiations are given, for which the query is provable. ? - father (X,Y). X=erik, Y=jonas ? - parent (X,jonas). X=erik ; X=lena Next possible variable instantiation query 3

4 Logic Programming: Example 1 – logic circuits A program that describes (models) logic circuits. a logic circuit contains: Logic gates: resistor and transistor. Connection points: Either points on the wire, connecting logic gates or power and ground. We will model a logic circuit as a program in Prolog, as follows: Connection points – are individual constants. Logic gates – are relations on the constants. Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 4

5 Logic Programming: Example 1 – logic circuits % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). Resistor procedure Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 5

6 Logic Programming: Example 1 – logic circuits % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). power, n1, n2 are individual symbols (start with lowercase characters). resistor is a predicate symbol, a name for the relation. End1 and End2 are variable symbols (start with uppercase characters). resistor(power, n1) is an atomic formula. Atomic formulas are either true, false or of the form relation(t 1,..., t n ) where t i are terms (individual symbols or variable symbols). Facts are atomic formulas (with a "." at the end) which are unconditionally true. A query is a sequence of atomic formulas that require a proof. A procedure starts with a comment containing its contract. Line numbers are for our reference only. Constant symbols Resistor procedure 6

7 Logic Programming: Example 1 – logic circuits % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). A query is a conjunction of goals, which are atomic formulas: “Is there an X such that the resistor relation holds for (power, X)?” ?- resistor(power, n1),resistor(n2, power). true; false ?- resistor(X, n1). X = power; false “Is there an X such that the resistor relation holds for (X, n1)?” ?- resistor(power, X). X = n1 ; X = n2 ; false Resistor procedure Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 7

8 Logic Programming: Example 1 – logic circuits % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). Note: In contrast to the resistor relation, the places of the arguments in the transistor predicate are important. Each place has a different role. Transistor procedure Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 8

9 % Signature: not_gate(Input, Output)/2 % Purpose: Used as logic gate that has one Input and one Output. % We can construct a not gate from a transistor and a resistor. 1. not_gate(Input, Output) :- transistor(Input, ground, Output), 2. resistor(power, Output). % Signature: not_gate(Input, Output)/2 % Purpose: Used as logic gate that has one Input and one Output. % We can construct a not gate from a transistor and a resistor. 1. not_gate(Input, Output) :- transistor(Input, ground, Output), 2. resistor(power, Output). Not-gate procedure Rule head: an atomic formula with variables Rule body Stands for “and” power ground gate source drain transistor resistor The relations combined to determine weather or not the Not_gate relation stands for Input and Output, are described by a rule. ?- not_gate(X,Y). X=n2, Y=n1; false Note: Variables, appearing in the rule’s head, are universally quantified: “For all Input and Output, the pair (Input, Output) is a not gate if (Input, ground, Output) is a transistor and (power, Output) is a resistor.” Logic Programming: Example 1 – logic circuits 9

10 Operational semantics: The unification algorithm o A query is a trigger for execution. Program execution is an attempt to prove goals. o The search for a proof, using the AnswerQuery algorithm, contains multiple attempts to apply rules on a selected goal. o This is done by applying a unification algorithm, Unify, on the rule's head and the goal. 10

11 Operational semantics: The unification algorithm substitution 1. A substitution is a set of bindings X=t, where X is a binding variable and t is a term. Every binding variable appears only once on the left side of some binding. If a variable appears on the left side of a binding, it cannot appear on a right side. application 2. The application of a substitution S (denoted º) to an atomic formula A replaces variables in A with corresponding terms in S. The result is an instance of A. For example: A=not_Gate(I, I), B=not_Gate(X,Y), s={I=X}: A º S = not_Gate(X, X) B º S = not_Gate(X, Y) Definitions Definitions: 11

12 Operational semantics: The unification algorithm Definitions Definitions: unifier 3. A unifier of two atomic formulas A and B is substitution α such that the result of its application to both A and B is the same. For example: A=not_Gate(I, I), B=not_Gate(X,Y), S={I=X} º{X=Y} = {I=Y, X=Y} A º S = not_Gate(Y, Y) B º S = not_Gate(Y, Y) unifier 4. The Unify algorithm (see the lecture notes [!]) returns the most general unifier of two atomic formulas, A and B. An mgu for the last example is: S={I=Y, X=Y} A º S = not_Gate(Y, Y) B º S = not_Gate(Y, Y) Combination of substitutions! (see next slide) 12

13 Operational semantics: The unification algorithm Combinations of substitutions (shown in class): In the following example: We combine the substitution S1={X=Y, Z=3, U=V} with the substitution S2= {Y=4, W=5, V=U, Z=X}. This is denote by: S1 º S2. (2 nd step) Variables in S2 that have a binding in S1 are removed from S2. So far : S1={ X=4, Z=3, U=U }, S2 remains the same So far : S1={ X=4, Z=3, U=U }, S2= { Y=4, W=5, V=U} (4 rd step) Identity bindings are removed. The result : S1={ X=4, Z=3, Y=4, W=5, V=U} (3 rd step) S2 is added to S1. So far : S1={ X=4, Z=3, U=U, Y=4, W=5, V=U} (1 st step) S2is applied to the terms of S1: { X=Y, Z=3, U=V }, { Y=4, W=5, V=U, Z=X } 13

14 Operational semantics: Proof trees Proof trees are formed by executing the AnswerQuery algorithm. The interpreter searches for a proof for a given query (a conjunction of formulae (goals)). The search is done by building and traversing a proof tree where all possibilities for a proof are taken into account. The possible outcomes are either one of the following: o The algorithm finishes, and possible values of the query variables are shown. o The algorithm finishes, but there is no proof for the query (false). o The proof attempt loops and does not end. 14

15 The tree structure depends on Prolog's goal selection and rule selection policies:  Query goals appear in the root node of the proof tree.  Choose a current goal (Prolog's policy: the leftmost goal).  Choose a rule to the current goal. (top to bottom program order).  Rename the variables in the rules and then apply the unify alg. If the unification succeeds, an edge in the proof tree is created.  A node in the proof tree is a leaf if: the goal list is empty (success), or the goal list is not empty but no rule can be applied to the selected goal (failure).  When a leaf node is reached, the search travels to the parent node (redo, backtrack) and tries to match another rule to it. Operational semantics: Proof trees 15 Q = ?- Q1,..., Qn

16 % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). 4 resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2 resistor(power, n2). 3 resistor(n1, power). 4 resistor(n2, power). nand_gate(In1, In2, Out) transistor(In1,X_1, Out), transistor(In2,ground,X_1), resistor(power, Out) transistor(In2,ground,ground), resistor(power, n1) transistor(In2,ground,n4), resistor(power, n2) % Signature: nand_gate(Input1,Input2,Output)/3 % Purpose: … 1 nand_gate(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). % Signature: nand_gate(Input1,Input2,Output)/3 % Purpose: … 1 nand_gate(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). { In1=n2, X_1=ground, Out=n1} Fact 1 – transistor % Signature: transistor (Gate, Source, Drain)/3… 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). % Signature: transistor (Gate, Source, Drain)/3… 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). { In1=n3, X_1=n4, Out=n2} Fact 2 – transistor fail { In2=n3} Fact 2 – transistor fail { In2=n5} Fact 3 – transistor true resistor(power, n2) { In2=n2} Fact 1 – transistor fail { In2=n3} Fact 2 – transistor { In2=n5} Fact 3 – transistor { In2=n2} Fact 1 – transistor fail Operational semantics: Proof trees { Input1_1=In1, Input2_1=In2, Output_1=Out } Rule 1 – nand_gate mgu 16

17  A success proof tree, has at least one success path in it.  A failure proof tree does not have a success path in it.  A proof tree is infinite if it contains an infinite path. For example, by repeatedly applying the rule p(X):-p(Y). In the example above, the proof tree is a finite success tree, with a success path and a failure path. Operational semantics: Proof trees 17

18 It is sometimes useful to think of relations as tables in a database of facts. For example, the relations resistor and transistor can be viewed as two tables: An example: Relational logic programming & SQL operations. Table name: resistor Schema: End1, End2 Data: (power, n1), (power, n2), (n1, power), (n2, power). Table name: transistor Schema: Gate, Source, Drain Data: (n2, ground, n1) (n3, n4, n2), (n5, ground, n4). % Signature: res_join_trans(End1, X, Gate, Source)/4 % Purpose: natural join between resistor and % transistor according to End2 of resistor and % Gate of transistor. res_join_trans(End1, X, Gate, Source):- resistor(End1,X), transistor(X, Gate, Source). % Signature: res_join_trans(End1, X, Gate, Source)/4 % Purpose: natural join between resistor and % transistor according to End2 of resistor and % Gate of transistor. res_join_trans(End1, X, Gate, Source):- resistor(End1,X), transistor(X, Gate, Source). End1= power, X= n2, Gate= ground, Source= n1 ; false. ?-res_join_trans(End1,X,Gate,Source). SQL Operation: Natural join 18

19 It is sometimes useful to think of relations as tables in a database of facts. For example, the relations resistor and transistor can be viewed as two tables: An example: Relational logic programming & SQL operations. Table name: resistor Schema: End1, End2 Data: (power, n1), (power, n2), (n1, power), (n2, power). Table name: transistor Schema: Gate, Source, Drain Data: (n2, ground, n1) (n3, n4, n2), (n5, ground, n4). %Signature: tr_res(X, Y)/2 tr_res(X, Y) :- resistor(X, Y). tr_res(X, Y) :- resistor(X, Z), tr_res(Z, Y). %Signature: tr_res(X, Y)/2 tr_res(X, Y) :- resistor(X, Y). tr_res(X, Y) :- resistor(X, Z), tr_res(Z, Y). X = power, Y = n1 ; X = power, Y = n2 ; X = n1, Y = power ; X = n2, Y = power ; X = Y, Y = power ; ?- tr_res(X, Y). Transitive closure for the resistor relation X = power, Y = n1 ; X = power, Y = n2 ; X = Y, Y = power ; X = power, Y = n1;... We get the same answer an infinite number of times. The reason lies on the symmetric nature of the resistor relation. 19

20 Relational logic programming does not have the ability to describe data structures, only relations. In contrast, in Full logic programming, composite data is described by value constructors, which are called functors. Full logic programming: % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). In the following procedure, what symbol denotes a predicate? And a functor? A Predicate symbol A Functor containing three data items. How can you tell? The functor looks the same as predicate, but its meaning and relative location in the program, are different: Predicate symbols appear as an identifier of an atomic formula. A functor is way to construct a term. A term is a part of a formula. A functor can be nested – a predicate can't. 20

21 Full logic programming: Unification with functors Unification is more complex with functors. Here is an execution of the Unify algorithm, step by step: Unify(A,B) where A= tree_member (tree (X, 10, f(Y)), W) ; B =tree_member (tree (Y, Y, Z), f(Z)). Initially, S={}  Disagreement-set = {X=Y}  X does not occur in Y  S=S  {X=Y} = {X=Y} A  s= tree_member (tree (X, 10, f(Y)), W ) B  s= tree_member (tree (Y, Y,Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {Y=10}  S=S  {Y=10} = {X=10, Y=10} A  s= tree_member (tree (Y, 10, f(Y)), W ) B  s= tree_member (tree (Y, Y,Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {Z=f(10)}  S=S  {Z=f(10)} = {X=10, Y=10, Z=f(10)} A  s= tree_member (tree (10, 10, f(10)), W ) B  s= tree_member (tree (10, 10, Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {W=f(f(10))}  S={X=10, Y=10, Z=f(10), W=f(f(10))} A  s= tree_member (tree (10, 10, f(10)), W ) B  s= tree_member (tree (10, 10, f(10)), f(f(10))) A  s ≠ B  s A  s= tree_member (tree (10, 10, f(10)), f(f(10)) ) B  s= tree_member (tree (10, 10, f(10)), f(f(10))) 21 Q: Why do we check for occurence?

22 Full logic programming: Unification with functors Question: What is the result of Unify(A,B) with the following two atomic formulas? A= tree_member (tree (X, Y, f(X)), X) B =tree_member (tree (Y, Y, Z), f(Z)) loop : X=Y, Z=f(Y), Y=f(Z)=f(f(Y)) the substitution cannot be successfully solved. 22

23 Full logic programming: % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, tree(1,nil, nil)). true ?- tree_member(2,tree(1,tree(2,nil,nil), tree(3,nil, nil))). true. ?- tree_member(1, tree(3,1, 3)). false. ?- tree_member(X,tree(1,tree(2,nil,nil), tree(3,nil, nil))). X=1; X=2; X=3; false. 23

24 Full logic programming: % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, T). T = tree(1, _G445, _G446) ; T = tree(_G444, tree(1, _G449, _G450), _G446) ; T = tree(_G444, tree(_G448, tree(1, _G453, _G454), _G450), _G446) ;... If we look for all trees that 1 is a member in we get an infinite success tree with partially instantiated answers (answers containing variables). Note: X might be equal to Y in the 2 nd and 3 rd clauses. This means that different proof paths provide repeated answers. 24


Download ppt "Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-"

Similar presentations


Ads by Google