-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract-eval.scm
63 lines (62 loc) · 2.3 KB
/
abstract-eval.scm
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
;;; "don't belive A.E., see for yourself the summer fields"
;;; abstract-eval : sex x metastore -> meta-sex
(define (abstract-eval expr meta-store)
(let AE ((expr expr))
(match expr
[(? ground-sex? g) (sex->metasex g)]
[(? var? v) (lookup v meta-store)]
[(op e)
(match `(,op ,(AE e))
[('car ('cons h t)) h]
[('cdr ('cons h t)) t]
[('car ('KONST _)) (error `(a-e error: ,expr))]
[('cdr ('KONST _)) (error `(a-e error: ,expr))]
[('atom? ('cons h t)) '(KONST ())]
[('atom? ('KONST k)) '(KONST T)]
[('atom? ('+ a b)) '(KONST T)]
[('atom? ('- a b)) '(KONST T)]
[('atom? ('* a b)) '(KONST T)]
[('atom? ('/ a b)) '(KONST T)]
[('atom? ('% a b)) '(KONST T)]
[('atom? ('= a b)) '(KONST T)]
[('atom? ('< a b)) '(KONST T)]
[('number? ('KONST k)) `(KONST ,(bool->T/nil (number? k)))]
[('number? ('cons h t)) '(KONST ())]
[('number? ('+ a b)) '(KONST T)]
[('number? ('- a b)) '(KONST T)]
[('number? ('* a b)) '(KONST T)]
[('number? ('/ a b)) '(KONST T)]
[('number? ('% a b)) '(KONST T)]
[otherwise otherwise])]
[(op e1 e2)
(match `(,op ,(AE e1) ,(AE e2))
[('+ ('KONST a) ('KONST b)) `(KONST ,(+ a b))]
[('- ('KONST a) ('KONST b)) `(KONST ,(- a b))]
[('* ('KONST a) ('KONST b)) `(KONST ,(* a b))]
[('/ ('KONST a) ('KONST b)) `(KONST ,(/ a b))] ; conv. to float?
[('% ('KONST a) ('KONST b)) `(KONST ,(modulo a b))]
[('= ('KONST a) ('KONST b)) `(KONST ,(bool->T/nil (eqv? a b)))]
[('< ('KONST a) ('KONST b)) `(KONST ,(bool->T/nil (< a b)))]
[('+ ('KONST 0) e) e]
[('+ e ('KONST 0)) e]
[('- e ('KONST 0)) e]
[('- e e) `(KONST 0)]
[('* ('KONST 1) e) e]
[('* e ('KONST 1)) e]
[('* ('KONST 0) e) `(KONST 0)]
[('* e ('KONST 0)) `(KONST 0)]
[('/ e 1) e]
[('/ e e) `(KONST 1)]
[('/ ('KONST 0) e) `(KONST 0)]
[('% e 1) e]
[('% ('KONST 0) e) `(KONST 0)]
[('= e ('cons h t)) `(KONST ())] ;; = works only on atoms, sir.
[('= ('cons h t) e) `(KONST ())]
;;; NB do not add simplifications in here!
;;; like (- (- (cv 1) (k 1)) (k 1)) -> (- (cv 1) (k 2))
;;; or even [('+ e e) `(* ,e (KONST 2))]
;;; because "should-be-generalized?" would not recognize them.
;;; only the ones leading to KONST are ok.
;;; and perhaps the ones with neutral element...
[otherwise otherwise])]
[otherwise 'ERROR]))) ;; todo: nice error quit sth