Reexport RealWorld
[darcs-mirrors/primitive.git] / Control / Monad / Primitive.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, TypeFamilies #-}
2
3 -- |
4 -- Module : Control.Monad.Primitive
5 -- Copyright : (c) Roman Leshchinskiy 2009
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Portability : non-portable
10 --
11 -- Primitive state-transformer monads
12 --
13
14 module Control.Monad.Primitive ( PrimMonad(..), RealWorld, primitive_ ) where
15
16 import GHC.Prim ( State#, RealWorld )
17 import GHC.IOBase ( IO(..) )
18 import GHC.ST ( ST(..) )
19
20 -- | Class of primitive state-transformer monads
21 class Monad m => PrimMonad m where
22 -- | State token type
23 type PrimState m
24
25 -- | Execute a primitive operation
26 primitive :: (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
27
28 -- | Execute a primitive operation with no result
29 primitive_ :: PrimMonad m
30 => (State# (PrimState m) -> State# (PrimState m)) -> m ()
31 {-# INLINE primitive_ #-}
32 primitive_ f = primitive (\s# -> (# f s#, () #))
33
34 instance PrimMonad IO where
35 type PrimState IO = RealWorld
36 primitive = IO
37
38 instance PrimMonad (ST s) where
39 type PrimState (ST s) = s
40 primitive = ST
41