(module monad * (import scheme) (import chicken.base) (import chicken.format) (import chicken.module) (import srfi-1) (reexport monad-core) (define-monad (lambda (a) a) (lambda (a f) (f a))) (define-monad (lambda (a) `(Just ,a)) (lambda (a f) (if (not (eq? 'Nothing a)) (f (cadr a)) 'Nothing)) (case-lambda (() 'Nothing) ((_ . _) 'Nothing))) (define-monad (lambda (a) `(Right ,a)) (lambda (a f) (if (not (eq? 'Left (car a))) (f (cadr a)) a)) (case-lambda (() '(Left #f)) ((e) `(Left ,e)))) (define-monad (lambda (a) (list a)) (lambda (a f) (concatenate (map f a)))) (define-monad (lambda (a) (lambda (s) `(,a . ,s))) (lambda (a f) (lambda (s) (let* ((p (a s)) (a^ (car p)) (s^ (cdr p))) ((f a^) s^))))) (define (-get s) `(,s . ,s)) (define (-put new-state) (lambda (s) `(() . ,new-state))) (define (-gets f) (do/m (s <- (/m get)) (return (f s)))) (define (-modify f) (do/m (s <- (/m get)) (/m! put (f s)))) (define-monad (lambda (a) (lambda (v) a)) (lambda (a f) (lambda (v) ((f (a v)) v)))) (define (-ask a) a) (define (-asks f) (do/m (x <- (/m ask)) (return (f x)))) (define (-local f r) (lambda (a) (r (f a)))) (define-monad (lambda (a) (cons a '())) (lambda (a f) (let* ((b (f (car a)))) (cons (car b) (append (cdr a) (cdr b)))))) (define (-tell . v) (cons '() v)) (define (-listen a) (cons a (cdr a))) (define (-listens f m) (do/m (pair <- (/m! listen m)) (return (cons (car pair) (f (cdr pair)))))) (define (-pass m) ; expects ((a f) w) (let* ((p (car m)) (a (car p)) (f (cadr p)) (w (cdr m))) (cons a (f w)))) (define (-censor f m) (-pass (do/m (apply (/m tell) (cdr m)) (return (/m! tell f))))) )