Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
225 lines (202 sloc) 7.15 KB
(define tagged? (lambda (t) (lambda (e) (and (pair? e) (eq? (car e) t)))))
(define s (lambda (e)
(((tagged? 'clo) e) `(clo _ ,(caddr e)))
((pair? e) (cons (s (car e)) (s (cdr e))))
(else e))))
(define code? (tagged? 'code))
(define code-exp cadr)
(define force-code
(lambda (f)
(if (code? f) (code-exp f)
(error 'force-code (format "expected code, not ~a" (s f))))))
(define make-let (lambda (e1 e2) `(let ,e1 ,e2)))
(define stFresh 0)
(define stBlock '()) ;; List[Exp]
(define stFun '()) ;; List[(Int,Env,Exp)]
(define reset!
(lambda ()
(set! stFresh 0)
(set! stBlock '())
(set! stFun '())))
(define run
(lambda (thunk)
(let ((sF stFresh)
(sB stBlock)
(sN stFun))
(let ((r (thunk)))
(set! stFresh sF)
(set! stBlock sB)
(set! stFun sN)
(define fresh
(lambda ()
(set! stFresh (+ 1 stFresh))
`(var ,(- stFresh 1))))
(define reify
(lambda (thunk)
(run (lambda ()
(set! stBlock '())
(let ((last (thunk)))
(fold-right make-let last stBlock))))))
(define reflect
(lambda (s)
(set! stBlock (append stBlock (list s)))
(define reifyc
(lambda (thunk)
(reify (lambda () (force-code (thunk))))))
(define reflectc
(lambda (s) `(code ,(reflect s))))
(define reifyv
(lambda (thunk)
(run (lambda ()
(set! stBlock '())
(let ((res (thunk)))
(if (not (null? stBlock))
;; if we are generating code at all
;; the result must be code
(let ((last (force-code res)))
`(code ,(fold-right make-let last stBlock)))
(define findFun
(lambda (v)
(let ((env (cadr v))
(e (caddr v)))
(define iter
(lambda (fs)
(if (null? fs)
(let ((f (car fs)))
(if (and (equal? env (cadr f)) (equal? e (caddr f)))
(car f)
(iter (cdr fs)))))))
(iter stFun))))
;; NBE-style reify operator (semantics -> syntax)
(define lift
(lambda (v)
((number? v) v)
((symbol? v) v)
(((tagged? 'clo) v)
(let ((n (findFun v)))
(if n `(var ,n)
(let ((env2 (cadr v))
(e2 (caddr v)))
(set! stFun (append stFun (list (list stFresh env2 e2))))
`(lambda ,(reify
(lambda ()
(force-code (evalms
(append env2 `((code ,(fresh)) (code ,(fresh))))
((code? v) (reflect `(lift ,(force-code v))))
((pair? v)
(let ((a (force-code (car v)))
(b (force-code (cdr v))))
(reflect `(cons ,a ,b)))))))
;; this is basically nth!
(define lookup
(lambda (env n)
(if (null? env) (error 'lookup "unbound variable")
(if (= n 0) (car env)
(lookup (cdr env) (- n 1))))))
(define binary-op
(lambda (fun)
(lambda (env e)
;; Use let* to enforce evaluation order.
(let* ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (and (code? v1) (code? v2))
(reflectc (list (car e) (force-code v1) (force-code v2)))
(if (and (not (code? v1)) (not (code? v2)))
(fun v1 v2)
(error 'binary-op (format "stage error in ~a ~a ~a" (car e) (s v1) (s v2)))))))))
(define unary-op
(lambda (fun)
(lambda (env e)
(let ((v1 (evalms env (cadr e))))
(if (code? v1)
(reflectc (list (car e) (force-code v1)))
(fun v1))))))
(define b2n (lambda (r) (if r 1 0)))
(define pred-op
(lambda (fun)
(lambda (env e)
(let ((v1 (evalms env (cadr e))))
(if (code? v1)
(reflectc (list (car e) (force-code v1)))
(b2n (fun v1)))))))
(define log (lambda (e) (begin (display (s e)) (newline) e)))
(define lift-ref (lambda (e1 e2)
`(code (proc ,e1 ,(lambda (ignore) (e2))))))
(define evalms
(lambda (env e)
((number? e) e)
((symbol? e) e)
(((tagged? 'proc) e) ((caddr e) env))
(((tagged? 'var) e) (lookup env (cadr e)))
(((tagged? 'lambda) e) `(clo ,env ,(cadr e)))
(((tagged? 'let) e)
(let ((v1 (evalms env (cadr e))))
(evalms (append env (list v1)) (caddr e))))
(((tagged? 'lift) e)
`(code ,(lift (evalms env (cadr e)))))
(((tagged? 'lift-ref) e)
(lift-ref (evalms env (cadr e)) (lambda () (evalms env (caddr e)))))
(((tagged? 'run) e)
(let ((v1 (evalms env (cadr e)))
(thunk (lambda () (evalms env (caddr e)))))
(if (code? v1)
(reflectc `(run ,(force-code v1) ,(reifyc thunk)))
(reifyv (lambda () (evalms env (reifyc (lambda () (set! stFresh (length env)) (thunk)))))))))
(((tagged? 'code?) e)
(let ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (code? v1)
(reflectc `(code? ,(force-code v1) ,(force-code v2)))
(b2n (code? v2)))))
(((tagged? 'log) e)
(let ((v1 (evalms env (cadr e)))
(v2 (evalms env (caddr e))))
(if (code? v1)
(reflectc `(log ,(force-code v1) ,(force-code (if (code? v2) v2 (lift-ref '_ (lambda () v2))))))
(log v2))))
(((tagged? 'if) e)
(let ((vc (evalms env (cadr e))))
(if (code? vc)
(reflectc `(if ,(force-code vc)
,(reifyc (lambda () (evalms env (caddr e))))
,(reifyc (lambda () (evalms env (cadddr e))))))
(if (number? vc)
(if (not (= vc 0)) (evalms env (caddr e)) (evalms env (cadddr e)))
(error 'evalms (format "if expects number for condition, not ~a in ~a" (s vc) (s e)))))))
(((tagged? '+) e) ((binary-op +) env e))
(((tagged? '-) e) ((binary-op -) env e))
(((tagged? '*) e) ((binary-op *) env e))
(((tagged? 'eq?) e) ((binary-op (lambda (x y) (b2n (eq? x y)))) env e))
(((tagged? 'car) e) ((unary-op car) env e))
(((tagged? 'cdr) e) ((unary-op cdr) env e))
(((tagged? 'number?) e) ((pred-op number?) env e))
(((tagged? 'symbol?) e) ((pred-op symbol?) env e))
(((tagged? 'pair?) e) ((pred-op pair?) env e))
(((tagged? 'null?) e) ((pred-op null?) env e))
;; cons is an introduction form, so needs explicit lifting
(((tagged? 'cons) e) (cons (evalms env (cadr e)) (evalms env (caddr e))))
(else ;; assume application
(let ((v1 (evalms env (car e)))
(v2 (evalms env (cadr e))))
(if (and (code? v1) (code? v2))
`(code ,(reflect `(,(force-code v1) ,(force-code v2))))
(if ((tagged? 'clo) v1)
(evalms (append (cadr v1) (list v1 v2)) (caddr v1))
(error 'evalms (format "app expects closure, not ~a in ~a" (s v1) (s e))))))))))
(define base-fac-anf
'(let (lambda
(let (if (var 1)
(let (- (var 1) 1)
(let ((var 0) (var 2)) (let (* (var 1) (var 3)) (var 4))))
(var 2)))
(var 0)))