summaryrefslogtreecommitdiff
path: root/SC/Monad.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2021-09-19 18:06:03 +0200
committerTom Smeding <tom@tomsmeding.com>2021-09-19 18:06:03 +0200
commit956a60dc5253da43dc0fddaecf88116597023fdf (patch)
treef56402103483c853a0bdd7551092418025156e51 /SC/Monad.hs
Initial
Diffstat (limited to 'SC/Monad.hs')
-rw-r--r--SC/Monad.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/SC/Monad.hs b/SC/Monad.hs
new file mode 100644
index 0000000..c58755f
--- /dev/null
+++ b/SC/Monad.hs
@@ -0,0 +1,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