{-# 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 :: ST s (NonceGenerator s)
newNonceGenerator = STRef s Word64 -> NonceGenerator s
forall s. STRef s Word64 -> NonceGenerator s
NonceGenerator (STRef s Word64 -> NonceGenerator s)
-> ST s (STRef s Word64) -> ST s (NonceGenerator s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Word64 -> ST s (STRef s Word64)
forall a s. a -> ST s (STRef s a)
newSTRef (Int -> Word64
forall a. Enum a => Int -> a
toEnum Int
0)
newtype Nonce (tp :: k) = Nonce { Nonce tp -> Word64
indexValue :: Word64 }
deriving (Nonce tp -> Nonce tp -> Bool
(Nonce tp -> Nonce tp -> Bool)
-> (Nonce tp -> Nonce tp -> Bool) -> Eq (Nonce tp)
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, Eq (Nonce tp)
Eq (Nonce tp)
-> (Nonce tp -> Nonce tp -> Ordering)
-> (Nonce tp -> Nonce tp -> Bool)
-> (Nonce tp -> Nonce tp -> Bool)
-> (Nonce tp -> Nonce tp -> Bool)
-> (Nonce tp -> Nonce tp -> Bool)
-> (Nonce tp -> Nonce tp -> Nonce tp)
-> (Nonce tp -> Nonce tp -> Nonce tp)
-> Ord (Nonce tp)
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
$cp1Ord :: forall k (tp :: k). Eq (Nonce tp)
Ord, Int -> Nonce tp -> Int
Nonce tp -> Int
(Int -> Nonce tp -> Int)
-> (Nonce tp -> Int) -> Hashable (Nonce tp)
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
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
[Nonce tp] -> ShowS
Nonce tp -> String
(Int -> Nonce tp -> ShowS)
-> (Nonce tp -> String) -> ([Nonce tp] -> ShowS) -> Show (Nonce tp)
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 :: Nonce a -> Nonce b -> Maybe (a :~: b)
testEquality Nonce a
x Nonce b
y | Nonce a -> Word64
forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce a
x Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Nonce b -> Word64
forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce b
y = (a :~: b) -> Maybe (a :~: b)
forall a. a -> Maybe a
Just a :~: b
forall k (a :: k) (b :: k). a :~: b
unsafeAxiom
| Bool
otherwise = Maybe (a :~: b)
forall a. Maybe a
Nothing
instance OrdF Nonce where
compareF :: Nonce x -> Nonce y -> OrderingF x y
compareF Nonce x
x Nonce y
y =
case Word64 -> Word64 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Nonce x -> Word64
forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce x
x) (Nonce y -> Word64
forall k (tp :: k). Nonce tp -> Word64
indexValue Nonce y
y) of
Ordering
LT -> OrderingF x y
forall k (x :: k) (y :: k). OrderingF x y
LTF
Ordering
EQ -> OrderingF Any Any -> OrderingF x y
forall a b. a -> b
unsafeCoerce OrderingF Any Any
forall k (x :: k). OrderingF x x
EQF
Ordering
GT -> OrderingF x y
forall k (x :: k) (y :: k). OrderingF x y
GTF
instance HashableF Nonce where
hashWithSaltF :: Int -> Nonce tp -> Int
hashWithSaltF Int
s (Nonce Word64
x) = Int -> Word64 -> Int
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 :: NonceGenerator s -> ST s (Nonce tp)
freshNonce (NonceGenerator STRef s Word64
r) = do
Word64
i <- STRef s Word64 -> ST s Word64
forall s a. STRef s a -> ST s a
readSTRef STRef s Word64
r
STRef s Word64 -> Word64 -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Word64
r (Word64 -> ST s ()) -> Word64 -> ST s ()
forall a b. (a -> b) -> a -> b
$! Word64 -> Word64
forall a. Enum a => a -> a
succ Word64
i
Nonce tp -> ST s (Nonce tp)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> Nonce tp
forall k (tp :: k). Word64 -> Nonce tp
Nonce Word64
i)
atLimit :: NonceGenerator s -> ST s Bool
atLimit :: NonceGenerator s -> ST s Bool
atLimit (NonceGenerator STRef s Word64
r) = do
Word64
i <- STRef s Word64 -> ST s Word64
forall s a. STRef s a -> ST s a
readSTRef STRef s Word64
r
Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64
i Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
forall a. Bounded a => a
maxBound)