garden
Safe HaskellSafe
LanguageHaskell2010

Demos

Synopsis

Effects demo.

data Load (e :: Type) (u :: Type) Source #

Operation to load an implicit, dynamic state value.

Constructors

forall s.(e ~ s, u ~ ()) => Load 

Instances

Instances details
Handles (StateHandler s a) Load Source #

StateHandler fulfills the Load operation.

Instance details

Defined in Demos

Associated Types

type Ex (StateHandler s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateHandler s a) Load => Load e u -> (Return (Load e u) -> StateHandler s a -> Result (StateHandler s a)) -> StateHandler s a -> Result (StateHandler s a) Source #

Handles (StateExceptHandler err s a) Load Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Load => Load e u -> (Return (Load e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

Handles (FStateHandler h s a) Load Source # 
Instance details

Defined in Demos

Associated Types

type Ex (FStateHandler h s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (FStateHandler h s a) Load => Load e u -> (Return (Load e u) -> FStateHandler h s a -> Result (FStateHandler h s a)) -> FStateHandler h s a -> Result (FStateHandler h s a) Source #

type Ex (StateHandler s a) Load Source # 
Instance details

Defined in Demos

type Ex (StateHandler s a) Load = s
type Ex (StateExceptHandler err s a) Load Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Load = s
type Ex (FStateHandler h s a) Load Source # 
Instance details

Defined in Demos

type Ex (FStateHandler h s a) Load = s
type Return (Load s ()) Source # 
Instance details

Defined in Demos

type Return (Load s ()) = s

data Save (e :: Type) (u :: Type) Source #

Operation to save an implicit, dynamic state value.

Constructors

forall s.(e ~ s, u ~ ()) => Save s 

Instances

Instances details
Handles (StateHandler s a) Save Source #

StateHandler also fulfills the Save operation.

Instance details

Defined in Demos

Associated Types

type Ex (StateHandler s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateHandler s a) Save => Save e u -> (Return (Save e u) -> StateHandler s a -> Result (StateHandler s a)) -> StateHandler s a -> Result (StateHandler s a) Source #

Handles (StateExceptHandler err s a) Save Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Save => Save e u -> (Return (Save e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

Handles (FStateHandler h s a) Save Source # 
Instance details

Defined in Demos

Associated Types

type Ex (FStateHandler h s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (FStateHandler h s a) Save => Save e u -> (Return (Save e u) -> FStateHandler h s a -> Result (FStateHandler h s a)) -> FStateHandler h s a -> Result (FStateHandler h s a) Source #

type Ex (StateHandler s a) Save Source # 
Instance details

Defined in Demos

type Ex (StateHandler s a) Save = s
type Ex (StateExceptHandler err s a) Save Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Save = s
type Ex (FStateHandler h s a) Save Source # 
Instance details

Defined in Demos

type Ex (FStateHandler h s a) Save = s
type Return (Save s ()) Source # 
Instance details

Defined in Demos

type Return (Save s ()) = ()

save :: h `Handles` Save => Ex h Save -> h ? () Source #

type Stateful s a = forall h. (h `Handles` Load, h `Handles` Save, s ~ Ex h Load, s ~ Ex h Save) => h ? a Source #

Computations which may load or save an implicit, dynamic state value.

newtype StateHandler (s :: Type) (a :: Type) Source #

An endofunctor corresponding to Stateful effect handlers.

Constructors

StateHandler s 

Instances

Instances details
Handles (StateHandler s a) Save Source #

StateHandler also fulfills the Save operation.

Instance details

Defined in Demos

Associated Types

type Ex (StateHandler s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateHandler s a) Save => Save e u -> (Return (Save e u) -> StateHandler s a -> Result (StateHandler s a)) -> StateHandler s a -> Result (StateHandler s a) Source #

Handles (StateHandler s a) Load Source #

StateHandler fulfills the Load operation.

Instance details

Defined in Demos

Associated Types

type Ex (StateHandler s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateHandler s a) Load => Load e u -> (Return (Load e u) -> StateHandler s a -> Result (StateHandler s a)) -> StateHandler s a -> Result (StateHandler s a) Source #

type Ex (StateHandler s a) Save Source # 
Instance details

Defined in Demos

type Ex (StateHandler s a) Save = s
type Ex (StateHandler s a) Load Source # 
Instance details

Defined in Demos

type Ex (StateHandler s a) Load = s
type Result (StateHandler s a) Source # 
Instance details

Defined in Demos

type Result (StateHandler s a) = (s, a)

state_handler :: s -> (StateHandler s a ? a) -> (s, a) Source #

Handles Stateful computations. Usage:

>>> let (nine, three) = state_handler 3 (load >>= \n -> save (n * n) >> return n)
>>> nine
9
>>> three
3

newtype FStateHandler (h :: Type) (s :: Type) (a :: Type) Source #

A "forwarding" state-handler.

Constructors

FStateHandler s 

Instances

Instances details
Handles (FStateHandler h s a) Save Source # 
Instance details

Defined in Demos

Associated Types

type Ex (FStateHandler h s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (FStateHandler h s a) Save => Save e u -> (Return (Save e u) -> FStateHandler h s a -> Result (FStateHandler h s a)) -> FStateHandler h s a -> Result (FStateHandler h s a) Source #

Handles (FStateHandler h s a) Load Source # 
Instance details

Defined in Demos

Associated Types

type Ex (FStateHandler h s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (FStateHandler h s a) Load => Load e u -> (Return (Load e u) -> FStateHandler h s a -> Result (FStateHandler h s a)) -> FStateHandler h s a -> Result (FStateHandler h s a) Source #

type Ex (FStateHandler h s a) Save Source # 
Instance details

Defined in Demos

type Ex (FStateHandler h s a) Save = s
type Ex (FStateHandler h s a) Load Source # 
Instance details

Defined in Demos

type Ex (FStateHandler h s a) Load = s
type Result (FStateHandler h s a) Source # 
Instance details

Defined in Demos

type Result (FStateHandler h s a) = a

f_state_handler :: Monad m => a -> CPS (m (a, b)) ((->) (FStateHandler h a (m (a, b)))) b -> m (a, b) Source #

A state-handler which forwards unsupported operations to another handler. Usage:

>>> (nine, unit) <- f_state_handler 3 (load >>= \n -> save (n * n))
>>> nine
9
>>> unit
()

data Throw (e :: Type) (u :: Type) Source #

Operation to throw a specified value in Exceptional circumstances.

Constructors

forall err a.(e ~ err, u ~ a) => Throw err 

Instances

Instances details
Handles (ExceptHandler err a) Throw Source # 
Instance details

Defined in Demos

Associated Types

type Ex (ExceptHandler err a) Throw :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (ExceptHandler err a) Throw => Throw e u -> (Return (Throw e u) -> ExceptHandler err a -> Result (ExceptHandler err a)) -> ExceptHandler err a -> Result (ExceptHandler err a) Source #

Handles (StateExceptHandler err s a) Throw Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Throw :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Throw => Throw e u -> (Return (Throw e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

type Ex (ExceptHandler err a) Throw Source # 
Instance details

Defined in Demos

type Ex (ExceptHandler err a) Throw = err
type Ex (StateExceptHandler err s a) Throw Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Throw = err
type Return (Throw err a) Source # 
Instance details

Defined in Demos

type Return (Throw err a) = a

throw :: h `Handles` Throw => Ex h Throw -> h ? a Source #

type Except err a = forall h. (h `Handles` Throw, Ex h Throw ~ err) => h ? a Source #

A computation which evaluates to a Except for errors.

newtype ExceptHandler (err :: Type) (a :: Type) Source #

This handler Results in Either an err value or answer a.

Constructors

ExceptHandler () 

Instances

Instances details
Handles (ExceptHandler err a) Throw Source # 
Instance details

Defined in Demos

Associated Types

type Ex (ExceptHandler err a) Throw :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (ExceptHandler err a) Throw => Throw e u -> (Return (Throw e u) -> ExceptHandler err a -> Result (ExceptHandler err a)) -> ExceptHandler err a -> Result (ExceptHandler err a) Source #

type Ex (ExceptHandler err a) Throw Source # 
Instance details

Defined in Demos

type Ex (ExceptHandler err a) Throw = err
type Result (ExceptHandler err a) Source # 
Instance details

Defined in Demos

type Result (ExceptHandler err a) = Either err a

except_handler :: (ExceptHandler err a ? a) -> Either err a Source #

Evaluates an exception-handling computation.

catch :: Monad m => (ExceptHandler err a ? a) -> (err -> m a) -> m a Source #

A more conventional throw-receiving idiom.

data Await (e :: Type) (u :: Type) Source #

Asynchronous streaming and coroutines.

Constructors

forall s.(e ~ s, u ~ ()) => Await 

Instances

Instances details
type Return (Await s ()) Source # 
Instance details

Defined in Demos

type Return (Await s ()) = s

data Yield (e :: Type) (u :: Type) Source #

Constructors

forall s.(e ~ s, u ~ ()) => Yield s 

Instances

Instances details
type Return (Yield s ()) Source # 
Instance details

Defined in Demos

type Return (Yield s ()) = ()

yield :: h `Handles` Yield => Ex h Yield -> h ? () Source #

type Tube i o a = forall h. (h `Handles` Await, h `Handles` Yield, i ~ Ex h Await, o ~ Ex h Yield) => h ? a Source #

newtype Prod s r Source #

Constructors

Prod (Cons s r -> r) 

newtype Cons s r Source #

Constructors

Cons (s -> Prod s r -> r) 

type StatefulExcept err s a = forall h. (h `Handles` Load, Ex h Load ~ s, h `Handles` Save, Ex h Save ~ s, h `Handles` Throw, Ex h Throw ~ err) => h ? a Source #

With a little elbow grease this could be derived automatically.

ex1 :: StatefulExcept String Int () Source #

Example computation combining Stateful and Except effects, which were defined in a separate module. The parlor trick here is that this type-checks without defining any handlers, combining 3 operations from 2 distinct effect types with existing, independent handlers.

newtype StateExceptHandler (err :: Type) (s :: Type) (a :: Type) Source #

Interprets StatefulExcept err s a as the type s -> (s, Either err a).

Constructors

SEH s 

Instances

Instances details
Handles (StateExceptHandler err s a) Throw Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Throw :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Throw => Throw e u -> (Return (Throw e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

Handles (StateExceptHandler err s a) Save Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Save :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Save => Save e u -> (Return (Save e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

Handles (StateExceptHandler err s a) Load Source # 
Instance details

Defined in Demos

Associated Types

type Ex (StateExceptHandler err s a) Load :: j Source #

Methods

clause :: forall (e :: j) (u :: k). e ~ Ex (StateExceptHandler err s a) Load => Load e u -> (Return (Load e u) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a)) -> StateExceptHandler err s a -> Result (StateExceptHandler err s a) Source #

type Ex (StateExceptHandler err s a) Throw Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Throw = err
type Ex (StateExceptHandler err s a) Save Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Save = s
type Ex (StateExceptHandler err s a) Load Source # 
Instance details

Defined in Demos

type Ex (StateExceptHandler err s a) Load = s
type Result (StateExceptHandler err s a) Source # 
Instance details

Defined in Demos

type Result (StateExceptHandler err s a) = (s, Either err a)

state_except_handler :: s -> (StateExceptHandler err s a ? a) -> (s, Either err a) Source #

A handler for both Stateful and Exceptional computations. If the typeclass instances above are the case-statements for different effects, this is more or less the case-statement for "return" / termination.

effects_demo :: IO () Source #

>>> effects_demo
(0,Right ())
(0,Left "error")

Sheets demo

data Cell Source #

A Cell is either "on" (I) or "off" (O).

Constructors

I 
O 

Instances

Instances details
Eq Cell Source # 
Instance details

Defined in Demos

Methods

(==) :: Cell -> Cell -> Bool Source #

(/=) :: Cell -> Cell -> Bool Source #

Show Cell Source # 
Instance details

Defined in Demos

rule110 :: Sheet2 Cell -> Cell Source #

The 2-dimensional cellular automaton "rule 110." To evaluate one time-step, each cell along with its immediate left and right neighbors is used to determine that cell's next value (proceed below). I believe it is the simplest Turing-complete CA.

bg_pattern :: [Cell] Source #

A background pattern to evaluate in our CA

tape_from_pattern :: [Cell] -> Tape Cell Source #

Lifts a list of Cells into a Tape.

ether :: Sheet2 (Sheet2 Cell -> Cell) Source #

A 2-dimensional sheet (Sheet2). Each Cell of the Sheet2 is a function which transforms a Sheet2 of Cells into a new value for that Cell.

son_a :: [Cell] Source #

Some Cell patterns which happen to be interesting in Rule 110.

son_b :: [Cell] Source #

Some Cell patterns which happen to be interesting in Rule 110.

son_c :: [Cell] Source #

Some Cell patterns which happen to be interesting in Rule 110.

automate :: [Cell] -> Sheet2 (Sheet2 Cell -> Cell) Source #

This lifts a pattern of Cell values into a 2-dimensional spreadsheet.

print_automaton :: Int -> Int -> Sheet2 (Sheet2 Cell -> Cell) -> IO () Source #

Prints a given slice of the output stream, which represents the evolution of the 2-D spreadsheet over time.

sheets_demo :: IO () Source #

Constructs an interesting-ish Rule 110 scenario and prints a slice of the output stream, showing the time-evolution of a 2-D spreadsheet. This aims son_a and son_b at each other. They collide, entangle, and then disentangle in swapped places.