forked from drspro/metta-wam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NatSimpleTest.metta
executable file
·167 lines (154 loc) · 8.29 KB
/
NatSimpleTest.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Definition of Nat and proofs of simple statements such as 2 + 2 = 4 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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 append function definition
(: Base_plus (=== (plus Z $y) $y))
;; Recursive step of append function definition
(: Rec_plus (=== (plus (S $x) $y) (S (plus $x $y)))))))
;; 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, unary and binary
(: 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, unary and binary
(: Repl1 (-> ;; Premises
(=== $x $x')
($op $x)
;; Conclusion
($op $x')))
;; Disabled to speed things up
;; (: Repl2 (-> ;; Premises
;; (=== $x $x')
;; (=== $y $y')
;; ($op $x $y)
;; ;; Conclusion
;; ($op $x' $y')))
)))
;; Prove that Z is left identity of plus
;; TODO: re-enable once subtitution is fixed
;; !(assertEqual
!(synthesize (: $proof (=== (plus Z $y) $y)) kb rb Z)
;; (: Base_plus (=== (plus Z $y#90) $y#90)))
;; Prove that 1 + 0 = 1
;;
;; They are many possible proof trees, the simplest one seems to be
;;
;; -----------------(Base_plus)
;; (=== (plus Z Z) Z)
;; ----------------------------------(Rec_plus) -------------------------(EqStruct1)
;; (=== (plus (S Z) Z) (S (plus Z Z))) (=== (S (plus Z Z)) (S Z))
;; -----------------------------------------------------------------------(EqTrans)
;; (=== (plus (S Z) Z) (S Z))
;;
;; or in MeTTa format:
;;
;; (EqTrans Rec_plus (EqStruct1 Base_plus))
;;
;; The second simplest one seems to be
;;
;; -----------------(Base_plus)
;; (=== (plus Z Z) Z)
;; ----------------------------------(EqRefl) -------------------------(EqStruct1) ----------------------------------(Rec_plus)
;; (=== (plus (S Z) Z) (plus (S Z) Z)) (=== (S (plus Z Z)) (S Z)) (=== (plus (S Z) Z) (S (plus Z Z)))
;; ------------------------------------------------------------------------------------------------------------------------(Repl2)
;; (=== (plus (S Z) Z) (S Z))
;; or in MeTTa format:
;;
;; (Repl2 EqRefl (EqStruct1 Base_plus) Rec_plus) (=== (plus (S Z) Z) (S Z)))
;;
;; But is not generated because BinaryRepl
!(assertEqualToResult
(synthesize (: $proof (=== (plus (S Z) Z) (S Z))) kb rb (fromNumber 2))
((: (EqTrans Rec_plus (EqStruct1 Base_plus))
(=== (plus (S Z) Z) (S Z)))
(: (EqTrans (EqTrans EqRefl Rec_plus) (EqStruct1 Base_plus))
(=== (plus (S Z) Z) (S Z)))
(: (EqTrans (EqTrans Rec_plus EqRefl) (EqStruct1 Base_plus))
(=== (plus (S Z) Z) (S Z)))))
;; Prove that 1 + 1 = 2
;;
;; The two simplest proof trees are the same as 1 + 0 = 1.
;;
;;
;; They are many possible proof trees, the simplest is the same as the
;; one for 1 + 0 = 1, that is:
;;
;; -----------------------(Base_plus)
;; (=== (plus Z (S Z) (S Z)
;; ------------------------------------------(Rec_plus) ----------------------------(EqStruct1)
;; (=== (plus (S Z) (S Z)) (S (plus (S Z) Z))) (=== (S (plus Z Z)) (S (S Z))
;; ----------------------------------------------------------------------------------(EqTrans)
;; (=== (plus (S Z) (S Z)) (S (S Z)))
;;
;; or in MeTTa format:
;;
;; (EqTrans Rec_plus (EqStruct1 Base_plus))
;;
;; This is due to the left recursive definition of plus. The proof
;; tree of 1 + N = (S N) where N is any large number would be equally
;; simple.
!(assertEqualToResult
(synthesize (: $proof (=== (plus (S Z) (S Z)) (S (S Z)))) kb rb (fromNumber 2))
((: (EqTrans Rec_plus (EqStruct1 Base_plus))
(=== (plus (S Z) (S Z)) (S (S Z))))
(: (EqTrans (EqTrans EqRefl Rec_plus) (EqStruct1 Base_plus))
(=== (plus (S Z) (S Z)) (S (S Z))))
(: (EqTrans (EqTrans Rec_plus EqRefl) (EqStruct1 Base_plus))
(=== (plus (S Z) (S Z)) (S (S Z))))))
;; Prove that 1 + 5 = 6
!(assertEqualToResult
(synthesize (: $proof (=== (plus (fromNumber 1) (fromNumber 5)) (fromNumber 6))) kb rb (fromNumber 2))
((: (EqTrans Rec_plus (EqStruct1 Base_plus))
(=== (plus (S Z) (S (S (S (S (S Z)))))) (S (S (S (S (S (S Z))))))))
(: (EqTrans (EqTrans EqRefl Rec_plus) (EqStruct1 Base_plus))
(=== (plus (S Z) (S (S (S (S (S Z)))))) (S (S (S (S (S (S Z))))))))
(: (EqTrans (EqTrans Rec_plus EqRefl) (EqStruct1 Base_plus))
(=== (plus (S Z) (S (S (S (S (S Z)))))) (S (S (S (S (S (S Z))))))))))
;; Prove that 2 + 2 = 4
;;
;;
;; --------------------------------------------------(Rec_plus) ---------------------------------(Base_plus)
;; (=== (plus (S Z) (S (S Z))) (S (plus Z (S (S Z))))) (=== (plus Z (S (S Z))) (S (S Z)))
;; ----------------------------------------------------------(Rec_plus) ----------------------------------------------------------(EqStruct1) -----------------------------------------(EqStruct1)
;; (=== (plus (S (S Z)) (S (S Z))) (S (plus (S Z) (S (S Z))))) (=== (S (plus (S Z) (S (S Z)))) (S (S (plus Z (S (S Z)))))) (=== (S (plus Z (S (S Z)))) (S (S (S Z))))
;; --------------------------------------------------------------------------------------------------------------------------------(EqTrans) -------------------------------------------------(EqStruct1)
;; (=== (plus (S (S Z)) (S (S Z))) (S (S (plus Z (S (S Z)))))) (=== (S (S (plus Z (S (S Z))))) (S (S (S (S Z)))))
;; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(EqTrans)
;; (=== (plus (S (S Z)) (S (S Z))) (S (S (S (S Z)))))
;;
;; or in MeTTa format:
;;
;; (EqTrans (EqTrans Rec_plus (EqStruct1 Rec_plus)) (EqStruct1 (EqStruct1 Base_plus)))
;; TODO: re-enable when we have found a way to get rid of the cruft
;; !(assertEqualToResult
!(synthesize (: $proof (=== (plus (fromNumber 2) (fromNumber 2)) (fromNumber 4))) kb rb (fromNumber 3))
;; (: (EqTrans (EqTrans Rec_plus (EqStruct1 Rec_plus)) (EqStruct1 (EqStruct1 Base_plus))) (=== (plus (S (S Z)) (S (S Z))) (S (S (S (S Z))))))
;; (: (EqTrans (EqTrans (EqTrans EqRefl Rec_plus) (EqStruct1 Rec_plus)) (EqStruct1 (EqStruct1 Base_plus))) (=== (plus (S (S Z)) (S (S Z))) (S (S (S (S Z))))))
;; (: (EqTrans (EqTrans (EqTrans Rec_plus EqRefl) (EqStruct1 Rec_plus)) (EqStruct1 (EqStruct1 Base_plus))) (=== (plus (S (S Z)) (S (S Z))) (S (S (S (S Z))))))