| Safe Haskell | Safe-Inferred |
|---|
Lessons.Lesson12
Description
Notes taken by Deimantė Davidavičiūtė
Synopsis
Documentation
type MyDomain a = Free MyDomainAlgebra a Source #
The Free monad over MyDomainAlgebra.
data MyDomainAlgebra next Source #
The algebra of our domain:
'Calculate Expr (Integer -> next)': evaluate an arithmetic expression, then pass the resulting Integer to the continuation.
'Store Integer (() -> next)': store a number and continue.
'Restore (Integer -> next)': fetch the last stored number and continue.
Instances
| Functor MyDomainAlgebra Source # | Our algebra must be a Functor to build a Free monad. Demonstration of functorial mapping (not tied to our type):
|
Defined in Lessons.Lesson12 Methods fmap :: (a -> b) -> MyDomainAlgebra a -> MyDomainAlgebra b (<$) :: a -> MyDomainAlgebra b -> MyDomainAlgebra a | |
myProgram :: MyDomain Integer Source #
A sample program composed of our domain actions.
Calculates an expression and binds it to r
Restores previously stored value v0.
Stores r and then stores 42.
Restores v1 and returns the sum.
runInIO :: MyDomain a -> IO a Source #
An interpreter that runs the program in IO. Effects are implemented as printing/prompts and pure evaluation.
runInState :: MyDomain a -> State Integer a Source #
Interpreter that runs the program in pure 'State Integer'. The state holds the latest stored number.
>>>runState (runInState myProgram) 0(27,42)