{-# LANGUAGE DeriveLift #-}
module Futhark.FreshNames
( VNameSource
, blankNameSource
, newNameSource
, newName
, newVName
, newVNameFromName
) where
import Language.Haskell.TH.Syntax (Lift)
import Language.Futhark.Core
newtype VNameSource = VNameSource Int
deriving (Lift, Eq, Ord)
instance Semigroup VNameSource where
VNameSource x <> VNameSource y = VNameSource (x `max` y)
instance Monoid VNameSource where
mempty = blankNameSource
newName :: VNameSource -> VName -> (VName, VNameSource)
newName (VNameSource i) k = (VName (baseName k) i, VNameSource (i+1))
blankNameSource :: VNameSource
blankNameSource = newNameSource 0
newNameSource :: Int -> VNameSource
newNameSource = VNameSource
newVName :: VNameSource -> String -> (VName, VNameSource)
newVName src = newVNameFromName src . nameFromString
newVNameFromName :: VNameSource -> Name -> (VName, VNameSource)
newVNameFromName src s = newName src $ VName s 0