tur/free
Free monad over a functor f for building effect DSLs.
Free f a = PureFree a | Suspend (f (Free f a)). Run a Free program by providing an interpreter for the functor f.
Since: Phase B1
Free
(defdata Free [^f a] (PureFree :int) (Suspend :int))
the Free monad over a functor f.
Free f a = PureFree a | Suspend (f (Free f a))
Any Functor ^f yields a Monad over Free ^f for free.
free-pure
(free-pure [x :int] :int)
lift a pure value into the Free monad.
| x | the value to lift |
A Free value in the pure leaf case.
(free-pure 42) ; => (PureFree 42)
Since: Phase RF3
free-lift
(free-lift [fx :int] :int)
lift one layer of f into Free.
| fx | a value of type (f a), where a is the "next step" value |
A Free value that suspends on fx.
(free-lift (some 5))
Since: Phase RF3
free-bind
(free-bind [ma :int ^fat kont :(fn [int] int)])
monadic bind for Free.
Sequentially composes two Free computations.
(PureFree a) >>= f = f a (Suspend m) >>= f = Suspend m (single-layer pass-through)
| ma | a Free value | |
| kont | a continuation from a to (Free f b), typed (fn [int] int) over | |
| the erased Free carrier; a fat closure via the ^fat marker (a bare | ||
| non-capturing fn is auto-shimmed) |
A new Free value representing the sequenced computation.
(free-bind (free-pure 3) (fn [x] (free-pure (* x 2)))) ; => (PureFree 6)
Updated: TS5 -- replaced fat-pointer extraction with TS1 typed thunk call.
Since: Phase RF3
free-fmap
(free-fmap [free :int ^fat f :(fn [int] int)])
functor map for Free: apply a function to all pure leaves.
| free | a Free value | |
| f | a function from a to b, typed (fn [int] int) over the erased | |
| carrier; a fat closure via the ^fat marker (a bare non-capturing | ||
| fn is auto-shimmed) |
A new Free value with f applied to all pure leaves.
Updated: TS5 -- replaced fat-pointer extraction with TS1 typed thunk call.
Since: Phase RF3
free-run
(free-run [^borrow ^fat interp :(fn [int] int) free :int])
interpret a Free computation with a natural transformation.
| interp | a natural transformation from (f a) to a, typed | |
(fn [int] int) over the erased carrier; a fat closure via the | ||
| ^fat marker (bare fns auto-shimmed) | ||
| free | a Free computation to run |
The result of running free under interp.
(free-run my-interp my-program)
Updated: TS5 -- replaced fat-pointer extraction with TS1 typed thunk call.
Since: Phase RF3