forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.hs
More file actions
36 lines (28 loc) · 909 Bytes
/
Class.hs
File metadata and controls
36 lines (28 loc) · 909 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
34
35
36
-- |
-- A class for monads supporting a supply of fresh names
--
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeFamilies #-}
module Control.Monad.Supply.Class where
import Prelude.Compat
import Control.Monad.Supply
import Control.Monad.State
import Control.Monad.Writer
import Data.Text (Text, pack)
class Monad m => MonadSupply m where
fresh :: m Integer
peek :: m Integer
default fresh :: (MonadTrans t, MonadSupply n, m ~ t n) => m Integer
fresh = lift fresh
default peek :: (MonadTrans t, MonadSupply n, m ~ t n) => m Integer
peek = lift peek
instance Monad m => MonadSupply (SupplyT m) where
fresh = SupplyT $ do
n <- get
put (n + 1)
return n
peek = SupplyT get
instance MonadSupply m => MonadSupply (StateT s m)
instance (Monoid w, MonadSupply m) => MonadSupply (WriterT w m)
freshName :: MonadSupply m => m Text
freshName = fmap (("$" <> ) . pack . show) fresh