Skip to content

Instantly share code, notes, and snippets.

@hvr
Created July 20, 2015 09:22

Revisions

  1. hvr created this gist Jul 20, 2015.
    23 changes: 23 additions & 0 deletions SETrans.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    {-# LANGUAGE ScopedTypeVariables #-}

    import Control.Monad.Identity
    import Control.Monad.Except
    import Control.Monad.State.Strict


    type SET s e m = StateT s (ExceptT e m)
    type EST e s m = ExceptT e (StateT s m)

    type SE s e = StateT s (Except e)
    type ES e s = ExceptT e (State s)

    data St = St
    data Err = Err

    -- errors w/ rollback
    runES :: forall e s a . ES e s a -> s -> (Either e a, s)
    runES act s0 = runState (runExceptT act :: State s (Either e a)) s0

    -- errors w/o rollback
    runSE :: forall s e a . SE s e a -> s -> Either e (a,s)
    runSE act s0 = runExcept (runStateT act s0)