{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE UnboxedTuples #-}
module GHC.Types.Unique.Supply (
UniqSupply,
uniqFromSupply, uniqsFromSupply,
takeUniqFromSupply, uniqFromTag,
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.Word
import GHC.Exts( Ptr(..), noDuplicate#, oneShot )
import Foreign.Storable
#include "MachDeps.h"
#if MIN_VERSION_GLASGOW_HASKELL(9,1,0,0) && WORD_SIZE_IN_BITS == 64
import GHC.Word( Word64(..) )
import GHC.Exts( fetchAddWordAddr#, plusWord#, readWordOffAddr# )
#if MIN_VERSION_GLASGOW_HASKELL(9,3,0,0)
import GHC.Exts( wordToWord64# )
#endif
#endif
#include "Unique.h"
data UniqSupply
= MkSplitUniqSupply {-# UNPACK #-} !Word64
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
!tag :: Word64
tag = Char -> Word64
mkTag Char
c
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 Word64 -> State# RealWorld -> (# State# RealWorld, Word64 #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO IO Word64
genSym State# RealWorld
s1 of { (# State# RealWorld
s2, Word64
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, Word64 -> UniqSupply -> UniqSupply -> UniqSupply
MkSplitUniqSupply (Word64
tag Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
u) UniqSupply
x UniqSupply
y #)
}}}}
#if !MIN_VERSION_GLASGOW_HASKELL(9,1,0,0) || WORD_SIZE_IN_BITS != 64
foreign import ccall unsafe "genSym" genSym :: IO Word64
#else
genSym :: IO Word64
genSym :: IO Word64
genSym = do
let !mask :: Word64
mask = (Word64
1 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
uNIQUE_BITS) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1
let !(Ptr Addr#
counter) = Ptr Word64
ghc_unique_counter64
let !(Ptr Addr#
inc_ptr) = Ptr Int
ghc_unique_inc
Word64
u <- (State# RealWorld -> (# State# RealWorld, Word64 #)) -> IO Word64
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Word64 #)) -> IO Word64)
-> (State# RealWorld -> (# State# RealWorld, Word64 #))
-> IO Word64
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 #) ->
#if !MIN_VERSION_GLASGOW_HASKELL(9,3,0,0)
let !u = W64# (val `plusWord#` inc) .&. mask
#else
let !u :: Word64
u = Word64# -> Word64
W64# (Word# -> Word64#
wordToWord64# (Word#
val Word# -> Word# -> Word#
`plusWord#` Word#
inc)) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
mask
#endif
in (# State# RealWorld
s2, Word64
u #)
#if defined(DEBUG)
massert (u /= mask)
#endif
Word64 -> IO Word64
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Word64
u
#endif
foreign import ccall unsafe "&ghc_unique_counter64" ghc_unique_counter64 :: Ptr Word64
foreign import ccall unsafe "&ghc_unique_inc" ghc_unique_inc :: Ptr Int
initUniqSupply :: Word64 -> Int -> IO ()
initUniqSupply :: Word64 -> Int -> IO ()
initUniqSupply Word64
counter Int
inc = do
Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word64
ghc_unique_counter64 Word64
counter
Ptr Int -> Int -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Int
ghc_unique_inc Int
inc
uniqFromTag :: Char -> IO Unique
uniqFromTag :: Char -> IO Unique
uniqFromTag !Char
tag
= do { Word64
uqNum <- IO Word64
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 -> Word64 -> Unique
mkUnique Char
tag Word64
uqNum }
{-# NOINLINE uniqFromTag #-}
splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
listSplitUniqSupply :: UniqSupply -> [UniqSupply]
uniqFromSupply :: UniqSupply -> Unique
uniqsFromSupply :: UniqSupply -> [Unique]
takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
splitUniqSupply (MkSplitUniqSupply Word64
_ UniqSupply
s1 UniqSupply
s2) = (UniqSupply
s1, UniqSupply
s2)
listSplitUniqSupply :: UniqSupply -> [UniqSupply]
listSplitUniqSupply (MkSplitUniqSupply Word64
_ UniqSupply
s1 UniqSupply
s2) = UniqSupply
s1 UniqSupply -> [UniqSupply] -> [UniqSupply]
forall a. a -> [a] -> [a]
: UniqSupply -> [UniqSupply]
listSplitUniqSupply UniqSupply
s2
uniqFromSupply :: UniqSupply -> Unique
uniqFromSupply (MkSplitUniqSupply Word64
n UniqSupply
_ UniqSupply
_) = Word64 -> Unique
mkUniqueGrimily Word64
n
uniqsFromSupply :: UniqSupply -> [Unique]
uniqsFromSupply (MkSplitUniqSupply Word64
n UniqSupply
_ UniqSupply
s2) = Word64 -> Unique
mkUniqueGrimily Word64
n Unique -> [Unique] -> [Unique]
forall a. a -> [a] -> [a]
: UniqSupply -> [Unique]
uniqsFromSupply UniqSupply
s2
takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
takeUniqFromSupply (MkSplitUniqSupply Word64
n UniqSupply
s1 UniqSupply
_) = (Word64 -> Unique
mkUniqueGrimily Word64
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. HasCallStack => 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)