Safe HaskellSafe-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.

Constructors

Calculate Expr (Integer -> next) 
Store Integer (() -> next) 
Restore (Integer -> next) 

Instances

Instances details
Functor MyDomainAlgebra Source #

Our algebra must be a Functor to build a Free monad. Demonstration of functorial mapping (not tied to our type):

>>> fmap (+5) (Just 5)
Just 10
Instance details

Defined in Lessons.Lesson12

Methods

fmap :: (a -> b) -> MyDomainAlgebra a -> MyDomainAlgebra b

(<$) :: a -> MyDomainAlgebra b -> MyDomainAlgebra a

calculte :: Expr -> MyDomain Integer Source #

Lift a Calculate instruction into the Free program.

store :: Integer -> MyDomain () Source #

Lift a Store instruction.

restore :: MyDomain Integer Source #

Lift a Restore instruction.

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)

runState :: State s a -> s -> (a, s) #