240 likes | 261 Views
Learn the fundamentals of logic programming using Prolog with examples of modeling logic circuits. Understand signatures, purposes, definitions, rules, and facts in a logic program. Practice creating and querying programs while exploring logical relationships.
E N D
Logic Programming • Two possible work modes: • 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). • 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.
Logic Programming: Introduction • A logic program is a set of procedures, defining relations in the problem domain. • A procedure is a set of axioms (rules and facts) with identical predicate symbol and arity. • 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). arity procedure predicate program fact axioms rule • if • and • Dad is male • The relation fatherholds between Dad and Child • parentholds
Logic Programming: Introduction • 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. query • % 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). ? - father (X,Y). X=erik, Y=jonas ? - parent (X,jonas). X=erik ; X=lena • Next possible variable instantiation
Power Power Resistor Connection point Transistor Ground Ground Logic Programming: Example 1 – logic circuits • A program that describes (models) logic circuits. a logic circuit contains: • Logic gates: resistorand transistor. • Connection points: Either points on the wire, connecting logic gates or power and ground. N1 N2 N3 N4 N5 • We will model a logic circuit as a program in Prolog, as follows: • Connection points – are individualconstants. • Logic gates – are relations on the constants.
Power Power Resistor Connection point Transistor Ground Ground Logic Programming: Example 1 – logic circuits Resistor procedure % 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). N1 N2 N3 N4 N5
Logic Programming: Example 1 – logic circuits Resistor procedure A procedure starts with a comment containing its contract. % 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). • Line numbers are for our reference only. Constant symbols • power, n1, n2 are individualsymbols (start with lowercase characters). • resistor is a predicatesymbol,a name for the relation. • End1 and End2 are variable symbols (start with uppercase characters). • resistor(power, n1) is anatomic formula.Atomic formulas are either true, false or of the • formrelation(t1 , ..., tn) where tiare 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.
Power Power Resistor Connection point Transistor Ground Ground Logic Programming: Example 1 – logic circuits Resistor procedure % 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). N1 N2 N3 N4 N5 • A query is a conjunction of goals, which are atomic formulas: ?- resistor(power, n1),resistor(n2, power). true; false “Is there an X such that the resistor relation holds for (power, X)?” • ?- resistor(power, X). • X = n1 ; • X = n2 ; • false • ?- resistor(X, n1). • X = power; • false • “Is there an X such that the resistor relation holds for (X, n1)?”
Power Power Resistor Connection point Transistor Ground Ground Logic Programming: Example 1 – logic circuits Transistor procedure % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1 transistor(n2,ground,n1). 2 transistor(n3,n4,n2). 3 transistor(n5,ground,n4). N1 N2 N3 N4 N5 • Note:In contrast to the resistor relation, the places of the arguments in the transistor • predicate are important. Each place has a different role.
power ground Logic Programming: Example 1 – logic circuits Not-gate procedure % 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). Stands for “and” resistor transistor Rule head:an atomic formula with variables Rule body source drain • The relations combined to determine weather or not the Not_gaterelation stands for Input and Output, are described by a rule. gate 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.” ?- not_gate(X,Y). X=n2, Y=n1; false
Operational semantics: The unification algorithm • A query is a trigger for execution. Program execution is an attempt to prove goals. • The search for a proof, using the AnswerQueryalgorithm, contains multiple attempts • to apply rules on a selected goal. • This is done by applying a unification algorithm, Unify, on the rule's head and the goal.
Operational semantics: The unification algorithm • Definitions: 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. 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)
Operational semantics: The unification algorithm • Definitions: 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) Combination of substitutions! (see next slide) 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)
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. (1st step) S2is applied to the terms of S1: { X=Y , Z=3 , U=V } , { Y=4 , W=5 , V=U , Z=X } So far : S1={ X=4 , Z=3 , U=U } , S2 remains the same (2nd step) Variables in S2 that have a binding in S1 are removed from S2. So far : S1={ X=4 , Z=3 , U=U } , S2= { Y=4 , W=5 , V=U} (3rd step) S2 is added to S1. So far : S1={ X=4 , Z=3 , U=U , Y=4 , W=5 , V=U} (4rd step) Identity bindings are removed. The result : S1={ X=4 , Z=3 , Y=4 , W=5 , V=U}
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: • The algorithm finishes, and possible values of the query variables are shown. • The algorithm finishes, but there is no proof for the query (false). • The proof attempt loops and does not end.
Operational semantics: Proof trees • 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. Q = ?- Q1, ..., Qn
Operational semantics: Proof trees % 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). nand_gate(In1, In2, Out) mgu { Input1_1=In1, Input2_1=In2, Output_1=Out } Rule 1 – nand_gate transistor(In1,X_1, Out), transistor(In2,ground,X_1),resistor(power, Out) % Signature: transistor (Gate, Source, Drain)/3… 1 transistor(n2,ground,n1). 2 transistor(n3,n4,n2). 3 transistor(n5,ground,n4). { In1=n2, X_1=ground, Out=n1} Fact 1 – transistor { In1=n3, X_1=n4, Out=n2} Fact 2 – transistor % 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). transistor(In2,ground,ground),resistor(power, n1) transistor(In2,ground,n4),resistor(power, n2) { In2=n5} Fact 3 – transistor { In2=n2} Fact 1 – transistor { In2=n5} Fact 3 – transistor { In2=n3} Fact 2 – transistor { In2=n2} Fact 1 – transistor { In2=n3} Fact 2 – transistor resistor(power, n2) fail fail fail fail fail true
Operational semantics: Proof trees • 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.
An example: Relational logic programming & SQL operations. 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: 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). SQL Operation: Natural join • ?-res_join_trans(End1,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.
An example: Relational logic programming & SQL operations. 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: 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). Transitive closure for the resistor relation • ?- tr_res(X, 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 ; 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.
Full logic programming: 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. In the following procedure, what symbol denotes a predicate? And a functor? A Predicate symbol % 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). A Functorcontaining 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.
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} As= tree_member (tree (X, 10, f(Y)), W ) Bs= tree_member (tree (Y, Y ,Z ), f(Z)) As ≠ Bs As ≠ Bs As ≠ Bs As ≠ Bs As= tree_member (tree (Y, 10, f(Y)), W ) Bs= tree_member (tree (Y, Y ,Z ), f(Z)) • Disagreement-set = {Y=10} • S=S {Y=10} = {X=10, Y=10} As= tree_member (tree (10, 10, f(10)), W ) Bs= tree_member (tree (10, 10, Z ), f(Z)) Disagreement-set = {Z=f(10)} S=S {Z=f(10)} = {X=10, Y=10, Z=f(10)} As= tree_member (tree (10, 10, f(10)), W ) Bs= tree_member (tree (10, 10, f(10)), f(f(10))) • Disagreement-set = {W=f(f(10))} • S={X=10, Y=10, Z=f(10), W=f(f(10))} As= tree_member (tree (10, 10, f(10)), f(f(10)) ) Bs= tree_member (tree (10, 10, f(10)), f(f(10))) Q: Why do we check for occurence?
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.
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). ?- 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.
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). ?- 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 2nd and 3rd clauses. This means that different proof paths provide repeated answers.