Logic Programming PDF
Logic Programming PDF
Logic Programming PDF
D. Vermeir
1 / 259
Preliminaries
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
2 / 259
Preliminaries
Organization
Evaluation: 50% project, 50% (closed book) theory exam. Exercise sessions: rst one on Wed. Sep. 30 2009, 15:00-17:00, IG. Book. Copies of transparencies. See website for further information.
3 / 259
Preliminaries
Contents
1 2 3 4 5 6 7 8
Introduction. Clausal logic. Logic programming in Prolog (incl. meta-programming). Representing structured knowledge. Search. Language processing using denite clause grammars. Reasoning using incomplete information (incl. abduction). Inductive logic programming (concept learning).
4 / 259
Introduction
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
5 / 259
Introduction
introduction
Introduction
7 / 259
Introduction
derived information
Two stations are near if they are on the same line, with at most one station in between
near(bond_street,oxford_circus) near(oxford_circus,tottenham_court_road) near(bond_street,tottenham_court_road) near(bond_street,green_park) near(green_park,charing_cross) near(bond_street,charing_cross) % etc. (16 formulas)
8 / 259
Introduction
reads: For any values of X , Y , Z and L, X is near Y if X is connected to Z via L, and Z is connected to Y via L. or X , Y , Z , L connected(X , Z , L) connected(Z , Y , L) near (X , Y )
9 / 259
Introduction
queries
?- connected(W,tottenham_court_road,L)
match it with the conclusion of near(X,Y) :- connected(X,Y,L) yielding the substitution { X = tottenham_court_road, Y = W } and try to nd an answer to the premises
?- connected(tottenham_court_road,W,L)
10 / 259
Introduction
{ W = leicester_square L = northern }
To solve a query ? Q1 , . . . , Qn nd a rule A : B1 , . . . , Bm where A matches Q1 and solve ? B1 , . . . , Bm , Q2 , . . . , Qn Resolution gives a procedural interpretation to logic.
11 / 259
Introduction
proof by refutation
Note: the procedural interpretation = the declarative semantics (e.g. because of looping). The proof technique used is reductio ad absurdum or proof by refutation: assume that the formula (query) is false and deduce a contradiction:
?- near(tottenham_court_road,W)
stands for W near (tottenham_court_road, W ) false or there are no stations near tottenham_court_road
12 / 259
Introduction
recursive rules
reachable(X,Y) :- connected(X,Y,L). reachable(X,Y) :- connected(X,Z,L), reachable(Z,Y). ?- reachable(bond_street,W)
: reachable(bond_street,W) reachable(X,Y) : connected(X,Z,L), reachable(Z,Y) { X = bond_street, Y=W} : connected(bond_stree,Z,L), reachable(Z,W) connected(bond_street,oxford_circus, central) { Z = oxford_circus, L = central } : reachable(oxford_circus,W) reachable(X,Y) : connected(X,Z,L), reachable(Z,Y). { X = oxford_circus, Y = W} : connected(oxford_circus,Z,L), reachable(Z,W) connected(oxford_circus, tottenham_court_road, central) { Z = tottenham_court_road,L=central} : reachable(tottenham_court_road,W) reachable(X,Y) : connected(X,Y,L) { X = tottenham_court_road, Y = W } : connected(tottenham_court_road,W,L) connected(tottenham_court_road, leicester_square,norhtern) { W = leicester_square, L = northern }
13 / 259
Introduction
Prolog uses depth-rst search when nding a proof, backtracking when it fails, until a solution is found or there are no more possibilities. It tries rules and facts in the given order, always trying to resolve the rst subgoal.
14 / 259
Introduction
functors
represents
route
tottenham_court_road
route
leicester_square
noroute
15 / 259
Introduction
using functors
reachable(X,Y,noroute) :- connected(X,Y,L). reachable(X,Y,route(Z,R)) :connected(X,Z,L), reachable(Z,Y,R). ?- reachable(oxford_circus,charing_cross,R) { R = route(tottenham_court_road, route(leicester_square,noroute))} { R = route(piccadilly_circus,noroute)} { R = route(piccadilly_circus, route(leicester_square,noroute))} route(oxford_circus, route(leicester_square,noroute))
represents a route via .. Note: functors are not evaluated in normal LP.
16 / 259
Introduction
lists
Lists are also represented by a functor . (compare cons). E.g. the list [a,b,c] is represented as .
a .
[]
which can also be written as .(a,.(b,.(c,[]))) We also use [Head | Tail ] where Tail is a list, as a shorthand for
.(Head,Tail)
17 / 259
Introduction
using lists
A route can be represented by a list:
reachable(X,Y,[]) :- connected(X,Y,L). reachable(X,Y,[Z|R]) :connected(X,Z,L), reachable(Z,Y,R). ?- reachable(oxford_circus,charing_cross,R) {R=[tottenham_court_road,leicester_square]} {R=[piccadilly_circus]} {R=[piccadilly_circus,leicester_square]}
To ask from which station we can reach charing_cross via 4 intermediate stations:
?- reachable(X,charing_cross,[A,B,C,D])
18 / 259
Clausal logic
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
19 / 259
Clausal logic
clausal logic
Any logic system has a: syntax: which sentences are legal. semantics: the meaning of sentences, i.e. what is the truth value of a sentence. proof theory: how to derive new sentences (theorems) from assumed ones (axioms) by means of inference rules. A logic system is called sound if anything you can prove is true complete if anything that is true can be proven
20 / 259
Clausal logic
proposition : atom atom : single word starting with lower case example:
married ; bachelor :- man, adult
Clausal logic
logic program
a program is a nite set of clauses, each terminated by a period; the clauses are to be read conjunctively
woman;man :- human. human :- man. human :- woman.
in traditional logic notation: (human (woman man)) (man human) (woman human) Using A B A B we get (human woman man) (man human) (woman human)
22 / 259
Clausal logic
clause
In general a clause H1 ; . . . ; Hn : B1 , . . . , Bm is equivalent with H1 . . . Hn B1 . . . Bm A clause can also be dened as L1 L2 . . . Ln where each Li is a literal, i.e. Li = Ai or Li = Ai , with Ai a proposition.
23 / 259
Clausal logic
special clauses
An empty head stands for false, an empty body stands for true:
man :- . :- impossible. % usually written as man.
24 / 259
Clausal logic
semantics
The Herbrand base BP of a program P is the set of all atoms occurring in P. A Herbrand interpretation of P is a mapping i : BP {true, false} We will represent i by I = i 1 (true), the set of true propositions. An interpretation is a model for a clause if the clause is true under the interpretation, i.e. if either the head is true or the body is false. An interpretation is a model for a program if it is a model for each clause in the program.
25 / 259
Clausal logic
example
Consider P:
woman;man :- human. human :- man. human :- woman.
Then: BP = {woman, man, human} and a possible interpretation is i = {(woman, true), (man, false), (human, true)} or I = {woman, human} All clauses in P are true under I so it is a model of P. The interpretation J = {woman} is not a model of P.
26 / 259
Clausal logic
logical consequence
A clause C is a logical consequence of a program P, denoted P |= C if every model of P is also a model of C. E.g. for P:
woman. woman;man :- human. human :- man. human :- woman.
we have that P |= human. Note that P has two models: M1 = {woman, human} M2 = {woman, man, human} Intuitively, M1 is preferred since it only accepts what must be true.
27 / 259
Clausal logic
minimal models
Thus we could dene the best model to be the minimal one. However, consider P :
woman;man :- human. human.
P has 3 models M1 = {woman, human} M2 = {man, human} M3 = {woman, man, human} and M1 and M2 are both minimal! If we restrict (as in Prolog) to denite clauses, which have at most one atom in the head, then:
Theorem
A denite logic program has a unique minimal model.
28 / 259
Clausal logic
proof theory
How to compute logical consequences without checking all the models?
has_wife;bachelor:man,adult
29 / 259
Clausal logic
resolution: intuition
married;bachelor :- man,adult. has_wife :- man,married.
either married and then man has_wife or married and then man adult bachelor thus man adult bachelor man has_wife
has_wife;bachelor :- man,adult.
30 / 259
Clausal logic
E1 E2 E2 E3 E1 E3
31 / 259
Clausal logic
A AB B In clause notation: A A B B
32 / 259
Clausal logic
B AB A In clause notation B A B A
33 / 259
Clausal logic
Denition
A proof or derivation of a clause C from a program P is a sequence of clauses C0 , . . . , Cn = C such that i = 0 . . . n : either Ci P or Ci is the resolvent of Ci1 and Ci2 (i1 < i, i2 < i). We write P C if there is a proof of C from P.
34 / 259
Clausal logic
soundness, completeness
Theorem
Resolution is sound for propositional clausal logic, i.e. if P P |= C. About completeness: P |= C iff each model of P is a model of C iff no model of P is a model of C If C L1 L2 . . . Ln then C then
(1)
C L1 L2 . . . Ln {L1 , L2 , . . . , Ln } i.e. C is a set of clauses and so (1) P C has no model iff P C is inconsistent (by denition)
35 / 259
Clausal logic
completeness
Theorem
If Q is inconsistent then Q where . is the empty clause (true false) which has no model.
Theorem
Resolution is complete for propositional clausal logic, i.e. if P |= C then P C I.e. C is a logical consequence of P iff one can derive, using resolution, the empty clause ( ) from P C.
36 / 259
Clausal logic
Example: consider P2
happy :- has_friends. friendly :- happy.
P2 C is
happy :- has_friends. friendly :- happy. has_friends. :- friendly. (1) (2) (3) (4)
The proof:
(2+4) :- happy (1+5) :- has_friends (3+6) <empty-clause> (5) (6)
37 / 259
Clausal logic
variable : single word starting with upper case term : constant | variable predicate : single word starting with lower case atom : predicate[(term[, term] )] clause : head[: body ] head body : [atom[; atom] ] : atom[, atom]
likes(peter,S) :- student_of(S,peter)
Clausal logic
39 / 259
Clausal logic
Consider P3
likes(peter,S) :- student_of(S,peter). % C1 student_of(maria,peter).
BP3 =
{ likes(peter,peter), likes(peter,maria), likes(maria,peter), likes(maria,maria), student_of(peter,peter), student_of(peter,maria), student_of(maria,peter), student_of(maria,maria) }
Denition
A substitution is a mapping : Var Trm. For a clause C, the result of on C, denoted C is obtained by replacing all occurrences of X Var in C by (X ). C is an instance of C. If = {S/maria} then C1 is
likes(peter,maria) :- student_of(maria,peter).
40 / 259
Clausal logic
semantics
Denition
A ground instance of a clause C is the result C of some substitution such that C contains but ground atoms. Ground clauses are like propositional clauses.
Denition
An interpretation I is a model of a clause C iff it is a model of every ground instance of C. An interpretation is a model of a program P iff it is a model of each clause C P. All ground instances of clauses in P3 :
likes(peter,peter) :- student_of(peter,peter). likes(peter,maria) :- student_of(maria,peter). student_of(maria,peter).
Clausal logic
proof theory
Naive version: do (propositional) resolution with all ground instances of clauses in P.
Denition
A substitution is a unier of two atoms a1 and a2 iff a1 = a2 . A substitution 1 is more general than 2 if 2 = 1 for some substitution . A unier of a1 and a2 is a most general unier (mgu) of a1 and a2 iff it is more general than any other unier of a1 and a2 .
Theorem
If two atoms are uniable then they their mgu is unique up to renaming.
42 / 259
Clausal logic
43 / 259
Clausal logic
44 / 259
Clausal logic
is there anyone whom peter likes (query) add peter likes nobody ( :- likes(peter,N))
:likes(peter,N) likes(peter,S):student_of(S,peter) {S/N} :student_of(N,peter) {S/N,T/peter} student_of(S,T):follows(S,C), teaches(T,C)
and thus
45 / 259
Clausal logic
soundness, completeness
Theorem
Relational clausal logic is sound and (refutation) complete: P C P |= C
P {C} inconsistent P {C} New formulation is because: (X p(X )) X p(X ) but p(X). X p(X ) while :- p(X). X p(X )
46 / 259
Clausal logic
Theorem
The question P |= C is decidable for relational clausal logic Because the Herbrand base is nite.
47 / 259
Clausal logic
Clausal logic
semantics
M5 =
{ plus(0,0,0), plus(s(0),0,s(0), plus(s(s(0)),0,s(s(0)), ... plus(0,s(0),s(0)), plus(s(0),s(0),s(s(0))), ... ... }
49 / 259
Clausal logic
Theorem
if solve({t1 = t2 }) succeeds, it returns mgu(t1 , t2 ).
50 / 259
Clausal logic
proc solve(var E
: set of equations)
repeat select s = t E case s = t of f (s1 , . . . , sn ) = f (t1 , . . . , tn ) (n 0) : replace s = t by {s1 = t1 , . . . , sn = tn } f (s1 , . . . , sm ) = g(t1 , . . . , tn ) (f /m = g/n) : fail X =X : remove X = X from E t = X (t Var) : replace t = X by X = t X = t (X Var X = t X occurs more than once in E) : if X occurs in t then fail else replace all occurrences of X in E (except in X = t) by t esac until no change
51 / 259
Clausal logic
examples
{f (X , g(Y )) = f (g(Z ), Z )} {X = g(Z ), g(Y ) = Z } {X = g(Z ), Z = g(Y )} {X = g(g(Y )), Z = g(Y )} {X /g(g(Y )), Z /g(Y )} {f (X , g(X ), b) = f (a, g(Z ), Z )} {X = a, g(X ) = g(Z ), b = Z } {X = a, X = Z , b = Z } {X = a, a = Z , b = Z } {X = a, Z = a, b = Z } {X = a, Z = a, b = a}
fail
52 / 259
Clausal logic
occur check
{l(Y , Y ) = l(X , f (X ))} {Y = X , Y = f (X )} {Y = X , X = f (X )} fail The last example illustrates the need for the occur check (which is not done in most Prolog implementations)
53 / 259
Clausal logic
soundness, completeness
Theorem
Full clausal logic is sound and (refutation) complete: P C P |= C
P {C} inconsistent P {C} However the question P |= C is only semi-decidable, i.e. there is no algorithm that will always answer the question (with yes or no) in nite time; but there is an algorithm that, if P |= C, will answer yes in nite time but this algorithm may loop if P |= C. This means that prolog may loop on certain queries.
54 / 259
Clausal logic
left..right
man(paul) :bachelor(paul)
55 / 259
Clausal logic
For efciency reasons: restriction to denite clauses where the head contains at most 1 atom. A : B1 , . . . , Bn prove A by proving each of B1 , . . . , Bn . This is the procedural interpretation of denite clauses. It makes the search for a refutation much more efcient. Problem: how to represent
married(X);bachelor(X) :- man(X), adult(X).
56 / 259
Clausal logic
general clauses
A (pseudo-denite) general clause may contain negations in the body:
married(X) :- man(X), adult(X), not(bachelor(X)).
as a minimal model.
57 / 259
Clausal logic
becomes (man adult married bachelor ) (married haswife) or, using A B A B and (A B) A B: (man adult married bachelor ) (married haswife) which is in conjunctive normal form (a conjunction of disjunctions of literals).
58 / 259
Clausal logic
becomes X Y Z RL : connected(X , Z , L) reachable(Z , Y , R) reachable(X , Y , route(Z , R)) Note that nonempty(X) :- contains(X,Y). becomes X Y : nonempty (X ) contains(X , Y ) X : (Y : nonempty (X ) contains(X , Y )) X : (nonempty (X ) (Y : contains(X , Y ))) X : nonempty (X ) (Y : contains(X , Y )) X : (Y : contains(X , Y )) nonempty (X )
For each rst order sentence, there exists an almost equivalent set of clauses.
59 / 259
Clausal logic
algorithm
X [brick (X ) (Y [on(X , Y ) pyramid(Y )] Y [on(X , Y ) on(Y , X )] Y [brick (Y ) equal(X , Y )])] Step 1: eliminate using A B A B. X [brick (X ) (Y [on(X , Y ) pyramid(Y )] Y [on(X , Y ) on(Y , X )] Y [(brick (Y )) equal(X , Y )])] Step 2: move inside using (A B) A B (A B) A B (A) A X [p(X )] X [p(X )] (X [p(X )] X [p(X )]
60 / 259
Clausal logic
X [brick (X ) (Y [on(X , Y ) pyramid(Y )] Y [on(X , Y ) on(Y , X )] Y [brick (Y ) equal(X , Y )])] Step 3: replace using skolem functors E.g. X Y : likes(X , Y ) becomes X : likes(X , f (X )) where f is a new Skolem functor. All universally quantied variables in whose scope occurs become arguments of the Skolem term. E.g. X : likes(peter , X ) becomes likes(peter , g). In clausal logic, one is forced to use abstract names (using functors) for existentially quantied individuals.
61 / 259
Clausal logic
In example: X [brick (X ) (Y [on(X , Y ) pyramid(Y )] Y [on(X , Y ) on(Y , X )] Y [brick (Y ) equal(X , Y )])] becomes X [brick (X ) ([on(X , sup(X )) pyramid(sup(X ))] Y [on(X , Y ) on(Y , X )] Y [brick (Y ) equal(X , Y )])] Step 4: rename variables (make unique) X [brick (X ) ([on(X , sup(X )) pyramid(sup(X ))] Y [on(X , Y ) on(Y , X )] Z [brick (Z ) equal(X , Z )])]
62 / 259
Clausal logic
X [brick (X ) ([on(X , sup(X )) pyramid(sup(X ))] Y [on(X , Y ) on(Y , X )] Z [brick (Z ) equal(X , Z )])] Step 5: bring to front X Y Z [brick (X ) ([on(X , sup(X )) pyramid(sup(X ))] [on(X , Y ) on(Y , X )] [brick (Z ) equal(X , Z )])]
63 / 259
Clausal logic
Step 6
: bring inside using A (B C) (A B) (A C) X Y Z [brick (X )([on(X , sup(X )) pyramid(sup(X ))] [on(X , Y ) on(Y , X )][brick (Z ) equal(X , Z )])] becomes X Y Z [brick (X ) ([on(X , sup(X ))pyramid(sup(X ))] [brick (X ) on(X , Y ) on(Y , X )] [brick (X ) brick (Z ) equal(X , Z )])] X Y Z [brick (X ) on(X , sup(X ))] [brick (X ) pyramid(sup(X ))] [brick (X ) on(X , Y ) on(Y , X )] [brick (X ) brick (Z ) equal(X , Z )])]
64 / 259
Clausal logic
Step 7
: eliminate by splitting X [brick (X ) on(X , sup(X ))] X [brick (X ) pyramid(sup(X ))] X Y [brick (X ) on(X , Y ) on(Y , X )] X Z [brick (X ) brick (Z ) equal(X , Z )])] Step 8: rename variables (make unique) X [brick (X ) on(X , sup(X ))] W [brick (W ) pyramid(sup(W ))] UY [brick (U) on(U, Y ) on(Y , U)] V Z [brick (V ) brick (Z ) equal(V , Z )])] Step 9: make clauses
on(X,sup(X)) :- brick(X). :- brick(W), pyramid(sup(W)). :- brick(U), on(U,Y), on(Y,U). brick(Z) :- brick(V), equal(V,Z).
65 / 259
Clausal logic
Theorem
Let f be an n-ary partial recursive function. There exists a denite clause program Pf and an n + 1-ary predicate symbol pf such that the query : pf (sk1 (0), . . . , skn (0), X ) returns {X /sk (0)} iff f (k1 , . . . , kn ) = k
66 / 259
Logic programming
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
67 / 259
Logic programming
logic programming
sentences in clausal logic: have a declarative meaning (e.g. order of atoms in the body is irrelevant) have a procedural meaning Thus clausal logic can be used as a programming language:
1
write down knowledge in a (declarative) program that species what the problem is rather than how it should be solved apply inference rules to nd solution algorithm = logic + control
68 / 259
Logic programming
where logic is declarative knowledge and control is procedural knowledge Prolog is not a purely declarative language since e.g. the order of the rules matters. Prologs proof procedure is based on resolution refutation in denite clause logic where a resolution strategy is xed: which literal to resolve upon which clause to resolve with
69 / 259
Logic programming
sld refutation
grandfather(X,Z) :- father(X,Y), parent(Y,Z). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(a,b). mother(b,c).
:grandfather(a,X) goal (query) grandfather(C,D):father(C,E),parent(E,D). {C/a,D/X} :father(a,E),parent(E,X). derived goal father(a,b). {E/b} :parent(b,X). parent(U,V):mother(U,V). {U/b,V/X} :mother(b,X). mother(b,c). computed substitution {X/c} {X/c,C/a,D/c,E/b,U/b,V/c} computed answer substitution
70 / 259
Logic programming
SLD
Always resolve with (derived) goal: Linear proof trees. Use Selection rule to determine literal in goal to resolve with. Programs with Denite clauses only. Prologs selection rule: Consider goal literals left to right. (Try clauses in order of occurrence in program) SLD tree: shows (only) alternative resolvents
71 / 259
Logic programming
SLD trees
:grandfather(a,X) :father(a,E),parent(E,X) :parent(b,X) :father(b,X) blocked :mother(b,X)
Every leaf corresponds to a successful refutation ( a success branch). A blocked leaf corresponds to a failed branch. Prolog does a depth-rst traversal of an SLD tree. What if an SLD tree has innite branches?
72 / 259
Logic programming
Logic programming
Get trapped in innite subtree: thus Prolog is incomplete. We could do e.g. breadth-rst search but Prolog sacrices completeness for efciency (also in memory usage). Any innite SLD tree may cause the interpreter to loop if no (more) answers are to be found: this is because clausal logic is only semi-decidable. Thus one should be aware of the Prolog strategy (procedural knowledge), e.g. recursive clauses after non-recursive ones be careful with symmetric predicates: p(X,Y) :- p(Y,X)
74 / 259
Logic programming
The meaning of cut (!): dont try alternatives for the literals to the left of the cut the clause in which the cut is found (A cut is always true.)
75 / 259
Logic programming
A green cut does not cut away any success branches. A red cut does, making the procedural meaning of the program different from the declarative meaning.
76 / 259
Logic programming
the dangers of !
77 / 259
Logic programming
the dangers of !
This is not equivalent! (try max(5,3,3) ). The program would be correct if only queries of the form
max(a,b,X)
were asked.
78 / 259
Logic programming
negation as failure
Cut can be used to ensure that the bodies of clauses are mutually exclusive.
p :- q,!,r. p :- s. % only if q fails
is equivalent to
p :- q, r. p :- not_q,s. not_q :- q,!,fail. not_q.
where fail is always false. More general: meta-predicate not/1 implementing negation by failure.
not(Goal) :- Goal,!,fail. not(Goal).
79 / 259
Logic programming
naf examples
p :- q, r. p :- not(q), s. s. % not(q) :- q,!,fail. % not(q).
:p :q,r :not(q),s.
:q,!,fail,s
:s
80 / 259
Logic programming
naf examples
p :- q,!,r. p :- s. s. % more efficient but less clear
:p :q,!,r :s
81 / 259
Logic programming
naf examples
p :- not(q), r. p :- q. q. r. % not(q) :- q,!,fail. % not(q).
:p :not(q),r :q
:q,!,fail,r
:r
:!,fail,r
:fail,r
82 / 259
Logic programming
oundering
occurs when the argument of not/1 is not grounded.
bachelor(X) :- not(married(X)), man(X). man(fred). man(peter). married(peter).
:bachelor(X)
:not(married(X)),man(X)
:married(X),!,fail,man(X)
man(X)
:!,fail,man(peter)
:fail,man(peter)
Logic programming
sldnf resolution
SLDNF resolution says that not(Goal) fails only if Goal has a refutation with an empty answer substitution (in the example: if married(X).). Prolog does not check this; hence Prolog is not sound w.r.t. negation by failure. If Goal is ground, only empty answer substitutions are possible... The example can be xed by changing the order of the body of the rule:
bachelor(X) :- man(X), not(married(X)).
84 / 259
Logic programming
if .. then .. else ..
p:- q,r,s,!,t. p:- q,r,u. q. r. u. q
In most prologs:
diagnosis(P,C):- % C: condition, P: patient temperature(P,T), (T=<37 -> blood_pressure(P,C) ;T>37,T<38 -> Condition = ok ;otherwise -> fever(P,C) ).
85 / 259
Logic programming
Cut ensures that no previous moves are reconsidered and optimizes tail recursion to iteration.
86 / 259
Logic programming
arithmetic in prolog
nat(0). nat(s(X)):- nat(X). add(0,X,X). add(s(X),Y,s(Z)):- add(X,Y,Z). mul(0,X,0). mul(s(X),Y,Z):mul(X,Y,Z1), add(Y,Z1,Z).
Not efcient!
is(Result,expression) is true iff expression can be evaluated as an expression and its resulting value unied with Result ?- X X ?- X X is 5+7-3 = 9 is 5*3+7/2 = 18.5
87 / 259
Logic programming
is/2 is different from =/2 ; the latter succeeds if its arguments can be unied. ?- X = 5+7-3 X = 5+7-3 ?- 9 = 5+7-3 no ?- X = Y+3 X = _947+3 Y = _947 ?- X = f(X) X = f(f(f(f(f(f(f(f(f(f(f(f(f(f(f( .. error: term being written is too deep
The last example illustrates that Prolog does not implement the occur check. Prolog also has other built-in arithmetic predicates: <,>,=<,>=. \=/2 succeeds if its arguments are not uniable.
88 / 259
Logic programming
accumulators
Tail-recursive clauses are more efcient.
length([],0). length([H|T],N) :length(T,N1), N is N1+1.
The program is not tail-recursive. It can be made tail-recursive by introducing an accumulator: Read length_acc(L,M,N) as N = M + length(L).
length(L,N) :- length_acc(L,0,N). length_acc([],N,N). length_acc([H|T],N0,N) :% N0 is "length so far" N1 is N0+1, length_acc(T,N1,N).
89 / 259
Logic programming
reverse/2
90 / 259
Logic programming
Logic programming
difference lists
X XY
Represent a list by a term L1-L2. [a,b,c,d]-[d] [a,b,c] [a,b,c,1,2]-[1,2] [a,b,c] [a,b,c|X]-X [a,b,c]
92 / 259
Logic programming
difference lists
reverse(X , Y , Z ) Z = reverse(X ) + Y reverse(X ) = Z Y and reverse([H|T ], Y , Z ) Z = reverse([H|T ]) + Y Z = reverse(T ) + [H|Y ] reverse(T ) = Z [H|Y ]
Logic programming
X Z + XZ ZY = XY Y
94 / 259
Logic programming
append_dl(X-Z, Z-Y, X-Y). :- append_dl([abc|A] - A, [de|B] - B, D) % unify with append_dl(X-Z, Z-Y, X-Y) X = [abc|A] Z = A = [de|B] Y = B D = X - Y = [abc|A] - B = [abcde|B] - B
95 / 259
Logic programming
example: atten/2
atomic/1
96 / 259
Logic programming
97 / 259
Logic programming
Most systems do not allow R(X,Y) in the body. Term=..List is true iff
Term Term
is a compound term f(A1,..,An) and List is a list with head f and whose tail unies with [A1,..,An]
map(R,[],[]). map(R,[X|Xs],[Y|Ys]):Goal =.. [R,X,Y], call(Goal), map(R,Xs,Ys).
98 / 259
Logic programming
ndall/3
is true iff Bag unies with the list of values to which a variable X not occurring in Term or Goal would be bound by successive resatisfactions of (call(Goal), X=Term) after systematic replacement of all variables in X by new variables.
findall(Term,Goal,Bag) parent(a,b). parent(a,c). parent(a,d). parent(e,f). parent(e,g). children(Parent,Children):findall(C,parent(Parent,C),Children). ?-children(a,Children) Children = [b,c,d]
99 / 259
Logic programming
bagof/3, setof/3
parent(a,b). parent(a,c). parent(e,f). parent(e,g). ?-bagof(C,parent(P,C),L) C = _951 P = a L = [b,c,d] ; C = _951 P = e L = [f,g] ?-bagof(C,P^parent(P,C),L) C = _957 P = _958 L = [b,c,d,f,g] (P ^ parent(P,C)) setof/3 parent(a,d).
Logic programming
assert/1, retract/1
Variables in Prolog are local to the clause. Global variables can be simulated using:
asserta(Clause)
adds Clause at the beginning of the Prolog adds Clause at the end of the Prolog database.
database.
assertz(Clause) retract(Clause)
removes rst clause that unies with Clause from the Prolog database.
101 / 259
Logic programming
operators
In Prolog, functors and predicates are called operators. Operators can be declared using
:op(Priority,Type,Name)
where Priority is a number between 0 and 1200 (lower priority binds stronger) Type is fx or fy (prex), xfx,xfy or yfx (inx), and xf or yf (postx) The x and y determine associativity:
associative X op Y op Z no xfx no right xfy op(X,op(Y,Z)) left yfx op(op(X,Y),Z)
102 / 259
Logic programming
meta-programs
Clauses are represented as terms :- (Head,Body) where :- can be treated as a functor (meta-level) or as a predicate (object-level).
% if A and B then C = if(then(and(A,B),C)) :- op(900,fx,if). :- op(800,xfx,then). :- op(700,yfx,and). % object-level rules if has_feathers and lays_eggs then is_bird. if has_gills and lays_eggs then is_fish. if tweety then has_feathers. if tweety then lays_eggs.
103 / 259
Logic programming
meta-program
derive(if Assumptions then Goal):if Body then Goal, derive(if Assumptions then Body). derive(if Assumptions then G1 and G2):derive(if Assumptions then G1), derive(if Assumptions then G2). derive(if Assumptions then Goal):assumed(Goal,Assumptions). % Goal is one of the assumptions assumed(A,A). assumed(A,A and As). assumed(A,B and As):assumed(A,As).
104 / 259
Logic programming
prolog meta-interpreter
prove(Goal):clause(Goal,Body), prove(Body). prove((Goal1,Goal2)):prove(Goal1), prove(Goal2). prove(true).
105 / 259
Logic programming
quicksort
% partition(l,n,Smalls,Bigs): % Smalls contains numbers in l % smaller than n, Bigs the rest. partition([],N,[],[]). partition([H|T],N,[H|Small],Big):H<N,partition(T,N,Small,Big). partition([H|T],N,Small,[H|Big]):H>=N,partition(T,N,Small,Big). quicksort([],[]). quicksort([X|Xs],Sorted):partition(Xs,X,Small,Big), quicksort(Small,S_Small), quicksort(Big,S_Big), append(S_Small,[X|S_Big],Sorted).
106 / 259
Logic programming
towers of hanoi
:- op(900,xfx,to). % hanoi(N,A,B,C,Moves): Moves is the list of moves to % move N disks from peg A to peg C, using peg B as % an intermediary. hanoi(0,A,B,C,[]). hanoi(N,A,B,C,Moves):N1 is N-1, % assume solved for N-1 disks hanoi(N1,A,C,B,Moves1), hanoi(N1,B,A,C,Moves2), append(Moves1,[A to C|Moves2],Moves). ?- hanoi(3,left,middle,right,M) M = [ left to right, left to middle, right to middle, left to right, middle to left, middle to right, left to right ]
107 / 259
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
108 / 259
Knowledge is structured if its components have certain logical relationships. Explicit relationships are represented directly (as facts). Implicit relationships are found by reasoning. Reasoning is often done by searching, e.g. in a graph, a term etc.
109 / 259
110 / 259
% term_arc(Tree, Arc): A is arc in Tree term_arc(Tree, [Root, SubRoot]):- % from root term_root(Tree, Root), term_subtree(Tree, SubTree), term_root(Subtree, SubRoot). term_arc(Tree, Arc):- % in subtree term_subtree(Tree, Subtree), term_arc(Subtree, Arc). % term_path(Tree, Path): Path is path in Tree term_path(Tree, Path):- % an arc is a path term_arc(Tree, Path). term_path(Tree, [Node1,Node2|Nodes]):term_arc(Tree,[Node1,Node2]), term_path(Tree,[Node2|Nodes]).
111 / 259
112 / 259
:-
term_write(f1(f2(f4,f5(f7),f6),f3(f8,f9(f10))))
113 / 259
114 / 259
sld trees
% resolve(goal, clause, newGoal): % newGoal is resolvent of goal using clause resolve([Literal|Literals], (Head:- Body), NewGoal):Literal = Head, % unify with head append(Body, Literals, NewGoal). % an arc in an SLD tree arc(Goal1, Goal2):clause(Head, Body), resolve(Goal1, (Head:- Body), Goal2). prove(Goal):path(Goal, []). % where path(N1, N2) :arc(N1, N2). path(N1, N3) :arc(N1, N2), path(N2, N3).
115 / 259
inheritance hierarchies
Instrument Wind String Percussion
Properties:
material(flute, metal). % string instruments are made of wood material(X, wood) :- woodwind(X). material(X, wood) :- string(X). material(X, metal) :- brass(X). material(X, metal) :- percussion(X). ?- material(piano, X) X = wood ?- material(flute, X) X = metal; X = wood
Putting most specic clauses rst ensures that the rst answer is correct.
117 / 259
function(X, musical) :- instrument(X). action(oboe, reed(double)). action(saxophone, reed(single)). action(piano, hammered). action(X, hammered) :- percussion(X).
118 / 259
Questions about classes are not easy to answer since one must resort to second-order programming. Design alternative representation where both classes and instances are represented by terms.
119 / 259
semantic networks
Represent hierarchy as set of facts.
isa(wind,instrument). isa(woodwind,wind). isa(brass,wind). % etc. inst(oboe,woodwind). inst(flute,woodwind). inst(trumpet,brass). % etc. % class properties: prop(instrument,function,musical). prop(woodwind,material,wood). prop(brass,material,metal). prop(brass,action,reed(lip)). % instance properties prop(flute,material,metal). prop(oboe,action,reed(double)). % ..
120 / 259
121 / 259
% override(SpecificProps,ClassProps,Ps): Ps contains all % SpecificProps and those ClassProps that are not % overridden by SpecificProps. override(Properties, [], Properties). override(Properties, [Attr=AnyValue|ClassProperties], ExtendedProperties):element(Attr=Value, Properties), override(Properties, ClassProperties, ExtendedProperties). override(Properties, [Attr=Value|ClassProperties], [Attr=Value|ExtendedProperties]):not(element(Attr=AnyValue, Properties)), override(Properties, ClassProperties, ExtendedProperties).
122 / 259
frame-based inheritance
123 / 259
frame-based inheritance 2
properties(Instance, Properties):instance(Instance, Class, InstanceProperties), inherit(Class, InstanceProperties, Properties). inherit(top, Properties, Properties). inherit(Class, Properties, AllProperties):class(Class, SuperClass, ClassProperties), override(Properties, ClassProperties, ExtendedProperties), inherit(SuperClass, ExtendedProperties, AllProperties).
124 / 259
Searching graphs
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
125 / 259
Searching graphs
searching graphs
A search space is a graph with one or more starting nodes and one or more goal nodes. A solution is a path from a start node to a goal node. A cost function assigns a cost to each arc. An optimal solution is a solution with minimal cost. Search algorithms differ w.r.t. completeness: is a solution always found? optimality: will shorter paths be found before longer ones? efciency of the algorithm.
126 / 259
Searching graphs
A general framework
% search(agenda,Goal): agenda contains reached but % untried nodes. % succeeds if a node Goal, for which goal(Goal), can % be reached from a node in Agenda. search(Agenda, Goal):% select/3 selects a node from the Agenda select(Agenda, Goal, RestOfAgenda), goal(Goal). search(Agenda, Goal):select(Agenda, CurrentNode, RestOfAgenda), children(CurrentNode, Children), add(Children, RestOfAgenda, NewAgenda), search(NewAgenda, Goal).
127 / 259
Searching graphs
depth-rst search
Agenda is a list (stack).
select/3 add/3
search_df([Goal|RestOfAgenda], Goal):goal(Goal). search_df([CurrentNode|RestOfAgenda], Goal):children(CurrentNode, Children), append(Children, RestOfAgenda, NewAgenda), search_df(NewAgenda, Goal). children(Node, Children):findall(Child, arc(Node,Child), Children).
128 / 259
Searching graphs
129 / 259
Searching graphs
Searching graphs
Using Prologs goal stack to keep agenda, but without loop detection:
search_df(Goal,Goal):goal(Goal). search_df(CurrentNode, Goal):arc(CurrentNode, Child), search_df(Child, Goal).
131 / 259
Searching graphs
132 / 259
Searching graphs
133 / 259
Searching graphs
breadth-rst search
Agenda is a list (queue).
select/3 add/3
search_bf([Goal|RestOfAgenda], Goal):goal(Goal). search_bf([CurrentNode|RestOfAgenda], Goal):children(CurrentNode, Children), append(RestOfAgenda ,Children, NewAgenda), search_bf(NewAgenda, Goal). children(Node, Children):findall(Child, arc(Node,Child), Children).
134 / 259
Searching graphs
Because findall(X,G,L) creates new variables for the unbound variables in X before putting it in L, we keep a copy of the original goal in order to be able to retrieve the computed substitution. The agenda is a list of pairs
agenda_item(SubGoalClause, OriginalGoal)
135 / 259
Searching graphs
refute(GoalClause):refute([agenda_item(GoalClause,GoalClause)], GoalClause). % The following clause unifies two versions of the % original clause where the first version contains % the answer substitution. refute([ agenda_item(([]:- []), GoalClause) | _ ], GoalClause). refute([agenda_item(InputClause,GoalClause)|RestOfAgenda], OriginalGoalClause):findall( agenda_item(Resolvent, GoalClause), ( clause(Resolver), resolve(InputClause, Resolver, Resolvent) ), Children), append(RestOfAgenda, Children, NewAgenda), % breadth-first refute(NewAgenda, OriginalGoalClause).
136 / 259
Searching graphs
% resolve(Clause1,Clause2,R) iff R is a resolvent % of Clause1 and Clause2. resolve((H1:-B1), (H2:-B2), (ResHead:-ResBody)):% remove common literals from H1, B2, yielding R1, R2 remove_common_element(H1, B2, R1, R2), append(R1, H2, ResHead), append(B1, R2, ResBody). resolve((H1:- B1), (H2:-B2), (ResHead:-ResBody)):remove_common_element(H2, B1, R2, R1), append(H1, R2, ResHead), append(R1, B2, ResBody).
137 / 259
Searching graphs
% remove_common_element(+L1, +L2, -R1, -R2) % iff (roughly) exists X such that els(Li) = els(Ri) + {X} % (note that, necessarily, els(Li) not empty remove_common_element([A|B], C, B, E) :remove_element(A, C, E). remove_common_element([A|B], C, [A|D], E) :remove_common_element(B, C, D, E). % remove_element(+A,+L,-R) % iff (roughly) els(L) = els(R) + {A} remove_element(A, [A|B], B). remove_element(A, [C|B], [C|D]) :A\=C, remove_element(A, B, D).
138 / 259
Searching graphs
clause(([bachelor(X),married(X)]:- [man(X),adult(X)])). clause(([has_wife(X)]:- [man(X),married(X)])). clause(([]:- [has_wife(paul)])). clause(([man(paul)]:- [])). clause(([adult(paul)]:- [])). ?- refute(([] :- [bach(X)])) X = paul
The resolution strategy (input resolution: every resolvent has at least one program clause as its parent) used is incomplete for general clauses (but complete for denite ones).
139 / 259
Searching graphs
forward chaining
% model(-M) iff M is a model of the clauses defined by cl/1 model(M):model([], M). % model(+M0,-M) iff M0 can be extended to a model M % of the cl/1 clauses. model(M0, M):clause((H:- B)), % find violated clause instance is_violated((H:- B), M0),!, element(L, H), % select ground literal from the head model([L|M0], M). % and add it to the model model(M, M). % no violated clauses % is_violated((H:- B),+M) iff instance of H:-B % is violated by M is_violated((H:- B), M):satisfied_body(B, M), % this will ground the variables not(satisfied_head(H, M)).
140 / 259
Searching graphs
% satisfied_body(L,+M) iff M |= A for all A in L, % may bind vars in L satisfied_body([], M). satisfied_body([A|B], M) :element(A, M), satisfied_body(B, M). % satisfied_head(+L,+M) iff exists A in els(L) % with M |= A satisfied_head(L,M):element(A, L), element(A, M). element(A, [A|_]). element(A, [_|B]) :element(A, B).
141 / 259
Searching graphs
clause(([bach(X),married(X)]:- [man(X),adult(X)])). clause(([has_wife(X)]:- [man(X),married(X)])). clause(([man(paul)]:- [])). clause(([adult(paul)]:- [])). ?- model(M) M = [has_wife(paul), married(paul), adult(paul), man(paul)]; M = [bach(paul), adult(paul), man(paul)]
The program works correctly only for clauses for which grounding the body also grounds the head.
clause(([man(X),woman(X)]:- [])). clause(([]:- [man(jane)])). % jane is not a man clause(([]:- [woman(peter)])). % peter is not a woman
142 / 259
Searching graphs
range-restricted clauses
Range-restricted clauses: where all variables in the head also occur in the body. Any program can be transformed into an equivalent one using only range-restricted clauses.
143 / 259
Informed search
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
144 / 259
Informed search
best-rst search
Informed search uses an heuristic estimate of the distance from a node to a goal.
% eval(Node,Value) estimates distance from node to goal search_best([Goal|RestAgenda], Goal):goal(Goal). search_best([CurrentNode|RestAgenda], Goal):children(CurrentNode, Children), add_best(Children, RestAgenda, NewAgenda), search_best(NewAgenda, Goal). % add_best(A,B,C): C contains % els from A,B sorted according to eval/2 add_best([], Agenda, Agenda). add_best([Node|Nodes], Agenda, NewAgenda):insert(Node, Agenda, TmpAgenda), add_best(Nodes, TmpAgenda, NewAgenda).
145 / 259
Informed search
insert(Node, Agenda, NewAgenda):eval(Node, Value), insert(Value, Node, Agenda, NewAgenda). insert(Value, Node, [], [Node]). insert(Value, Node, [FirstNode|RestOfAgenda], [Node, FirstNode|RestOfAgenda]):eval(FirstNode, FirstNodeValue), Value < FirstNodeValue. insert(Value, Node, [FirstNode|RestOfAgenda], [FirstNode|NewRestOfAgenda]):eval(FirstNode, FirstNodeValue), Value >= FirstNodeValue, insert(Value, Node, RestOfAgenda, NewRestOfAgenda).
Best-rst search is not complete since, with certain estimate functions, it may get lost in an innite subgraph.
146 / 259
Informed search
example puzzle
A tile may be moved to the empty spot if there are at most 2 tiles between it and the empty spot. Find a series of moves that bring all the black tiles to the right of all the white tiles.
147 / 259
Informed search
148 / 259
Informed search
An agenda is list of terms move_value(Move, Value) where Value is the heuristic evaluation of the position reached by Move.
149 / 259
Informed search
tiles/2
tiles(ListOfPositions, TotalCost):start_move(StartMove), % Value is heuristic of distance to goal eval(StartMove, Value), % best-first search accumulating moves tiles([move_value(StartMove, Value)], FinalMove, [], VisitedMoves), % accumulator % find (and print) a backward path and its cost in % VisitedMoves from the final move to the start move order_moves(FinalMove, VisitedMoves, [], ListOfPositions, % accumulator 0, TotalCost). % accumulator
150 / 259
Informed search
tiles/4
% tiles(Agenda, LastMove, V0, V): goal can be % reached from a move in Agenda where LastMove % is the last move leading to the goal, % and V is V0 + the set of moves tried. tiles([move_value(LastMove,Value)|RestAgenda], LastMove, VisitedMoves, VisitedMoves):goal(LastMove). % eval(LastMove, 0), i.e. goal reached tiles([move_value(Move,Value)|RestAgenda], Goal, VisitedMoves, FinalVisitedMoves):show_move(Move, Value), % show move closest to goal % find and evaluate possible next moves from M setof0( move_value(NextMove, NextValue), ( next_move(Move, NextMove), eval(NextMove, NextValue) ), Children), merge(Children, RestAgenda, NewAgenda), tiles(NewAgenda, Goal, [Move|VisitedMoves], FinalVisitedMoves).
151 / 259
Informed search
next_move/2
next_move( move(Position, LastPosition, LastCost), move(LastPosition, NewPosition, Cost) ) :% consecutive moves: NewPosition can be reached from % LastPosition in 1 move at cost Cost % Ne = index of empty spot get_tile(LastPosition, Ne, e), % Nbw = index of nonempty spot get_tile(LastPosition, Nbw, BW), not(BW=e), Diff is abs(Ne-Nbw), Diff<4, % not too far from Ne replace(LastPosition, Ne, BW, IntermediatePosition), replace(IntermediatePosition, Nbw, e, NewPosition), ( Diff=1 -> Cost=1 ; otherwise -> Cost is Diff-1 ).
152 / 259
Informed search
% order_moves(FinalMove, VisitedMoves, % Positions, FinalPositions, % TotalCost,FinalTotalCost): % FinalPositions = Positions + connecting sequence of % target positions from VisitedMoves ending in % FinalMoves target position. % FinalTotalCost = TotalCost + total cost of moves % added to Positions to obtain FinalPositions. order_moves(move(noparent,StartPosition,0), VisitedMoves, Positions, [StartPosition|Positions], TotalCost, TotalCost). order_moves(move(FromPosition, ToPosition, Cost), VisitedMoves, Positions, FinalPositions, TotalCost, FinalTotalCost):element( PreviousMove, VisitedMoves), PreviousMove = move(PreviousPosition, FromPosition, CostOfPreviousMove), NewTotalCost is TotalCost + Cost, order_moves(PreviousMove, VisitedMoves, [ToPosition|Positions], FinalPositions, NewTotalCost, FinalTotalCost).
153 / 259
Informed search
utilities
% setof0/3: variant of setof/3 % which succeeds with empty list if no solutions are found setof0(X, G, L):setof(X, G, L), !. setof0(X, G, []). merge([], Agenda, Agenda). % avoid succeeding twice on merge([],[],L). merge([C|Cs],[],[C|Cs]). merge([C|Cs],[N|Ag],[C|NewAg]):eval(C,CVal), eval(N,NVal), CVal<NVal, merge(Cs,[N|Ag],NewAg]). merge([C|Cs],[N|Ag],[N|NewAg]):eval(C,CVal), eval(N,NVal), CVal>=NVal, merge([C|Cs],Ag,NewAg]).
154 / 259
Informed search
eval/1
goal(Move):eval(Move,0). eval(move(OldPosition,Position,C),Value):bLeftOfw(Position,Value). % Val is the sum of the number of black tiles % to the left of each white tile bLeftOfw(Pos,Val):findall((Nb,Nw), ( get_tile(Pos,Nb,b), get_tile(Pos,Nw,w), Nb<Nw), L), length(L,Val).
155 / 259
Informed search
example run
?- tiles(M,C). [b,b,b,e,w,w,w]-9 [b,b,b,w,e,w,w]-9 [b,b,e,w,b,w,w]-8 [b,b,w,w,b,e,w]-7 [b,b,w,w,b,w,e]-7 [b,b,w,w,e,w,b]-6 [b,e,w,w,b,w,b]-4 [b,w,e,w,b,w,b]-4 [e,w,b,w,b,w,b]-3 [w,w,b,e,b,w,b]-2 [w,w,b,w,b,e,b]-1 M = [[b,b,b,e,w,w,w],[b,b,b,w,e,w,w], [b,b,e,w,b,w,w],[b,b,w,w,b,e,w], [b,b,w,w,b,w,e],[b,b,w,w,e,w,b], [b,e,w,w,b,w,b],[b,w,e,w,b,w,b], [e,w,b,w,b,w,b],[w,w,b,e,b,w,b], [w,w,b,w,b,e,b],[w,w,e,w,b,b,b]] C = 15
156 / 259
Informed search
157 / 259
Language processing
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
158 / 259
Language processing
language processing
Syntax: denite clause grammars Semantics: terms instead of nonterminals Language generation An example interpreter
159 / 259
Language processing
160 / 259
Language processing
A rule
property --> [mortal]
Language processing
without append/3
A rule
sentence --> noun_phrase, verb_phrase
corresponds to
sentence(L,L0) :noun_phrase(L,L1), verb_phrase(L1,L0).
reading sentence(L,L0) as L consists of a sentence followed by L0 . The conversion of rules to clauses is often built into prolog, as is the meta-predicate phrase/2 where phrase(sentence,L) sentence(L,[])
162 / 259
Language processing
non-terminals can have arguments goals can be put into the rules no need for deterministic (LL(k), LR(k)) grammars! a single formalism for specifying syntax, semantics
163 / 259
Language processing
164 / 259
Language processing
165 / 259
Language processing
166 / 259
Language processing
167 / 259
Language processing
The rule
n1_99(N) --> tens(N1), n1_9(N2), {N is N1+N2}.
168 / 259
Language processing
169 / 259
Language processing
170 / 259
Language processing
Language processing
grammar
:- op(600,xfy,=>). sentence(C) --> determiner(N,M1,M2,C), noun(N,M1), verb_phrase(N,M2). sentence([(L:- true)]) --> proper_noun(N,X), verb_phrase(N,X=>L). verb_phrase(s,M) --> [is], property(s,M). verb_phrase(p,M) --> [are], property(p,M). property(N,X=>mortal(X)) --> [mortal]. property(s,M) --> noun(s,M). property(p,M) --> noun(p,M). determiner(s, X=>B , X=>H, [(H:- B)]) --> [every]. determiner(p, sk=>H1, sk=>H2, [(H1 :- true),(H2 :- true)]) -->[some]. proper_noun(s,socrates) --> [socrates]. noun(s,X=>human(X)) --> [human]. noun(p,X=>human(X)) --> [humans]. noun(s,X=>living_being(X)) --> [living],[being]. noun(p,X=>living_being(X)) --> [living],[beings].
172 / 259
Language processing
questions
question(Q) --> [who], [is], property(s,X=>Q) question(Q) --> [is], proper_noun(N,X), property(N,X=>Q) question((Q1,Q2)) --> [are], [some], noun(p,sk=>Q1), property(p,sk=>Q2)
173 / 259
Language processing
174 / 259
Language processing
auxiliary clauses
show_rules([]). show_rules([R|Rs]) :phrase(sentence(R),Sentence), show_answer(Sentence), show_rules(Rs). get_input(Input) :write(? ),read(Input). show_answer(Answer) :write(! ),write(Answer), nl.
175 / 259
Language processing
answering questions
prove(true,RB) :- !. prove((A,B),RB) :- !, prove(A,RB),prove(B,RB). prove(A,RB) :find_clause((A:- B),RB), prove(B,RB). % find_clause(C,[R|Rs]) :% dont instantiate rule copy_element(C,R). find_clause(C,[R|Rs]) :find_clause(C,Rs). copy_element(X,Ys) :element(X1,Ys), % copy with fresh variables copy_term(X1,X). transform((A,B),[(A:- true)|Rest]) :transform(B,Rest). transform(A,(A:- true)).
176 / 259
Language processing
example session
? ? ? ! ? ? !
177 / 259
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
178 / 259
Forms of reasoning where conclusions are plausible but not guaranteed to be true: default reasoning: when a normal state of affairs is assumed (birds y).
179 / 259
Forms of reasoning where conclusions are plausible but not guaranteed to be true: default reasoning: when a normal state of affairs is assumed (birds y). abductive resoning when there is a choice between several explanations that explain observations, e.g. in a diagnosis
179 / 259
Forms of reasoning where conclusions are plausible but not guaranteed to be true: default reasoning: when a normal state of affairs is assumed (birds y). abductive resoning when there is a choice between several explanations that explain observations, e.g. in a diagnosis inductive reasoning when a general rule is learned from examples.
179 / 259
Forms of reasoning where conclusions are plausible but not guaranteed to be true: default reasoning: when a normal state of affairs is assumed (birds y). abductive resoning when there is a choice between several explanations that explain observations, e.g. in a diagnosis inductive reasoning when a general rule is learned from examples. Such reasoning is unsound. Sound reasoning is called deduction. Deduction only makes implicit information explicit.
179 / 259
Default Reasoning
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
180 / 259
Default Reasoning
default reasoning
has 3 models:
{bird(tweety)} {bird(tweety), flies(tweety)} {bird(tweety), flies(tweety), normal(tweety)}
181 / 259
Default Reasoning
182 / 259
Default Reasoning
non-monotonic reasoning
In the example, new information invalidates previous conclusions. This is not the case for deductive reasoning where Th p Th {q} p p} we get that
deduction is monotonic
Th1 Th2 Closure(Th1 ) Closure(Th2 )
183 / 259
Default Reasoning
non-monotonic reasoning
In the example, new information invalidates previous conclusions. This is not the case for deductive reasoning where Th p Th {q} p p} we get that
deduction is monotonic
Th1 Th2 Closure(Th1 ) Closure(Th2 ) Default reasoning using not/1 is problematic because not/1 has no declarative semantics (but see later).
183 / 259
Default Reasoning
non-monotonic reasoning
In the example, new information invalidates previous conclusions. This is not the case for deductive reasoning where Th p Th {q} p p} we get that
deduction is monotonic
Th1 Th2 Closure(Th1 ) Closure(Th2 ) Default reasoning using not/1 is problematic because not/1 has no declarative semantics (but see later). Alternatively we can distinguish between rules with exceptions (default rules) and rules without exceptions. Rules are applied whenever possible, but default rules are only applied when they do not lead to an inconsistency.
183 / 259
Default Reasoning
Example
default((flies(X) :- bird(X))). rule((not(flies(X)) :- penguin(X))). rule((bird(X) :- penguin(X))). rule((penguin(tweety) :- true)). rule((bird(opus) :- true)).
184 / 259
Default Reasoning
% E explains F from rules, defaults explain(F,E):explain(F,[],E). explain(true,E,E) :- !. explain((A,B),E0,E) :- !, explain(A,E0,E1), explain(B,E1,E). explain(A,E0,E):prove(A,E0,E). explain(A,E0,[default((A:-B))|E]):default((A:-B)), explain(B,E0,E), not(contradiction(A,E)).
185 / 259
Default Reasoning
% prove using non-defaults prove(true,E,E) :- !. prove((A,B),E0,E) :- !, prove(A,E0,E1), prove(B,E1,E). prove(A,E0,[rule((A:-B))|E]):rule((A:-B)), prove(B,E0,E). contradiction(not(A),E) :- !, prove(A,E,E1). contradiction(A,E):prove(not(A),E,E1).
186 / 259
Default Reasoning
Example
?- explain(flies(X),E) X=opus E=[default((flies(opus) :- bird(opus))), rule((bird(opus) :- true))] ?- explain(not(flies(X)),E) X=tweety E=[rule((not(flies(tweety)) :- penguin(tweety))), rule((penguin(tweety) :- true))]
187 / 259
Default Reasoning
Example
default((not(flies(X)) :- mammal(X))). default((flies(X) :- bat(X))). default((not(flies(X)) :- dead(X))). rule((mammal(X) :- bat(X))). rule((bat(a) :- true)). rule((dead(a) :- true)). ?-explain(flies(a),E) E=[default((flies(a) :- bat(a))), rule((bat(a) :- true))] ?-explain(not(flies(a)),E) E=[default((not(flies(a)) :- mammal(a))) rule((mammal(a) :- bat(a))), rule((bat(a) :- true))] E=[default((not(flies(a)) :- dead(a))) rule((dead(a) :- true))]
Default Reasoning
We can rene by naming defaults and allow rules to cancel a specic default by name.
Example
default(mammals_dont_fly(X), (not(flies(X)):- mammal(X))). default(bats_fly(X),(flies(X):- bat(X))). default(dead_things_dont_fly(X), (not(flies(X)):- dead(X))). rule((mammal(X):- bat(X))). rule((bat(a):- true)). rule((dead(a):- true)). rule((not(mammals_dont_fly(X)):- bat(X))). % cancels mammals_dont_fly rule((not(bats_fly(X)):- dead(X))). % cancels bats_fly
189 / 259
Default Reasoning
190 / 259
Default Reasoning
Example
?-explain(flies(a),E) no ?-explain(not(flies(a)),E) E=[default(dead_things_dont_fly(a)), rule((dead(a):- true))]
191 / 259
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
192 / 259
semantics of negation
A program P is complete if for every (ground) fact f , we have that P |= f or P |= f We consider two methods to complete programs: The closed world assumption works for denite clauses.
193 / 259
semantics of negation
A program P is complete if for every (ground) fact f , we have that P |= f or P |= f We consider two methods to complete programs: The closed world assumption works for denite clauses. Predicate completion works for general clauses (with negation in the body) but leads to inconsistencies for some programs.
193 / 259
semantics of negation
A program P is complete if for every (ground) fact f , we have that P |= f or P |= f We consider two methods to complete programs: The closed world assumption works for denite clauses. Predicate completion works for general clauses (with negation in the body) but leads to inconsistencies for some programs. Alternatively, the stable model semantics introduces nondeterminism.
193 / 259
CWA: everything that is not known to be true must be false (e.g. databases). CWA(P) = P {:A | A BP P |= A} CWA(P) is the intended program of P, according to the CWA.
194 / 259
Example
likes(peter,S) :- student_of(S,peter). student_of(paul,peter).
CWA(P):
likes(peter,S) :- student_of(S,peter). student_of(paul,peter). :- student(paul,paul). :- student(peter,paul). :- student(peter,peter). :- likes(paul,paul). :- likes(paul,peter). :- likes(peter,peter).
195 / 259
Example
bird(tweety). flies(X);abnormal(X) :bird(X).
CWA(P):
bird(tweety). flies(X);abnormal(X) ::- flies(tweety). :- abnormal(tweety). bird(X).
which is inconsistent: CWA is unable to handle indenite (or general, pseudo-denite) clauses.
196 / 259
predicate completion
is the only clause with head likes/2, its completion is X S likes(X , S) X = peter student(S, peter ) which can be translated back to clausal form:
likes(peter,S) :- student(S,peter). X=peter :- likes(X,S). student(S,peter) :- likes(X,S)
197 / 259
1. Ensure that each argument of the head of each clause is a distinct variable by adding literals of the form Var = Term to the body.
likes(X,S) :likes(X,Y) :X=peter, student(S,peter). friend(X,Y).
198 / 259
likes(X,S) :likes(X,Y) :-
2. If there are several clauses for the same predicate (in the head), combine them into a single formula with a disjunctive body. X Y likes(X , Y ) (X = peter student(Y , peter )) friend(X , Y )
199 / 259
3. Replace the implication by an equivalence. X Y likes(X , Y ) (X = peter student(Y , peter )) friend(X , Y ) Note: predicates that have no clauses, e.g. p/1 becomes X p(X )
200 / 259
is equivalent to X Y Z ancestor (X , Y ) parent(X , Z ) ancestor (Z , Y ) but also with X Y ancestor (X , Y ) (Z parent(X , Z ) ancestor (Z , Y ))
201 / 259
Example
ancestor(X,Y) :ancestor(X,Y) :parent(X,Y). parent(X,Z), ancestor(Z,Y).
becomes X Y ancestor (X , Y ) parent(X , Y ) (Z parent(X , Z ) ancestor (Z , Y )) in step 2, and X Y ancestor (X , Y ) parent(X , Y ) (Z parent(X , Z ) ancestor (Z , Y )) in step 3.
202 / 259
Example
bird(tweety). flies(X) :- bird(X), not(abnormal(X)).
Comp(P) becomes: X bird(X ) X = tweety X ies(X ) (bird(X ) abnormal(X )) X abnormal(X ) which has a single model
{ bird(tweety), flies(tweety) }
203 / 259
Example
wise(X) :- not(teacher(X)). teacher(peter) :- wise(peter).
204 / 259
stratied programs
Denition
A program P is stratied if its predicate symbols can be partitioned into disjoint sets S0 , . . . , Sn such that for each clause p(. . .) L1 , . . . , Lj where p Sk , any literal Lj is such that if Lj = q(. . .) then q S0 . . . Sk
205 / 259
stratied programs
Denition
A program P is stratied if its predicate symbols can be partitioned into disjoint sets S0 , . . . , Sn such that for each clause p(. . .) L1 , . . . , Lj where p Sk , any literal Lj is such that if Lj = q(. . .) then q S0 . . . Sk if Lj = q(. . .) then q S0 . . . Sk 1
205 / 259
stratied programs
Denition
A program P is stratied if its predicate symbols can be partitioned into disjoint sets S0 , . . . , Sn such that for each clause p(. . .) L1 , . . . , Lj where p Sk , any literal Lj is such that if Lj = q(. . .) then q S0 . . . Sk if Lj = q(. . .) then q S0 . . . Sk 1
Theorem
If P is stratied then Comp(P) is consistent.
205 / 259
Theorem
If P is stratied then Comp(P) is consistent. The condition is sufcient but not necessary:
Example
win(X ) loose(X ). loose(X ) win(X ). is not stratied but its completion is consistent.
206 / 259
Intuition: Guess a model and verify that it can be reconstructed from the program (stability).
Example
win:- not(loose). loose :- not(win).
while the second rule is not applicable (since its body is false).
207 / 259
Removing all (true) negations not(b), where b I from the bodies of the rules. Remove all (blocked) rules that still contain negations after the previous step.
Example
win:- not(loose). loose :- not(win).
208 / 259
Denition
M is a stable model of P iff M is the (unique) minimal model of PM .
Example
win:- not(loose). loose :- not(win).
209 / 259
Any stable model of the above program represents a solution to this NP-complete problem.
210 / 259
211 / 259
211 / 259
211 / 259
211 / 259
211 / 259
is true in M iff it contains between 2 and 3 elements of is shorthand for the set {p(a, X ) | q(X )}.
212 / 259
{p, q, r }.
{p(a,X) : q(X)}
Abduction
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
213 / 259
Abduction
abduction
Given a theory T and an observation O, nd an explanation E such that T E O E.g. given the theory
likes(peter,S) :- student_of(S,peter). likes(X,Y) :- friend(X,Y).
and the observation likes(peter,paul), possible explanations are {student_of(paul,peter)} or {friend(peter,paul)} Another possible explanation is {(likes(X,Y) :- friendly(Y)), friendly(paul)} but abductive explanations are usually restricted to ground literals with predicates that are undened in the theory (abducibles).
214 / 259
Abduction
an abduction algorithm
Try to prove observation from theory; when an abducible literal is encountered that cannot be resolved, add it to the explanation.
abduce(O,E):- % P+E |- O abduce(O,[],E). abduce(true,E,E) :- !. abduce((A,B),E0,E) :- !, abduce(A,E0,E1), abduce(B,E1,E). abduce(A,E0,E):clause(A,B), abduce(B,E0,E). abduce(A,E,E):- element(A,E). abduce(A,E,[A|E]):- not(element(A,E)), abducible(A). abducible(A):- not(clause(A,B)). % clauses are assumed to be definitions
215 / 259
Abduction
Example
likes(peter,S) :- student_of(S,peter). likes(X,Y) :- friend(X,Y). ?-abduce(likes(peter,paul),E) E = [student_of(paul,peter)]; E = [friend(paul,peter)]
216 / 259
Abduction
Example
flies(X) :- bird(X), not(abnormal(X)). abnormal(X) :- penguin(X). bird(X) :- penguin(X). bird(X) :- sparrow(X). ?-abduce(flies(tweety),E) E = [not(abnormal(tweety)),penguin(tweety)]; E = [not(abnormal(tweety)),sparrow(tweety)];
217 / 259
Abduction
succeeds and thus, if flies(X):- not(abnormal(X)),bird(X) any explanation of bird(X) will explain flies(X). Thus we need a special abduce_not/3 that provides evidence for accepting not(..).
218 / 259
Abduction
Abduction
Abduction
Example
flies(X) :- bird(X),not(abnormal(X)). flies1(X) :- not(abnormal(X)),bird(X). abnormal(X) :- penguin(X). abnormal(X) :- dead(X). bird(X) :- penguin(X). bird(X) :- sparrow(X). ?- abduce(flies(tweety),E). E = [not(penguin(tweety)),not(dead(tweety)), sparrow(tweety)] ?- abduce(flies1(tweety),E). E = [sparrow(tweety), not(penguin(tweety)),not(dead(tweety))]
221 / 259
Abduction
adder(X,Y,Z,Sum,Carry) :xor(X,Y,S),xor(Z,S,Sum), and(X,Y,C1),and(Z,S,C2), or(C1,C2,Carry). xor(0,0,0). xor(0,1,1). xor(1,0,1). xor(1,1,0). and(0,0,0). and(0,1,0). and(1,0,0). and(1,1,1). or(0,0,0). or(0,1,1). or(1,0,1). or(1,1,1).
Abduction
The fault model of a system describes the behavior of each component when in a faulty state. We distinguish 2 such states: s0 (stuck at 0) and s1 (stuck at 1). A faulty component is described by a literal fault(NameComponent=State) Names of components can be nested as in
nameSubSystem-nameComponent adder(N,X,Y,Z,Sum,Carry):xorg(N-xor1,X,Y,S), xorg(N-xor2,Z,S,Sum), andg(N-and1,X,Y,C1), andg(N-and2,X,S,C2), org(N-or1,C1,C2,Carry). xorg(N,X,Y,Z) xorg(N,0,0,1) xorg(N,0,1,0) xorg(N,1,0,0) xorg(N,1,1,1) :::::xor(X,Y,Z). fault(N=s1). fault(N=s0). fault(N=s0). fault(N=s1).
223 / 259
Abduction
xandg(N,X,Y,Z):- and(X,Y,Z). xandg(N,0,0,1):- fault(N=s1). xandg(N,1,0,1):- fault(N=s1). org(N,X,Y,Z):- or(X,Y,Z). org(N,0,0,1):- fault(N=s1). org(N,1,0,0):- fault(N=s0).
224 / 259
Abduction
225 / 259
Outline
1 2 3 4
Language processing Reasoning with Incomplete Information Default Reasoning The Semantics of Negation Abduction Inductive Logic Programming
10
Logic programming
11
12 13
6 7
226 / 259
Introduction
227 / 259
Introduction
228 / 259
Introduction
ILP as discussed here can be seen as concept learning where the description space consists of LPs. The generalization relationship may be based on subsumption between clauses.
229 / 259
Introduction
?- induce_rlgg([ +append([1,2],[3,4],[1,2,3,4]), +append([a],[],[a]), +append([],[],[]), +append([],[1,2,3],[1,2,3]), +append([2],[3,4],[2,3,4]), +append([],[3,4],[3,4]), -append([a],[b],[b]), -append([c],[b],[c,a]), -append([1,2],[],[1,3]) ], Clauses).
230 / 259
Introduction
[append(Y,Z,U)])]
231 / 259
Generalizing clauses
Denition
A clause c1 -subsumes a clause c2 iff there exists a substitution such that c1 c2 (c1 is more general or more widely applicable thant c2 ). Here clauses are seen as sets of (positive and negative) literals (disjunctions).
232 / 259
Generalizing clauses
-subsumption examples
The clause
element(X,V) :element(X,Z)
233 / 259
Generalizing clauses
-subsumption implementation
% (H1:- B1) subsumes (H2 :- B2) theta_subsumes((H1:- B1),(H2 :- B2)):verify((grounded((H2:- B2)), H1=H2,subset(B1,B2))). % H1=H2 creates substitution, note that H2 has no vars grounded(Term):% instantiate vars in Term to terms of the form % $VAR(i) where i is different for each distinct % var, first i=0, last = N-1 numbervars(Term,0,N). verify(Goal) :% prove without binding not(not(call(Goal))).
234 / 259
Generalizing clauses
-subsumption implementation
Example
?theta_subsumes( (element(X,V):- []), (element(X,V):- [element(X,Z)])).
235 / 259
Generalizing clauses
Theorem
The set of (reduced) clauses form a lattice, i.e. a unique least general generalization lgg(c1 , c2 ) exists for any two clauses c1 and c2 . (a clause is reduced if it is minimal in the collection of equivalent clauses)
236 / 259
Generalizing clauses
generalizing 2 terms
a3 subsumes a1 using {X/1, Y/[]} and a3 subsumes a2 using {X/z, Y/[y,x]} Moreover, a3 is the least generalization, i.e. every other term that -subsumes a1 and a2 also -subsumes a3 .
237 / 259
Generalizing clauses
anti_unify
:- op(600,xfx,<-). % to record (inverse) substitutions anti_unify(Term1,Term2,Term):% use accumulators S1, S2 anti_unify(Term1,Term2,Term,[],S1,[],S2). anti_unify(Term1,Term2,Term1,S1,S1,S2,S2):Term1 == Term2,!. anti_unify(Term1,Term2,V,S1,S1,S2,S2):subs_lookup(S1,S2,Term1,Term2,V), !. % already substituted anti_unify(Term1,Term2,Term,S10,S1,S20,S2):nonvar(Term1), nonvar(Term2), functor(Term1,F,N), functor(Term2,F,N),!, functor(Term,F,N), % create F(X1,..,Xn) anti_unify_args(N,Term1,Term2,Term,S10,S1,S20,S2). % Create new variable V and substitutions V->Term1, V->Term2 anti_unify(Term1,Term2, V, S10,[Term1<-V|S10], S20,[Term2<-V|S20]).
238 / 259
Generalizing clauses
% anti_unify_args(N,T1,T2,T,..): % anti-unify first N arguments of T1, T2 anti_unify_args(0,Term1,Term2,Term,S1,S1,S2,S2). anti_unify_args(N,Term1,Term2,Term,S10,S1,S20,S2):N>0,N1 is N-1, arg(N,Term1,Arg1), arg(N,Term2,Arg2), arg(N,Term,ArgN), anti_unify(Arg1,Arg2,ArgN,S10,S11,S20,S21), anti_unify_args(N1,Term1,Term2,Term,S11,S1,S21,S2). % subs_lookup(+subst1,+subst2,+term1,+term2,-var) % subst1(V) = term1, subst2(V) = term2 subs_lookup([T1<-V|Subs1],[T2<-V|Subs2],Term1,Term2,V):T1 == Term1, T2 == Term2,!. subs_lookup([S1|Subs1],[S2|Subs2],Term1,Term2,V):subs_lookup(Subs1,Subs2,Term1,Term2,V).
Example
?- anti_unify(2*2=2+2,2*3=3+3,T,[],S1,[],S2). T = 2 * _G191 = _G191 + _G191 S1 = [2 <- _G191] S2 = [3 <- _G191]
239 / 259
Generalizing clauses
theta_lgg((H1:-B1),(H2:-B2),(H:-B)):anti_unify(H1,H2,H,[],S10,[],S20), theta_lgg_bodies(B1,B2, [],B, S10,S1, S20,S2). % theta_lgg_bodies considers each pair of literals % from both bodies theta_lgg_bodies([],B2,B,B,S1,S1,S2,S2). theta_lgg_bodies([Lit|B1],B2, B0,B, S10,S1, S20,S2):theta_lgg_literal(Lit,B2, B0,B00, S10,S11, S20,S21), theta_lgg_bodies(B1,B2, B00,B, S11,S1, S21,S2).
240 / 259
Generalizing clauses
% theta_lgg_literal anti-unifies Lit1 with each % literal in 2nd arg theta_lgg_literal(Lit1,[], B,B, S1,S1, S2,S2). theta_lgg_literal(Lit1,[Lit2|B2], B0,B, S10,S1, S20,S2):same_predicate(Lit1,Lit2), anti_unify(Lit1,Lit2,Lit,S10,S11,S20,S21), theta_lgg_literal(Lit1,B2,[Lit|B0],B,S11,S1,S21,S2). theta_lgg_literal(Lit1,[Lit2|B2],B0,B,S10,S1,S20,S2):not(same_predicate(Lit1,Lit2)), theta_lgg_literal(Lit1,B2,B0,B,S10,S1,S20,S2). same_predicate(Lit1,Lit2) :functor(Lit1,P,N), functor(Lit2,P,N).
241 / 259
Generalizing clauses
theta_lgg example
Example
?- theta_lgg( (element(c,[b,c]):- [ element(c,[c]) ]), (element(d,[b,c,d]):- [ element(d,[c,d]), element(d,[d]) ]), C). C = element(X, [b, c|Y]):- [element(X,[X]), element(X,[c|Y])]
242 / 259
Generalizing clauses
theta_lgg example
Example
?- theta_lgg( (reverse([2,1],[3],[1,2,3]):- [ reverse([1],[2,3],[1,2,3]) ]), (reverse([a],[],[a]) :- [ reverse([],[a],[a]) ]), C). C = reverse([X|Y], Z, [U|V]) :- [reverse(Y, [X|Z], [U|V])]
243 / 259
Denition
The relative least general generalization rlgg(e1 , e2 , M) of two positive examples relative to a (partial) model M is dened by rlgg(e1 , e2 , M) = lgg((e1 : M ), (e2 : M ))
244 / 259
example: result
append([X|Y], Z, [X|U]) :- [ append([2], [3, 4], [2, 3, 4]), append(Y, Z, U), append([V], Z, [V|Z]), append([K|L], [3, 4], [K, M, N|O]), append(L, P, Q), append([], [], []), append(R, [], R), append(S, P, T), append([A], P, [A|P]), append(B, [], B), append([a], [], [a]), append([C|L], P, [C|Q]), append([D|Y], [3, 4], [D, E, F|G]), append(H, Z, I), append([X|Y], Z, [X|U]), append([1, 2], [3, 4], [1, 2, 3, 4]) ]
too complex!
246 / 259
constrained clauses
We remove: ground facts (examples) are redundant literals involving variables not occurring in the head: i.e. we restrict to constrained clauses. The example result then becomes:
append([X|Y], Z, [X|U]) :append(Y, Z, U), append([X|Y], Z, [X|U]).
The head is part of the body: it can also be removed if we restrict to strictly constrained clauses where the variables in the body are a strict subset of the variables in the head.
247 / 259
248 / 259
rlgg_literal(L1,[],B,B,S1,S1,S2,S2,V). rlgg_literal(L1,[L2|B2],B0,B,S10,S1,S20,S2,V):same_predicate(L1,L2), anti_unify(L1,L2,L,S10,S11,S20,S21), varsin(L,Vars), var_proper_subset(Vars,V), % no new variables in literal !, rlgg_literal(L1,B2,[L|B0],B,S11,S1,S21,S2,V). rlgg_literal(L1,[L2|B2],B0,B,S10,S1,S20,S2,V):rlgg_literal(L1,B2,B0,B,S10,S1,S20,S2,V).
249 / 259
varsin/2
% varsin(+term,-list) list is list of % variables occurring in term varsin(Term,Vars):varsin(Term,[],V), sort(V,Vars). varsin(V,Vars,[V|Vars]):var(V). varsin(Term,V0,V):functor(Term,F,N), varsin_args(N,Term,V0,V). % varsin_args(N,T,V0,V) add vars in first % N args of T to V0, yielding V varsin_args(0,Term,Vars,Vars). varsin_args(N,Term,V0,V):N>0, N1 is N-1, arg(N,Term,ArgN), varsin(ArgN,V0,V1), varsin_args(N1,Term,V1,V).
250 / 259
var_remove_one/3, var_proper_subset/2
251 / 259
rlgg
Example
?rlgg( append([1,2],[3,4],[1,2,3,4]), append([a],[],[a]), [ append([1,2],[3,4],[1,2,3,4]), append([a],[],[a]), append([],[],[]), append([2],[3,4],[2,3,4]) ], (H:- B)). append([X|Y], Z, [X|U]) :- [ append([2], [3, 4], [2, 3, 4]), append(Y, Z, U), append([], [], []), append([a], [], [a]), append([1, 2], [3, 4], [1, 2, 3, 4]) ]
252 / 259
main algorithm
construct rlgg of two examples remove positive examples that are covered by the resulting clause remove further literals (generalizing the clause) as long as the clause does not cover any negative examples based on GOLEM system (Muggleton & Feng, 1990)
253 / 259
induce_rlgg implementation
induce_rlgg(Exs,Clauses):pos_neg(Exs,Poss,Negs), bg_model(BG), append(Poss,BG,Model), induce_rlgg(Poss,Negs,Model,Clauses). % induce_rlgg(+pos_exs,+neg_exs,+model,-clauses) induce_rlgg(Poss,Negs,Model,Clauses):covering(Poss,Negs,Model,[],Clauses). % pos_neg(+exs,-poss,-negs) split % positive and negative examples pos_neg([],[],[]). pos_neg([+E|Exs],[E|Poss],Negs):pos_neg(Exs,Poss,Negs). pos_neg([-E|Exs],Poss,[E|Negs]):pos_neg(Exs,Poss,Negs).
254 / 259
% covering(+pos_exs, +neg_exs, +model,+old_hypothesis, % -new_hypothesis): construct new_hypothesis % covering all of pos_exs and none of the neg_exs covering(Poss,Negs,Model,Hyp0,NewHyp) :construct_hypothesis(Poss,Negs,Model,Hyp), !, remove_pos(Poss,Model,Hyp,NewPoss), % cover remaining posexs covering(NewPoss,Negs,Model,[Hyp|Hyp0],NewHyp). covering(P,N,M,H0,H) :append(H0,P,H). % add uncovered exs to hypothesis % remove_pos(+old_pos_exs,+model,+clause,-new_pos_ex) % remove posexs that are covered by clause + model, % yielding new_pos_ex remove_pos([],M,H,[]). remove_pos([P|Ps],Model,Hyp,NewP) :covers_ex(Hyp,P,Model), !, write(Covered example: ), write_ln(P), remove_pos(Ps,Model,Hyp,NewP). remove_pos([P|Ps],Model,Hyp,[P|NewP]):remove_pos(Ps,Model,Hyp,NewP).
255 / 259
% covers_ex(+clause,+example,+model): % example is covered by clause covers_ex((Head:- Body),Example,Model):verify( (Head=Example, forall(element(L,Body),element(L,Model)) )). % construct_hypothesis(+pos_exs,+neg_exs,+model,-clause) construct_hypothesis([E1,E2|Es],Negs,Model,Clause):write(RLGG of ), write(E1), write( and ), write(E2), write( is), rlgg(E1,E2,Model,Cl), reduce(Cl,Negs,Model,Clause), !, nl,tab(5), write_ln(Clause). construct_hypothesis([E1,E2|Es],Negs,Model,Clause):write_ln( too general), construct_hypothesis([E2|Es],Negs,Model,Clause).
256 / 259
% reduce(+old_clause,+neg_exs,+model,-new_clause) % remove redundant literals from body and ensure % that no negexs are covered reduce((H:- B0),Negs,M,(H:-B)):% remove literals of M from B0, giving B1 setof0(L, (element(L,B0), not(var_element(L,M))), B1), % body B consists of literals from B1 that are necessary % not to cover negative examples reduce_negs(H,B1,[],B,Negs,M). % covers_neg(+clause,+negs,+model,-n) % n negative example from negs covered by clause + model covers_neg(Clause,Negs,Model,N):- element(N,Negs), covers_ex(Clause,N,Model).
257 / 259
% reduce_negs(+H,+In,+B0,-B,+Negs,+Model) % B is B0 + subsequence of In such that (H:- B) + Model % does not cover elements of Negs reduce_negs(H,[L|Rest],B0,B,Negs,Model):% try removing L append(B0,Rest,Body), not(covers_neg((H:- Body),Negs,Model,N)), !, reduce_negs(H,Rest,B0,B,Negs,Model). reduce_negs(H,[L|Rest],B0,B,Negs,Model):% L cannot be removed reduce_negs(H,Rest,[L|B0],B,Negs,Model). reduce_negs(H,[],Body,Body,Negs,Model):not(covers_neg((H:- Body),Negs,Model,N)). var_element(X,[Y|Ys]):X == Y. % syntactic identity var_element(X,[Y|Ys]):var_element(X,Ys).
258 / 259
further developments
259 / 259