basement-0.0.4: Foundation scrap box of array & string

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | experimental
Portability | portable
Safe Haskell | None
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 :: * -> *
- class Monad m => MonadFailure m where
  - type Failure 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 Source #

Primitive monad that can handle mutation.

For example: IO and ST.

Minimal complete definition

primitive, primThrow, unPrimMonad, primVarNew, primVarRead, primVarWrite

Associated Types

type PrimState m Source #

type of state token associated with the PrimMonad m

type PrimVar m :: * -> * Source #

type of variable associated with the PrimMonad m

Methods

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

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 Source #

Throw Exception in the primitive monad

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

Run a Prim monad from a dedicated state#

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

Build a new variable in the Prim Monad

primVarRead :: PrimVar m a -> m a Source #

Read the variable in the Prim Monad

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

Write the variable in the Prim Monad

Instances

PrimMonad IO Source # |

Associated Types

type PrimState (IO :: * -> *) :: * Source #

type PrimVar (IO :: * -> *) :: * -> * Source #

Methods

primitive :: (State# (PrimState IO) -> (#VoidRep, PtrRepLifted, State# (PrimState IO), a#)) -> IO a Source #

primThrow :: Exception e => e -> IO a Source #

unPrimMonad :: IO a -> State# (PrimState IO) -> (#VoidRep, PtrRepLifted, State# (PrimState IO), a#) Source #

primVarNew :: a -> IO (PrimVar IO a) Source #

primVarRead :: PrimVar IO a -> IO a Source #

primVarWrite :: PrimVar IO a -> a -> IO () Source #

PrimMonad (ST s) Source # |

Associated Types

type PrimState (ST s :: * -> *) :: * Source #

type PrimVar (ST s :: * -> *) :: * -> * Source #

Methods

primitive :: (State# (PrimState (ST s)) -> (#VoidRep, PtrRepLifted, State# (PrimState (ST s)), a#)) -> ST s a Source #

primThrow :: Exception e => e -> ST s a Source #

unPrimMonad :: ST s a -> State# (PrimState (ST s)) -> (#VoidRep, PtrRepLifted, State# (PrimState (ST s)), a#) Source #

primVarNew :: a -> ST s (PrimVar (ST s) a) Source #

primVarRead :: PrimVar (ST s) a -> ST s a Source #

primVarWrite :: PrimVar (ST s) a -> a -> ST s () Source #

class Monad m => MonadFailure m where Source #

Monad that can represent failure

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

Minimal complete definition

mFail

Associated Types

type Failure m Source #

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

Methods

mFail :: Failure m -> m () Source #

Raise a Failure through a monad.

Instances

MonadFailure Maybe Source # |
Associated Types type Failure (Maybe :: * -> *) :: * Source # Methods mFail :: Failure Maybe -> Maybe () Source #
MonadFailure (Either a) Source # |
Associated Types type Failure (Either a :: * -> *) :: * Source # Methods mFail :: Failure (Either a) -> Either a () Source #
Monad state => MonadFailure (Builder collection mutCollection step state err) Source # |
Associated Types type Failure (Builder collection mutCollection step state err :: * -> *) :: * Source # Methods mFail :: Failure (Builder collection mutCollection step state err) -> Builder collection mutCollection step state err () Source #

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

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

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

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 Source #

Convert any prim monad to an ST monad

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

Convert any prim monad to an IO monad

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

Convert any IO monad to a prim monad

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

Touch primitive lifted to any prim monad