-
Notifications
You must be signed in to change notification settings - Fork 5
/
NatTest.metta
172 lines (154 loc) · 6.84 KB
/
NatTest.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Definition of Nat, plus and some proofs. ;;
;; ;;
;; The main proof of interest is that Z is the right identity of plus. ;;
;; That experiment does not prove it completely because it assumes its ;;
;; inductive property. ;;
;; ;;
;; For the complete proof see NatStandaloneTest.metta. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Import synthesizer (Nat is already defined in it)
!(import! &self ../synthesis/Synthesize.metta)
;; Knowledge base
(: kb (-> Atom))
(= (kb) (superpose
(;; Equality is reflexive. We use === instead of == to make sure it
;; ;; does not get reduced by the MeTTa interpreter.
;; (: EqRefl (=== $x $x))
;; Base case of plus function definition
(: Base_plus (=== (plus Z $y) $y))
;; ;; Recursive step of plus function definition
;; (: Rec_plus (=== (plus (S $x) $y) (S (plus $x $y))))
;; If Z is the right identity of x then it is the right
;; identity of (S x). This can be proved but is provided
;; here for simplicity. See NatStandaloneTest.metta for the
;; complete proof.
(: IndZRID_plus (-> (ZRID_plus $x) (ZRID_plus (S $x))))
;; 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.
(: PropZRID_plus (=== (ZRID_plus $x) (=== (plus $x Z) $x))))))
;; Rule base
(: rb (-> Atom))
(= (rb) (superpose
(;; Equality is transitive
;; (: EqTrans (-> ;; Premises
;; (=== $x $y)
;; (=== $y $z)
;; ;; Conclusion
;; (=== $x $z)))
;; Equality is symmetric
(: EqSym (-> ;; Premise
(=== $x $y)
;; Conclusion
(=== $y $x)))
;; Structural preservation of equality
(: EqStruct1 (-> ;; Premise
(=== $x $x')
;; Conclusion
(=== ($op $x) ($op $x'))))
;; (: EqStruct2 (-> ;; Premises
;; (=== $x $x')
;; (=== $y $y')
;; ;; Conclusion
;; (=== ($op $x $y) ($op $x' $y'))))
;; Rule of replacement
(: Repl0 (-> ;; Premise
(=== $x $x')
$x
;; Conclusion
$x'))
;; (: Repl1 (-> ;; Premises
;; (=== $x $x')
;; ($op $x)
;; ;; Conclusion
;; ($op $x')))
;; (: Repl2 (-> ;; Premises
;; (=== $x $x')
;; (=== $y $y')
;; ($op $x $y)
;; ;; Conclusion
;; ($op $x' $y')))
;; (: IndZRID_plus (-> (ZRID_plus $x) (ZRID_plus (S $x))))
;; Structural induction on Nat
;; (: IndNat (-> ;; Premises
;; (: $p (-> Nat Type)) ; property
;; ($p Z) ; base case
;; (-> (: $x Nat)
;; ($p $x)
;; ($p (S $x))) ; inductive step
;; ;; Conclusion
;; (-> (: $x' Nat) ($p $x')))))))
;; Structural induction on Nat
(: IndNat (-> ;; Premises
($p Z) ; base case
(-> ($p $x)
($p (S $x))) ; inductive step
;; Conclusion
($p $x'))))))
;; Prove that Z is left identity of plus
;; TODO: re-enable when subtitution is fixed
;; !(assertEqual
!(synthesize (: $prf (=== (plus Z $y) $y)) kb rb Z)
;; (: Base_plus (=== (plus Z $y) $y)))
;; Prove that (=== (plus Z Z) Z)
!(assertEqual
(synthesize (: $prf (=== (plus Z Z) Z)) kb rb Z)
(: Base_plus (=== (plus Z Z) Z)))
;; Prove that (=== (=== (plus Z Z) Z) (ZRID_plus Z)).
;;
;; The following proof tree is expected to prove that:
;;
;; -------------------------------------(PropZRID_plus)
;; (=== (ZRID_plus Z) (=== (plus Z Z) Z))
;; -------------------------------------(EqSym)
;; (=== (=== (plus Z Z) Z) (ZRID_plus Z))
;;
;; on in MeTTa format:
;;
;; (EqSym PropZRID_plus)
!(assertEqual
(synthesize (: $prf (=== (=== (plus Z Z) Z) (ZRID_plus Z))) kb rb (fromNumber 2))
(: (EqSym PropZRID_plus) (=== (=== (plus Z Z) Z) (ZRID_plus Z))))
;; Prove that (ZRID_plus Z) holds. Meaning that Z is the right
;; identity of plus for Z.
;;
;; The following proof tree does that:
;;
;; -------------------------------------(PropZRID_plus)
;; (=== (ZRID_plus Z) (=== (plus Z Z) Z))
;; -------------------------------------(EqSym) -----------------(Base_plus)
;; (=== (=== (plus Z Z) Z) (ZRID_plus Z)) (=== (plus Z Z) Z)
;; ---------------------------------------------------------------(Repl0)
;; (ZRID_plus Z)
;;
;; or in MeTTa format:
;;
;; (Repl0 (EqSym PropZRID_plus) Base_plus)
!(assertEqual
(synthesize (: $prf (ZRID_plus Z)) kb rb (fromNumber 2))
(: (Repl0 (EqSym PropZRID_plus) Base_plus) (ZRID_plus Z)))
;; Prove that (-> (ZRID_plus $x) (ZRID_plus (S $x)))
;;
;; It is an axiom for now.
;; TODO: re-enable when subtitution is fixed
;; !(assertEqual
!(synthesize (: $prf (-> (ZRID_plus $x) (ZRID_plus (S $x)))) kb rb Z)
;; (: IndZRID_plus (-> (ZRID_plus $x) (ZRID_plus (S $x)))))
;; Prove that Z is the right identity of plus
;;
;; -------------------------------------(PropZRID_plus)
;; (=== (ZRID_plus Z) (=== (plus Z Z) Z))
;; -------------------------------------(EqSym) -----------------(Base_plus)
;; (=== (=== (plus Z Z) Z) (ZRID_plus Z)) (=== (plus Z Z) Z)
;; ---------------------------------------------------------------(Repl0) -------------------------------------(IndZRID_plus)
;; (ZRID_plus Z) (-> (ZRID_plus $x) (ZRID_plus (S $x)))
;; --------------------------------------------------------------------------------------(IndNat)
;; (ZRID_plus $x)
;;
;; TODO: re-enable when an assertContainResults is supported
;;
;; !(assertEqual
!(synthesize (: $prf (ZRID_plus $x)) kb rb (fromNumber 3))
;; (: (IndNat (Repl0 (EqSym PropZRID_plus) Base_plus) IndZRID_plus) (ZRID_plus $x)))