Control.Monad.Primitive

primitive-0.6.1.0: Primitive memory-related operations

Copyright | (c) Roman Leshchinskiy 2009
License | BSD-style
Maintainer | Roman Leshchinskiy <rl@cse.unsw.edu.au>
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Control.Monad.Primitive

Description

Primitive state-transformer monads

Synopsis

- class Monad m => PrimMonad m where
  - type PrimState m
  - primitive :: (State# (PrimState m) -> (#State# (PrimState m), a#)) -> m a
- data RealWorld :: *
- primitive_ :: PrimMonad m => (State# (PrimState m) -> State# (PrimState m)) -> m ()
- class PrimMonad m => PrimBase m where
  - internal :: m a -> State# (PrimState m) -> (#State# (PrimState m), a#)
- liftPrim :: (PrimBase m1, PrimMonad m2, PrimState m1 ~ PrimState m2) => m1 a -> m2 a
- primToPrim :: (PrimBase m1, PrimMonad m2, PrimState m1 ~ PrimState m2) => m1 a -> m2 a
- primToIO :: (PrimBase m, PrimState m ~ RealWorld) => m a -> IO a
- primToST :: PrimBase m => m a -> ST (PrimState m) a
- unsafePrimToPrim :: (PrimBase m1, PrimMonad m2) => m1 a -> m2 a
- unsafePrimToIO :: PrimBase m => m a -> IO a
- unsafePrimToST :: PrimBase m => m a -> ST s a
- unsafeInlinePrim :: PrimBase m => m a -> a
- unsafeInlineIO :: IO a -> a
- unsafeInlineST :: ST s a -> a
- touch :: PrimMonad m => a -> m ()

# Documentation

class Monad m => PrimMonad m where

Class of monads which can perform primitive state-transformer actions

Associated Types

type PrimState m

State token type

Methods

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

Execute a primitive operation

Instances

PrimMonad IO |
PrimMonad (ST s) |
PrimMonad m => PrimMonad (MaybeT m) |
PrimMonad m => PrimMonad (ListT m) |
PrimMonad m => PrimMonad (IdentityT m) |
(Monoid w, PrimMonad m) => PrimMonad (WriterT w m) |
(Monoid w, PrimMonad m) => PrimMonad (WriterT w m) |
(Error e, PrimMonad m) => PrimMonad (ErrorT e m) |
PrimMonad m => PrimMonad (ExceptT e m) |
PrimMonad m => PrimMonad (StateT s m) |
PrimMonad m => PrimMonad (StateT s m) |
PrimMonad m => PrimMonad (ReaderT r m) |
(Monoid w, PrimMonad m) => PrimMonad (RWST r w s m) |
(Monoid w, PrimMonad m) => PrimMonad (RWST r w s m) |

data RealWorld :: *

`RealWorld` is deeply magical. It is primitive, but it is not unlifted (hence `ptrArg`). We never manipulate values of type `RealWorld`; it's only used in the type system, to parameterise `State#`.

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

Execute a primitive operation with no result

class PrimMonad m => PrimBase m where

Class of primitive monads for state-transformer actions.

Unlike `PrimMonad`, this typeclass requires that the `Monad` be fully expressed as a state transformer, therefore disallowing other monad transformers on top of the base `IO` or `ST`.

Methods

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

Expose the internal structure of the monad

Instances

PrimBase IO |
PrimBase (ST s) |

liftPrim :: (PrimBase m1, PrimMonad m2, PrimState m1 ~ PrimState m2) => m1 a -> m2 a

Lifts a `PrimBase` into another `PrimMonad` with the same underlying state token type.

primToPrim :: (PrimBase m1, PrimMonad m2, PrimState m1 ~ PrimState m2) => m1 a -> m2 a

Convert a `PrimBase` to another monad with the same state token.

primToIO :: (PrimBase m, PrimState m ~ RealWorld) => m a -> IO a

Convert a `PrimBase` with a `RealWorld` state token to `IO`

primToST :: PrimBase m => m a -> ST (PrimState m) a

Convert a `PrimBase` to `ST`

unsafePrimToPrim :: (PrimBase m1, PrimMonad m2) => m1 a -> m2 a

Convert a `PrimBase` to another monad with a possibly different state token. This operation is highly unsafe!

unsafePrimToIO :: PrimBase m => m a -> IO a

Convert any `PrimBase` to `IO`. This operation is highly unsafe!

unsafePrimToST :: PrimBase m => m a -> ST s a

Convert any `PrimBase` to `ST` with an arbitrary state token. This operation is highly unsafe!

unsafeInlinePrim :: PrimBase m => m a -> a

unsafeInlineIO :: IO a -> a

unsafeInlineST :: ST s a -> a

touch :: PrimMonad m => a -> m ()