Information Technology Reference
In-Depth Information
Figure 11.11.
The induce predicate in Prolog
induce.pl
% This program uses three predicates defined elsewhere:
% est - the procedure that establishes a query
% background - the background knowledge to be used
% predicate - an atom that can be used in a rule
induce(Q,R) :- background(K), rule(R), est([R|K],Q).
rule([H|B]) :- hb(H,B,_,_), \+ B=[].
% hb(H,B,P,V): H is an atom with pred P and vars V and
% B is a list of literals with pred not P and vars among the V.
hb(H,[],P,V) :- predicate(H,P,V).
hb(H,[L|B],P,V) :- hb(H,B,P,V), lit(L,Q,U,B), \+ Q=P, subl(U,V).
% lit(L,P,V,B): L is a literal with pred P and vars V, where the
% neg case can only be used if the literals in B are also neg.
lit(A,P,V,_) :- predicate(A,P,V). % the positive case
lit(not(A),P,V,[]) :- predicate(A,P,V). % the 1st neg case
lit(not(A),P,V,[not(_)|_]) :- predicate(A,P,V). % a 2nd case
% subl(L1,L2): the elements of list L1 are all elements of L2.
subl([],_).
subl([X|L1],L2) :- member(X,L2), subl(L1,L2).
Figure 11.12.
A knowledge base about animals
animals.pl
% An example knowledge base about animals
background([
[animal(X),dog(X)], % Dogs are animals.
[animal(X),cat(X)], % Cats are animals.
[animal(X),duck(X)], % Ducks are animals.
% Some example animals
[dog(fido)],
[dog(spot)],
[dog(rover)],
[cat(kitty)], [cat(kelly)],
[duck(donald)], [duck(daffy)], [duck(huey)] ]).
% Some predicates to learn about
predicate(animal(X),animal,[X]). predicate(dog(X),dog,[X]).
predicate(cat(X),cat,[X]). predicate(duck(X),duck,[X]).
predicate(barks(X),barks,[X]). predicate(quacks(X),quacks,[X]).
predicate(four_legged(X),four_legged,[X]).
What rule can be learned from observing that Fido barks:
?- induce([barks(fido)], R).
R = [barks(_G404), animal(_G404)]
% Every animal barks?
Search WWH ::




Custom Search