-
Notifications
You must be signed in to change notification settings - Fork 2
/
ifc-xp.metta
401 lines (349 loc) · 12.3 KB
/
ifc-xp.metta
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
;; Standalone iterative forward chaining experiments.
;;;;;;;;;
;; Nat ;;
;;;;;;;;;
;; Define Nat
(: Nat Type)
(: Z Nat)
(: S (-> Nat Nat))
;; Define <=
(: <= (-> $a $a Bool))
(= (<= $x $y) (or (< $x $y) (== $x $y)))
;; Define cast functions between Nat and Number
(: fromNumber (-> Number Nat))
(= (fromNumber $n) (if (<= $n 0) Z (S (fromNumber (- $n 1)))))
(: fromNat (-> Nat Number))
(= (fromNat Z) 0)
(= (fromNat (S $k)) (+ 1 (fromNat $k)))
;;;;;;;;;;;;;;;;;;;;;;;;;
;; Forward Bare Entail ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
;; Reuse the Bare Entail forward chainer of
;; `../forward-chaining/fc-xp.metta`. However it only returns results
;; at the exact provided depth, that way we avoid redundancy, which is
;; better in the context of testing the iterative chainer.
;; Foward chainer
(: fc_bare (-> Atom (-> Atom) (-> Atom) Nat Atom))
;; Base case
(= (fc_bare $premise $kb $rb Z) $premise)
;; Recursive steps
(= (fc_bare $premise1 $kb $rb (S $k))
(let* (((⊢ $premise1 $premise2 $conclusion) ($rb))
($premise2 ($kb)))
(fc_bare $conclusion $kb $rb $k)))
(= (fc_bare $premise2 $kb $rb (S $k))
(let* (((⊢ $premise1 $premise2 $conclusion) ($rb))
($premise1 ($kb)))
(fc_bare $conclusion $kb $rb $k)))
;; Knowledge base
(: kb_bare (-> Atom))
(= (kb_bare) (superpose ((→ A B)
(→ B C)
A)))
;; Rule base
(: rb_bare (-> Atom))
(= (rb_bare) (superpose ((⊢ ; Modus Ponens
;; Premises
(→ $p $q)
$p
;; Conclusion
$q)
(⊢ ; Deduction
;; Premises
(→ $p $q)
(→ $q $r)
;; Conclusion
(→ $p $r)))))
;; Test forward chainer
! "=== Test Forward Bare Entail ==="
!(assertEqual
(fc_bare A kb_bare rb_bare (fromNumber 1))
B)
!(assertEqual
(fc_bare A kb_bare rb_bare (fromNumber 2))
C)
!(assertEqualToResult
(fc_bare (→ A B) kb_bare rb_bare (fromNumber 2))
(C
C))
;;;;;;;;;;;;;;;;;;;;;;;
;; Utility Functions ;;
;;;;;;;;;;;;;;;;;;;;;;;
;; Define quoted to prevent wrapped atom from being interpreted
(: quoted (-> Atom Atom))
;; Define nullary lambda
(: lambda0 (-> Atom (-> $t)))
(= ((lambda0 $body)) $body)
;; Define unary lambda
(: lambda1 (-> Variable Atom (-> $a $t)))
(= ((lambda1 $var $body) $val)
(let (quoted $var) (quoted $val) $body))
;; Define ad-atom-nodup, that adds an atom only if it is not already
;; in the atomspace
(: add-atom-nodup (-> $st Atom ()))
(= (add-atom-nodup $space $atom)
(case (match $space $atom $atom)
(($atom ())
(%void% (add-atom $space $atom)))))
;; Add all atoms from an expression to a given atomspace
(: add-atoms-nodup (-> $st Expression ()))
(= (add-atoms-nodup $space $atoms)
(if (== $atoms ())
()
(let* (($head (car-atom $atoms))
($tail (cdr-atom $atoms))
($dummy (add-atom-nodup $space $head)))
(add-atoms-nodup $space $tail))))
;; Test utility functions
! "=== Test Utility Functions ==="
;; Test lambda0
!(assertEqualToResult
((lambda0 "f"))
("f"))
!(assertEqualToResult
((let $s (superpose ("f" "g")) (lambda0 $s)))
("f" "g"))
;; Test add-atom-nodup
!(bind! &space-test (new-space))
!(add-atom-nodup &space-test A)
!(add-atom-nodup &space-test A)
!(assertEqualToResult
(get-atoms &space-test)
(A))
;; Test add-atoms-nodup
!(add-atoms-nodup &space-test (A B C A))
!(assertEqualToResult
(get-atoms &space-test)
(A B C))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Iterative Chainer Wrapped Around Forward Bare Entail (no collapse) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Call the forward chainer iteratively, adding the conclusions to the
;; knowledge base after each iterations. That version does not
;; collapse the results of the forward chainer between iterations.
(: ic_bare (-> Atom ; Premise
(-> Atom) ; Knowledge base
(-> Atom) ; Rule base
Nat ; Depth
Nat ; Number of iterations
(-> Atom))) ; Updated knowledge base
;; Base case. For now it terminates at exactly iteration Z to avoid
;; having to deal with too many redundant results. Ideally it should
;; terminate at any iteration an avoid redundancy by not inserting
;; knowledge already in the knowledge base.
(= (ic_bare $prms $kb $rb $depth Z) $kb)
;; Iterative step
(= (ic_bare $prms $kb $rb $depth (S $k))
(case (fc_bare $prms $kb $rb $depth) ; call the forward chainer
(($ccln
;; Iterate
(ic_bare $ccln (superpose ($kb (lambda0 $ccln))) $rb $depth $k))
(%void% (empty)))))
;; Test Iterative Chainer Wrapped Ardoun Forward Bare Entail (no collapse)
! "=== Test Iterative Chainer Wrapped Ardoun Forward Bare Entail (no collapse) ==="
;; No iteration
!(assertEqual
(ic_bare A kb_bare rb_bare Z Z)
kb_bare)
;; No iteration (apply result)
!(assertEqualToResult
((ic_bare A kb_bare rb_bare Z Z))
((→ A B)
(→ B C)
A))
;; One iteration of one step forward chainer (apply result)
!(assertEqualToResult
(ic_bare A kb_bare rb_bare (fromNumber 1) (fromNumber 1))
(kb_bare
(lambda0 B)))
;; One iteration of one step forward chainer (apply result)
!(assertEqualToResult
((ic_bare A kb_bare rb_bare (fromNumber 1) (fromNumber 1)))
((→ A B)
(→ B C)
A
B))
;; Two iterations of one step forward chainer
!(assertEqualToResult
(ic_bare A kb_bare rb_bare (fromNumber 1) (fromNumber 2))
(kb_bare
(lambda0 C)))
;; Two iterations of one step forward chainer (apply result)
!(assertEqualToResult
((ic_bare A kb_bare rb_bare (fromNumber 1) (fromNumber 2)))
((→ A B)
(→ B C)
A
C))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Iterative Chainer Wrapped Around Forward Bare Entail (collapse) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Call the forward chainer iteratively, adding the conclusions to the
;; knowledge after each iterations. That version collapses the
;; results of the forward chainer between iterations.
(: ic_bare_collapse (-> Atom ; Premise
(-> Atom) ; Knowledge base
(-> Atom) ; Rule base
Nat ; Depth
Nat ; Number of iterations
(-> Atom))) ; Updated knowledge base
;; Base case. For now it terminates at exactly iteration Z to avoid
;; having to deal with too many redundant results. Ideally it should
;; terminate at any iteration and avoid redundancy by not inserting
;; knowledge already in the knowledge base.
(= (ic_bare_collapse $prms $kb $rb $depth Z) $kb)
;; Iterative step
(= (ic_bare_collapse $prms $kb $rb $depth (S $k))
(let* (($cres (collapse (fc_bare $prms $kb $rb $depth)))
($scres (superpose $cres))
($kb_ext (lambda0 $scres))
($nu_kb (superpose ($kb $kb_ext))))
(ic_bare_collapse $scres $nu_kb $rb $depth $k)))
;; Test Iterative Chainer Wrapped Around Forward Bare Entail (no collapse)
! "=== Test Iterative Chainer Wrapped Around Forward Bare Entail (collapse) ==="
;; No iteration
!(assertEqual
(ic_bare_collapse A kb_bare rb_bare Z Z)
kb_bare)
;; No iteration (apply result)
!(assertEqualToResult
((ic_bare_collapse A kb_bare rb_bare Z Z))
((→ A B)
(→ B C)
A))
;; One iteration of one step forward chainer
!(assertEqualToResult
(ic_bare_collapse A kb_bare rb_bare (fromNumber 1) (fromNumber 1))
(kb_bare
(lambda0 B)))
;; One iteration of one step forward chainer (apply result)
!(assertEqualToResult
((ic_bare_collapse A kb_bare rb_bare (fromNumber 1) (fromNumber 1)))
((→ A B)
(→ B C)
A
B))
;; Two iterations of one step forward chainer
;; TODO: can't use assertEqualToResult due to let* cruft
;; !(assertEqualToResult
!(ic_bare_collapse A kb_bare rb_bare (fromNumber 1) (fromNumber 2))
;; (kb_bare
;; (lambda0 C))
;; Two iterations of one step forward chainer (apply result);
;; TODO: can't use assertEqualToResult due to let* cruft
;; !(assertEqualToResult
!((ic_bare_collapse A kb_bare rb_bare (fromNumber 1) (fromNumber 2)))
;; ((→ A B)
;; (→ B C)
;; A
;; C
;;;;;;;;;;;;;;;;;;;;;;;
;; Forward DTL Match ;;
;;;;;;;;;;;;;;;;;;;;;;;
;; Forward revertant implementation, using spaces for knowledge and
;; rule bases.
;; Knowledge base
!(bind! &kb (new-space))
!(add-atom &kb (: ab (→ A B)))
!(add-atom &kb (: bc (→ B C)))
!(add-atom &kb (: a A))
;; Rule base
!(bind! &rb (new-space))
!(add-atom &rb (: ModusPonens (->
;; Premises
(→ $p $q)
$p
;; Conclusion
$q)))
!(add-atom &rb (: Deduction (->
;; Premises
(→ $p $q)
(→ $q $r)
;; Conclusion
(→ $p $r))))
;; Forward revertant chainer, based on the DTL version of
;; `../backward-chaining/bc-xp.metta` but matching premises is
;; replaced by calling the backward chainer defined above.
(: fc (-> Atom Nat Atom))
;; Base case
(= (fc (: $prf $prms) $depth) (: $prf $prms))
;; Recursive cases
(= (fc (: $prf1 $prms1) (S $k))
(match &rb (: $ctor (-> $prms1 $prms2 $ccln))
(match &kb (: $prf2 $prms2)
(fc (: ($ctor $prf1 $prf2) $ccln) $k))))
(= (fc (: $prf2 $prms2) (S $k))
(match &rb (: $ctor (-> $prms1 $prms2 $ccln))
(match &kb (: $prf1 $prms1)
(fc (: ($ctor $prf1 $prf2) $ccln) $k))))
;; Test forward chainer DTL match
! "=== Test Forward Chainer DTL Match ==="
!(assertEqual
(fc (: a A) Z)
(: a A))
!(assertEqualToResult
(fc (: a A) (fromNumber 1))
((: a A)
(: (ModusPonens ab a) B)))
;; Disable assertEqualToResult due to leftover
!(assertEqualToResult
(fc (: a A) (fromNumber 2))
((: a A)
(: (ModusPonens ab a) B)
(: (ModusPonens bc (ModusPonens ab a)) C)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Iterative Chainer Wrapped Around Forward DTL Match (collapse) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Call the forward revertant chainer iteratively, adding the
;; conclusions to the knowledge space after each iterations. To avoid
;; irreproducible behavior (due to the side effects of modifying the
;; atomspace), each call of the forward revertant chainer collapses
;; between iterations.
(: ifc (-> $a ; Premise
Nat ; Depth
Nat ; Number of iterations
$a)) ; Conclusion
;; Base case. For now it terminates at exactly iteration Z to avoid
;; collecting too many redundant results.
(= (ifc $prms $depth Z) $prms)
;; Iterative step
(= (ifc $prms $depth (S $k))
(let* (($cres (collapse (fc $prms $depth)))
($dummy (add-atoms-nodup &kb $cres)))
(ifc (superpose $cres) $depth $k)))
;; Test Iterative Chainer Wrapped Around Forward Revertant (collapse)
! "=== Test Iterative Chainer Wrapped Around Forward Revertant (collapse) ==="
;; No iteration
!(assertEqual
(ifc (: a A) Z Z)
(: a A))
!(assertEqualToResult
(get-atoms &kb)
((: ab (→ A B))
(: bc (→ B C))
(: a A)))
;; One iteration of one step forward chainer
!(assertEqualToResult
(ifc (: a A) (fromNumber 1) (fromNumber 1))
((: a A)
(: (ModusPonens ab a) B)))
!(assertEqualToResult
(get-atoms &kb)
((: ab (→ A B))
(: bc (→ B C))
(: a A)
(: (ModusPonens ab a) B)))
;; Two iterations of one step forward chainer
!(assertEqualToResult
(ifc (: a A) (fromNumber 1) (fromNumber 2))
((: a A)
(: (ModusPonens ab a) B)
(: (ModusPonens ab a) B)
(: (ModusPonens bc (ModusPonens ab a)) C)))
!(assertEqualToResult
(get-atoms &kb)
((: ab (→ A B))
(: bc (→ B C))
(: a A)
(: (ModusPonens ab a) B)
(: (ModusPonens bc (ModusPonens ab a)) C)))