{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE Unsafe #-}
module Data.Parameterized.Nonce.Unsafe
{-# DEPRECATED "Migrate to use Data.Parameterized.Nonce instead, this module will be removed soon." #-}
( NonceGenerator
, newNonceGenerator
, freshNonce
, atLimit
, Nonce
, indexValue
) where
import Control.Monad.ST
import Data.Hashable
import Data.STRef
import Data.Word
import Unsafe.Coerce
import Data.Parameterized.Axiom
import Data.Parameterized.Classes
newtype NonceGenerator s = NonceGenerator (STRef s Word64)
newNonceGenerator :: ST s (NonceGenerator s)
newNonceGenerator :: forall s. ST s (NonceGenerator s)
newNonceGenerator = forall s. STRef s Word64 -> NonceGenerator s
NonceGenerator forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a s. a -> ST s (STRef s a)
newSTRef (forall a. Enum a => Int -> a
toEnum Int
0)
newtype Nonce (tp :: k) = Nonce { forall k (tp :: k). Nonce tp -> Word64
indexValue :: Word64 }
deriving (Nonce tp -> Nonce tp -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
/= :: Nonce tp -> Nonce tp -> Bool
$c/= :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
== :: Nonce tp -> Nonce tp -> Bool
$c== :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
Eq, Nonce tp -> Nonce tp -> Bool
Nonce tp -> Nonce tp -> Ordering
Nonce tp -> Nonce tp -> Nonce tp
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall k (tp :: k). Eq (Nonce tp)
forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
forall k (tp :: k). Nonce tp -> Nonce tp -> Ordering
forall k (tp :: k). Nonce tp -> Nonce tp -> Nonce tp
min :: Nonce tp -> Nonce tp -> Nonce tp
$cmin :: forall k (tp :: k). Nonce tp -> Nonce tp -> Nonce tp
max :: Nonce tp -> Nonce tp -> Nonce tp
$cmax :: forall k (tp :: k). Nonce tp -> Nonce tp -> Nonce tp
>= :: Nonce tp -> Nonce tp -> Bool
$c>= :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
> :: Nonce tp -> Nonce tp -> Bool
$c> :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
<= :: Nonce tp -> Nonce tp -> Bool
$c<= :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
< :: Nonce tp -> Nonce tp -> Bool
$c< :: forall k (tp :: k). Nonce tp -> Nonce tp -> Bool
compare :: Nonce tp -> Nonce tp -> Ordering
$ccompare :: forall k (tp :: k). Nonce tp -> Nonce tp -> Ordering
Ord, Int -> Nonce tp -> Int
Nonce tp -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall k (tp :: k). Eq (Nonce tp)
forall k (tp :: k). Int -> Nonce tp -> Int
forall k (tp :: k). Nonce tp -> Int
hash :: Nonce tp -> Int
$chash :: forall k (tp :: k). Nonce tp -> Int
hashWithSalt :: Int -> Nonce tp -> Int
$chashWithSalt :: forall k (tp :: k). Int -> Nonce tp -> Int
Hashable, Int -> Nonce tp -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (tp :: k). Int -> Nonce tp -> ShowS
forall k (tp :: k). [Nonce tp] -> ShowS
forall k (tp :: k). Nonce tp -> String
showList :: [Nonce tp] -> ShowS
$cshowList :: forall k (tp :: k). [Nonce tp] -> ShowS
show :: Nonce tp -> String
$cshow :: forall k (tp :: k). Nonce tp -> String
showsPrec :: Int -> Nonce tp -> ShowS
$cshowsPrec :: forall k (tp :: k). Int -> Nonce tp -> ShowS
Show)
type role Nonce nominal
instance TestEquality Nonce where
testEquality :: forall (a :: k) (b :: k). Nonce a -> Nonce b -> Maybe (a :~: b)
testEquality Nonce a
x Nonce b
y | forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce a
x forall a. Eq a => a -> a -> Bool
== forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce b
y = forall a. a -> Maybe a
Just forall {k} (a :: k) (b :: k). a :~: b
unsafeAxiom
| Bool
otherwise = forall a. Maybe a
Nothing
instance OrdF Nonce where
compareF :: forall (x :: k) (y :: k). Nonce x -> Nonce y -> OrderingF x y
compareF Nonce x
x Nonce y
y =
case forall a. Ord a => a -> a -> Ordering
compare (forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce x
x) (forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce y
y) of
Ordering
LT -> forall {k} (x :: k) (y :: k). OrderingF x y
LTF
Ordering
EQ -> forall a b. a -> b
unsafeCoerce forall {k} (x :: k). OrderingF x x
EQF
Ordering
GT -> forall {k} (x :: k) (y :: k). OrderingF x y
GTF
instance HashableF Nonce where
hashWithSaltF :: forall (tp :: k). Int -> Nonce tp -> Int
hashWithSaltF Int
s (Nonce Word64
x) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
s Word64
x
instance ShowF Nonce
{-# INLINE freshNonce #-}
freshNonce :: NonceGenerator s -> ST s (Nonce tp)
freshNonce :: forall {k} s (tp :: k). NonceGenerator s -> ST s (Nonce tp)
freshNonce (NonceGenerator STRef s Word64
r) = do
Word64
i <- forall s a. STRef s a -> ST s a
readSTRef STRef s Word64
r
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Word64
r forall a b. (a -> b) -> a -> b
$! forall a. Enum a => a -> a
succ Word64
i
forall (m :: * -> *) a. Monad m => a -> m a
return (forall k (tp :: k). Word64 -> Nonce tp
Nonce Word64
i)
atLimit :: NonceGenerator s -> ST s Bool
atLimit :: forall s. NonceGenerator s -> ST s Bool
atLimit (NonceGenerator STRef s Word64
r) = do
Word64
i <- forall s a. STRef s a -> ST s a
readSTRef STRef s Word64
r
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64
i forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
maxBound)