Basement.Monad

basement-0.0.15: Foundation scrap box of array & string

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Basement.Monad

Description

Allow to run operation in ST and IO, without having to distinguinsh between the two. Most operations exposes the bare nuts and bolts of how IO and ST actually works, and relatively easy to shoot yourself in the foot

this is highly similar to the Control.Monad.Primitive in the primitive package

Synopsis

- class (Functor m, Applicative m, Monad m) => PrimMonad m where
  - type PrimState m
  - type PrimVar m :: * -> *
  - primitive :: (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
  - primThrow :: Exception e => e -> m a
  - unPrimMonad :: m a -> State# (PrimState m) -> (# State# (PrimState m), a #)
  - primVarNew :: a -> m (PrimVar m a)
  - primVarRead :: PrimVar m a -> m a
  - primVarWrite :: PrimVar m a -> a -> m ()
- class Monad m => MonadFailure m where
  - type Failure m
  - mFail :: Failure m -> m ()
- unPrimMonad_ :: PrimMonad m => m () -> State# (PrimState m) -> State# (PrimState m)
- unsafePrimCast :: (PrimMonad m1, PrimMonad m2) => m1 a -> m2 a
- unsafePrimToST :: PrimMonad prim => prim a -> ST s a
- unsafePrimToIO :: PrimMonad prim => prim a -> IO a
- unsafePrimFromIO :: PrimMonad prim => IO a -> prim a
- primTouch :: PrimMonad m => a -> m ()

# Documentation

class (Functor m, Applicative m, Monad m) => PrimMonad m where #

Primitive monad that can handle mutation.

For example: IO and ST.

Associated Types

type PrimState m #

type of state token associated with the PrimMonad m

type PrimVar m :: * -> * #

type of variable associated with the PrimMonad m

Methods

primitive :: (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a #

Unwrap the State# token to pass to a function a primitive function that returns an unboxed state and a value.

primThrow :: Exception e => e -> m a #

Throw Exception in the primitive monad

unPrimMonad :: m a -> State# (PrimState m) -> (# State# (PrimState m), a #) #

Run a Prim monad from a dedicated state#

primVarNew :: a -> m (PrimVar m a) #

Build a new variable in the Prim Monad

primVarRead :: PrimVar m a -> m a #

Read the variable in the Prim Monad

primVarWrite :: PrimVar m a -> a -> m () #

Write the variable in the Prim Monad

#### Instances

Instances details

PrimMonad IO # |
Instance details Defined in Basement.Monad Associated Types type PrimState IO # type PrimVar IO :: Type -> Type # Methods primitive :: (State# (PrimState IO) -> (# State# (PrimState IO), a #)) -> IO a # primThrow :: Exception e => e -> IO a # unPrimMonad :: IO a -> State# (PrimState IO) -> (# State# (PrimState IO), a #) # primVarNew :: a -> IO (PrimVar IO a) # primVarRead :: PrimVar IO a -> IO a # primVarWrite :: PrimVar IO a -> a -> IO () #
PrimMonad (ST s) # |
Instance details Defined in Basement.Monad Associated Types type PrimState (ST s) # type PrimVar (ST s) :: Type -> Type # Methods primitive :: (State# (PrimState (ST s)) -> (# State# (PrimState (ST s)), a #)) -> ST s a # primThrow :: Exception e => e -> ST s a # unPrimMonad :: ST s a -> State# (PrimState (ST s)) -> (# State# (PrimState (ST s)), a #) # primVarNew :: a -> ST s (PrimVar (ST s) a) # primVarRead :: PrimVar (ST s) a -> ST s a # primVarWrite :: PrimVar (ST s) a -> a -> ST s () #

class Monad m => MonadFailure m where #

Monad that can represent failure

Similar to MonadFail but with a parametrized Failure linked to the Monad

Associated Types

type Failure m #

The associated type with the MonadFailure, representing what failure can be encoded in this monad

Methods

mFail :: Failure m -> m () #

Raise a Failure through a monad.

#### Instances

Instances details

MonadFailure Maybe # |
Instance details Defined in Basement.Monad Associated Types type Failure Maybe # Methods mFail :: Failure Maybe -> Maybe () #
MonadFailure (Either a) # |
Instance details Defined in Basement.Monad Associated Types type Failure (Either a) # Methods mFail :: Failure (Either a) -> Either a () #
Monad state => MonadFailure (Builder collection mutCollection step state err) # |
Instance details Defined in Basement.MutableBuilder Associated Types type Failure (Builder collection mutCollection step state err) # Methods mFail :: Failure (Builder collection mutCollection step state err) -> Builder collection mutCollection step state err () #

unPrimMonad_ :: PrimMonad m => m () -> State# (PrimState m) -> State# (PrimState m) #

just like `unwrapPrimMonad` but throw away the result and return just the new State#

unsafePrimCast :: (PrimMonad m1, PrimMonad m2) => m1 a -> m2 a #

Convert a prim monad to another prim monad.

The net effect is that it coerce the state repr to another, so the runtime representation should be the same, otherwise hilary ensues.

unsafePrimToST :: PrimMonad prim => prim a -> ST s a #

Convert any prim monad to an ST monad

unsafePrimToIO :: PrimMonad prim => prim a -> IO a #

Convert any prim monad to an IO monad

unsafePrimFromIO :: PrimMonad prim => IO a -> prim a #

Convert any IO monad to a prim monad

primTouch :: PrimMonad m => a -> m () #

Touch primitive lifted to any prim monad