Title: Logic, Language and Learning
1Logic, Language and Learning
- Chapter 5
- Advanced Prolog Programming Techniques
- Luc De Raedt
- Original slides by Peter Flach
2Outline
- Meta-Interpreters
- Incomplete Data Structures
- Eliza
3Meta-Interpreter
4Prolog meta-interpreters
p.70-2
A meta-interpreter for a language is an
interpreter for the language written in the
language itself. / prove(Goal) -
Goal is true given the pure Prolog program
defined by clause/2. / prove(true)-!.
prove((A,B))-!, prove(A), prove(B).
prove(A)- / not Atrue, not A(X,Y) /
clause(A,B), prove(B).
5Meta-level vs. object-level
p.71
6Meta-interpreter computing proof trees
- / solve(Goal,Tree) - Tree is a proof
tree for Goal given the program defined by
clause/2. / - solve(true,true) - !.
- solve((A,B),(ProofA,ProofB)) - !, solve(A,Pro
ofA), solve(B,ProofB).solve(A,(A-builtin))
- predicate_property(A, built_in), !, call(A).
solve(A,(A-Proof)) - clause(A,B), solve(B,Pr
oof).
7Meta-interpreter computing proof trees
- -dynamic daughter/2 , father/2, son/2,
male/1, female/1.father(abraham,isaac).male(isa
ac).father(haran,lot).
male(lot).father(haran,milcah).
female(milcah).father(haran,yiscah).
female(yiscah).daughter(X,Y) -
father(Y,X), female(X).son(X,Y) - father(Y,X),
male(X).?-solve(son(lot,X),P).P
(son(lot,haran)-(father(haran,lot)-true),(male(l
ot)-true)),X haran ?
8Meta-interpreter with depth-limit
- / solve(Goal, MaxDepth, Tree) - Tree is
a proof tree of MaxDepth for Goal given the
program defined by clause/2./solve(true,D,true
) - D gt 0,
!.solve((A,B),D,(ProofA,ProofB)) - D gt
0, !, solve(A,D,ProofA),
solve(B,D,ProofB). solve(A,D,(A-Proof)) -
D gt 0, clause(A,B), NewD is D -
1, solve(B,NewD,Proof).
9Simple tracer for pure Prolog
- / trace(Goal) - a simple tracer for pure
Prolog, tracing the proof by side
effects /trace(true) - !.trace((A,B)) -
!, trace(A),
trace(B).trace(A) - clause(A,B),
write(A), trace(B).trace(A) - \
clause(A,B), write('No clause for '),
write(A), nl, fail.
10Explanation-Based Learning
11Explanation-based learning background
- Observation Learning is more than inductive
generalization from positive and negative
examples - Sometimes a single example is enough to
generalize! - Prerequisite a non-operational theory of the
application domain - Explanation-Based Learning- derive the given
learning instance from the theory until only
operational predicates occur in the derivation-
generalize the derivation by abstracting from the
given instance (e.g., replacing constants by
variables).
12Explanation-based learning example
- safe_to_stack(X,Y) - lighter(X,Y).lighter(X,Y
) - weight(X,XW), weight(Y,YW), XW lt
YW.weight(X, W) - isa(X,box), weight1(X,W).
weight(X,5) - isa(X,table).weight1(X,W)
- volume(X,V), density(X,D), W is VD.
13Explanation-based learning example
- on(o1,o2).isa(o1,box).isa(o2,table).color(o1,r
ed).color(o2,blue).volume(o1,1).volume(o2,4).d
ensity(o1,1).op(isa(_,_)).op(on(_,_)).op(color
(_,_)).op((_ is _)).op(volume(_,_)).op(density(
_,_)).op((_ lt _)).
14Explanation-based learning example
- ?- ebl(safe_to_stack(o1,o2),
safe_to_stack(A,B), Explanation).Explanat
ion isa(A,box), volume(A,_A),
density(A,_B), times(_A,_B,_C),
isa(B,table), less_than(_C,5)
15Explanation-based learning program
- ebl((A,B),(GA,GB),Result) - !
, ebl(A,GA,ResultA), ebl(B,GB,ResultB), append(
ResultA,ResultB,Result).ebl(A,GA,GA)
- clause(A,true) . - ebl(A,GA,Result) - \ op(A), clause(GA,GB), c
opy_term((GA - GB),(A - B)), ebl(B,GB,Result).
ebl(A,GA,GA) - op(A), call(A).
16A Theorem Prover for Full Clausal Logic SATCHMO
17SATCHMO
- If M is not a model for c then c is a violated
clause wrt M - An interpretation M is a model for clausal theory
(a set of clauses) if and only if it is a model
for all clauses in the theory - A clausal theory C is not satisfiable
(inconsistent)C if and only if there
exists no model for C. Otherwise, it is
satisfiable. - Proof by refutation if and only if
- A model M is minimal for a theory T if no proper
subset of M is a model of T.
18Generating models
- Assumption clauses are range-restricted
- (all variables in the head of a clause also
- appear in the body of the clause)
-
19Generating Models
20Give some model?
- woman man - human.human - man.human -
woman.false - man, woman.woman(X) man(X)
- human(X).human(X) - man(X).human(X) -
woman(X).false - man(X), woman(X).man(dracula).
21Give some model?
- woman(X) man(X) - human(X).human(X) -
man(X).human(X) - woman(X).false - man(X),
woman(X).false - human(X), bat(X).man(dracula).
bat(dracula).
22Towards SATCHMO
- clauses predicate cl/1cl(( woman(X) man(X) -
human(X))) - Models are represented by lists of facts
- Implementation of predicates-
satisfied_head(Head,Model)- satisfied_body(Body,M
odel)
23SATCHMO in Prolog
p.111
- model(M) lt- M is a model of the clauses defined
by cl/1 - model(M)-model(,M).
- model(M0,M)-is_violated(Head,M0),!, instance
of violated clause - disj_element(L,Head), L ground literal
from headmodel(LM0,M). add L to the
model - model(M,M). no more violated
clauses - is_violated(H,M)-cl((H-B)),satisfied_body(B,M)
, grounds the variablesnot
satisfied_head(H,M).
24SATCHMO in Prolog
- satisfied_body(true,M).
- satisfied_body(A,M) -member(A,M).
- satisfied_body((A,B),M) -member(A,M),satisfied_
body(B,M). - satisfied_head(A,M) -member(A,M).
- satisfied_head((AB), M) -member(A,M).
- satisfied_head((AB), M) -satisfied_head(B, M).
25SATCHMO in Prolog
- disj_element(X,X) - \ X false, \ X
(OneTheOther).disj_element(X,(XYs)).disj_ele
ment(X,(YYs)) - disj_element(X,
Ys). cl(((woman(X)man(X)) -
human(X))).cl((human(X) - man(X))).cl((human(X)
- woman(X))).cl((false - man(X),
woman(X))).cl((false - human(X), bat(X))).
cl((man(dracula) - true)).cl((bat(dracula) -
true)). -
26Forward chaining example
married(X)bachelor(X)-man(X),adult(X). has_wife(
X)-married(X),man(X). man(paul). adult(paul).
?-model(,M)
27Difference Lists and Incomplete Data Structures
28Difference lists
- Every list can be represented as the difference
of two lists. 1,2,3 as difference between
1,2,3,4,5 and 4,5or as difference between
1,2,3Xs and Xs - Notation As\Bs.
- Such a structure is called difference list
- Why difference lists?
- Efficiency e.g., append in constant time
-
29Difference lists append
- /
- append_dl(As,Bs,Cs) -
- The difference-list Cs is the result of
appending Bs to As, where As and Bs are
compatible difference-lists. - /
- append_dl(Xs\Ys,Ys\Zs,Xs\Zs).
30Difference lists
p.65
append_dl(XPlus\XMinus,YPlus\YMinus,XPlus\YMinus)
-XMinusYPlus.
?-append_dl(a,bX\X,c,dY\Y,Z).X c,dY,
Z a,b,c,dY\Y
31Example reverse acc
- reverse(X,Y) -
- reverse (X,,Y).
- reverse(,X,X).
- reverse(XXs,Zs,Ys) -
- reverse(Xs,XZs,Ys).
32Example reverse
- reverse(Xs,Ys) - reverse_dl(Xs,Ys\).
- reverse_dl(,Xs\Xs).
- reverse_dl(XXs,Ys\Zs) - reverse_dl(Xs,Ys\XZ
s).
33Example queue fifo
- queue(S) - queue(S, Q\Q).
- queue(, Q).
- queue(enqueue(X)Xs, Q) -enqueue(X, Q, Q1),
queue(Xs, Q1). - queue(dequeue(X)Xs, Q) -dequeue(X, Q, Q1),
queue(Xs, Q1). - enqueue(X, Qh\XQt, Qh\Qt).
- dequeue(X, XQh\Qt, Qh\Qt).
34Eliza
35Eliza
- I am unhappy.
- gtgt How long have you been unhappy?
- Six months. Can you help me?
- gtgt What makes you think I help you?
- You remind me of my sister.
- gtgt Can you tell me more about your sister?
- I like teasing my sister.
- gtgt Does anyone else in your family like teasing
your sister? - No. Only me.
- gtgt Please go on.
36Eliza
- Stimulus-response pair- I am ltstatementgt.- How
long have you been ltstatementgt? - Read the input- While the input is not bye -
Choose a stimulus-response pair - Match the
input to the stimulus - Generate the reply from
the response and the above match - Output
the response - Read the next input
37Example dictionary lookup
- /
- lookup(Key,Dictionary,Value) - Dictionary
contains Value indexed under key. Dictionary
is represented as an incomplete list of
pairs of the form (Key,Value). - /
- lookup(Key,(Key,Value)Dict,Value).
- lookup(Key,(Key1,Value1)Dict,Value) - Key
\ Key1, lookup(Key,Dict,Value).
38Example dictionary lookup
- / lookup(Key,Dictionary,Value) -
Dictionary contains the value indexed under
Key. Dictionary is represented as an ordered
binary tree./lookup(Key,dict(Key,X,Left,Right)
,Value) - !, X Value.lookup(Key,dict(Key1
,X,Left,Right),Value) - Key lt Key1,
lookup(Key,Left,Value).lookup(Key,dict(Key1,X,Le
ft,Right),Value) - Key gt Key1,
lookup(Key,Right,Value).
39Eliza (1)
- / pattern(Stimulus,Response) - Response
is an applicable response pattern to the
pattern Stimulus./pattern(i,am,1,'How',long
,have,you,been,1,?).pattern(1,you,2,me,'What'
,makes,you,think,'I',2,you,?).pattern(i,like,1
,'Does',anyone,else,in,your,family,like,1,?).pa
ttern(i,feel,1,'Do',you,often,feel,that,way,?)
.pattern(1,X,2,'Please',you,tell,me,more,about
,X) - important(X).pattern(1,'Please',go,o
n,'.'). important(father).important(mother).i
mportant(sister).important(brother).important(so
n).important(daughter).
40Eliza (2)
- reply() - nl.
- reply(HeadTail) -write(Head),write('
'),reply(Tail). - lookup(X,(X,V)XVs,V).
- lookup(X,(X1,V1)XVs,V) -X \
X1,lookup(X,XVs,V).
41Eliza (3)
- /
- eliza - Simulates a conversation via side
effects. - /
- eliza - read(Input), eliza(Input), !.
- eliza(bye) -writeln('Goodbye. I hope I have
helped you'). - eliza(Input) -pattern(Stimulus,Response),match(
Stimulus,Table,Input),match(Response,Table,Output
),reply(Output),read(Input1),!,eliza(Input1).
42Eliza (4)
- / match(Patterm,Dictionary,Words) -
Pattern matches the list of words Words, and
matchings are recorded in the Dictionary./matc
h(NPattern,Table,Target) - integer(N),
lookup(N,Table,LeftTarget), append(LeftTarget,Ri
ghtTarget,Target), match(Pattern,Table,RightTarg
et).match(WordPattern,Table,WordTarget)
- atom(Word), match(Pattern,Table,Target).m
atch(,Table,).