-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple-interp.scm
37 lines (34 loc) · 994 Bytes
/
simple-interp.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
(define evalo
(lambda (expr val)
(eval-expro expr '() val)))
(define eval-expro
(lambda (expr env val)
(conde
((fresh (rator rand x body env^ a)
(== `(,rator ,rand) expr)
(eval-expro rator env `(closure ,x ,body ,env^))
(eval-expro rand env a)
(eval-expro body `((,x . ,a) . ,env^) val)))
((fresh (x body)
(== `(lambda (,x) ,body) expr)
(symbolo x)
(== `(closure ,x ,body ,env) val)
(not-in-envo 'lambda env)))
((symbolo expr) (lookupo expr env val)))))
(define not-in-envo
(lambda (x env)
(conde
((== '() env))
((fresh (y v rest)
(== `((,y . ,v) . ,rest) env)
(=/= y x)
(not-in-envo x rest))))))
(define lookupo
(lambda (x env t)
(conde
((fresh (y v rest)
(== `((,y . ,v) . ,rest) env) (== y x)
(== v t)))
((fresh (y v rest)
(== `((,y . ,v) . ,rest) env) (=/= y x)
(lookupo x rest t))))))