summaryrefslogtreecommitdiff
path: root/SC/Monad.hs
blob: c58755f858ae815d2646ef6035ce7e1f10a1b272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-# LANGUAGE DerivingVia #-}
module SC.Monad where

import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except
import Control.Monad.Trans.State.Strict


newtype SC a = SC (ExceptT String (State Int) a)
  deriving (Functor, Applicative, Monad) via (ExceptT String (State Int))

instance MonadFail SC where
    fail = throw

evalSC :: SC a -> Either String a
evalSC (SC m) = evalState (runExceptT m) 1

genId :: SC Int
genId = SC $ do
    value <- lift get
    lift (modify (+1))
    return value

throw :: String -> SC a
throw = SC . throwE