-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Implementation of difficult monads made easy
with operational semantics.
--
-- This library makes it easy to implement monads with tricky control
-- flow.
--
-- This is useful for: writing web applications in a sequential style,
-- programming games with a uniform interface for human and AI players
-- and easy replay capababilities, implementing fast parser monads,
-- designing monadic DSLs, etc.
--
-- See the project homepage
-- http://haskell.org/haskellwiki/Operational for a more detailed
-- introduction and features.
--
-- Related packages: MonadPrompt
-- http://hackage.haskell.org/package/MonadPrompt.
@package operational
@version 0.2.2.1
module Control.Monad.Operational
-- | The abstract data type Program instr a represents
-- programs, i.e. sequences of primitive instructions.
--
--
-- - The primitive instructions are given by the type
-- constructor instr :: * -> *.
-- - a is the return type of a program.
--
--
-- Program instr is always a monad and automatically
-- obeys the monad laws.
type Program instr = ProgramT instr Identity
-- | Program made from a single primitive instruction.
singleton :: instr a -> ProgramT instr m a
-- | View type for inspecting the first instruction. It has two
-- constructors Return and :>>=. (For technical
-- reasons, they are documented at ProgramViewT.)
type ProgramView instr = ProgramViewT instr Identity
-- | View function for inspecting the first instruction.
view :: Program instr a -> ProgramView instr a
-- | Utility function that extends a given interpretation of instructions
-- as monadic actions to an interpration of Programs as monadic
-- actions.
--
-- This function can be useful if you are mainly interested in mapping a
-- Program to different standard monads, like the state monad. For
-- implementing a truly custom monad, you should write your interpreter
-- directly with view instead.
interpretWithMonad :: Monad m => (forall a. instr a -> m a) -> (Program instr b -> m b)
-- | The abstract data type ProgramT instr m a represents
-- programs over a base monad m, i.e. sequences of primitive
-- instructions and actions from the base monad.
--
--
-- - The primitive instructions are given by the type
-- constructor instr :: * -> *.
-- - m is the base monad, embedded with lift.
-- - a is the return type of a program.
--
--
-- ProgramT instr m is a monad transformer and
-- automatically obeys both the monad and the lifting laws.
data ProgramT instr m a
-- | View type for inspecting the first instruction. This is very similar
-- to pattern matching on lists.
--
--
-- - The case (Return a) means that the program contains no
-- instructions and just returns the result a.
-- - The case (someInstruction :>>= k) means that the
-- first instruction is someInstruction and the remaining
-- program is given by the function k.
--
data ProgramViewT instr m a
Return :: a -> ProgramViewT instr m a
(:>>=) :: instr b -> (b -> ProgramT instr m a) -> ProgramViewT instr m a
-- | View function for inspecting the first instruction.
viewT :: Monad m => ProgramT instr m a -> m (ProgramViewT instr m a)
-- | Lift a plain sequence of instructions to a sequence of instructions
-- over a monad m. This is the counterpart of the lift
-- function from MonadTrans.
--
-- It can be defined as follows:
--
--
-- liftProgram = eval . view
-- where
-- eval :: ProgramView instr a -> ProgramT instr m a
-- eval (Return a) = return a
-- eval (i :>>= k) = singleton i >>= liftProgram . k
--
liftProgram :: Monad m => Program instr a -> ProgramT instr m a
instance MonadIO m => MonadIO (ProgramT instr m)
instance MonadState s m => MonadState s (ProgramT instr m)
instance Monad m => Applicative (ProgramT instr m)
instance Monad m => Functor (ProgramT instr m)
instance MonadTrans (ProgramT instr)
instance Monad m => Monad (ProgramT instr m)