on c19ccfe help sign in
elk/compiler/Unique.hs
19 lines 457 B history raw
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2module Unique (guid, set, run, runAt, Unique) where
4import Control.Monad.State (evalState, State, get, put)
6-- Wrapper around State monad.
7newtype Unique a = U { runU :: State Int a }
8 deriving (Monad)
10-- Get the next GUID, incrementing the counter.
11guid :: Unique Int
12guid = U $ do n <- get
13 put (n + 1)
14 return n
16set n = U (put n)
18run = runAt 0
19runAt n x = evalState (runU x) n