summaryrefslogtreecommitdiff
path: root/src/Util/IdGen.hs
blob: fcfb6e7fe648441021b705e337cf77ff211352ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Util.IdGen where

import Control.Monad.Trans.State.Strict


newtype IdGen a = IdGen (State Int a)
  deriving newtype (Functor, Applicative, Monad)

genId :: IdGen Int
genId = IdGen (state (\i -> (i, i + 1)))

runIdGen :: Int -> IdGen a -> a
runIdGen start (IdGen m) = evalState m start

runIdGen' :: Int -> IdGen a -> (a, Int)
runIdGen' start (IdGen m) = runState m start