picrin/contrib/partcont/piclib/partcont.scm

33 lines
655 B
Scheme
Raw Normal View History

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
(export shift
2014-05-19 07:35:31 -04:00
reset))