forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupply.hs
More file actions
33 lines (23 loc) · 921 Bytes
/
Supply.hs
File metadata and controls
33 lines (23 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- |
-- Fresh variable supply
--
module Control.Monad.Supply where
import Prelude.Compat
import Control.Applicative
import Control.Monad.Error.Class (MonadError(..))
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer
import Data.Functor.Identity
newtype SupplyT m a = SupplyT { unSupplyT :: StateT Integer m a }
deriving (Functor, Applicative, Monad, MonadTrans, MonadError e, MonadWriter w, MonadReader r, Alternative, MonadPlus)
runSupplyT :: Integer -> SupplyT m a -> m (a, Integer)
runSupplyT n = flip runStateT n . unSupplyT
evalSupplyT :: (Functor m) => Integer -> SupplyT m a -> m a
evalSupplyT n = fmap fst . runSupplyT n
type Supply = SupplyT Identity
runSupply :: Integer -> Supply a -> (a, Integer)
runSupply n = runIdentity . runSupplyT n
evalSupply :: Integer -> Supply a -> a
evalSupply n = runIdentity . evalSupplyT n