{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE UnboxedTuples #-}
module GHC.Types.Unique.Supply (
UniqSupply,
uniqFromSupply, uniqsFromSupply,
takeUniqFromSupply, uniqFromMask,
mkSplitUniqSupply,
splitUniqSupply, listSplitUniqSupply,
UniqSM, MonadUnique(..),
initUs, initUs_,
initUniqSupply
) where
import GHC.Prelude
import GHC.Types.Unique
import GHC.Utils.Panic.Plain
import GHC.IO
import GHC.Utils.Monad
import Control.Monad
import Data.Char
import GHC.Exts( Ptr(..), noDuplicate#, oneShot )
#if MIN_VERSION_GLASGOW_HASKELL(9,1,0,0)
import GHC.Exts( Int(..), word2Int#, fetchAddWordAddr#, plusWord#, readWordOffAddr# )
#endif
import Foreign.Storable
#include "Unique.h"
data UniqSupply
= MkSplitUniqSupply {-# UNPACK #-} !Int
UniqSupply UniqSupply
mkSplitUniqSupply :: Char -> IO UniqSupply
mkSplitUniqSupply :: Char -> IO UniqSupply
mkSplitUniqSupply Char
c
= IO UniqSupply -> IO UniqSupply
forall a. IO a -> IO a
unsafeDupableInterleaveIO ((State# RealWorld -> (# State# RealWorld, UniqSupply #))
-> IO UniqSupply
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO State# RealWorld -> (# State# RealWorld, UniqSupply #)
mk_supply)
where
!mask :: Int
mask = Char -> Int
ord Char
c Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
uNIQUE_BITS
mk_supply :: State# RealWorld -> (# State# RealWorld, UniqSupply #)
mk_supply State# RealWorld
s0 =
case State# RealWorld -> State# RealWorld
forall d. State# d -> State# d
noDuplicate# State# RealWorld
s0 of { State# RealWorld
s1 ->
case IO Int -> State# RealWorld -> (# State# RealWorld, Int #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO IO Int
genSym State# RealWorld
s1 of { (# State# RealWorld
s2, Int
u #) ->
case IO UniqSupply
-> State# RealWorld -> (# State# RealWorld, UniqSupply #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (IO UniqSupply -> IO UniqSupply
forall a. IO a -> IO a
unsafeDupableInterleaveIO ((State# RealWorld -> (# State# RealWorld, UniqSupply #))
-> IO UniqSupply
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO State# RealWorld -> (# State# RealWorld, UniqSupply #)
mk_supply)) State# RealWorld
s2 of { (# State# RealWorld
s3, UniqSupply
x #) ->
case IO UniqSupply
-> State# RealWorld -> (# State# RealWorld, UniqSupply #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (IO UniqSupply -> IO UniqSupply
forall a. IO a -> IO a
unsafeDupableInterleaveIO ((State# RealWorld -> (# State# RealWorld, UniqSupply #))
-> IO UniqSupply
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO State# RealWorld -> (# State# RealWorld, UniqSupply #)
mk_supply)) State# RealWorld
s3 of { (# State# RealWorld
s4, UniqSupply
y #) ->
(# State# RealWorld
s4, Int -> UniqSupply -> UniqSupply -> UniqSupply
MkSplitUniqSupply (Int
mask Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
u) UniqSupply
x UniqSupply
y #)
}}}}
#if !MIN_VERSION_GLASGOW_HASKELL(9,1,0,0)
foreign import ccall unsafe "genSym" genSym :: IO Int
#else
genSym :: IO Int
genSym :: IO Int
genSym = do
let !mask :: Int
mask = (Int
1 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
uNIQUE_BITS) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
let !(Ptr Addr#
counter) = Ptr Word
ghc_unique_counter
let !(Ptr Addr#
inc_ptr) = Ptr Int
ghc_unique_inc
Int
u <- (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int)
-> (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s0 -> case Addr# -> Int# -> State# RealWorld -> (# State# RealWorld, Word# #)
forall d. Addr# -> Int# -> State# d -> (# State# d, Word# #)
readWordOffAddr# Addr#
inc_ptr Int#
0# State# RealWorld
s0 of
(# State# RealWorld
s1, Word#
inc #) -> case Addr# -> Word# -> State# RealWorld -> (# State# RealWorld, Word# #)
forall d. Addr# -> Word# -> State# d -> (# State# d, Word# #)
fetchAddWordAddr# Addr#
counter Word#
inc State# RealWorld
s1 of
(# State# RealWorld
s2, Word#
val #) ->
let !u :: Int
u = Int# -> Int
I# (Word# -> Int#
word2Int# (Word#
val Word# -> Word# -> Word#
`plusWord#` Word#
inc)) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
mask
in (# State# RealWorld
s2, Int
u #)
#if defined(DEBUG)
massert (u /= mask)
#endif
Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
u
#endif
foreign import ccall unsafe "&ghc_unique_counter" ghc_unique_counter :: Ptr Word
foreign import ccall unsafe "&ghc_unique_inc" ghc_unique_inc :: Ptr Int
initUniqSupply :: Word -> Int -> IO ()
initUniqSupply :: Word -> Int -> IO ()
initUniqSupply Word
counter Int
inc = do
Ptr Word -> Word -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word
ghc_unique_counter Word
counter
Ptr Int -> Int -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Int
ghc_unique_inc Int
inc
uniqFromMask :: Char -> IO Unique
uniqFromMask :: Char -> IO Unique
uniqFromMask !Char
mask
= do { Int
uqNum <- IO Int
genSym
; Unique -> IO Unique
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Unique -> IO Unique) -> Unique -> IO Unique
forall a b. (a -> b) -> a -> b
$! Char -> Int -> Unique
mkUnique Char
mask Int
uqNum }
{-# NOINLINE uniqFromMask #-}
splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
listSplitUniqSupply :: UniqSupply -> [UniqSupply]
uniqFromSupply :: UniqSupply -> Unique
uniqsFromSupply :: UniqSupply -> [Unique]
takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
splitUniqSupply (MkSplitUniqSupply Int
_ UniqSupply
s1 UniqSupply
s2) = (UniqSupply
s1, UniqSupply
s2)
listSplitUniqSupply :: UniqSupply -> [UniqSupply]
listSplitUniqSupply (MkSplitUniqSupply Int
_ UniqSupply
s1 UniqSupply
s2) = UniqSupply
s1 UniqSupply -> [UniqSupply] -> [UniqSupply]
forall a. a -> [a] -> [a]
: UniqSupply -> [UniqSupply]
listSplitUniqSupply UniqSupply
s2
uniqFromSupply :: UniqSupply -> Unique
uniqFromSupply (MkSplitUniqSupply Int
n UniqSupply
_ UniqSupply
_) = Int -> Unique
mkUniqueGrimily Int
n
uniqsFromSupply :: UniqSupply -> [Unique]
uniqsFromSupply (MkSplitUniqSupply Int
n UniqSupply
_ UniqSupply
s2) = Int -> Unique
mkUniqueGrimily Int
n Unique -> [Unique] -> [Unique]
forall a. a -> [a] -> [a]
: UniqSupply -> [Unique]
uniqsFromSupply UniqSupply
s2
takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
takeUniqFromSupply (MkSplitUniqSupply Int
n UniqSupply
s1 UniqSupply
_) = (Int -> Unique
mkUniqueGrimily Int
n, UniqSupply
s1)
type UniqResult result = (# result, UniqSupply #)
pattern UniqResult :: a -> b -> (# a, b #)
pattern $mUniqResult :: forall {r} {a} {b}.
(# a, b #) -> (a -> b -> r) -> ((# #) -> r) -> r
$bUniqResult :: forall a b. a -> b -> (# a, b #)
UniqResult x y = (# x, y #)
{-# COMPLETE UniqResult #-}
newtype UniqSM result = USM { forall result. UniqSM result -> UniqSupply -> UniqResult result
unUSM :: UniqSupply -> UniqResult result }
instance Functor UniqSM where
fmap :: forall a b. (a -> b) -> UniqSM a -> UniqSM b
fmap a -> b
f (USM UniqSupply -> UniqResult a
m) = (UniqSupply -> UniqResult b) -> UniqSM b
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM ((UniqSupply -> UniqResult b) -> UniqSM b)
-> (UniqSupply -> UniqResult b) -> UniqSM b
forall a b. (a -> b) -> a -> b
$ \UniqSupply
us ->
case UniqSupply -> UniqResult a
m UniqSupply
us of
(# a
r, UniqSupply
us' #) -> b -> UniqSupply -> UniqResult b
forall a b. a -> b -> (# a, b #)
UniqResult (a -> b
f a
r) UniqSupply
us'
mkUniqSM :: (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM :: forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM UniqSupply -> UniqResult a
f = (UniqSupply -> UniqResult a) -> UniqSM a
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
USM ((UniqSupply -> UniqResult a) -> UniqSupply -> UniqResult a
forall a b. (a -> b) -> a -> b
oneShot UniqSupply -> UniqResult a
f)
{-# INLINE mkUniqSM #-}
instance Monad UniqSM where
>>= :: forall a b. UniqSM a -> (a -> UniqSM b) -> UniqSM b
(>>=) = UniqSM a -> (a -> UniqSM b) -> UniqSM b
forall a b. UniqSM a -> (a -> UniqSM b) -> UniqSM b
thenUs
>> :: forall a b. UniqSM a -> UniqSM b -> UniqSM b
(>>) = UniqSM a -> UniqSM b -> UniqSM b
forall a b. UniqSM a -> UniqSM b -> UniqSM b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
instance Applicative UniqSM where
pure :: forall a. a -> UniqSM a
pure = a -> UniqSM a
forall a. a -> UniqSM a
returnUs
(USM UniqSupply -> UniqResult (a -> b)
f) <*> :: forall a b. UniqSM (a -> b) -> UniqSM a -> UniqSM b
<*> (USM UniqSupply -> UniqResult a
x) = (UniqSupply -> UniqResult b) -> UniqSM b
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM ((UniqSupply -> UniqResult b) -> UniqSM b)
-> (UniqSupply -> UniqResult b) -> UniqSM b
forall a b. (a -> b) -> a -> b
$ \UniqSupply
us0 -> case UniqSupply -> UniqResult (a -> b)
f UniqSupply
us0 of
UniqResult a -> b
ff UniqSupply
us1 -> case UniqSupply -> UniqResult a
x UniqSupply
us1 of
UniqResult a
xx UniqSupply
us2 -> b -> UniqSupply -> UniqResult b
forall a b. a -> b -> (# a, b #)
UniqResult (a -> b
ff a
xx) UniqSupply
us2
*> :: forall a b. UniqSM a -> UniqSM b -> UniqSM b
(*>) = UniqSM a -> UniqSM b -> UniqSM b
forall a b. UniqSM a -> UniqSM b -> UniqSM b
thenUs_
instance MonadFail UniqSM where
fail :: forall a. String -> UniqSM a
fail = String -> UniqSM a
forall a. String -> a
panic
initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
initUs :: forall a. UniqSupply -> UniqSM a -> (a, UniqSupply)
initUs UniqSupply
init_us UniqSM a
m = case UniqSM a -> UniqSupply -> UniqResult a
forall result. UniqSM result -> UniqSupply -> UniqResult result
unUSM UniqSM a
m UniqSupply
init_us of { UniqResult a
r UniqSupply
us -> (a
r, UniqSupply
us) }
initUs_ :: UniqSupply -> UniqSM a -> a
initUs_ :: forall a. UniqSupply -> UniqSM a -> a
initUs_ UniqSupply
init_us UniqSM a
m = case UniqSM a -> UniqSupply -> UniqResult a
forall result. UniqSM result -> UniqSupply -> UniqResult result
unUSM UniqSM a
m UniqSupply
init_us of { UniqResult a
r UniqSupply
_ -> a
r }
{-# INLINE thenUs #-}
{-# INLINE returnUs #-}
{-# INLINE splitUniqSupply #-}
liftUSM :: UniqSM a -> UniqSupply -> (a, UniqSupply)
liftUSM :: forall a. UniqSM a -> UniqSupply -> (a, UniqSupply)
liftUSM (USM UniqSupply -> UniqResult a
m) UniqSupply
us0 = case UniqSupply -> UniqResult a
m UniqSupply
us0 of UniqResult a
a UniqSupply
us1 -> (a
a, UniqSupply
us1)
instance MonadFix UniqSM where
mfix :: forall a. (a -> UniqSM a) -> UniqSM a
mfix a -> UniqSM a
m = (UniqSupply -> UniqResult a) -> UniqSM a
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> let (a
r,UniqSupply
us1) = UniqSM a -> UniqSupply -> (a, UniqSupply)
forall a. UniqSM a -> UniqSupply -> (a, UniqSupply)
liftUSM (a -> UniqSM a
m a
r) UniqSupply
us0 in a -> UniqSupply -> UniqResult a
forall a b. a -> b -> (# a, b #)
UniqResult a
r UniqSupply
us1)
thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
thenUs :: forall a b. UniqSM a -> (a -> UniqSM b) -> UniqSM b
thenUs (USM UniqSupply -> UniqResult a
expr) a -> UniqSM b
cont
= (UniqSupply -> UniqResult b) -> UniqSM b
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> case (UniqSupply -> UniqResult a
expr UniqSupply
us0) of
UniqResult a
result UniqSupply
us1 -> UniqSM b -> UniqSupply -> UniqResult b
forall result. UniqSM result -> UniqSupply -> UniqResult result
unUSM (a -> UniqSM b
cont a
result) UniqSupply
us1)
thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
thenUs_ :: forall a b. UniqSM a -> UniqSM b -> UniqSM b
thenUs_ (USM UniqSupply -> UniqResult a
expr) (USM UniqSupply -> UniqResult b
cont)
= (UniqSupply -> UniqResult b) -> UniqSM b
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> case (UniqSupply -> UniqResult a
expr UniqSupply
us0) of { UniqResult a
_ UniqSupply
us1 -> UniqSupply -> UniqResult b
cont UniqSupply
us1 })
returnUs :: a -> UniqSM a
returnUs :: forall a. a -> UniqSM a
returnUs a
result = (UniqSupply -> UniqResult a) -> UniqSM a
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us -> a -> UniqSupply -> UniqResult a
forall a b. a -> b -> (# a, b #)
UniqResult a
result UniqSupply
us)
getUs :: UniqSM UniqSupply
getUs :: UniqSM UniqSupply
getUs = (UniqSupply -> UniqResult UniqSupply) -> UniqSM UniqSupply
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> case UniqSupply -> (UniqSupply, UniqSupply)
splitUniqSupply UniqSupply
us0 of (UniqSupply
us1,UniqSupply
us2) -> UniqSupply -> UniqSupply -> UniqResult UniqSupply
forall a b. a -> b -> (# a, b #)
UniqResult UniqSupply
us1 UniqSupply
us2)
class Monad m => MonadUnique m where
getUniqueSupplyM :: m UniqSupply
getUniqueM :: m Unique
getUniquesM :: m [Unique]
getUniqueM = (UniqSupply -> Unique) -> m UniqSupply -> m Unique
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM UniqSupply -> Unique
uniqFromSupply m UniqSupply
forall (m :: * -> *). MonadUnique m => m UniqSupply
getUniqueSupplyM
getUniquesM = (UniqSupply -> [Unique]) -> m UniqSupply -> m [Unique]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM UniqSupply -> [Unique]
uniqsFromSupply m UniqSupply
forall (m :: * -> *). MonadUnique m => m UniqSupply
getUniqueSupplyM
instance MonadUnique UniqSM where
getUniqueSupplyM :: UniqSM UniqSupply
getUniqueSupplyM = UniqSM UniqSupply
getUs
getUniqueM :: UniqSM Unique
getUniqueM = UniqSM Unique
getUniqueUs
getUniquesM :: UniqSM [Unique]
getUniquesM = UniqSM [Unique]
getUniquesUs
getUniqueUs :: UniqSM Unique
getUniqueUs :: UniqSM Unique
getUniqueUs = (UniqSupply -> UniqResult Unique) -> UniqSM Unique
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> case UniqSupply -> (Unique, UniqSupply)
takeUniqFromSupply UniqSupply
us0 of
(Unique
u,UniqSupply
us1) -> Unique -> UniqSupply -> UniqResult Unique
forall a b. a -> b -> (# a, b #)
UniqResult Unique
u UniqSupply
us1)
getUniquesUs :: UniqSM [Unique]
getUniquesUs :: UniqSM [Unique]
getUniquesUs = (UniqSupply -> UniqResult [Unique]) -> UniqSM [Unique]
forall a. (UniqSupply -> UniqResult a) -> UniqSM a
mkUniqSM (\UniqSupply
us0 -> case UniqSupply -> (UniqSupply, UniqSupply)
splitUniqSupply UniqSupply
us0 of
(UniqSupply
us1,UniqSupply
us2) -> [Unique] -> UniqSupply -> UniqResult [Unique]
forall a b. a -> b -> (# a, b #)
UniqResult (UniqSupply -> [Unique]
uniqsFromSupply UniqSupply
us1) UniqSupply
us2)