-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monad.hs
executable file
·54 lines (39 loc) · 1.47 KB
/
Monad.hs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Turbinado.Controller.Monad (
-- * The 'Controller' Monad
Controller,
runController,
withController,
get,
put,
-- * Functions
doIO, catch
) where
import Control.Exception (catchDyn)
import Control.Monad.State
import Control.Monad.Trans (MonadIO(..))
import Data.Maybe
import Prelude hiding (catch)
import Turbinado.Environment.Types
import Turbinado.Controller.Exception
import Turbinado.Utility.General
--------------------------------------------------------------
-- The Controller Monad
-- | The Controller monad is a state wrapper around
-- the IO monad.
type Controller = StateT Environment IO
-- | Runs a Controller computation in a particular environment. Since Controller wraps the IO monad,
-- the result of running it will be an IO computation.
runController :: Controller () -> Environment -> IO Environment
runController c e = (execStateT c) e
withController :: (Environment -> Environment) -> Controller a -> Controller a
withController = withStateT
-- | Execute an IO computation within the Controller monad.
doIO :: IO a -> Controller a
doIO = liftIO
-----------------------------------------------------------------------
-- Exception handling
-- | Catch a user-caused exception.
catch :: Controller a -> (Exception -> Controller a) -> Controller a
catch (StateT f) handler = StateT $ \e ->
f e `catchDyn` (\ex -> (let (StateT g) = handler ex
in g e))