-
Notifications
You must be signed in to change notification settings - Fork 5
/
00_lang_case.metta
executable file
·116 lines (79 loc) · 1.92 KB
/
00_lang_case.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
; cases are processed sequentially
!(assertEqual
(+ 1 (superpose (1 2 3)))
(+ 1 (superpose (1 2 3))))
!(assertEqual
(+ 1 (superpose (1 2 3)))
(+ 1 (superpose (1 (superpose (2 3))))))
!(assertEqual
((superpose (1 2 3)))
((superpose (1 (superpose ((+ 1 1) 3))))))
; cases are processed sequentially
!(assertEqual
(case (+ 1 5)
((5 Error)
(6 OK)
(6 Error)))
OK)
; we can use variables as cases
!(assertEqual
(case (+ 1 5)
(($x (+ 1 $x))))
7)
; cases are not necessarily exhaustive,
; and the result can be empty
!(assertEqual
(case 5
((6 OK)))
(superpose ()))
; it is non-deterministic: each value is matched against all cases
!(assertEqual
(case (+ 1 (superpose (1 2 3)))
((3 OK-3)
(4 OK-4)))
(superpose (OK-3 OK-4)))
; one case can produce multiple results
!(assertEqual
(case (+ 1 (superpose (1 2 3)))
(($x (+ 1 $x))))
(superpose (3 4 5)))
!(assertEqualToResult (superpose ()) ())
!(assertEqualToResult (collapse (superpose ())) (()))
!(assertEqualToResult (superpose ((nop 1))) ())
!(assertEqualToResult (collapse (superpose ((nop 1)))) (()))
(= (Rel-P P P P) 10)
(Rel-P A B)
(Rel-Q A C)
!(match &self $1 $1)
!(get-atoms &self)
!(assertEqual (match &self $1 $1) (get-atoms &self))
;;l !(repl!)
!(assertEqualToResult
(match &self
($Rel A $X)
($Rel A $X))
((Rel-P A B)
(Rel-Q A C)))
!(assertEqualToResult
(superpose ((Rel-P B) (Rel-Q C)))
((Rel-P B) (Rel-Q C)))
; Basic Match
!(assertEqual
(match &self ($rel A $x) ($rel $x))
(superpose ((Rel-P B) (Rel-Q C))))
; cases can be used for deconstruction
!(assertEqual
(superpose ((P B) (Q C)))
(case
(match &self ($rel A $x) ($rel $x))
(((Rel-P $y) (P $y))
((Rel-Q $y) (Q $y)))))
; %void% can be used to capture empty results
!(assertEqual
(case
(match &self ($rel B $x) ($rel $x))
( ((Rel-P $y) (P $y))
((Rel-Q $y) (Q $y))
(%void% no-match
)))
no-match )