-
Notifications
You must be signed in to change notification settings - Fork 1
/
discr.metta
409 lines (369 loc) · 14.3 KB
/
discr.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
402
403
404
405
406
407
408
; (convert-to-metta-file discr $_59064 discr/discr.pl discr/discr.metta)
!(dynamic (/ :: 2))
; /******************************************************************/
; /* DISCR.PL Last Modification: Fri Jan 14 19:22:58 1994 */
; /* Brazdil's generation of discriminants from derivation trees */
; /******************************************************************/
; ; ; Copyright (c) 1989 Thomas Hoppe ; ; This program is free software; you can redistribute it and/or ; modify it under the terms of the GNU General Public License ; Version 1 as published by the Free Software Foundation. ; ; This program is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public ; Licensealong with this program; if not, write to the Free ; SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, ; USA. ;
; /******************************************************************/
; /* impl. by : Thomas Hoppe */
; /* Mommsenstr. 50 */
; /* D-10629 Berlin */
; /* F.R.G. */
; /* E-Mail: [email protected] */
; /* 1989 */
; /* */
; /* reference : problem 93 */
; /* chapter 9 */
; /* MeTTa by example */
; /* Helder Coelho, Jose' C. Cotta */
; /* Berlin, Heidelberg, New York */
; /* Springer-Verlag, 1988 */
; /* */
; /* call : ex1, ex2 */
; /* */
; /******************************************************************/
; /* One of the common errors in learning is over generalisation: */
; /* a given term Q is applicable in certain contexts instead of */
; /* failing. The purpose of the following programm is to correct */
; /* this. This can be done by computation of a discriminant, which */
; /* can be used to backup from an overgeneralisation. For this */
; /* purpose we need two kind of contexts: */
; /* */
; /* - an application context (app) in which the proof of Q */
; /* should suceed, and */
; /* - a rejection context (rej) in which the proof should fail. */
; /* */
; /* All clauses determining what Q is (this can be viewed as the */
; /* background knowledge) and how it is related to the contexts */
; /* (app and rej) (this can be viewed as the user-given examples) */
; /* should also be given. The expression generated which is */
; /* applicable in the application context (app) only is referred */
; /* to as discriminant, and the process of generating the */
; /* discriminat (obviously) as discrimination. */
; /******************************************************************/
; /* The programm assumes that clauses are represented in the */
; /* following way: */
; /* */
; /* cn :: HEAD <- PRED1 & PRED2 & ..... PREDN */
; /* */
; /* where cn is a unique name for every single clause, <- denotes */
; /* implication and & denotes conjunction. PREDN can be a MeTTa */
; /* build-in predicate, which is evaluated in the normal fasion. */
; /* See discr_1.pro and discr_2.pro. Sorry I haven't yet some nice */
; /* examples. */
; /******************************************************************/
; ; TH Sat May 29 23:58:27 1993 - made some minor modifications
; /******************************************************************/
; /* SWI-, YAP-, C- and M-MeTTa specific declaration of dynamical */
; /* clauses. */
; /******************************************************************/
!(op 150 yfx ::)
!(op 145 xfx <-)
!(op 140 xfy &)
!(op 135 xfx :=)
(= (derivation (<- $P $C) $TYP)
( (name a1 $NAME)
(add-context $NAME $C)
(generate-goal-ids $P $ID1 1 $I1)
(expand-derivation $P $P2 $ID1 $ID2
(:: $I1 $ID1)
(:: $I2 $DERIVATION))
(add-atom &self
(:: $TYP $DERIVATION))
(write (:: $TYP $DERIVATION)) nl fail))
; /******************************************************************/
; /* */
; /* call : derivation(+EXPRESSION,+TYP) */
; /* */
; /* arguments : EXPRESSION = Expression of the form P <- C */
; /* TYPE = context type */
; /* */
; /* side effects: Asserting derivation trees in the database */
; /* */
; /******************************************************************/
; /* The generation of all possible derivation trees of an */
; /* EXPRESSION of the form P <- C, whose truth/falsity should be */
; /* established, is done with this predicate. TYPE is the context */
; /* type (app or rej). */
; /******************************************************************/
(= (derivation $_ $_)
(name a1
(:: $N1 $N2))
(del-context (:: $N1 $_)))
(= (add-context (:: $N1 $N2) (& $P1 $P2))
( (set-det)
(name $C
(:: $N1 $N2))
(add-atom &self
(:: $C
(<- $P1 true)))
(is $N3
(+ $N2 1))
(add-context
(:: $N1 $N3) $P2)))
; /******************************************************************/
; /* */
; /* call : add_context(+CLAUSENAME,+EXPRESSION) */
; /* */
; /* arguments : CLAUSENAME = List of charaters */
; /* EXPRESSION = Conjunction of Facts */
; /* */
; /* side effects: Asserting contexts in the database */
; /* */
; /******************************************************************/
; /* The assertion of contexts is done with this predicate. CLAUSE- */
; /* NAME is a list of characters of length 2, and EXPRESSION a */
; /* conjunction of Facts. */
; /******************************************************************/
(= (add-context (:: $N1 $N2) $P1)
( (name $C
(:: $N1 $N2)) (add-atom &self (:: $C (<- $P1 true)))))
(= (del-context (:: $N1 $N2))
( (:: $C
(<- $P1 True))
(name $C
(:: $N1 $_))
(remove-atom &self
(:: $C
(<- $P1 true))) fail))
; /******************************************************************/
; /* */
; /* call : del_context(+CLAUSENAME) */
; /* */
; /* arguments : CLAUSENAME = List of charaters */
; /* */
; /* side effects: Retracting contexts from the database */
; /* */
; /******************************************************************/
; /* The deletion of contexts is done with this predicate. CLAUSE- */
; /* NAME is a list of characters of length 2. */
; /******************************************************************/
(= (del-context $_) True)
(= (generate-goal-ids (& $P1 $P2) (& $I1 $I2) $I1 $I4)
(set-det)
(is $I3
(+ $I1 1))
(generate-goal-ids $P2 $I2 $I3 $I4))
; /******************************************************************/
; /* */
; /* call : generate_goal_ids(+GOALCONJUNCTION, */
; /* -IDCONJUNCTION, */
; /* +ID1, */
; /* -ID2) */
; /* */
; /* arguments : GOALCONJUNCTION = actual conjunction of goals */
; /* IDCONJUNCTION = conjunction of goal ids */
; /* ID1 = last used id */
; /* ID2 = updated last used id */
; /* */
; /******************************************************************/
; /* Given a conjunction of goals this predicate generates goal */
; /* identifiers (integers) using the information of the last used */
; /* id and returning the last new identifier. */
; /******************************************************************/
(= (generate-goal-ids $P1 $I1 $I1 $I4)
(is $I4
(+ $I1 1)))
(= (expand-derivation True True $ID1 $ID1 $D1 $D1)
(set-det))
; /******************************************************************/
; /* */
; /* call : expand_derivation(+GOALCONJUNCTION1, */
; /* -GOALCONJUNCTION2, */
; /* +IDCONJUNCTION1, */
; /* -IDCONJUNCTION2, */
; /* +DERIVATION1, */
; /* -DERIVATION2) */
; /* */
; /* arguments : GOALCONJUNCTION1 = actual conjunction of goals */
; /* GOALCONJUNCTION2 = reduced conjunction of goals */
; /* IDCONJUNCTION1 = actual goal id conjunction */
; /* IDCONJUNCTION2 = reduced goal id conjunction */
; /* DERIVATION1 = actual derivation tree */
; /* DERIVATION2 = expanded derivation tree */
; /* */
; /******************************************************************/
; /* Given a conjunction of goals (GOALCONJUNCTION1), a conjunction */
; /* of the corresponding goal ids (IDCONJUNCTION1) and a previous */
; /* derivation (DERIVATION1) this predicate generates the expanded */
; /* derivation tree (DERIVATION2) while solving (in a backward- */
; /* chaining manner) a goal of GOALCONJUNCTION1. It returns the */
; /* still unsolved goals in GOALCONJUNCTION2 and their */
; /* corresponding goal ids in IDCONJUNCTION2. Notice, this is a */
; /* kind of MeTTa meta-interpreter, which collect the derivation */
; /* tree. Derivation tree's in the sense of this programm are */
; /* ordered, ::-connected lists. */
; /******************************************************************/
(= (expand-derivation (& True $P3) $P3 (& $ID1 $ID3) $ID3 $D1 $D1)
(set-det))
(= (expand-derivation (& $P1 $P3) $P5 (& $ID1 $ID3) $ID5 $D1 $D3)
(expand-derivation- $P1 $P2 $ID1 $ID2 $D1 $D2)
(join-goals
(& $P2 $P3) $P4
(& $ID2 $ID3) $ID4)
(expand-derivation $P4 $P5 $ID4 $ID5 $D2 $D3))
; (error
; (syntax-error operator_expected)
; (file discr/discr.pl 201 8 11767))
(= (expand-derivation- $P1 $P2 $ID1 $ID2 (:: $I1 $D1) (:: $I2 $D2))
(:: $C
(<- $P1 $P2))
(generate-goal-ids $P2 $ID2 $I1 $I2)
(= $D2
(::
(:: $D1 $C)
(<- $ID1 $ID2))))
(= (join-goals (& $P1 (& $P2 $P3)) (& $P1 $P5) (& $ID1 (& $ID3 $ID3)) (& $ID1 $ID5))
(set-det)
(join-goals
(& $P2 $P3) $P5
(& $ID2 $ID3) $ID5))
; /******************************************************************/
; /* */
; /* call : join_goals(+GOALCONJUNCTION1,-GOALCONJUNCTION2, */
; /* +IDCONJUNCTION1,-IDCONJUNCTION2) */
; /* */
; /* arguments : GOALCONJUNCTION1 = actual conjunction of goals */
; /* GOALCONJUNCTION2 = joined conjunction of goals */
; /* IDCONJUNCTION1 = actual goal id conjunction */
; /* IDCONJUNCTION2 = joined goal id conjunction */
; /* */
; /******************************************************************/
; /* The joining of goals is done by this predicate. */
; /******************************************************************/
(= (join-goals (& True $P3) $P3 (& $ID1 $ID3) $ID3)
(set-det))
(= (join-goals $P1 $P1 $ID1 $ID1) True)
; (error
; (syntax-error operator_expected)
; (file discr/discr.pl 248 8 14466))
(= (spec $T1 $T2)
(ground $T2 1 $_)
(= $T1 $T2))
(= (ground (skolem-function $N1) $N1 $N2)
(set-det)
(is $N2
(+ $N1 1)))
(= (ground $T $N1 $N2)
(=.. $T
(Cons $_ $TS))
(== $TS Nil)
(set-det))
(= (ground $T $N1 $N2)
(=.. $T
(Cons $_ $TS))
(grounds $TS $N1 $N2))
(= (grounds (Cons $T $TS) $N1 $N3)
(ground $T $N1 $N2)
(grounds $TS $N1 $N2))
(= (grounds () $N1 $N1) True)
(= (generate-discriminants $P $PA $PR)
( (generate-goal-ids $P $ID 1 $_)
(determine-discriminant
(::
(::
(:: $P $ID) $P) $ID)
(::
(::
(:: $PA $IA) $PR) $IR))
(add-atom &self
(:: disc $PA))
(write (:: disc $PA)) nl fail))
; /******************************************************************/
; /* */
; /* call : generate_discriminants(+EXPRESSION, */
; /* -DISCRIMINANT1, */
; /* -DISCRIMINANT2) */
; /* */
; /* arguments : EXPRESSION = Expression to be specialized */
; /* DISCRIMINANT1 = */
; /* DISCRIMINANT2 = */
; /* */
; /* side effects: Asserting discriminants in the database */
; /* */
; /******************************************************************/
; /* Generates all possible discriminants an asserts them in the */
; /* database. More than one discriminant can be generated, if more */
; /* the EXPRESSION is computable from more than one derivations. */
; /* See discr_2.pro for an example. All discriminants generated */
; /* should be specific enough so that they would fail in all */
; /* rejection contexts. As we can see from discr_2.pro this is not */
; /* the case for the second discriminant !. */
; /******************************************************************/
(= (generate-discriminants $_ $_ $_) True)
; (error
; (syntax-error operator_expected)
; (file discr/discr.pl 293 8 16547))
(= (determine-discriminant (:: (:: (:: (& True $PA3) (& $_ $IA3)) (& True $PR3)) (& $_ $IR3)) $P3)
(set-det)
(= $P3
(::
(::
(:: $PA3 $IA3) $PR3) $IR3)))
(= (determine-discriminant (:: (:: (:: (& $PA1 $PA3) (& $IA1 $IA3)) (& $PR1 $PR3)) (& $IR1 $IR3)) $P3)
(determine-discriminant
(::
(::
(:: $PA1 $IA1) $PR1) $IR1)
(::
(::
(:: $PA2 $IA2) $PR2) $IR2))
(join-goals
(& $PA2 $PA3) $PA5
(& $IA2 $IA3) $IA5)
(join-goals
(& $PR2 $PR3) $PR5
(& $IR2 $IR3) $IR5)
(= $P3
(::
(::
(:: $PA5 $IA5) $PR5) $IR5)))
(= (determine-discriminant (:: (:: (:: (& $PA1 $PA3) (& $IA1 $IA3)) (& $PR1 $PR3)) (& $IR1 $IR3)) $P3)
(determine-discriminant
(::
(::
(:: $PA3 $IA3) $PR3) $IR3)
(::
(::
(:: $PA4 $IA4) $PR4) $IR4))
(= $P3
(::
(::
(::
(& $PA1 $PA4)
(& $IA1 $IA4))
(& $PR1 $PR4))
(& $IR1 $PR4))))
; (error
; (syntax-error operator_expected)
; (file discr/discr.pl 305 8 17158))
(= (determine-discriminant- $P1 $P3)
(= $P1
(::
(::
(:: $PA1 $IA1) $PR1) $IR1))
(:: $CA
(<- $PA1 $PA2))
(:: app $DA)
(in-derivation-p
(:: $CA
(<- $IA1 $IA2)) $DA)
(:: $CR
(<- $PR1 $PR2))
(:: rej $DR)
(in-derivation-p
(:: $CR
(<- $IR1 $IR2)) $DR)
(= $P3
(::
(::
(:: $PA2 $IA2) $PR2) $IR2)))
(= (in-derivation-p (:: $X $C) (:: (:: $DER $X) $C))
(set-det))
(= (in-derivation-p (:: $X $C) (:: $DER $_))
(in-derivation-p
(:: $X $C) $DER))
(= help
( (write 'Load data set with command: [Filename].') nl))
!(help *)