Safe Haskell | None |
---|---|
Language | Haskell2010 |
Making interpreters:
singletonInterpreter
, from a handler
Growing interpreters:
Shrinking an interpreter:
downcastInterpreter
, when it handles more than you need
If you want to do more with them,
just unwrap the newtype
and use Data.Vinyl.
- newtype Interpreter m effects = Interpreter {
- getInterpreter :: Rec (HandlerM m) effects
- newtype HandlerM m f = HandlerM {
- getHandlerM :: forall x. f (m x) -> m x
- data ProductF fs a = ProductF {
- getProductF :: Rec (Apply a) fs
- interpretLanguage :: Monad m => Interpreter m effects -> Language effects :~> m
- interpretLanguageF :: Interpreter m effects -> AnAlgebra (LanguageF effects) (m a)
- singletonInterpreter :: (forall x. AnAlgebra f (m x)) -> Interpreter m `[f]`
- appendInterpreters :: Interpreter m fs -> Interpreter m gs -> Interpreter m (fs ++ gs)
- downcastInterpreter :: gs ⊆ fs => Interpreter m fs -> Interpreter m gs
- asInterpreter1 :: (Rec (HandlerM m) fs -> Rec (HandlerM m) gs) -> Interpreter m fs -> Interpreter m gs
- asInterpreter2 :: (Rec (HandlerM m) fs -> Rec (HandlerM m) gs -> Rec (HandlerM m) hs) -> Interpreter m fs -> Interpreter m gs -> Interpreter m hs
- fromSingletonInterpreter :: Interpreter m `[f]` -> AnAlgebra f (m a)
Documentation
newtype Interpreter m effects Source
a product of handlers.
each field holds a co-algebra:
f (m a) -> m a
e.g.
runFG :: Interpreter IO '[f,g] runFG = Interpreter $HandlerM
runF :&HandlerM
runG :& RNil runF :: f (m a) -> m a runG :: g (m a) -> m a
specialization:
Interpreter IO [f,g]
~
Rec (HandlerM
IO) [f,g]
~
(HandlerM IO f, HandlerM IO g)
~
( (forall x. f (IO x) -> IO x)
, (forall y. g (IO y) -> IO y)
)
Interpreter | |
|
monadically handle a functor.
HandlerM | |
|
A (1) lifted, (2) n-ary, (3) associative product.
In particular, a product
of "handlers".
fs
must all be Functor
s.
e.g.
ProductF [f,g] a
generalizes '(,)':
ProductF '[f,g] a ~ (f a, g a)
ProductF | |
|
interpretLanguage :: Monad m => Interpreter m effects -> Language effects :~> m Source
interpret a language into some monad.
e.g.
interpretLanguage anInterpreter aLanguage :: m ()
calls iterLM
.
interpretLanguageF :: Interpreter m effects -> AnAlgebra (LanguageF effects) (m a) Source
you consume a coproduct with a product of consumers i.e. you must handle every case.
like match
.
singletonInterpreter :: (forall x. AnAlgebra f (m x)) -> Interpreter m `[f]` Source
make a interpreter from a single handler.
e.g.
data ClipboardF k = GetClipboard (String -> k) | SetClipboard String k deriving Functor clipboardInterpreter :: Interpreter IO '[ClipboardF] a clipboardInterpreter = singletonInterpreter $ \case GetClipboard f -> ... >>= f SetClipboard s k -> ... s >> k
appendInterpreters :: Interpreter m fs -> Interpreter m gs -> Interpreter m (fs ++ gs) Source
compose two interpreters. ordered.
downcastInterpreter :: gs ⊆ fs => Interpreter m fs -> Interpreter m gs Source
Discard any number of handlers from the interpreter.
asInterpreter1 :: (Rec (HandlerM m) fs -> Rec (HandlerM m) gs) -> Interpreter m fs -> Interpreter m gs Source
Lift a (unary) function on records to interpreters (a newtype thereof).
e.g.
downcastInterpreter
= asInterpreter1rcast
asInterpreter2 :: (Rec (HandlerM m) fs -> Rec (HandlerM m) gs -> Rec (HandlerM m) hs) -> Interpreter m fs -> Interpreter m gs -> Interpreter m hs Source
Lift an operation on records to interpreters (a newtype thereof).
e.g.
appendInterpreters
= asInterpreter2 (<+>
)
fromSingletonInterpreter :: Interpreter m `[f]` -> AnAlgebra f (m a) Source
inverts singletonInterpreter
, for convenient access.