Generalize calls to monad functions

Use call instead of : to call functions on monad modules in order to
support tuple modules. This will come in handy when implementing monad
transformers.
This commit is contained in:
Correl Roush 2014-05-11 17:25:17 -04:00
parent df4d65f0b3
commit f88d977017
2 changed files with 14 additions and 5 deletions

View file

@ -4,16 +4,25 @@
(monad:do-transform monad statements))) (monad:do-transform monad statements)))
(defmacro >>= (monad m f) (defmacro >>= (monad m f)
`(: ,monad >>= ,m ,f)) (if (: lfe-utils atom? monad)
`(call ',monad '>>= ,m ,f)
`(call ,monad '>>= ,m ,f)))
(defmacro >> (monad m1 m2) (defmacro >> (monad m1 m2)
`(: ,monad >>= ,m1 (lambda (_) ,m2))) (let ((f `(lambda (_) ,m2)))
(if (: lfe-utils atom? monad)
`(call ',monad '>>= ,m1 ,f)
`(call ,monad '>>= ,m1 ,f))))
(defmacro return (monad expr) (defmacro return (monad expr)
`(: ,monad return ,expr)) (if (: lfe-utils atom? monad)
`(call ',monad 'return ,expr)
`(call ,monad 'return ,expr)))
(defmacro fail (monad expr) (defmacro fail (monad expr)
`(: ,monad fail ,expr)) (if (: lfe-utils atom? monad)
`(call ',monad 'fail ,expr)
`(call ,monad 'fail ,expr)))
(defmacro sequence (monad list) (defmacro sequence (monad list)
`(: lists foldr `(: lists foldr

View file

@ -10,7 +10,7 @@
(defun do-transform (defun do-transform
((monad (cons h '())) h) ((monad (cons h '())) h)
((monad (cons (list f '<- m) t)) (list ': monad '>>= ((monad (cons (list f '<- m) t)) (list '>>= monad
m m
(list 'lambda (list f) (do-transform monad t)))) (list 'lambda (list f) (do-transform monad t))))
((monad (cons h t)) (list '>> monad h (do-transform monad t))) ((monad (cons h t)) (list '>> monad h (do-transform monad t)))