{-# LANGUAGE DeriveLift #-}
module Futhark.FreshNames
( VNameSource,
blankNameSource,
newNameSource,
newName,
)
where
import Language.Futhark.Core
import Language.Haskell.TH.Syntax (Lift)
newtype VNameSource = VNameSource Int
deriving ((forall (m :: * -> *). Quote m => VNameSource -> m Exp)
-> (forall (m :: * -> *).
Quote m =>
VNameSource -> Code m VNameSource)
-> Lift VNameSource
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => VNameSource -> m Exp
forall (m :: * -> *). Quote m => VNameSource -> Code m VNameSource
liftTyped :: forall (m :: * -> *). Quote m => VNameSource -> Code m VNameSource
$cliftTyped :: forall (m :: * -> *). Quote m => VNameSource -> Code m VNameSource
lift :: forall (m :: * -> *). Quote m => VNameSource -> m Exp
$clift :: forall (m :: * -> *). Quote m => VNameSource -> m Exp
Lift, VNameSource -> VNameSource -> Bool
(VNameSource -> VNameSource -> Bool)
-> (VNameSource -> VNameSource -> Bool) -> Eq VNameSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VNameSource -> VNameSource -> Bool
$c/= :: VNameSource -> VNameSource -> Bool
== :: VNameSource -> VNameSource -> Bool
$c== :: VNameSource -> VNameSource -> Bool
Eq, Eq VNameSource
Eq VNameSource
-> (VNameSource -> VNameSource -> Ordering)
-> (VNameSource -> VNameSource -> Bool)
-> (VNameSource -> VNameSource -> Bool)
-> (VNameSource -> VNameSource -> Bool)
-> (VNameSource -> VNameSource -> Bool)
-> (VNameSource -> VNameSource -> VNameSource)
-> (VNameSource -> VNameSource -> VNameSource)
-> Ord VNameSource
VNameSource -> VNameSource -> Bool
VNameSource -> VNameSource -> Ordering
VNameSource -> VNameSource -> VNameSource
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: VNameSource -> VNameSource -> VNameSource
$cmin :: VNameSource -> VNameSource -> VNameSource
max :: VNameSource -> VNameSource -> VNameSource
$cmax :: VNameSource -> VNameSource -> VNameSource
>= :: VNameSource -> VNameSource -> Bool
$c>= :: VNameSource -> VNameSource -> Bool
> :: VNameSource -> VNameSource -> Bool
$c> :: VNameSource -> VNameSource -> Bool
<= :: VNameSource -> VNameSource -> Bool
$c<= :: VNameSource -> VNameSource -> Bool
< :: VNameSource -> VNameSource -> Bool
$c< :: VNameSource -> VNameSource -> Bool
compare :: VNameSource -> VNameSource -> Ordering
$ccompare :: VNameSource -> VNameSource -> Ordering
Ord)
instance Semigroup VNameSource where
VNameSource Int
x <> :: VNameSource -> VNameSource -> VNameSource
<> VNameSource Int
y = Int -> VNameSource
VNameSource (Int
x Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
y)
instance Monoid VNameSource where
mempty :: VNameSource
mempty = VNameSource
blankNameSource
newName :: VNameSource -> VName -> (VName, VNameSource)
newName :: VNameSource -> VName -> (VName, VNameSource)
newName (VNameSource Int
i) VName
k = Int
i' Int -> (VName, VNameSource) -> (VName, VNameSource)
`seq` (Name -> Int -> VName
VName (VName -> Name
baseName VName
k) Int
i, Int -> VNameSource
VNameSource Int
i')
where
i' :: Int
i' = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
blankNameSource :: VNameSource
blankNameSource :: VNameSource
blankNameSource = Int -> VNameSource
newNameSource Int
0
newNameSource :: Int -> VNameSource
newNameSource :: Int -> VNameSource
newNameSource = Int -> VNameSource
VNameSource