forked from trueagi-io/hyperon-experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_e2_states_dia.metta
More file actions
68 lines (56 loc) · 1.92 KB
/
_e2_states_dia.metta
File metadata and controls
68 lines (56 loc) · 1.92 KB
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
; This example rewritten with `case` works (as per 19 Oct 22),
; but it might not be good as a unit test:
; - what's going on is not obvious
; - there might be a better way to implement what the code is doing
; - processing commands with side effect and empty results and
; chaining them might not be idiomatic
; - it's relatively slow (why? FIXME?)
!(bind! &memory (new-space))
; catching empty results
(: do (-> Atom %Undefined%))
(= (do $expr)
(case $expr
(($x $x)
(%void% None))))
; `remove-state` will return an empty result if there is no
; corresponding state in the memory
(= (remove-state $var)
(match &memory (state $var $y)
(call:remove_atom &memory (state $var $y))))
; using `do` assures that the next clause will be executed
; even if the result of the previous clause is empty
(= (change-state $var $value)
(nop (do (remove-state $var))
(do (add-atom &memory (state $var $value)))))
(= (get-state $var)
(match &memory (state $var $value) $value))
; Earlier, `(get-state (person name))` got cached, and it changes
; in one equality for `listen` didn't affect other equalities.
; `case` accepts an `Atom` and forcefully evaluates it.
; However, the idea of using equalities as sequentially processed
; rules with side effects is still questionable
(= (listen (My name is $x))
(case (get-state (person name))
(($x (Yes, I remember))
; $y cannot be %void%
($y (I thought you are $y)))
)
)
(= (listen (My name is $x))
(change-state (person name) $x))
(= (listen $_)
(case (get-state greeted)
((%void%
(case (get-state (person name))
((%void% (Hi. What is your name?))
($p-name (Nice to meet you $p-name)))))
)
)
)
(= (listen $_)
(nop (change-state greeted True)))
;!(listen (Hello))
;!(listen (My name is Alexey))
;!(listen (My name is Alexey))
;!(listen (My name is Ben))
;!(call:get_atoms &memory)