Macros¶
hymn.macros  monad operations implemented as macros
hymn.macros
provide macros for monad computations
Operation Macros¶

domonad [bindingforms expr]
Changed in version 0.9: This macro was named
domonadm
before version 0.9macro for sequencing monadic computations, a.k.a do notation in haskell
=> (import [hymn.types.maybe [Just]])
=> (require [hymn.macros [domonad]])
=> (domonad [a (Just 41)] (mreturn (inc a)))
Just(42)

domonadreturn [bindingforms expr]
Changed in version 0.9: This macro was named
domonad
before version 0.9macro for sequencing monadic computations, with automatic return
=> (import [hymn.types.maybe [Just]])
=> (require [hymn.macros [domonadreturn]])
=> (domonadreturn [a (Just 41)] (inc a))
Just(42)

domonadwith [monad bindingforms expr]
macro for sequencing monadic composition, with said monad as default.
useful when the only binding form is
:when
, we do not know which monad we are working with otherwise
=> (import [hymn.types.maybe [maybem]])
=> (require [hymn.macros [domonadwith]])
=> (domonadwith maybem [:when True] 42)
Just(42)
=> (domonadwith maybem [:when False] 42)
Nothing
All do monad macros support :let
binding, like this:
=> (import [hymn.types.maybe [Just]])
=> (require [hymn.macros [domonadreturn]])
=> (defn half [x]
... (domonadreturn
... [:let [two 2]
... a x
... :let [b (/ a two)]]
... b))
=> (half (Just 42))
Just(21.0)
All do monad macros support :when
if the monad is of type
MonadPlus
.
=> (import [hymn.types.maybe [maybem]])
=> (require [hymn.macros [domonadwith]])
=> (defn div [a b] (domonadwith maybem [:when (not (zero? b))] (/ a b)))
=> (div 1 2)
Just(0.5)
=> (div 1 0)
Nothing

monad> [initvalue &rest actions]
threading macro for monadic actions
=> (import [hymn.types.maybe [maybem]])
=> (setv minc (maybem.monadic inc))
=> (setv mdiv (maybem.monadic /))
=> (require [hymn.macros [monad>]])
=> ;; threading macro for monadic actions
=> (monad> (maybem.unit 99) minc (mdiv 5) (mdiv 2))
Just(10.0)
=> ;; is equivalent to
=> (require [hymn.macros [domonad]])
=> (domonad [a (maybem.unit 99) b (minc a) c (mdiv b 5)] (mdiv c 2))
Just(10.0)

monad>> [initvalue &rest actions]
threading tail macro for monadic actions
=> (import [hymn.types.maybe [maybem]])
=> (setv minc (maybem.monadic inc))
=> (setv mdiv (maybem.monadic /))
=> (require [hymn.macros [monad>>]])
=> ;; threading tail macro for monadic actions
=> (monad>> (maybem.unit 4) minc (mdiv 25) (mdiv 100))
Just(20.0)
=> ;; is equivalent to
=> (require [hymn.macros [domonad]])
=> (domonad [a (maybem.unit 4) b (minc a) c (mdiv 25 b)] (mdiv 100 c))
Just(20.0)

mfor [[n seq] &rest expr]
macro for sequencing monadic actions
=> (import [hymn.types.maybe [maybem]])
=> (require [hymn.macros [mfor]])
=> ;; with simple monad, e.g. maybe
=> (mfor [a (range 3)] (maybem.unit a))
Just([0, 1, 2])
=> ;; with reader monad
=> (import [hymn.types.reader [<]])
=> (setv readers
... (mfor [a (range 5)]
... (print "create reader" a)
... (< a)))
create reader 0
create reader 1
create reader 2
create reader 3
create reader 4
=> (.run readers [11 12 13 14 15 16])
[11, 12, 13, 14, 15]
=> (.run readers "abcdefg")
['a', 'b', 'c', 'd', 'e']
=> ;; with writer monad
=> (import [hymn.types.writer [tell]])
=> (.execute (mfor [a (range 1 101)] (tell a)))
5050

mwhen [test mexpr]
conditional execution of monadic expressions

withmonad [monad &rest exprs]
provide default function mreturn as the unit of the monad
=> (import [hymn.types.maybe [maybem]])
=> (require [hymn.macros [mwhen withmonad]])
=> (withmonad maybem (mwhen (even? 1) (mreturn 42)))
Just(None)
=> (withmonad maybem (mwhen (even? 2) (mreturn 42)))
Just(42)

monadcomp [expr bindingforms &optional condition]
different syntax for
domonadreturn
, in the style of list/dict/set comprehensions, thecondition
part is optional and can only be used withMonadPlus
as indomonadreturn
=> (import [hymn.types.maybe [Just]])
=> (require [hymn.macros [monadcomp]])
=> (monadcomp (+ a b) [a (Just 1) b (Just 2)])
Just(3)
=> (monadcomp (/ a b) [a (Just 1) b (Just 0)] (not (zero? b)))
Nothing
=> (import [hymn.types.list [listm]])
=> (list (monadcomp (/ a b) [a (listm [1 2]) b (listm [4 8])]))
[0.25, 0.125, 0.5, 0.25]
=> (list (monadcomp (/ a b) [a (listm [1 2]) b (listm [0 1])] (not (zero? b))))
[1.0, 2.0]
Tag Macros¶

^ [f]
lift()
tag macro,#^ f
is expanded to(lift f)
=> (import [hymn.types.maybe [Just Nothing]])
=> (require [hymn.macros [^]])
=> (#^ + (Just 1) (Just 2))
Just(3)
=> (#^ + (Just 1) Nothing)
Nothing

= [value]
tag macro for
mreturn
, theunit
inside domonadreturn macros,#= v
is expanded to(mreturn v)
=> (import [hymn.types.maybe [Just maybem]])
=> (require [hymn.macros [= domonad domonadwith]])
=> (domonadwith maybem [a #= 1 b #= 2] (+ a b))
Just(3)
=> (domonad [a (Just 1)] #= (inc a))
Just(2)