-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter3.pl
268 lines (208 loc) · 5.73 KB
/
chapter3.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Prolog programs from Chapter 3 of the book %
% SIMPLY LOGICAL: Intelligent reasoning by example %
% (c) Peter A. Flach/John Wiley & Sons, 1994. %
% %
% Predicates: length/2 %
% length2/2,3 %
% naive_reverse/2 %
% reverse/2,3 %
% reverse_dl/3 %
% append/3 %
% prove/1 %
% prove_var/1 %
% prove_r/1 %
% prove_p/1,2 %
% write_proof/2 %
% partition/4 %
% sort/2 %
% insert/3 %
% and various other small programs. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 3.1 SLD resolution %%%
student_of(X,T):-follows(X,C),teaches(T,C).
follows(paul,computer_science).
follows(paul,expert_systems).
follows(maria,ai_techniques).
teaches(adrian,expert_systems).
teaches(peter,ai_techniques).
teaches(peter,computer_science).
brother_of(X,Y):-brother_of(Y,X).
brother_of(paul,peter).
brother_of(peter,adrian).
brother_of(X,Y):-brother_of(X,Z),brother_of(Z,Y).
%%% 3.1 Pruning the search by means of cut %%%
parent(X,Y):-father(X,Y),!.
parent(X,Y):-mother(X,Y).
father(john,paul).
father(john,peter).
mother(mary,paul).
mother(mary,peter).
likes(peter,Y):-friendly(Y).
likes(T,S):-student_of(S,T).
student_of(maria,peter).
student_of(paul,peter).
friendly(maria).
%%% 3.3 Negation as failure %%%
max(M,N,M):-M >= N.
max(M,N,N):-M =< N.
/*
% (incorrect) version with cut
max(M,N,M):-M >= N,!.
max(M,N,N).
*/
/*
not(Goal):-Goal,!,fail.
not(Goal).
*/
bachelor(X):-not(married(X)),man(X).
man(fred).
man(peter).
married(fred).
%%% 3.5 Arithmetic expressions %%%
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).
%%% 3.6 Accumulators %%%
length([],0).
length([H|T],N):-length(T,M),N is M+1.
length2(L,N):-length2(L,0,N).
length2([],N,N).
length2([H|T],N0,N):-N1 is N0+1,length2(T,N1,N).
naive_reverse([],[]).
naive_reverse([H|T],R):-
naive_reverse(T,R1),
append(R1,[H],R).
append([],Y,Y).
append([H|T],Y,[H|Z]):-
append(T,Y,Z).
reverse(X,Y):-
reverse(X,[],Y).
reverse([],Y,Y).
reverse([H|T],Y0,Y):-
reverse(T,[H|Y0],Y).
/*
reverse(X,Y):-
reverse_dl(X,Y-[]).
*/
reverse_dl([],Y-Y).
reverse_dl([H|T],Y-Y0):-
reverse_dl(T,Y-[H|Y0]).
%%% 3.7 Second-order predicates %%%
parents([],[]).
parents([P|Ps],[C|Cs]):-
parent(P,C),
parents(Ps,Cs).
rel(R,[],[]).
rel(R,[X|Xs],[Y|Ys]):-
L =.. [R,X,Y],call(L), % R(X,Y)
rel(R,Xs,Ys).
parent(john,peter).
parent(john,paul).
parent(john,mary).
parent(mick,davy).
parent(mick,dee).
parent(mick,dozy).
children(Parent,Children):-
findall(C,parent(Parent,C),Children).
%%% 3.8 Meta-programs %%%
% if A and B then C means 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.
% meta-program
derive(if Assumptions then Goal):-
if Body then Goal,
derive(if Assumptions then Body).
derive(if Assumptions then Goal1 and Goal2):-
derive(if Assumptions then Goal1),
derive(if Assumptions then Goal2).
derive(if Assumptions then Goal):-
assumed(Goal,Assumptions).
assumed(A,A).
assumed(A,A and As).
assumed(A,B and As):-
assumed(A,As).
prove(true):-!.
prove((A,B)):-!,
prove(A),
prove(B).
prove(A):-
/* not A=true, not A=(X,Y) */
clause(A,B),
prove(B).
prove_var(true):-!.
prove_var((A,B)):-!,
prove(A),
prove(B).
prove_var(A):-
clause(Head,Body),
unify(A,Head,MGU,Result),
apply(Body,MGU,NewBody),
prove_var(NewBody).
is_bird(X):-has_feathers(X),lays_eggs(X).
is_fish(X):-has_gills(X),lays_eggs(X).
has_feathers(tweety).
lays_eggs(tweety).
% meta-interpreter with complete resolvent
prove_r(true):-!.
prove_r((A,B)):-!,
clause(A,C),
conj_append(C,B,D),
prove_r(D).
prove_r(A):-
clause(A,B),
prove_r(B).
%%% conj_append/3: Utility predicate
% display a proof tree
prove_p(A):-
prove_p(A,P),
write_proof(P).
% prove_p(A,P) <- P is proof tree of A
prove_p(true,[]):-!.
prove_p((A,B),[p((A,B),Clause)|Proof]):-!,
clause(A,C),
copy_term((A:-C),Clause), % make copy of the clause
conj_append(C,B,D),
prove_p(D,Proof).
prove_p(A,[p(A,(A:-B))|Proof]):-
clause(A,B),
prove_p(B,Proof).
write_proof([]):-
tab(15),write('[]'),nl.
write_proof([p(A,B)|Proof]):-
write((:-A)),nl,
tab(5),write('|'),tab(10),write(B),nl,
tab(5),write('|'),tab(20),write('/'),nl,
write_proof(Proof).
%%% 3.9 A methodology of Prolog programming %%%
% partition(L,N,Littles,Bigs) <- Littles contains numbers
% in L smaller than N,
% Bigs contains the rest
partition([],N,[],[]).
partition([Head|Tail],N,[Head|Littles],Bigs):-
Head < N,
partition(Tail,N,Littles,Bigs).
partition([Head|Tail],N,Littles,[Head|Bigs]):-
Head >= N,
partition(Tail,N,Littles,Bigs).
sort([],[]).
sort([Head|Tail],WholeSorted):-
sort(Tail,Sorted),
insert(Head,Sorted,WholeSorted).
insert(X,[],[X]).
insert(X,[Head|Tail],[Head|Inserted]):-
X > Head,
insert(X,Tail,Inserted).
insert(X,[Head|Tail],[X,Head|Inserted]):-
X =< Head.