moving stuff around some more
This commit is contained in:
+122
@@ -0,0 +1,122 @@
|
||||
(define (bq-process2 x d)
|
||||
(define (splice-form? x)
|
||||
(or (and (pair? x) (or (eq? (car x) 'unquote-splicing)
|
||||
(eq? (car x) 'unquote-nsplicing)
|
||||
(and (eq? (car x) 'unquote)
|
||||
(length> x 2))))
|
||||
(eq? x 'unquote)))
|
||||
;; bracket without splicing
|
||||
(define (bq-bracket1 x)
|
||||
(if (and (pair? x) (eq? (car x) 'unquote))
|
||||
(if (= d 0)
|
||||
(cadr x)
|
||||
(list cons ''unquote
|
||||
(bq-process2 (cdr x) (- d 1))))
|
||||
(bq-process2 x d)))
|
||||
(define (bq-bracket x)
|
||||
(cond ((atom? x) (list list (bq-process2 x d)))
|
||||
((eq? (car x) 'unquote)
|
||||
(if (= d 0)
|
||||
(cons list (cdr x))
|
||||
(list list (list cons ''unquote
|
||||
(bq-process2 (cdr x) (- d 1))))))
|
||||
((eq? (car x) 'unquote-splicing)
|
||||
(if (= d 0)
|
||||
(list 'copy-list (cadr x))
|
||||
(list list (list list ''unquote-splicing
|
||||
(bq-process2 (cadr x) (- d 1))))))
|
||||
((eq? (car x) 'unquote-nsplicing)
|
||||
(if (= d 0)
|
||||
(cadr x)
|
||||
(list list (list list ''unquote-nsplicing
|
||||
(bq-process2 (cadr x) (- d 1))))))
|
||||
(else (list list (bq-process2 x d)))))
|
||||
(cond ((symbol? x) (list 'quote x))
|
||||
((vector? x)
|
||||
(let ((body (bq-process2 (vector->list x) d)))
|
||||
(if (eq? (car body) list)
|
||||
(cons vector (cdr body))
|
||||
(list apply vector body))))
|
||||
((atom? x) x)
|
||||
((eq? (car x) 'quasiquote)
|
||||
(list list ''quasiquote (bq-process2 (cadr x) (+ d 1))))
|
||||
((eq? (car x) 'unquote)
|
||||
(if (and (= d 0) (length= x 2))
|
||||
(cadr x)
|
||||
(list cons ''unquote (bq-process2 (cdr x) (- d 1)))))
|
||||
((or (> d 0) (not (any splice-form? x)))
|
||||
(let ((lc (lastcdr x))
|
||||
(forms (map bq-bracket1 x)))
|
||||
(if (null? lc)
|
||||
(cons list forms)
|
||||
(if (null? (cdr forms))
|
||||
(list cons (car forms) (bq-process2 lc d))
|
||||
(nconc (cons list* forms) (list (bq-process2 lc d)))))))
|
||||
(else
|
||||
(let loop ((p x) (q ()))
|
||||
(cond ((null? p) ;; proper list
|
||||
(cons 'nconc (reverse! q)))
|
||||
((pair? p)
|
||||
(cond ((eq? (car p) 'unquote)
|
||||
;; (... . ,x)
|
||||
(cons 'nconc
|
||||
(nreconc q
|
||||
(if (= d 0)
|
||||
(cdr p)
|
||||
(list (list list ''unquote)
|
||||
(bq-process2 (cdr p)
|
||||
(- d 1)))))))
|
||||
(else
|
||||
(loop (cdr p) (cons (bq-bracket (car p)) q)))))
|
||||
(else
|
||||
;; (... . x)
|
||||
(cons 'nconc (reverse! (cons (bq-process2 p d) q)))))))))
|
||||
|
||||
#|
|
||||
tests
|
||||
|
||||
> ``(,a ,,a ,b ,@b ,,@b)
|
||||
`(,a ,1 ,b ,@b (unquote 2 3))
|
||||
> `(,a ,1 ,b ,@b (unquote 2 3))
|
||||
(1 1 (2 3) 2 3 2 3)
|
||||
|
||||
(define a 1)
|
||||
|
||||
(bq-process2 '`(,a (unquote unquote a)) 0)
|
||||
|
||||
(define b '(unquote a))
|
||||
(define unquote 88)
|
||||
(bq-process2 '``(,a ,,,@b) 0)
|
||||
; etc. => (1 88 1)
|
||||
|
||||
(define b '(a a))
|
||||
(bq-process2 '``(,a ,,,@b) 0)
|
||||
; etc. => (1 1 1)
|
||||
|#
|
||||
|
||||
;; minimal version with no optimizations, vectors, or dotted lists
|
||||
(define (bq-process0 x d)
|
||||
(define (bq-bracket x)
|
||||
(cond ((and (pair? x) (eq? (car x) 'unquote))
|
||||
(if (= d 0)
|
||||
(cons list (cdr x))
|
||||
(list list (list cons ''unquote
|
||||
(bq-process0 (cdr x) (- d 1))))))
|
||||
((and (pair? x) (eq? (car x) 'unquote-splicing))
|
||||
(if (= d 0)
|
||||
(list 'copy-list (cadr x))
|
||||
(list list (list list ''unquote-splicing
|
||||
(bq-process0 (cadr x) (- d 1))))))
|
||||
(else (list list (bq-process0 x d)))))
|
||||
(cond ((symbol? x) (list 'quote x))
|
||||
((atom? x) x)
|
||||
((eq? (car x) 'quasiquote)
|
||||
(list list ''quasiquote (bq-process0 (cadr x) (+ d 1))))
|
||||
((eq? (car x) 'unquote)
|
||||
(if (and (= d 0) (length= x 2))
|
||||
(cadr x)
|
||||
(list cons ''unquote (bq-process0 (cdr x) (- d 1)))))
|
||||
(else
|
||||
(cons 'nconc (map bq-bracket x)))))
|
||||
|
||||
#t
|
||||
@@ -0,0 +1,308 @@
|
||||
; -*- scheme -*-
|
||||
(define (begin->cps forms k)
|
||||
(cond ((atom? forms) `(,k ,forms))
|
||||
((null? (cdr forms)) (cps- (car forms) k))
|
||||
(#t (let ((_ (gensym))) ; var to bind ignored value
|
||||
(cps- (car forms) `(lambda (,_)
|
||||
,(begin->cps (cdr forms) k)))))))
|
||||
|
||||
(define-macro (lambda/cc args body)
|
||||
`(cons 'lambda/cc (lambda ,args ,body)))
|
||||
|
||||
; a utility used at run time to dispatch a call with or without
|
||||
; the continuation argument, depending on the function
|
||||
(define (funcall/cc f k . args)
|
||||
(if (and (pair? f) (eq (car f) 'lambda/cc))
|
||||
(apply (cdr f) (cons k args))
|
||||
(k (apply f args))))
|
||||
(define *funcall/cc-names*
|
||||
(list->vector
|
||||
(map (lambda (i) (symbol (string 'funcall/cc- i)))
|
||||
(iota 6))))
|
||||
(define-macro (def-funcall/cc-n args)
|
||||
(let ((name (aref *funcall/cc-names* (length args))))
|
||||
`(define (,name f k ,@args)
|
||||
(if (and (pair? f) (eq (car f) 'lambda/cc))
|
||||
((cdr f) k ,@args)
|
||||
(k (f ,@args))))))
|
||||
(def-funcall/cc-n ())
|
||||
(def-funcall/cc-n (a0))
|
||||
(def-funcall/cc-n (a0 a1))
|
||||
(def-funcall/cc-n (a0 a1 a2))
|
||||
(def-funcall/cc-n (a0 a1 a2 a3))
|
||||
(def-funcall/cc-n (a0 a1 a2 a3 a4))
|
||||
|
||||
(define (rest->cps xformer form k argsyms)
|
||||
(let ((el (car form)))
|
||||
(if (or (atom? el) (constant? el))
|
||||
(xformer (cdr form) k (cons el argsyms))
|
||||
(let ((g (gensym)))
|
||||
(cps- el `(lambda (,g)
|
||||
,(xformer (cdr form) k (cons g argsyms))))))))
|
||||
|
||||
(define (make-funcall/cc head ke args)
|
||||
(let ((n (length args)))
|
||||
(if (< n 6)
|
||||
`(,(aref *funcall/cc-names* n) ,head ,ke ,@args)
|
||||
`(funcall/cc ,head ,ke ,@args))))
|
||||
|
||||
; (f x) => (cps- f `(lambda (F) ,(cps- x `(lambda (X) (F ,k X)))))
|
||||
(define (app->cps form k argsyms)
|
||||
(cond ((atom? form)
|
||||
(let ((r (reverse argsyms)))
|
||||
(make-funcall/cc (car r) k (cdr r))))
|
||||
(#t (rest->cps app->cps form k argsyms))))
|
||||
|
||||
; (+ x) => (cps- x `(lambda (X) (,k (+ X))))
|
||||
(define (builtincall->cps form k)
|
||||
(prim->cps (cdr form) k (list (car form))))
|
||||
(define (prim->cps form k argsyms)
|
||||
(cond ((atom? form) `(,k ,(reverse argsyms)))
|
||||
(#t (rest->cps prim->cps form k argsyms))))
|
||||
|
||||
(define *top-k* (gensym))
|
||||
(set-top-level-value! *top-k* identity)
|
||||
|
||||
(define (cps form)
|
||||
(η-reduce
|
||||
(β-reduce
|
||||
(expand
|
||||
(cps- (expand form) *top-k*)))))
|
||||
(define (cps- form k)
|
||||
(let ((g (gensym)))
|
||||
(cond ((or (atom? form) (constant? form))
|
||||
`(,k ,form))
|
||||
|
||||
((eq (car form) 'lambda)
|
||||
`(,k (lambda/cc ,(cons g (cadr form)) ,(cps- (caddr form) g))))
|
||||
|
||||
((eq (car form) 'begin)
|
||||
(begin->cps (cdr form) k))
|
||||
|
||||
((eq (car form) 'if)
|
||||
(let ((test (cadr form))
|
||||
(then (caddr form))
|
||||
(else (cadddr form)))
|
||||
(if (atom? k)
|
||||
(cps- test `(lambda (,g)
|
||||
(if ,g
|
||||
,(cps- then k)
|
||||
,(cps- else k))))
|
||||
`(let ((,g ,k))
|
||||
,(cps- form g)))))
|
||||
|
||||
((eq (car form) 'and)
|
||||
(cond ((atom? (cdr form)) `(,k #t))
|
||||
((atom? (cddr form)) (cps- (cadr form) k))
|
||||
(#t
|
||||
(if (atom? k)
|
||||
(cps- (cadr form)
|
||||
`(lambda (,g)
|
||||
(if ,g ,(cps- `(and ,@(cddr form)) k)
|
||||
(,k ,g))))
|
||||
`(let ((,g ,k))
|
||||
,(cps- form g))))))
|
||||
|
||||
((eq (car form) 'or)
|
||||
(cond ((atom? (cdr form)) `(,k #f))
|
||||
((atom? (cddr form)) (cps- (cadr form) k))
|
||||
(#t
|
||||
(if (atom? k)
|
||||
(cps- (cadr form)
|
||||
`(lambda (,g)
|
||||
(if ,g (,k ,g)
|
||||
,(cps- `(or ,@(cddr form)) k))))
|
||||
`(let ((,g ,k))
|
||||
,(cps- form g))))))
|
||||
|
||||
((eq (car form) 'while)
|
||||
(let ((test (cadr form))
|
||||
(body (caddr form))
|
||||
(lastval (gensym)))
|
||||
(cps- (expand
|
||||
`(let ((,lastval #f))
|
||||
((label ,g (lambda ()
|
||||
(if ,test
|
||||
(begin (set! ,lastval ,body)
|
||||
(,g))
|
||||
,lastval))))))
|
||||
k)))
|
||||
|
||||
((eq (car form) 'set!)
|
||||
(let ((var (cadr form))
|
||||
(E (caddr form)))
|
||||
(cps- E `(lambda (,g) (,k (set! ,var ,g))))))
|
||||
|
||||
((eq (car form) 'reset)
|
||||
`(,k ,(cps- (cadr form) *top-k*)))
|
||||
|
||||
((eq (car form) 'shift)
|
||||
(let ((v (cadr form))
|
||||
(E (caddr form))
|
||||
(val (gensym)))
|
||||
`(let ((,v (lambda/cc (,g ,val) (,g (,k ,val)))))
|
||||
,(cps- E *top-k*))))
|
||||
|
||||
((eq (car form) 'without-delimited-continuations)
|
||||
`(,k ,(cadr form)))
|
||||
|
||||
((and (constant? (car form))
|
||||
(builtin? (eval (car form))))
|
||||
(builtincall->cps form k))
|
||||
|
||||
; ((lambda (...) body) ...)
|
||||
((and (pair? (car form))
|
||||
(eq (caar form) 'lambda))
|
||||
(let ((largs (cadr (car form)))
|
||||
(lbody (caddr (car form))))
|
||||
(cond ((null? largs) ; ((lambda () body))
|
||||
(cps- lbody k))
|
||||
((symbol? largs) ; ((lambda x body) args...)
|
||||
(cps- `((lambda (,largs) ,lbody) (list ,@(cdr form))) k))
|
||||
(#t
|
||||
(cps- (cadr form) `(lambda (,(car largs))
|
||||
,(cps- `((lambda ,(cdr largs) ,lbody)
|
||||
,@(cddr form))
|
||||
k)))))))
|
||||
|
||||
(#t
|
||||
(app->cps form k ())))))
|
||||
|
||||
; (lambda (args...) (f args...)) => f
|
||||
; but only for constant, builtin f
|
||||
(define (η-reduce form)
|
||||
(cond ((or (atom? form) (constant? form)) form)
|
||||
((and (eq (car form) 'lambda)
|
||||
(let ((body (caddr form))
|
||||
(args (cadr form)))
|
||||
(and (pair? body)
|
||||
(equal? (cdr body) args)
|
||||
(constant? (car (caddr form))))))
|
||||
(car (caddr form)))
|
||||
(#t (map η-reduce form))))
|
||||
|
||||
(define (contains x form)
|
||||
(or (eq form x)
|
||||
(any (lambda (p) (contains x p)) form)))
|
||||
|
||||
(define (β-reduce form)
|
||||
(if (or (atom? form) (constant? form))
|
||||
form
|
||||
(β-reduce- (map β-reduce form))))
|
||||
|
||||
(define (β-reduce- form)
|
||||
; ((lambda (f) (f arg)) X) => (X arg)
|
||||
(cond ((and (length= form 2)
|
||||
(pair? (car form))
|
||||
(eq (caar form) 'lambda)
|
||||
(let ((args (cadr (car form)))
|
||||
(body (caddr (car form))))
|
||||
(and (pair? body) (pair? args)
|
||||
(length= body 2)
|
||||
(length= args 1)
|
||||
(eq (car body) (car args))
|
||||
(not (eq (cadr body) (car args)))
|
||||
(symbol? (cadr body)))))
|
||||
`(,(cadr form)
|
||||
,(cadr (caddr (car form)))))
|
||||
|
||||
; (identity x) => x
|
||||
((eq (car form) *top-k*)
|
||||
(cadr form))
|
||||
|
||||
; uncurry:
|
||||
; ((lambda (p1) ((lambda (args...) body) exprs...)) s) =>
|
||||
; ((lambda (p1 args...) body) s exprs...)
|
||||
; where exprs... doesn't contain p1
|
||||
((and (length= form 2)
|
||||
(pair? (car form))
|
||||
(eq (caar form) 'lambda)
|
||||
(or (atom? (cadr form)) (constant? (cadr form)))
|
||||
(let ((args (cadr (car form)))
|
||||
(s (cadr form))
|
||||
(body (caddr (car form))))
|
||||
(and (pair? args) (length= args 1)
|
||||
(pair? body)
|
||||
(pair? (car body))
|
||||
(eq (caar body) 'lambda)
|
||||
(let ((innerargs (cadr (car body)))
|
||||
(innerbody (caddr (car body)))
|
||||
(params (cdr body)))
|
||||
(and (not (contains (car args) params))
|
||||
`((lambda ,(cons (car args) innerargs)
|
||||
,innerbody)
|
||||
,s
|
||||
,@params)))))))
|
||||
|
||||
(#t form)))
|
||||
|
||||
(define-macro (with-delimited-continuations . code)
|
||||
(cps `((lambda () ,@code))))
|
||||
|
||||
(define-macro (define-generator form . body)
|
||||
(let ((ko (gensym))
|
||||
(cur (gensym))
|
||||
(name (car form))
|
||||
(args (cdr form)))
|
||||
`(define (,name ,@args)
|
||||
(let ((,ko #f)
|
||||
(,cur #f))
|
||||
(lambda ()
|
||||
(with-delimited-continuations
|
||||
(if ,ko (,ko ,cur)
|
||||
(reset
|
||||
(let ((yield
|
||||
(lambda (v)
|
||||
(shift yk
|
||||
(begin (set! ,ko yk)
|
||||
(set! ,cur v))))))
|
||||
,@body)))))))))
|
||||
|
||||
; a test case
|
||||
(define-generator (range-iterator lo hi)
|
||||
((label loop
|
||||
(lambda (i)
|
||||
(if (< hi i)
|
||||
'done
|
||||
(begin (yield i)
|
||||
(loop (+ 1 i))))))
|
||||
lo))
|
||||
|
||||
; example from Chung-chieh Shan's paper
|
||||
(assert (equal?
|
||||
(with-delimited-continuations
|
||||
(cons 'a (reset (cons 'b (shift f (cons 1 (f (f (cons 'c ())))))))))
|
||||
'(a 1 b b c)))
|
||||
|
||||
#t
|
||||
|
||||
#|
|
||||
todo:
|
||||
* tag lambdas that accept continuation arguments, compile computed
|
||||
calls to calls to funcall/cc that does the right thing for both
|
||||
cc-lambdas and normal lambdas
|
||||
|
||||
* handle dotted arglists in lambda
|
||||
|
||||
- optimize constant functions, e.g. (funcall/cc-0 #:g65 (lambda (#:g58) 'done))
|
||||
|
||||
- implement CPS version of apply
|
||||
|
||||
- use fewer gensyms
|
||||
|
||||
here's an alternate way to transform a while loop:
|
||||
|
||||
(let ((x 0))
|
||||
(while (< x 10)
|
||||
(begin (print x) (set! x (+ 1 x)))))
|
||||
=>
|
||||
(let ((x 0))
|
||||
(reset
|
||||
(let ((l #f))
|
||||
(let ((k (shift k (k k))))
|
||||
(if (< x 10)
|
||||
(begin (set! l (begin (print x)
|
||||
(set! x (+ 1 x))))
|
||||
(k k))
|
||||
l)))))
|
||||
|#
|
||||
@@ -0,0 +1,25 @@
|
||||
; -*- scheme -*-
|
||||
|
||||
(define (rule30-step b)
|
||||
(let ((L (ash b -1))
|
||||
(R (ash b 1)))
|
||||
(let ((~b (lognot b))
|
||||
(~L (lognot L))
|
||||
(~R (lognot R)))
|
||||
(logior (logand L ~b ~R)
|
||||
(logand ~L b R)
|
||||
(logand ~L b ~R)
|
||||
(logand ~L ~b R)))))
|
||||
|
||||
(define (bin-draw s)
|
||||
(string.map (lambda (c) (case c
|
||||
(#\1 #\#)
|
||||
(#\0 #\ )
|
||||
(else c)))
|
||||
s))
|
||||
|
||||
(for-each (lambda (n)
|
||||
(begin
|
||||
(princ (bin-draw (string.lpad (number->string n 2) 63 #\0)))
|
||||
(newline)))
|
||||
(nestlist rule30-step (uint64 0x0000000080000000) 32))
|
||||
Reference in New Issue
Block a user