1 / 22

Signature: parent (Parent, Child) /2 Purpose: Parent is a parent of Child

Logic Programming: Introduction. A procedure: a set of axioms (rules and facts) with identical signature ( predicate symbol and arity ). A logic program : a set of procedures (predicates) , defining relations in the program domain. Signature: parent (Parent, Child) /2

talamantes
Download Presentation

Signature: parent (Parent, Child) /2 Purpose: Parent is a parent of Child

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic Programming: Introduction • A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). • A logic program: a set of procedures (predicates), defining relations in the program domain. • Signature: parent (Parent, Child) /2 • Purpose: Parent is a parent of Child • 1. parent (erik, jonas). • 2. 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

  2. Logic Programming: Introduction • The prolog interpreter operates in a read-eval-print loop. Given a query,it attempts to prove it based on the program: • If it fails, it answers false. • Else, if the query has no variables, it answers true. • Else, it outputs all possible variables assignmentsfound during proof process. 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 assignment

  3. Power Power Connection point Resistor(symmetric) Transistor(asymmetric) Ground Ground Logic Programming: Example 1 – logic circuits • An electronic logic circuit combines: • Logic gates: resistor, transistor, implement simple logic functions. • Connection points: connects one logic gateto another, or to poweror ground. N1 N2 N3 • A program that models electronic logic circuits: • Connection points: are individual constants. • Logic gates: are relations on the constants. N4 N5 % 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: transistor (Gate, Source, Drain)/3 % Purpose: … 1 transistor(n2,ground,n1). 2 transistor(n3,n4,n2). 3 transistor(n5,ground,n4). • Note:In contrast resistor, the order of arguments in transistor is important. Each has a different role. 

  4. Logic Programming: Example 1 – logic circuits % 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 End1,End2 1 resistor(power,n1). 2 resistor(power, n2). 3 resistor(n1, power). • Reminders… • A procedure begins with a contract. • Constants start with lowercase characters. • A predicatename is also a constant,which definesa relation between its arguments. • variablesstart with uppercase characters (‘_’ for wildcard). • Atomic formulas are either true, false or of the formpredicate(t1 , ..., tn), ti is a term. • A rule is a formula defining a relation that depends on certain conditions. • A fact is an atomic formula which is unconditionally true. terms

  5. Logic Programming: Example 1 – logic circuits % 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 End1,End2 1 resistor(power,n1). 2 resistor(power, n2). 3 resistor(n1, power). • Reminders… • A query is a sequence of atomic formulas: ?- resistor(power, n1),resistor(n2, power). true; false No more answers “Does an Xexists such that the resistor relation holds for (power, X)?” • ?- resistor(power, X). • X = n1 ; • X = n2; • false Is there another answer?

  6. power ground Logic Programming: Example 1 – logic circuits • Combining logic gates to create a “not” logic circuit: • The resistor and transistor relations are based on facts. • The relations can be combined by a rule to determine whether or not the not_circuitrelation stands for Input and Output. resistor not circuit transistor Output Input

  7. power ground Logic Programming: Example 1 – logic circuits Combining logic gates to create a NOT logic circuit: % Signature: not_circuit(Input, Output)/2 % Purpose: not logic circuit. 1. not_circuit(Input, Output) :- transistor(Input, ground, Output) , 2 . resistor(power, Output). “and” Rule head:an atomic formula with variables Rule body resistor not circuit ?- not_circuit(X,Y). X=n2, Y=n1; false transistor Output Input • “For all Input and Output: • (Input, Output) stands in the relation not_circuitif: • (Input, ground, Output) stand in the relation transistorand • (power, Output) stand in the relation resistor.”

  8. Power Power Connection point Ground Ground Logic Programming: Example 1 – logic circuits Combining logic gates to create a NAND logic circuit: % Signature: nand_circuit(Input1, Input2, Output)/3 % Purpose: nand logic circuit nand_circuit(Input1, Input2, Output) :- transistor(Input1, X, Output), transistor(Input2, ground, X), resistor(power, Output). N1 N2 N3 N4 N5 ?- not_circuit(X, Y), nand_circuit(In1, In2, X). X = n2, Y = n1, In1 = n3, In2 = n5; false nand circuit

  9. Semantics: Unification algorithm • A program execution is triggered by a queryin attempt to prove its goals: • To find a possible proof, the answer-queryalgorithm is used. • It makes multiple attempts to apply rules on a selected goal. • This is done by applying a unification algorithm, Unify, to the rule head and the goal.

  10. Semantics: Unification algorithm Definitions: binding:a non-circular expression, X=t, where X is a variableand t is a termnot including X. Substitution:a function from a finite set of variables to a finite set of terms (or bindings). Application of a sub. S to an atomic formula A, replaces vars in A with corresponding terms in S:S = {I=X} A = not_circuit(I, I) , A º S = not_circuit(X, X)B = not_circuit(X,Y) , Bº S = not_circuit(X, Y)the result is an instance of A Unifier:a substitution S is called a unifier of formulas A and B if A º S = B º S. For example:S = {I=X} º {X=Y} = {I=Y, X=Y} A º S = not_circuit(Y, Y)B º S = not_circuit(Y, Y) The algorithm Unify receives two atomic formulas and returns their most general unifier. S = {J=5, X=5, Y=5} is a unifier for A and B, but not the most general one.

  11. Semantics: Proof trees • Executing answer-query: • The interpreter searches for a proof for a given query (a conjunction of goals). • The search is done by building and traversing a proof tree where all possibilities are examined. • The possible outcome is one of the following: • The algorithm finishes, and possible values of the query variables are given. • The algorithm finishes, but there is no proof for the query (false). • The proof attempt loops and never ends.

  12. Semantics: Proof trees • The tree structure depends on Prolog's goal selection and rule selection policies: • Query goals (Q1,…,Qn) are at the root of the proof tree. • Choose current goal (atomic formula). Prolog's policy: the leftmost goal. • Choose current rule to prove current goal. (top to bottom program order). • Rename the variables in the rule and unify the rule head with the goal.If unification succeeds: • A new child node is created. • The query for this node is the rule body. • A leaf may be created if the goal list is empty (success), or if the goal cannot be proven (failure). • Backtracking: When a leaf is reached, the search travels up to the first parent node where another rule can be matched. Q = ?- Q1, ..., Qn

  13. Semantics: Example 2 – A generated proof tree ?- nand_circuit(In1, In2, Out). nand_circuit(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). nand_circuit(In1, In2, Out) mgu { Input1_1 = In1, Input2_1 = In2, Output_1 = Out } Rule 1 transistor (Gate, Source, Drain) :- transistor(n2,ground,n1). transistor(n3,n4,n2). transistor(n5,ground,n4). transistor(In1, X_1, Out), transistor(In2, ground, X_1),resistor(power, Out) { In1=n5, X_1=ground, Out=n4} Fact 2 – transistor { In1=n2, X_1=ground, Out=n1} Fact 1 – transistor resistor(End1,End2) :- resistor(power, n1). resistor(power, n2). resistor(n1, power). resistor(n2, power). {In1=n3, X_1=n4, Out=n2} Fact 2 – transistor transistor(In2,ground,ground),resistor(power, n1) transistor(In2,ground,n4),resistor(power, n2) transistor(In2,ground,ground),resistor(power, n2) { In2=n5} Fact 3 – transistor {In2=n2} Fact 1 – transistor { In2=n2} Fact 1 – transistor { In2=n5} Fact 3 – transistor { In2=n3} Fact 2 – transistor • a finite success treewith one success path. { In2=n3} Fact 2 – transistor fail fail fail fail fail resistor(power, n2) true

  14. Semantics: proof trees • Possible types of proof trees: • A success tree has at least one success path in it. • A failure tree is one in which every path is a failure path. • A proof tree isan infinite tree if it contains an infinite path. • Otherwise, it is a finite tree. • Example: • An infinite is generated by repeatedly applying the rule p(X):-p(Y),q(X,Y) (left recursion).To avoid the infinite path, we could rewrite the rule: p(X):-q(X,Y),p(Y) (tail recursion).

  15. Semantics: Example 3 – SQL in Relational Logic Programming An example: Relational logic programming & SQL operations. • Relations may be regarded as tables in a database of facts. • Recall the resistor and transistor relations presented earlier: 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,Source,Drain). % Signature: res_join_trans(End1, X, Gate, Source)/4 % Purpose: Join between resistor and transistor % according to End2 of resistor and Gate of transistor. res_join_trans(End1, X, Source, Drain):- resistor(End1,X), transistor(X, Source, Drain). End1 = power, X = n2, Source = ground, Drain = n1 ; false.

  16. Semantics: Example 3 – SQL in Relational Logic Programming An example: Relational logic programming & SQL operations. • Relations may be regarded as tables in a database of facts. • Recall the resistor and transistor relations presented earlier: 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 • ?- res_closure(X, Y). %Signature: res_closure(X, Y)/2 res_closure(X, Y) :- resistor(X, Y). res_closure(X, Y) :- resistor(X, Z), res_closure(Z, Y). X = power, Y = n1 ; X = power, Y = n2 ; X = n1, Y = power ; X = n2, Y = power ; X = power , Y = power ; X = power, Y = n1 ; X = power, Y = n2 ; X = power, Y = power ; X = power, Y = n1; ... The resistor relation is symmetric. Therefore, we get a finite series of answers repeated an infinite number of times.

  17. Logic Programming: An example: Relational logic programming & SQL operations. • Relational logic programming has no ability to describe composite data, only atoms. • Logic programming is equipped with functors to describe composite data. Q: In the following procedure, which symbols are predicates? Which are functors? % Signature: tree_member(Element,Tree)/ 2 % Purpose: 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). Predicate A Functorapplied to three data items. • Q: how can you tell? by their relative location in the program: • A predicate appears 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 cannot. • NOTE: The same name may be used for both a predicate and a functor!

  18. Logic Programming: Unification with functors An example: Relational logic programming & SQL operations. 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(X)), 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 As ≠ Bs As ≠ Bs As ≠ Bs As= tree_member (tree (Y, 10, f(Y)), W ) Bs= tree_member (tree (Y, Y ,Z ), f(Z)) • Disagreement-set = {Y=10} •  s=s  {Y=10} = {X=10, Y=10} As= tree_member (tree (10, 10, f(10)), W ) Bs= 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)} As= tree_member (tree (10, 10, f(10)), W ) Bs= 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))} As= tree_member (tree (10, 10, f(10)), f(f(10)) ) Bs= tree_member (tree (10, 10, f(10)), f(f(10))) Q:Why do we check for occurence?

  19. Logic Programming: Unification with functors An example: Relational logic programming & SQL operations. A: Consider the following two atomic formulas: A = tree_member (tree (X, Y, f(X)), X ) B = tree_member(tree (Y, Y, Z ), f(Z)) Applying Unify(A,B) will result in a loop: X=Y, Z=f(Y), Y=f(Z)=f(f(Y))… the substitution cannot be successfully solved.

  20. Logic Programming: Example queries An example: Relational logic programming & SQL operations. % 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.

  21. Full Logic Programming: An example proof tree ?- tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))). • tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))) {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} {X_1 = 1, X = 1Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} true • tree_member (X, tree(3,nil,nil)) • tree_member (X, tree(2,nil,nil)) {X_2 = 2, X = 2Left_2 = nil,Right_2 = nil} {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} {X=1} • tree_member (X, nil) • tree_member (X, nil) true fail fail {X=2} {X_2 = 3, X = 3 Left_2 = nil,Right_2 = nil} {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} % Signature: tree_member(Element,Tree)/ 2 % …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). {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} • tree_member (X, nil) • tree_member (X, nil) true {X=3} fail fail

  22. Full Logic Programming: An example proof tree % 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) ; • ... • Looking for all trees in which 1 is a member, we get an infinite success tree with partially instantiated answers (containing variables). • We use a rule that requires a defined input, but our input is a variable. Possible answers are generated by the proof algorithm. • In this case we call the rule a generator rule.

More Related