tur/free
stdlib/free.tur
Free monad over a functor f for building effect DSLs.
Since: Phase B1
defdata
Free
(defdata Free [^f a] (PureFree) (Suspend))
the Free monad over a functor f.
defn
free-pure
(free-pure [x : int])
lift a pure value into the Free monad.
Parameters
| x | the value to lift |
Returns
A Free value in the pure leaf case.
Example
(free-pure 42) ; => (PureFree 42)
Since: Phase RF3
defn
free-lift
(free-lift [fx : int])
lift one layer of f into Free.
Parameters
| fx | a value of type (f a), where a is the "next step" value |
Returns
A Free value that suspends on fx.
Example
(free-lift (some 5))
Since: Phase RF3
defn
free-bind
(free-bind [ma : int ^fat kont : int])
monadic bind for Free.
Parameters
| ma | a Free value | |
| kont | a continuation from a to (Free f b); carried as an :int fat-closure | |
| handle via the ^fat marker (a bare non-capturing fn is auto-shimmed) |
Returns
A new Free value representing the sequenced computation.
Example
(free-bind (free-pure 3) (fn [x] (free-pure (* x 2)))) ; => (PureFree 6)
Since: Phase RF3
defn
free-fmap
(free-fmap [free : int ^fat f : int])
functor map for Free: apply a function to all pure leaves.
Parameters
| free | a Free value | |
| f | a function from a to b; carried as an :int fat-closure handle via | |
| the ^fat marker (a bare non-capturing fn is auto-shimmed) |
Returns
A new Free value with f applied to all pure leaves.
Since: Phase RF3
defn
free-run
(free-run [^fat interp : int free : int])
interpret a Free computation with a natural transformation.
Parameters
| interp | a natural transformation from (f a) to a; carried as an :int | |
| fat-closure handle via the ^fat marker (bare fns auto-shimmed) | ||
| free | a Free computation to run |
Returns
The result of running free under interp.
Example
(free-run my-interp my-program)
Since: Phase RF3