Download PS12

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Propositional calculus wikipedia , lookup

Law of thought wikipedia , lookup

Intuitionistic logic wikipedia , lookup

Laws of Form wikipedia , lookup

Canonical normal form wikipedia , lookup

Transcript
1022 ’‫ סמסטר ב‬- ‫עקרונות שפות תכנות‬
Logic Programming- Part 1 :12 ‫תרגול‬
Topics
1. Logic programming concepts.
2. Operational semantics: Unification algorithm, proof trees.
3. Full logic programming.
Setup
Two possible work modes:
1. Use SICStus Prolog, in the labs.
To load a prolog file (*.pl or *.pro extension) to the interpreter: ?- ['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. Introduction
1. A logic program is a set of procedures, defining relations in the problem domain.
2. A procedure is a set of axioms (rules and facts) with equal predicate symbol and
arity.
3. The prolog interpreter loads a program; then operates in a read-eval-print loop.
4. Given a query, the interpreter attempts to prove it. The answer is either:
 No (or false) – If the query is not provable under the program axioms, or
 Yes (or true) – If the query is provable. All possible query variable instantiations
are given, for which the query is provable.
1
2- Logic programming concepts
Example 1 – logic circuits
We would like to write a program that describes (models) logic circuits.


A logic gate is a theoretical or physical device implementing a Boolean function.
It performs a logical operation on one or more logic inputs and produces a single
logic output.
A logic circuit is a combination of individual logic gates, connected in a specific way.
A logic circuit also implements a Boolean function.
In this question, a logic circuits contain:
1. Logic gates: resistor and transistor.
2. Connection points: They are either points on the wire, connecting logic gates or
power(‫ )מקור מתח‬and ground)‫(הארקה‬.
For example, the logic circuit below, gives a specific combination of connection points (ni),
resistors and transistors.
Power
Power
Resistor
Connection
point
Transistor
Ground
Ground
We will model a logic circuit as a program in Prolog, as follows:
1. Connection points – are individual constants.
2. Logic gates – are relations on the constants.
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).
Note: resistor is a symmetric relation. (hence, facts 3 and 4).
Explanations and definitions:

A procedure starts with a comment containing its contract. Line numbers are for our
reference only.
2

power, n1, n2 are individual constant symbols. Constant symbols start with
lowercase characters.

resistor is a predicate symbol constant. Predicate symbols are the name of the
relation.

atomic formula are either true, false or of the form rel(t1,..,tn) where ti are
terms(e.g. individual constant symbols or variables).

Facts are atomic formulas with a "." at the end.
Query examples

A query is a conjunction of goals, which are atomic formulas.
?- resistor(power, n1),resistor(n2, power).
true.
Variables start with upper case
?- resistor(power, X).
“Does there exist an X such that the pair (power, X) belongs to the relation resistor?”
; user requests another answer
X = n1 ;
X = n2 ;
No additional answers
false
?- resistor(X, n1).
“Does there exist an X such that (X,n1) belongs to the relation resistor?”
X = power;
false
Transistor procedure:
%
%
%
%
1
2
3
Signature: transistor (Gate, Source, Drain)/3
Purpose: a transistor is commonly used as electronic
switches. The signal at the Gate controls the current
between the Source and the Drain.
transistor(n2,ground,n1).
transistor(n3,n4,n2).
transistor(n5,ground,n4).

In contrast to the resistor relation, the places of the arguments in the transistor
predicate are important. Each place has a different role.
3
Combining gates:

Combining several logic gates in a certain way, can create a complex gate.

For example: The not gate:
Combining a transistor and a resistor, as follows:
The transistor is connected to the ground at its ‘Source’
and is connected to a resistor at its ‘Drain’.
The resistor is connected on one end to the transistor and on the other end to the
power.
This creates a not gate, between the transistor's Gate and Drain.

In the program, these relations can be described by rules.
Not gate procedure:
%
%
%
%
Signature: not_gate(Input, Output)/2
Purpose: not gate is commonly used as logic
gate has one Input and one Output and we can
construct a not gate from transistor and a resistor.
1
not_gate(Input, Output) :transistor(Input, ground, Output),
resistor(power, Output).
Rule body.
, stands for “and”
Rule head
An atomic formula with variables
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.”
power
Query example:
?- not_gate(X,Y).
X=n2, Y=n1;
no
ground
4
Optional page - #1
Nand Gate procedure:
Another complex gate is the nand-gate, which is a combination of three gates.
% Signature: nand_gate(Input1,Input2,Output)/3
% Purpose: nand gate is commonly used as logic gate has two
% Inputs and one Output. We can construct nand gate from two
% transistors and resistor.
1
nand_gate(Input1,Input2,Output) :transistor(Input1,X,Output),
transistor(Input2,ground,X),
resistor(power, Output).
Note: Variables, appearing in the rule body but not in the head are existentially quantified:
"There is a nand gate of (Input1,Input2,Output), if there exists an X such that …".
power
Query examples:
?- nand_gate(In1, In2, X).
In1 = n3, In2 = n5, X = n2 ;
false.
?- not_gate(X,Y), nand_gate(In1, In2, X).
X = n2, Y = n1, In1 = n3, In2 = n5 ;
ground
Question: How can we write a query that will check if there is a nand gate that has a not
gate in each of its inputs?
answer:
?- nand_gate(In1,In2,X),
not_gate(_A,In1),
not_gate(_B,In2).

anonymous variables _A, _B are not reported.
5
2. Operational semantics: Unification algorithm, proof trees
2.1
The unification algorithm for Relational Logic Programming

The search for a proof, using the answerQuery algorithm, 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.
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 once and binding variables do not appear in the
terms.
2. A substitution can be applied on an atomic formula.
For example,
A=not_Gate(I, I) , B=not_Gate(X,Y), s={I=X}:
A º s = not_Gate(I, I) º s =
not_Gate(X, X)
B º s = not_Gate(X, Y)
3. A substitution α is a unifier of A and B if the result of its application to both A and B
is the same.
For example,
s={I=X} º{X=Y} = {I=Y, X=Y}
A º s = not_Gate(Y, Y)
B º s = not_Gate(Y, Y)
4. The Unify algorithm (appearing in the lecture notes) returns the most general
unifier of A and B.
In the example above, the most general unifier is s ={I=Y, X=Y }.
Question: Is it possible to apply the pattern matcher in ML on the above examples?
Answer: NO. In ML we saw a simpler pattern matcher and worked on simple inputs:
1. One term (parameters, data) does not contain variables (only constants).
In our example both A and B include variable.
2. A variable does not appear more than once in the second term (pattern) like
not_gate(I,I).
6
2.2
Proof trees (execution of 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,

The algorithm finishes, and the possible values of the query variables are shown.

The proof attempt loops and does not end.
The tree structure depends on Prolog's goal selection and rule selection policies:
1. Query goals appear in the root node of the proof tree.
2. Choose a current goal (Prolog's policy the leftmost goal).
3. Choose a rule to the current goal. (Prolog's policy by top to bottom program order).
4. Variables in rules are (consecutively) renamed before applying the unification algorithm.
If the unification succeeds an edge in the proof tree is created.
5. 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).
6. When a leaf node is reached, the search travels to the parent node (redo, backtrack)
and tries to match another rule to it.
Example 2 – proof trees for the logic circuit program
?-
nand_gate(In1, In2, Out).
nand_gate(In1, In2, Out)
{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)
{ In1=n2, X_1=ground, Out=n1}
{ In1=n3, X_1=n4, Out=n2}
Fact 1 – transistor
Fact 3 – transistor
Fact 2 – transistor
transistor(In2,ground,ground),
resistor(power, n1)
transistor(In2,ground,n4),
resistor(power, n2)
{ In2=n5} Fact 3 – transistor
fail
resistor(power, n2)
Fact 2 – resistor
true
7
Explanation: of actions on the edge leaving the root:
1. Rule 1 – nand_gate is renamed:
Variable names are added a consecutive number suffix.
nand_gate(Input1_1,Input2_1,Output_1) :transistor(Input1_1,X_1,Output_1),
transistor(Input2_1,ground,X_1),
resistor(power,Output_1).
2. Unify nand_gate(Input1_1,Input2_1,Output_1)
with current goal nand_gate(In1, In2, Out).
mgu={Input1_1=In1, Input2_1=In2, Output_1=Out}
3. The body of the rule after substitution is added at the top of the current goal:
transistor(In1,X_1, Out),
transistor(In2,ground,X_1),
resistor(power, Out)
Definitions:
1. A success proof tree, has at least one success path in it. A failure proof tree
does not have a success path in it.
2. A proof tree is infinite if it contains an infinite path.
For example, by repeatedly applying the rule p(X):-p(Y), q.
In the example above, the proof tree is a finite success tree, with a success path and
a failure path.
Example 3: Relational logic programs and 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
Table name: transistor
Schema: End1, End2
Schema: Gate, Source, Drain
Data: (power, n1),
Data: (n2,ground,n1)
(power, n2),
(n3,n4,n2),
(n1, power),
(n5,ground,n4).
(n2, power).
8
SQL Operations:
1) Natural Join
%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)
Query:
?- res_join_trans(End1, X, Gate, Source).
End1 = power,
X = n2,
Gate = ground,
Source = n1 ;
false.
2) Transitive closure of the resistor relation
%Signature: tr_res(X, Y)/2
tr_res(X, Y) :- resistor(X, Y).
tr_res(X, Y) :- resistor(X, Z), tr_res(Z, Y).
Query:
?- tr_res(X, 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.
9
Optional page - #2
SQL Operations:
3) Projection operation
%Signature: resistor_end1(End)/1
resistor_end1(End1):-resistor(End1,End2).
resistor_end2(End2):-resistor(End1,End2).
Query:
?- resistor_end1(End1).
End1 = power ;
End1 = power ;
End1 = n1 ;
End1 = n2.
4) Selection operation
%Signature: tr_ground_source(X1, X1, X3)/3
%Purpose: Select all transistors with ground sources.
tr_ground_source(X1, X2, X3) :- transistor(X1, X2, X3), X2 = ground
or , simply:
tr_ground_source(X1, ground, X3).
Query:
?- tr_ground_source(Y1, Y2, Y3).
Y1 = n2,
Y2 = ground,
Y3 = n1 ;
Y1 = n5,
Y2 = ground,
Y3 = n4.
Question:
Compare the SQL embedding in relational logic programming, with the SQL embedding in Scheme (in
the homework).
10
4. 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.
Example 4
tree_member procedure:
% 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 this procedure, tree_member is a predicate symbol and tree is a functor
containing three data items.
Note: 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.
Query exaples:
?- tree_member(1, tree(1,nil, nil)).
true
?- tree_member(2,tree(1,tree(2,nil,nil), tree(3,nil, nil))).
true.
?- tree_member(X,tree(1,tree(2,nil,nil), tree(3,nil, nil))).
X=1;
X=2;
X=3;
no.
?- tree_member(1, tree(3,1, 3)).
false.
?- 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 an infinite success tree with partially
instantiated answers (answers containing variables).
11
Unification in full logic programming:
Unification is more complex with functors. Here is an execution of the Unify algorithm, step
by step:
Unify(A,B) where A and B are the following atomic formulas:
A= tree_member (tree (X, 10, f(X)), W)
B =tree_member (tree (Y, Y, Z), f(Z))
1. s={X=Y}
As= tree_member (tree (Y, 10, f(Y)), W)
Bs= tree_member (tree (Y,Y,Z), f(Z))
2. s={X=10,Y=10}
As= tree_member (tree (10, 10, f(10)), W)
Bs= tree_member (tree (10,10,Z), f(Z))
3. s={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)))
4. 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)))
Question: What is the result of Unify(A,B) with:
A= tree_member (tree (X, Y, f(X)), X)
B =tree_member (tree (Y, Y, Z), f(Z))
loop : X=Y, Z=f(Y), X=f(Z)=f(f(Y))=f(f(X)) the substitution cannot be successfully solved.
12
Optional page - #3
Example 5

In this question we want to handle logic formulas and determine if they are true or
false. The formulas are compositions of basic ‘true’ and ‘false’ values, with the
operations ‘and’, ‘or’ and ‘not’.

To separate the individual constants of the problem domain, that are the basic logic
formulas (true and false), from the Prolog primitive atomic formula true and false,
we arbitrarily rename the individual constants da and niet.
% Signature: satisfiable(Formula) / 1
% Purpose: There is a true instance of the Boolean formula.
satisfiable(da).
satisfiable(and(X,Y)) :- satisfiable(X), satisfiable(Y).
satisfiable(or(X,Y)) :— satisfiable(X).
satisfiable(or(X,Y)) :- satisfiable(Y).
satisfiable(not(X))
:— invalid(X) .
% Signature: invalid (Formula) / 1
% Purpose: There is a false instance of the Boolean
%
formula (De Morgan's laws).
invalid(niet).
invalid(or(X,Y)):- invalid(X),invalid(Y).
invalid(and(X,Y)) :- invalid(X).
invalid(and(X,Y)) :- invalid(Y).
invalid(not(Y)) :- satisfiable(Y).

Here, invalid and satisfiable are predicate symbol and or, and and not
are a functors containing two (Or, And) or one (Not) data items.

Mutual recursion.
Proof tree example:
satisfiable(and(not(niet), da)).
{ X1=niet, Y1 =da}
Rule 1: satisfiabile
satisfiabile(not(niet))
satisfiabile(da)
{ X2= niet}
Rule 4: satisfiabile
invalid(niet)
satisfiabile(da)
satisfiabile(da)
Fact 2: invalid
true
13