forked from drspro/metta-wam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NatSimpleTest~OLD.metta
executable file
·136 lines (123 loc) · 6.64 KB
/
NatSimpleTest~OLD.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Definition of Nat and proofs of simple statements such 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
(: UnaryEqStruct (-> ;; Premise
(=== $x $x')
;; Conclusion
(=== ($op $x) ($op $x'))))
(: BinaryEqStruct (-> ;; Premises
(=== $x $x')
(=== $y $y')
;; Conclusion
(=== ($op $x $y) ($op $x' $y'))))
;; Rule of replacement
(: UnaryRepl (-> ;; Premises
(=== $x $x')
($op $x)
;; Conclusion
($op $x')))
;; Disabled because it takes too much time and is not needed
;; (: BinaryRepl (-> ;; Premises
;; (=== $x $x')
;; (=== $y $y')
;; ($op $x $y)
;; ;; Conclusion
;; ($op $x' $y')))
)))
;; Prove that Z is left identity of plus
!(synthesize (: $proof (=== (plus Z $y) $y)) kb rb Z)
;; 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) -------------------------(UnaryEqStruct)
;; (=== (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 (UnaryEqStructPres Base_plus))
;;
;; The second simplest one seems to be
;;
;; -----------------(Base_plus)
;; (=== (plus Z Z) Z)
;; ----------------------------------(EqRefl) -------------------------(UnaryEqStruct) ----------------------------------(Rec_plus)
;; (=== (plus (S Z) Z) (plus (S Z) Z)) (=== (S (plus Z Z)) (S Z)) (=== (plus (S Z) Z) (S (plus Z Z)))
;; ------------------------------------------------------------------------------------------------------------------------(BinaryRepl)
;; (=== (plus (S Z) Z) (S Z))
;; or in MeTTa format:
;;
;; (BinaryRepl EqRefl (UnaryEqStruct Base_plus) Rec_plus) (=== (plus (S Z) Z) (S Z)))
!(synthesize (: $proof (=== (plus (S Z) Z) (S Z))) kb rb (fromNumber 2))
;; 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) ----------------------------(UnaryEqStruct)
;; (=== (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 (UnaryEqStructPres 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.
!(synthesize (: $proof (=== (plus (S Z) (S Z)) (S (S Z)))) kb rb (fromNumber 2))
;; Prove that 1 + 10 = 11
!(synthesize (: $proof (=== (plus (fromNumber 1) (fromNumber 10)) (fromNumber 11))) kb rb (fromNumber 2))
;; 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) ----------------------------------------------------------(UnaryEqStruct) -----------------------------------------(UnaryEqStruct)
;; (=== (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) -------------------------------------------------(UnaryEqStruct)
;; (=== (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 (UnaryEqStruct Rec_plus)) (UnaryEqStruct (UnaryEqStruct Base_plus)))
!(synthesize (: $proof (=== (plus (fromNumber 2) (fromNumber 2)) (fromNumber 4))) kb rb (fromNumber 3))