{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE BangPatterns #-}
#if !defined(GHC_LOADED_INTO_GHCI)
{-# LANGUAGE UnboxedTuples #-}
#endif
module UniqSupply (
UniqSupply,
uniqFromSupply, uniqsFromSupply,
takeUniqFromSupply, uniqFromMask,
mkSplitUniqSupply,
splitUniqSupply, listSplitUniqSupply,
UniqSM, MonadUnique(..),
initUs, initUs_,
initUniqSupply
) where
import GhcPrelude
import Unique
import PlainPanic (panic)
import GHC.IO
import MonadUtils
import Control.Monad
import Data.Bits
import Data.Char
import Control.Monad.Fail as Fail
#include "Unique.h"
data UniqSupply
= MkSplitUniqSupply {-# UNPACK #-} !Int
UniqSupply UniqSupply
mkSplitUniqSupply :: Char -> IO UniqSupply
splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
listSplitUniqSupply :: UniqSupply -> [UniqSupply]
uniqFromSupply :: UniqSupply -> Unique
uniqsFromSupply :: UniqSupply -> [Unique]
takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
uniqFromMask :: Char -> IO Unique
uniqFromMask :: Char -> IO Unique
uniqFromMask Char
mask
= do { Int
uqNum <- IO Int
genSym
; Unique -> IO Unique
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 }
mkSplitUniqSupply :: Char -> IO UniqSupply
mkSplitUniqSupply Char
c
= case Char -> Int
ord Char
c Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
uNIQUE_BITS of
!Int
mask -> let
mk_supply :: IO UniqSupply
mk_supply
= IO UniqSupply -> IO UniqSupply
forall a. IO a -> IO a
unsafeInterleaveIO (
IO Int
genSym IO Int -> (Int -> IO UniqSupply) -> IO UniqSupply
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ Int
u ->
IO UniqSupply
mk_supply IO UniqSupply -> (UniqSupply -> IO UniqSupply) -> IO UniqSupply
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ UniqSupply
s1 ->
IO UniqSupply
mk_supply IO UniqSupply -> (UniqSupply -> IO UniqSupply) -> IO UniqSupply
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ UniqSupply
s2 ->
UniqSupply -> IO UniqSupply
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> UniqSupply -> UniqSupply -> UniqSupply
MkSplitUniqSupply (Int
mask Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
u) UniqSupply
s1 UniqSupply
s2)
)
in
IO UniqSupply
mk_supply
foreign import ccall unsafe "ghc_lib_parser_genSym" genSym :: IO Int
foreign import ccall unsafe "ghc_lib_parser_initGenSym" initUniqSupply :: Int -> Int -> IO ()
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)
#if !defined(GHC_LOADED_INTO_GHCI)
type UniqResult result = (# result, UniqSupply #)
pattern UniqResult :: a -> b -> (# a, b #)
pattern $bUniqResult :: a -> b -> (# a, b #)
$mUniqResult :: forall r a b. (# a, b #) -> (a -> b -> r) -> (Void# -> r) -> r
UniqResult x y = (# x, y #)
{-# COMPLETE UniqResult #-}
#else
data UniqResult result = UniqResult !result {-# UNPACK #-} !UniqSupply
deriving (Functor)
#endif
newtype UniqSM result = USM { UniqSM result -> UniqSupply -> UniqResult result
unUSM :: UniqSupply -> UniqResult result }
deriving (a -> UniqSM b -> UniqSM a
(a -> b) -> UniqSM a -> UniqSM b
(forall a b. (a -> b) -> UniqSM a -> UniqSM b)
-> (forall a b. a -> UniqSM b -> UniqSM a) -> Functor UniqSM
forall a b. a -> UniqSM b -> UniqSM a
forall a b. (a -> b) -> UniqSM a -> UniqSM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> UniqSM b -> UniqSM a
$c<$ :: forall a b. a -> UniqSM b -> UniqSM a
fmap :: (a -> b) -> UniqSM a -> UniqSM b
$cfmap :: forall a b. (a -> b) -> UniqSM a -> UniqSM b
Functor)
instance Monad UniqSM where
>>= :: 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
>> :: UniqSM a -> UniqSM b -> UniqSM b
(>>) = UniqSM a -> UniqSM b -> UniqSM b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
instance Applicative UniqSM where
pure :: a -> UniqSM a
pure = a -> UniqSM a
forall a. a -> UniqSM a
returnUs
(USM UniqSupply -> UniqResult (a -> b)
f) <*> :: UniqSM (a -> b) -> UniqSM a -> UniqSM b
<*> (USM UniqSupply -> UniqResult a
x) = (UniqSupply -> UniqResult b) -> UniqSM b
forall result. (UniqSupply -> UniqResult result) -> UniqSM result
USM ((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
*> :: UniqSM a -> UniqSM b -> UniqSM b
(*>) = UniqSM a -> UniqSM b -> UniqSM b
forall a b. UniqSM a -> UniqSM b -> UniqSM b
thenUs_
instance Fail.MonadFail UniqSM where
fail :: String -> UniqSM a
fail = String -> UniqSM a
forall a. String -> a
panic
initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
initUs :: 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_ :: 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 :: 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 :: (a -> UniqSM a) -> UniqSM a
mfix a -> UniqSM a
m = (UniqSupply -> UniqResult a) -> UniqSM a
forall result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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 :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
thenUs (USM UniqSupply -> UniqResult a
expr) a -> UniqSM b
cont
= (UniqSupply -> UniqResult b) -> UniqSM b
forall result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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_ :: UniqSM a -> UniqSM b -> UniqSM b
thenUs_ (USM UniqSupply -> UniqResult a
expr) (USM UniqSupply -> UniqResult b
cont)
= (UniqSupply -> UniqResult b) -> UniqSM b
forall result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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 :: a -> UniqSM a
returnUs a
result = (UniqSupply -> UniqResult a) -> UniqSM a
forall result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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 result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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 result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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 result. (UniqSupply -> UniqResult result) -> UniqSM result
USM (\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)