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

Copyright(c) Galois Inc 2014
MaintainerJoe Hendrix <jhendrix@galois.com>
Stabilityprovisional
Safe HaskellUnsafe
LanguageHaskell98

Data.Parameterized.Nonce.Unsafe

Description

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

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. This is only OK because of the discipline by which nonces should be used: they should only be generated from a NonceGenerator (i.e., should not be built directly), and nonces from different generators must never be compared! Arranging to compare Nonces from different origins would allow users to build unsafeCoerce via the testEquality function.

A somewhat safer API would be to brand the generated Nonces with the state type variable of the NonceGenerator whence they came, and to only provide NonceGenerators via a Rank-2 continuation-passing API, similar to runST. This would (via a meta-argument involving parametricity) help to prevent nonces of different origin from being compared. However, this would force us to push the ST type brand into a significant number of other structures and APIs.

Another alternative would be to use unsafePerformIO magic to make a global nonce generator, and make that the only way to generate nonces. It is not clear that this is actually an improvement from a type safety point of view, but an argument could be made.

For now, be careful using Nonces, and ensure that you do not mix Nonces from different NonceGenerators.

Synopsis

Documentation

data NonceGenerator s Source #

A simple type that for getting fresh indices in the ST monad. The type parameter s is used for the ST monad parameter.

newNonceGenerator :: ST s (NonceGenerator s) Source #

Create a new counter.

freshNonce :: NonceGenerator s -> ST s (Nonce tp) Source #

Get a fresh index and increment the counter.

atLimit :: NonceGenerator s -> ST s Bool Source #

Return true if counter has reached the limit, and can't be incremented without risk of error.

data Nonce (tp :: k) Source #

An index generated by the counter.

Instances

TestEquality k (Nonce k) Source # 

Methods

testEquality :: f a -> f b -> Maybe ((Nonce k :~: a) b) #

HashableF k (Nonce k) Source # 

Methods

hashWithSaltF :: Int -> f tp -> Int Source #

hashF :: f tp -> Int Source #

ShowF k (Nonce k) Source # 

Methods

withShow :: p f -> q tp -> (Show (f tp) -> a) -> a Source #

showF :: f tp -> String Source #

showsF :: f tp -> String -> String Source #

OrdF k (Nonce k) Source # 

Methods

compareF :: ktp x -> ktp y -> OrderingF (Nonce k) x y Source #

leqF :: ktp x -> ktp y -> Bool Source #

ltF :: ktp x -> ktp y -> Bool Source #

geqF :: ktp x -> ktp y -> Bool Source #

gtF :: ktp x -> ktp y -> Bool Source #

Eq (Nonce k tp) Source # 

Methods

(==) :: Nonce k tp -> Nonce k tp -> Bool #

(/=) :: Nonce k tp -> Nonce k tp -> Bool #

Ord (Nonce k tp) Source # 

Methods

compare :: Nonce k tp -> Nonce k tp -> Ordering #

(<) :: Nonce k tp -> Nonce k tp -> Bool #

(<=) :: Nonce k tp -> Nonce k tp -> Bool #

(>) :: Nonce k tp -> Nonce k tp -> Bool #

(>=) :: Nonce k tp -> Nonce k tp -> Bool #

max :: Nonce k tp -> Nonce k tp -> Nonce k tp #

min :: Nonce k tp -> Nonce k tp -> Nonce k tp #

Show (Nonce k tp) Source # 

Methods

showsPrec :: Int -> Nonce k tp -> ShowS #

show :: Nonce k tp -> String #

showList :: [Nonce k tp] -> ShowS #

Hashable (Nonce k tp) Source # 

Methods

hashWithSalt :: Int -> Nonce k tp -> Int #

hash :: Nonce k tp -> Int #