{-# Language FlexibleInstances #-}
-- | Unique identifiers.
module Sound.DF.Uniform.LL.UId where

import Control.Monad {- base -}
import Control.Monad.Trans.State {- transformers -}
import Data.Unique {- base -}

-- * UId Class

-- | Identifiers are integers.
type Id = Int

-- | Class of monads generating identifers
class (Monad m) => UId m where
   generateId :: m Id

instance UId IO where
   generateId = liftM (fromIntegral . hashUnique) newUnique

instance UId (State Id) where
    generateId = do
      i <- get
      put (i + 1)
      return i

-- | Evaluate /m/ 'DF'.
evalId :: State Id a -> a
evalId c = evalState c 0