{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Parameterized.Nonce.Transformers
( MonadNonce(..)
, NonceT(..)
, NonceST
, NonceIO
, getNonceSTGen
, runNonceST
, runNonceIO
, module Data.Parameterized.Nonce
) where
import Control.Monad.Reader
import Control.Monad.ST
import Control.Monad.State
import Data.Kind
import Data.Parameterized.Nonce
class Monad m => MonadNonce m where
type NonceSet m :: Type
freshNonceM :: forall k (tp :: k) . m (Nonce (NonceSet m) tp)
newtype NonceT s m a =
NonceT { forall s (m :: * -> *) a.
NonceT s m a -> ReaderT (NonceGenerator m s) m a
runNonceT :: ReaderT (NonceGenerator m s) m a }
deriving (forall a b. a -> NonceT s m b -> NonceT s m a
forall a b. (a -> b) -> NonceT s m a -> NonceT s m b
forall s (m :: * -> *) a b.
Functor m =>
a -> NonceT s m b -> NonceT s m a
forall s (m :: * -> *) a b.
Functor m =>
(a -> b) -> NonceT s m a -> NonceT s m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> NonceT s m b -> NonceT s m a
$c<$ :: forall s (m :: * -> *) a b.
Functor m =>
a -> NonceT s m b -> NonceT s m a
fmap :: forall a b. (a -> b) -> NonceT s m a -> NonceT s m b
$cfmap :: forall s (m :: * -> *) a b.
Functor m =>
(a -> b) -> NonceT s m a -> NonceT s m b
Functor, forall a. a -> NonceT s m a
forall a b. NonceT s m a -> NonceT s m b -> NonceT s m a
forall a b. NonceT s m a -> NonceT s m b -> NonceT s m b
forall a b. NonceT s m (a -> b) -> NonceT s m a -> NonceT s m b
forall a b c.
(a -> b -> c) -> NonceT s m a -> NonceT s m b -> NonceT s m c
forall {s} {m :: * -> *}. Applicative m => Functor (NonceT s m)
forall s (m :: * -> *) a. Applicative m => a -> NonceT s m a
forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m a -> NonceT s m b -> NonceT s m a
forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m a -> NonceT s m b -> NonceT s m b
forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m (a -> b) -> NonceT s m a -> NonceT s m b
forall s (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> NonceT s m a -> NonceT s m b -> NonceT s m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. NonceT s m a -> NonceT s m b -> NonceT s m a
$c<* :: forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m a -> NonceT s m b -> NonceT s m a
*> :: forall a b. NonceT s m a -> NonceT s m b -> NonceT s m b
$c*> :: forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m a -> NonceT s m b -> NonceT s m b
liftA2 :: forall a b c.
(a -> b -> c) -> NonceT s m a -> NonceT s m b -> NonceT s m c
$cliftA2 :: forall s (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> NonceT s m a -> NonceT s m b -> NonceT s m c
<*> :: forall a b. NonceT s m (a -> b) -> NonceT s m a -> NonceT s m b
$c<*> :: forall s (m :: * -> *) a b.
Applicative m =>
NonceT s m (a -> b) -> NonceT s m a -> NonceT s m b
pure :: forall a. a -> NonceT s m a
$cpure :: forall s (m :: * -> *) a. Applicative m => a -> NonceT s m a
Applicative, forall a. a -> NonceT s m a
forall a b. NonceT s m a -> NonceT s m b -> NonceT s m b
forall a b. NonceT s m a -> (a -> NonceT s m b) -> NonceT s m b
forall {s} {m :: * -> *}. Monad m => Applicative (NonceT s m)
forall s (m :: * -> *) a. Monad m => a -> NonceT s m a
forall s (m :: * -> *) a b.
Monad m =>
NonceT s m a -> NonceT s m b -> NonceT s m b
forall s (m :: * -> *) a b.
Monad m =>
NonceT s m a -> (a -> NonceT s m b) -> NonceT s m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> NonceT s m a
$creturn :: forall s (m :: * -> *) a. Monad m => a -> NonceT s m a
>> :: forall a b. NonceT s m a -> NonceT s m b -> NonceT s m b
$c>> :: forall s (m :: * -> *) a b.
Monad m =>
NonceT s m a -> NonceT s m b -> NonceT s m b
>>= :: forall a b. NonceT s m a -> (a -> NonceT s m b) -> NonceT s m b
$c>>= :: forall s (m :: * -> *) a b.
Monad m =>
NonceT s m a -> (a -> NonceT s m b) -> NonceT s m b
Monad)
instance MonadTrans (NonceT s) where
lift :: forall (m :: * -> *) a. Monad m => m a -> NonceT s m a
lift m a
m = forall s (m :: * -> *) a.
ReaderT (NonceGenerator m s) m a -> NonceT s m a
NonceT forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m
instance Monad m => MonadNonce (NonceT s m) where
type NonceSet (NonceT s m) = s
freshNonceM :: forall k (tp :: k). NonceT s m (Nonce (NonceSet (NonceT s m)) tp)
freshNonceM = forall s (m :: * -> *) a.
ReaderT (NonceGenerator m s) m a -> NonceT s m a
NonceT forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) s k (tp :: k).
NonceGenerator m s -> m (Nonce s tp)
freshNonce forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall r (m :: * -> *). MonadReader r m => m r
ask
instance MonadNonce m => MonadNonce (StateT s m) where
type NonceSet (StateT s m) = NonceSet m
freshNonceM :: forall k (tp :: k). StateT s m (Nonce (NonceSet (StateT s m)) tp)
freshNonceM = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) k (tp :: k).
MonadNonce m =>
m (Nonce (NonceSet m) tp)
freshNonceM
type NonceST t s = NonceT s (ST t)
type NonceIO s = NonceT s IO
getNonceSTGen :: NonceST t s (NonceGenerator (ST t) s)
getNonceSTGen :: forall t s. NonceST t s (NonceGenerator (ST t) s)
getNonceSTGen = forall s (m :: * -> *) a.
ReaderT (NonceGenerator m s) m a -> NonceT s m a
NonceT forall r (m :: * -> *). MonadReader r m => m r
ask
runNonceST :: (forall t s. NonceST t s a) -> a
runNonceST :: forall a. (forall t s. NonceST t s a) -> a
runNonceST forall t s. NonceST t s a
m = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ forall t r. (forall s. NonceGenerator (ST t) s -> ST t r) -> ST t r
withSTNonceGenerator forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a.
NonceT s m a -> ReaderT (NonceGenerator m s) m a
runNonceT forall t s. NonceST t s a
m
runNonceIO :: (forall s. NonceIO s a) -> IO a
runNonceIO :: forall a. (forall s. NonceIO s a) -> IO a
runNonceIO forall s. NonceIO s a
m = forall r. (forall s. NonceGenerator IO s -> IO r) -> IO r
withIONonceGenerator forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a.
NonceT s m a -> ReaderT (NonceGenerator m s) m a
runNonceT forall s. NonceIO s a
m