parameterized-utils-2.1.11.0: Classes and data structures for working with data-kind indexed types

Copyright | (c) Galois Inc 2014-2019
Maintainer | Joe Hendrix <jhendrix@galois.com>
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Parameterized.Nonce

Contents

- NonceGenerator
- Accessing a nonce generator
- Global nonce generator

Description

This module provides a simple generator of new indexes in the `ST` monad. It is predictable and not intended for cryptographic purposes.

This module also provides a global nonce generator that will generate 2^64 nonces before repeating.

NOTE: The `TestEquality` and `OrdF` instances for the `Nonce` type simply compare the generated nonce values and then assert to the compiler (via `unsafeCoerce`) that the types ascribed to the nonces are equal if their values are equal.

Synopsis

- data NonceGenerator (m :: Type -> Type) s
- freshNonce :: forall m s k (tp :: k). NonceGenerator m s -> m (Nonce s tp)
- countNoncesGenerated :: NonceGenerator m s -> m Integer
- data Nonce s (tp :: k)
- indexValue :: Nonce s tp -> Word64
- newSTNonceGenerator :: ST t (Some (NonceGenerator (ST t)))
- newIONonceGenerator :: IO (Some (NonceGenerator IO))
- withIONonceGenerator :: (forall s. NonceGenerator IO s -> IO r) -> IO r
- withSTNonceGenerator :: (forall s. NonceGenerator (ST t) s -> ST t r) -> ST t r
- runSTNonceGenerator :: (forall s. NonceGenerator (ST s) s -> ST s a) -> a
- withGlobalSTNonceGenerator :: (forall t. NonceGenerator (ST t) t -> ST t r) -> r
- data GlobalNonceGenerator
- globalNonceGenerator :: NonceGenerator IO GlobalNonceGenerator

# NonceGenerator

data NonceGenerator (m :: Type -> Type) s Source #

Provides a monadic action for getting fresh typed names.

The first type parameter `m` is the monad used for generating names, and the second parameter `s` is used for the counter.

freshNonce :: forall m s k (tp :: k). NonceGenerator m s -> m (Nonce s tp) Source #

countNoncesGenerated :: NonceGenerator m s -> m Integer Source #

The number of nonces generated so far by this generator. Only really useful for profiling.

data Nonce s (tp :: k) Source #

An index generated by the counter.

#### Instances

Instances details

TestEquality (Nonce s :: k -> Type) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods testEquality :: forall (a :: k) (b :: k). Nonce s a -> Nonce s b -> Maybe (a :~: b) Source #
EqF (Nonce s :: k -> Type) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods eqF :: forall (a :: k). Nonce s a -> Nonce s a -> Bool Source #
HashableF (Nonce s :: k -> Type) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods hashWithSaltF :: forall (tp :: k). Int -> Nonce s tp -> Int Source # hashF :: forall (tp :: k). Nonce s tp -> Int Source #
OrdF (Nonce s :: k -> Type) Source # |

Instance details

Defined in Data.Parameterized.Nonce

Methods

compareF :: forall (x :: k) (y :: k). Nonce s x -> Nonce s y -> OrderingF x y Source #

leqF :: forall (x :: k) (y :: k). Nonce s x -> Nonce s y -> Bool Source #

ltF :: forall (x :: k) (y :: k). Nonce s x -> Nonce s y -> Bool Source #

geqF :: forall (x :: k) (y :: k). Nonce s x -> Nonce s y -> Bool Source #

gtF :: forall (x :: k) (y :: k). Nonce s x -> Nonce s y -> Bool Source #

ShowF (Nonce s :: k -> Type) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods withShow :: forall p q (tp :: k) a. p (Nonce s :: k -> Type) -> q tp -> (Show (Nonce s tp) => a) -> a Source # showF :: forall (tp :: k). Nonce s tp -> String Source # showsPrecF :: forall (tp :: k). Int -> Nonce s tp -> String -> String Source #
Show (Nonce s tp) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods showsPrec :: Int -> Nonce s tp -> ShowS Source # show :: Nonce s tp -> String Source # showList :: [Nonce s tp] -> ShowS Source #
Eq (Nonce s tp) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods (==) :: Nonce s tp -> Nonce s tp -> Bool Source # (/=) :: Nonce s tp -> Nonce s tp -> Bool Source #
Ord (Nonce s tp) Source # |

Instance details

Defined in Data.Parameterized.Nonce

Methods

compare :: Nonce s tp -> Nonce s tp -> Ordering Source #

(<) :: Nonce s tp -> Nonce s tp -> Bool Source #

(<=) :: Nonce s tp -> Nonce s tp -> Bool Source #

(>) :: Nonce s tp -> Nonce s tp -> Bool Source #

(>=) :: Nonce s tp -> Nonce s tp -> Bool Source #

max :: Nonce s tp -> Nonce s tp -> Nonce s tp Source #

min :: Nonce s tp -> Nonce s tp -> Nonce s tp Source #

Hashable (Nonce s tp) Source # |
Instance details Defined in Data.Parameterized.Nonce Methods hashWithSalt :: Int -> Nonce s tp -> Int Source # hash :: Nonce s tp -> Int Source #

indexValue :: Nonce s tp -> Word64 Source #

# Accessing a nonce generator

newSTNonceGenerator :: ST t (Some (NonceGenerator (ST t))) Source #

Create a new nonce generator in the `ST` monad.

newIONonceGenerator :: IO (Some (NonceGenerator IO)) Source #

Create a new nonce generator in the `IO` monad.

withIONonceGenerator :: (forall s. NonceGenerator IO s -> IO r) -> IO r Source #

Run an `IO` computation with a new nonce generator in the `IO` monad.

withSTNonceGenerator :: (forall s. NonceGenerator (ST t) s -> ST t r) -> ST t r Source #

Run an `ST` computation with a new nonce generator in the `ST` monad.

runSTNonceGenerator :: (forall s. NonceGenerator (ST s) s -> ST s a) -> a Source #

This combines `runST` and `newSTNonceGenerator` to create a nonce generator that shares the same phantom type parameter as the `ST` monad.

This can be used to reduce the number of type parameters when we know a ST computation only needs a single `NonceGenerator`.

# Global nonce generator

withGlobalSTNonceGenerator :: (forall t. NonceGenerator (ST t) t -> ST t r) -> r Source #

Create a new counter.

data GlobalNonceGenerator Source #

globalNonceGenerator :: NonceGenerator IO GlobalNonceGenerator Source #

A nonce generator that uses a globally-defined counter.