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