machines-0.5.1: Networked stream transducers

Copyright(C) 2012 Edward Kmett, Rúnar Bjarnason
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
PortabilityRank-N Types, MPTCs
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Machine.Plan

Contents

Description

 

Synopsis

Plans

type Plan k o a = forall m. PlanT k o m a Source

A Plan k o a is a specification for a pure Machine, that reads inputs selected by k with types based on i, writes values of type o, and has intermediate results of type a.

A Plan k o a can be used as a PlanT k o m a for any Monad m.

It is perhaps easier to think of Plan in its un-cps'ed form, which would look like:

data Plan k o a
  = Done a
  | Yield o (Plan k o a)
  | forall z. Await (z -> Plan k o a) (k z) (Plan k o a)
  | Fail

runPlan :: PlanT k o Identity a -> (a -> r) -> (o -> r -> r) -> (forall z. (z -> r) -> k z -> r -> r) -> r -> r Source

Deconstruct a Plan without reference to a Monad.

newtype PlanT k o m a Source

You can construct a Plan (or PlanT), turning it into a Machine (or MachineT).

Constructors

PlanT 

Fields

runPlanT :: forall r. (a -> m r) -> (o -> m r -> m r) -> (forall z. (z -> m r) -> k z -> m r -> m r) -> m r -> m r
 

Instances

MonadState s m => MonadState s (PlanT k o m) 
MonadReader e m => MonadReader e (PlanT k o m) 
MonadError e m => MonadError e (PlanT k o m) 
MonadWriter w m => MonadWriter w (PlanT k o m) 
MonadTrans (PlanT k o) 
Alternative (PlanT k o m) 
Monad (PlanT k o m) 
Functor (PlanT k o m) 
MonadPlus (PlanT k o m) 
Applicative (PlanT k o m) 
MonadIO m => MonadIO (PlanT k o m) 

yield :: o -> Plan k o () Source

Output a result.

maybeYield :: Maybe o -> Plan k o () Source

Like yield, except stops if there is no value to yield.

await :: Category k => Plan (k i) o i Source

Wait for input.

await = awaits id

awaits :: k i -> Plan k o i Source

Wait for a particular input.

awaits L  :: Plan (T a b) o a
awaits R  :: Plan (T a b) o b
awaits id :: Plan (Is i) o i

exhaust :: Monad m => m (Maybe a) -> PlanT k a m () Source

Run a monadic action repeatedly yielding its results, until it returns Nothing.