-
Notifications
You must be signed in to change notification settings - Fork 3
/
3-Learn-Rules.metta
executable file
·118 lines (87 loc) · 3.45 KB
/
3-Learn-Rules.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
; !(import! &self examples/RUN_tests1.metta)
!(extend-py! metta_learner)
;A "better" if which works for both if-then and if-then-else:
(: If (-> Bool Atom Atom))
(= (If True $then) $then)
(= (If False $then) ())
(: If (-> Bool Atom Atom Atom))
(= (If $cond $then $else) (if $cond $then $else))
;Concats tuples:
(= (TupleConcat $Ev1 $Ev2) (collapse (superpose ((superpose $Ev1) (superpose $Ev2)))))
;Sequential by exploiting order preservation of current superpose implementation:
(: sequential (-> Expression %Undefined%))
(= (sequential $1) (superpose $1))
;Do not return result of expression
(: do (-> Expression %Undefined%))
(= (do $1) (case $1 ()))
; functional recursive factorial
(= (factorial 0) 1)
(= (factorial $n) (* $n (factorial (- $n 1))))
;;!(assertEqual (factorial 0) 1)
;;!(assertEqual (factorial 1) 1)
;;!(assertEqual (factorial 2) 2)
;;!(assertEqual (factorial 4) 24)
;;!(assertEqual (factorial 5) 120)
; functional tail-recursive factorial
(= (factorial-tail-rec $n) (factorial-tail-rec-helper $n 1))
(= (factorial-tail-rec-helper 0 $acc) $acc)
(= (factorial-tail-rec-helper $n $acc) (factorial-tail-rec-helper (- $n 1) (* $n $acc)))
;;!(assertEqual (factorial-tail-rec 0) 1)
;;!(assertEqual (factorial-tail-rec 1) 1)
;;!(assertEqual (factorial-tail-rec 2) 2)
;;!(assertEqual (factorial-tail-rec 3) 6)
;;!(assertEqual (factorial-tail-rec 4) 24)
;;!(assertEqual (factorial-tail-rec 5) 120)
; functional factorial using accumulators
(= (factorial-acc $n) (factorial-acc-helper $n 1))
(= (factorial-acc-helper 0 $acc) $acc)
(= (factorial-acc-helper $n $acc) (factorial-acc-helper (- $n 1) (* $n $acc)))
;;!(assertEqual (factorial-acc 0) 1)
;;!(assertEqual (factorial-acc 1) 1)
;;!(assertEqual (factorial-acc 2) 2)
;;!(assertEqual (factorial-acc 3) 6)
;;!(assertEqual (factorial-acc 4) 24)
;;!(assertEqual (factorial-acc 5) 120)
(= (is $x $x) true)
; logical recursive factorial
(= (factorial-pred 0 1) true)
(= (factorial-pred $n $out)
(and (> $n 0)
(is $f2 (- $n 1))
(factorial-pred $f2 $r)
(is $out (* $n $r))))
;;!(assertTrue (factorial-pred 0 1))
;;!(assertTrue (factorial-pred 1 1))
;;!(assertTrue (factorial-pred 2 2))
;;!(assertTrue (factorial-pred 3 6))
;;!(assertTrue (factorial-pred 4 24))
;;!(assertTrue (factorial-pred 5 120))
; logical factorial using accumulators
(= (factorial-pred-acc $f $accout) (factorial-pred-acc-helper $f 1 $accout))
(= (factorial-pred-acc-helper 0 $accout $accout) true)
(= (factorial-pred-acc-helper $n $f $accout)
(and (> $n 0)
(is $x (* $f $n))
(is $f1 (- $n 1))
(factorial-pred-acc-helper $f1 $x $accout)))
;;!(assertTrue (factorial-pred-acc 0 1))
;;!(assertTrue (factorial-pred-acc 1 1))
;;!(assertTrue (factorial-pred-acc 2 2))
;;!(assertTrue (factorial-pred-acc 3 6))
;;!(assertTrue (factorial-pred-acc 4 24))
;;!(assertTrue (factorial-pred-acc 5 120))
; logical tail-recursive factorial
(= (factorial-pred-tail-rec $f $rout) (factorial-pred-tail-rec-helper $f 1 $rout))
(= (factorial-pred-tail-rec-helper 0 $h $h) true)
(= (factorial-pred-tail-rec-helper $n1 $f $rout)
(and (> $n1 0)
(is $x (* $f $n1))
(is $f2 (- $n1 1))
(factorial-pred-tail-rec-helper $f2 $x $rout)))
;;!(assertTrue (factorial-pred-tail-rec 0 1))
;;!(assertTrue (factorial-pred-tail-rec 1 1))
;;!(assertTrue (factorial-pred-tail-rec 2 2))
;;!(assertTrue (factorial-pred-tail-rec 3 6))
;;!(assertTrue (factorial-pred-tail-rec 4 24))
;;!(assertTrue (factorial-pred-tail-rec 5 120))
!(metta_learner::vspace-main)