2014-05-19 01:38:32 -04:00
|
|
|
(define-library (picrin control)
|
|
|
|
(import (scheme base))
|
|
|
|
|
2014-05-19 01:45:38 -04:00
|
|
|
; based on paper "Representing Monads" (Filinski 1994)
|
|
|
|
|
2014-05-19 01:38:32 -04:00
|
|
|
(define m #f)
|
|
|
|
|
2014-05-19 07:35:31 -04:00
|
|
|
(define (abort t)
|
|
|
|
(let ((v (t))) ; (t) may update m. do not place me like (m (t))
|
|
|
|
(m v)))
|
|
|
|
|
2014-05-19 01:38:32 -04:00
|
|
|
(define (reset t)
|
2014-05-19 07:35:31 -04:00
|
|
|
(let ((n m))
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
2014-05-19 01:38:32 -04:00
|
|
|
(set! m (lambda (r)
|
|
|
|
(set! m n)
|
|
|
|
(k r)))
|
2014-05-19 07:35:31 -04:00
|
|
|
(abort t)))))
|
2014-05-19 01:38:32 -04:00
|
|
|
|
|
|
|
(define (shift h)
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
2014-05-19 07:35:31 -04:00
|
|
|
(abort
|
|
|
|
(lambda ()
|
|
|
|
(h (lambda (v)
|
|
|
|
(reset (lambda ()
|
|
|
|
(k v))))))))))
|
2014-05-19 01:38:32 -04:00
|
|
|
|
2015-07-18 02:39:34 -04:00
|
|
|
(define-syntax reset*
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ expr ...)
|
|
|
|
(reset (lambda () expr ...)))))
|
|
|
|
|
|
|
|
(define-syntax shift*
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ k expr ...)
|
|
|
|
(shift (lambda (k) expr ...)))))
|
|
|
|
|
|
|
|
(export (rename shift* shift)
|
|
|
|
(rename reset* reset)))
|
2014-05-19 07:35:31 -04:00
|
|
|
|