ogic 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). 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
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). - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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).
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.
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 Power
Ground
N1
N2 N3
N4N5
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).
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
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.
11
Operational semantics: The unification algorithm
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)
Definitions:
12
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:
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)
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.
(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 remains the same
So far : S1={ X=4 , Z=3 , U=U } , S2= { 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}
(3rd step) S2 is added to S1.
So far : S1={ X=4 , Z=3 , U=U , Y=4 , W=5 , V=U}
(1st step) S2is applied to the terms of S1: { X=Y , Z=3 , U=V } , { Y=4 , W=5 , V=U , Z=X }
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.
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
Q = ?- Q1, ..., Qn
16
% Signature: resistor(End1,End2)/2% Purpose: A resistor component connects two ends1 resistor(power, n1). 2 resistor(power, n2).3 resistor(n1, power). 4 resistor(n2, power).
% 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):-
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.
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).
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.
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}
As= tree_member (tree (X, 10, f(Y)), W )Bs= tree_member (tree (Y, Y ,Z ), f(Z)) As ≠ Bs
As= tree_member (tree (10, 10, f(10)), W )Bs= tree_member (tree (10, 10, f(10)), f(f(10))) As ≠ Bs
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?
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.
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).