forked from drspro/metta-wam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NatDTLTest.metta
executable file
·295 lines (256 loc) · 13.5 KB
/
NatDTLTest.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Definition of a chainer, Nat, plus and some proofs.
;;
;; Similar to NatStandaloneTest.metta but the chainer assumes rules
;; are formatted as dependent products. In that manner we can make a
;; more verbatim replication of
;;
;; https://idris2.readthedocs.io/en/latest/tutorial/theorems.html#proving-theorems
;;
;; Eventually we should be able to use that chainer to do constructive
;; mathematics with dependent sum/product in place of
;; existential/universal quantifier.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
;; 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)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Knowledge and rule base ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!(bind! &kb (new-space))
;; Knowledge base
;; Refl is disabled because not necessary in all examples so far
;; Equality is reflexive. We use === instead of == to make sure it
;; does not get reduced by the MeTTa interpreter.
;;
;; TODO: maybe we want to curry === as well.
!(add-atom &kb (: Refl (=== $x $x)))
;; ;; Equality is transitive
;; !(add-atom &kb (: Trans (-> (: $prf1 (=== $x $y)) ; Premise 1
;; (-> (: $prf2 (=== $y $z)) ; Premise 2
;; (=== $x $z))))) ; Conclusion
;; ;; Equality is symmetric
;; !(add-atom &kb (: Sym (-> (: $prf (=== $x $y)) ; Premise
;; (=== $y $x)))) ; Conclusion
;; ;; Equality respects function application
;; !(add-atom &kb (: Cong (-> (: $f (-> $a $b)) ; Premise 1
;; (-> (: $prf (=== $x $x')) ; Premise 2
;; (=== ($f $x) ($f $x')))))) ; Conclusion
;; ;; Rule of replacement (nullary operator)
;; ;; TODO: could use Replace1 combine with identity function instead.
;; !(add-atom &kb (: Replace0 (-> (: $prf1 (=== $x $x')) ; Premise 1
;; (-> (: $prf2 $x) ; Premise 2
;; $x')))) ; Conclusion
;; ;; Structural induction on Nat
;; !(add-atom &kb (: NatInd
;; (-> (: $x Nat) ; Premise 1
;; (-> (: $p (-> Nat Type)) ; Premise 2
;; (-> (: $prf_base ($p Z)) ; Premise 3 (base case)
;; (-> (: $prf_rec (-> ($p $x) ($p (S $x)))) ; Premise 4 (inductive step)
;; ($p $x'))))))) ; Conclusion
;; ;; Definition of plus (base case)
;; ;; TODO: maybe could be replaced by (=== (plus Z) id)
;; !(add-atom &kb (: plusBase (-> (: $y Nat) ; Premise
;; (=== ((plus Z) $y) $y)))) ; Conclusion
;; ;; Definition of plus (recursive step)
;; !(add-atom &kb (: plusRec (-> (: $x Nat) ; Premise 1
;; (-> (: $y Nat) ; Premise 2
;; (=== ((plus (S $x)) $y) (S ((plus $x) $y))))))) ; Conclusion
;; ;; Property expressing that for any natural, Z is the right identity
;; ;; of plus. Note that the property definition is the axiom, not the
;; ;; property itself as it is what we attempt to prove.
;; !(add-atom &kb (: plusRightIdProp (-> (: $x Nat) ; Premise
;; (=== (plusRightId $x) (=== ((plus $x) Z) $x))))) ; Conclusion
;; ;; Composition operator
;; !(add-atom &kb (: . (-> (-> $b $c) ; Premise 1
;; (-> (-> $a $b) ; Premise 2
;; (-> $a $c))))) ; Conclusion
;; Anything can serve as a variable. Even a closed term, it simply
;; will not unify with any other closed term than itself, which makes
;; for a very specific, yet valid, abstraction.
!(add-atom &kb (: $x Variable))
;; Lambda
;;
;; TODO: we could add as premise that $t is a type.
!(add-atom &kb (: lambda (-> (: $x Variable) ; Premise 1
(-> (: $prf $thm) ; Premise 2
(-> (: $x $t) $thm))))) ; Conclusion
;; ;; The witness of (: $x $t) is $x.
;; ;; It is probably a bad idea!
;; !(add-atom &kb (: $x (: $x $t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Backward DTL Curried ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Similar to the backward chainer in NatStandaloneTest but rules are
;; assumed represented as dependent products. Meaning rule premises
;; must contain type-of relationships. That is, instead of
;;
;; (: RULE_CONSTRUCTOR (-> PREMISE CONCLUSION))
;;
;; it must be
;;
;; (: RULE_CONSTRUCTOR (-> (: PROOF PREMISE) CONCLUSION))
;;
;; regardless of whether PROOF is present or not in CONCLUSION.
;; Typically PROOF will be a variable, but it can also be a term
;; containing variables, in which case CONCLUSION may contain that
;; term, or variables of that term.
;; Base case
(: bc (-> $a Nat $a))
(= (bc (: $prf $ccln) $_)
;; Query the knowledge for a rule or a fact
(trace! (⊷ bc-base (: $prf $ccln) $_)
(match &kb (: $prf $ccln) (: $prf $ccln))))
;; Recursive step
(= (bc (: ($prfabs $prfarg) $ccln) (S $k))
(trace! (⊷ bc-rec (: ($prfabs $prfarg) $ccln) (S $k))
(let* (;; Recurse on proof abstraction
((: $prfabs (-> (: $prfarg $prms) $ccln))
(bc (: $prfabs (-> (: $prfarg $prms) $ccln)) $k))
;; Recurse on proof argument
((: $prfarg $prms)
(bc (: $prfarg $prms) $k)))
(: ($prfabs $prfarg) $ccln))))
;;;;;;;;;;;;;;;
;; Reduction ;;
;;;;;;;;;;;;;;;
;; Reduction rules to simplify proofs and reduce redundancy
;; TODO: these rules should be proven first. Then they could
;; automatically be inserted.
;; Involution of symmetry
(= (Sym (Sym $prf)) $prf)
;; Composition to application
(= (((. $g) $f) $x) ($g ($f $x)))
;; Define plus
(: plus (-> Nat (-> Nat Nat)))
(= ((plus Z) $y) $y)
(= ((plus (S $x)) $y) (S ((plus $x) $y)))
;;;;;;;;;;;;
;; Proofs ;;
;;;;;;;;;;;;
;; ;; Prove that Z is left identity of plus
;; ;; !(assertEqual
;; !(bc (: $prf (=== ((plus Z) $y) $y)) Z)
;; ;; (: Refl (=== $y $y)))
;; ;; Prove that any natural equals to itself
;; ;; !(assertEqual
;; !(bc (: $prf (-> (: $y Nat) (=== $y $y))) (fromNumber 2))
;; ;; (: ((lambda $y) Refl) (-> (: $y Nat) (=== $y $y))))
;; Prove that (=== (=== (plus Z Z) Z) (ZRID_plus Z)).
;;
;; The following proof tree is expected to prove that:
;;
;; ---------------------------------------(plusRightIdProp)
;; (=== (plusRightId Z) (=== (plus Z Z) Z))
;; ---------------------------------------(Sym)
;; (=== (=== (plus Z Z) Z) (plusRightId Z))
;;
;; Or in MeTTa format:
;;
;; (Sym plusRightIdProp)
;; !(assertEqual
!(bc (: $prf (=== (=== ((plus Z) Z) Z) (plusRightId Z))) (fromNumber 2))
;; (: (Sym plusRightIdProp) (=== (=== (plus Z Z) Z) (plusRightId Z))))
;; ;; Prove that (plusRightId Z) holds. Meaning that Z is the right
;; ;; identity of plus for Z.
;; ;;
;; ;; The following proof tree does that:
;; ;;
;; ;; ---------------------------------------(plusRightIdProp)
;; ;; (=== (plusRightId Z) (=== (plus Z Z) Z))
;; ;; ---------------------------------------(Sym) -----------------(plusBase)
;; ;; (=== (=== (plus Z Z) Z) (plusRightId Z)) (=== (plus Z Z) Z)
;; ;; ---------------------------------------------------------------(Replace0)
;; ;; (plusRightId Z)
;; ;;
;; ;; or in MeTTa format:
;; ;;
;; ;; (Replace0 (Sym plusRightIdProp) plusBase)
;; ;;
;; ;; TODO: re-enable when assertContainResults is introduced
;; !(assertEqualToResult
;; (bc (: $prf (plusRightId Z)) (fromNumber 4))
;; ((: ((Replace0 plusBase) ((Replace0 (Sym plusBase)) ((Replace0 (Sym plusRightIdProp)) plusBase))) (plusRightId Z))
;; (: ((Replace0 ((Trans plusBase) (Sym plusRightIdProp))) ((Replace0 (Sym plusBase)) plusBase)) (plusRightId Z))
;; (: ((Replace0 ((Trans plusRightIdProp) (Sym plusRightIdProp))) ((Replace0 (Sym plusRightIdProp)) plusBase)) (plusRightId Z))
;; (: ((Replace0 (Sym plusRightIdProp)) ((Trans (Sym plusBase)) ((Trans plusBase) plusBase))) (plusRightId Z))
;; (: ((Replace0 (Sym plusRightIdProp)) plusBase) (plusRightId Z))))
;; ;; Prove that (-> (=== (plus $x Z) $x) (=== (plus (S $x) Z) (S $x)))
;; ;;
;; ;; The proof tree should be:
;; ;;
;; ;; ------------------------------------(plusRec)
;; ;; (=== (plus (S $x) Z) (S (plus $x Z)))
;; ;; -------------------------------------------------------------(Trans) -----------------------------------------------------(Cong)
;; ;; (-> (=== (S (plus $x Z)) (S $x)) (=== (plus (S $x) Z) (S $x))) (-> (=== (plus $x Z) $x) (=== (S (plus $x Z)) (S $x)))
;; ;; ---------------------------------------------------------------------------------------------------------------------------(.)
;; ;; (-> (=== (plus $x Z) $x) (=== (plus (S $x) Z) (S $x)))
;; ;;
;; ;; or in MeTTa format:
;; ;;
;; ;; ((. (Trans plusRec)) Cong)
;; ;;
;; ;; TODO: re-enable when assertContainResults is introduced
;; ;; !(assertEqualToResult
;; !(bc (: $prf (-> (=== (plus $x Z) $x) (=== (plus (S $x) Z) (S $x)))) (fromNumber 3))
;; ;; (: ((. (Trans plusRec)) Cong) (-> (=== (plus $x Z) $x) (=== (plus (S $x) Z) (S $x))))
;; ;; Prove that (-> (plusRightId $x) (plusRightId (S $x)))
;; ;;
;; ;; The proof tree should be:
;; ;;
;; ;; ------------------------------------(plusRec)
;; ;; (=== (plus (S $x) Z) (S (plus $x Z)))
;; ;; ------------------------------------------(plusRightIdProp) ------------------------------------------------------(plusRightIdProp) -------------------------------------------------------------(Trans) -----------------------------------------------------(Cong)
;; ;; (=== (plusRightId $x) (=== (plus $x Z) $x)) (=== (plusRightId (S $x)) (=== (plus (S $x) Z) (S $x))) (-> (=== (S (plus $x Z)) (S $x)) (=== (plus (S $x) Z) (S $x))) (-> (=== (plus $x Z) $x) (=== (S (plus $x Z)) (S $x)))
;; ;; ------------------------------------------(Sym) ------------------------------------------------------(Sym) ---------------------------------------------------------------------------------------------------------------------------(.)
;; ;; (=== (=== (plus $x Z) $x) (plusRightId $x)) (=== (=== (plus (S $x) Z) (S $x)) (plusRightId (S $x))) (-> (=== (plus $x Z) $x) (=== (plus (S $x) Z) (S $x)))
;; ;; ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(Replace2)
;; ;; (-> (plusRightId $x) (plusRightId (S $x)))
;; ;;
;; ;; or in MeTTa format:
;; ;;
;; ;; (((Replace2 (Sym plusRightIdProp)) (Sym plusRightIdProp)) ((. (Trans plusRec)) Cong))
;; ;;
;; ;; There is also a proof not involving Replace2, using two instances of Replace0 instead
;; ;;
;; ;; ((. (Replace0 (Sym plusRightIdProp))) ((. (Trans plusRec)) ((. Cong) (Replace0 plusRightIdProp))))
;; ;;
;; ;; It's a bit more verbose but requires a smaller rule base which
;; ;; speeds up backward chaining, so we are aiming for that instead.
;; ;;
;; ;; TODO: re-enable when assertContainResults is introduced
;; ;; !(assertEqualToResult
;; !(bc (: $prf (-> (plusRightId $x) (plusRightId (S $x)))) (fromNumber 4))
;; ;; (: ((. (Replace0 (Sym plusRightIdProp))) ((. (Trans plusRec)) ((. Cong) (Replace0 plusRightIdProp)))) (-> (plusRightId $x) (plusRightId (S $x))))
;; ;; Prove that Z is the right identity of plus
;; ;;
;; ;; ---------------------------------------(plusRightIdProp)
;; ;; (=== (plusRightId Z) (=== (plus Z Z) Z))
;; ;; ---------------------------------------(Sym) -----------------(plusBase)
;; ;; (=== (=== (plus Z Z) Z) (plusRightId Z)) (=== (plus Z Z) Z) <PROOF IS PROVIDED ABOVE>
;; ;; ---------------------------------------------------------------(Replace0) -----------------------------------------(Replace2)
;; ;; (plusRightId Z) (-> (plusRightId $x) (plusRightId (S $x)))
;; ;; ---------------------------------------------------------------------------------------------(NatInd)
;; ;; (plusRightId $x)
;; ;;
;; ;; or in MeTTa format
;; ;;
;; ;; ((NatInd ((Replace0 (Sym plusRightIdProp)) plusBase)) ((. (Replace0 (Sym plusRightIdProp))) ((. (Trans plusRec)) ((. Cong) (Replace0 plusRightIdProp)))))
;; ;;
;; ;; We provide half of the proof otherwise the search takes too much memory (over 64GB)
;; !(assertEqual
;; (bc (: ((NatInd ((Replace0 (Sym plusRightIdProp)) plusBase)) $subprf) (plusRightId $x)) (fromNumber 5))
;; (: ((NatInd ((Replace0 (Sym plusRightIdProp)) plusBase)) ((. (Replace0 (Sym plusRightIdProp))) ((. (Trans plusRec)) ((. Cong) (Replace0 plusRightIdProp))))) (plusRightId $x)))