on master help sign in
Add example impls of Monad*Control
cd6db37Michael Xavier
katip/examples/example.hs+29−4
@@ -1,19 +1,24 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE TemplateHaskell #-}
6{-# LANGUAGE TemplateHaskell #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE UndecidableInstances #-}
6module Main
9module Main
7 ( main
10 ( main
8 ) where
11 ) where
9
12
10
13
11-------------------------------------------------------------------------------
14-------------------------------------------------------------------------------
12−import qualified Control.Applicative as A
15+import qualified Control.Applicative as A
16+import Control.Monad.Base
13import Control.Monad.Reader
17import Control.Monad.Reader
18+import Control.Monad.Trans.Control
14import Data.Aeson
19import Data.Aeson
15−import Data.Monoid as M
20+import Data.Monoid as M
16−import System.IO (stdout)
21+import System.IO (stdout)
17-------------------------------------------------------------------------------
22-------------------------------------------------------------------------------
18import Katip
23import Katip
19-------------------------------------------------------------------------------
24-------------------------------------------------------------------------------
@@ -78,7 +83,27 @@ data MyState = MyState {
78-------------------------------------------------------------------------------
83-------------------------------------------------------------------------------
79newtype MyStack m a = MyStack {
84newtype MyStack m a = MyStack {
80 unStack :: ReaderT MyState m a
85 unStack :: ReaderT MyState m a
81− } deriving (MonadReader MyState, Functor, A.Applicative, Monad, MonadIO)
86+ } deriving (MonadReader MyState, Functor, A.Applicative, Monad, MonadIO, MonadTrans)
87+
88+
89+-- MonadBase, MonadTransControl, and MonadBaseControl aren't strictly
90+-- needed for this example, but they are commonly required and
91+-- MonadTransControl/MonadBaseControl are a pain to implement, so I've
92+-- included them. Note that KatipT and KatipContextT already do this work for you.
93+instance MonadBase b m => MonadBase b (MyStack m) where
94+ liftBase = liftBaseDefault
95+
96+
97+instance MonadTransControl MyStack where
98+ type StT MyStack a = StT (ReaderT Int) a
99+ liftWith = defaultLiftWith MyStack unStack
100+ restoreT = defaultRestoreT MyStack
101+
102+
103+instance MonadBaseControl b m => MonadBaseControl b (MyStack m) where
104+ type StM (MyStack m) a = ComposeSt MyStack m a
105+ liftBaseWith = defaultLiftBaseWith
106+ restoreM = defaultRestoreM
82
107
83
108
84instance (MonadIO m) => Katip (MyStack m) where
109instance (MonadIO m) => Katip (MyStack m) where
katip/examples/example_lens.hs+29−5
@@ -1,20 +1,25 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE TemplateHaskell #-}
6{-# LANGUAGE TemplateHaskell #-}
7+{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE UndecidableInstances #-}
6module Main
9module Main
7 ( main
10 ( main
8 ) where
11 ) where
9
12
10
13
11-------------------------------------------------------------------------------
14-------------------------------------------------------------------------------
12−import Control.Applicative as A
15+import Control.Applicative as A
13−import Control.Lens hiding ((.=))
16+import Control.Lens hiding ((.=))
17+import Control.Monad.Base
14import Control.Monad.Reader
18import Control.Monad.Reader
19+import Control.Monad.Trans.Control
15import Data.Aeson
20import Data.Aeson
16−import Data.Monoid as M
21+import Data.Monoid as M
17−import System.IO (stdout)
22+import System.IO (stdout)
18-------------------------------------------------------------------------------
23-------------------------------------------------------------------------------
19import Katip
24import Katip
20-------------------------------------------------------------------------------
25-------------------------------------------------------------------------------
@@ -86,9 +91,28 @@ confrabulateWidgets = return ()
86-------------------------------------------------------------------------------
91-------------------------------------------------------------------------------
87newtype MyStack m a = MyStack {
92newtype MyStack m a = MyStack {
88 unStack :: ReaderT MyState m a
93 unStack :: ReaderT MyState m a
89− } deriving (MonadReader MyState, Functor, A.Applicative, Monad, MonadIO)
94+ } deriving (MonadReader MyState, Functor, A.Applicative, Monad, MonadIO, MonadTrans)
90
95
91
96
97+-- MonadBase, MonadTransControl, and MonadBaseControl aren't strictly
98+-- needed for this example, but they are commonly required and
99+-- MonadTransControl/MonadBaseControl are a pain to implement, so I've
100+-- included them. Note that KatipT and KatipContextT already do this work for you.
101+instance MonadBase b m => MonadBase b (MyStack m) where
102+ liftBase = liftBaseDefault
103+
104+
105+instance MonadTransControl MyStack where
106+ type StT MyStack a = StT (ReaderT Int) a
107+ liftWith = defaultLiftWith MyStack unStack
108+ restoreT = defaultRestoreT MyStack
109+
110+
111+instance MonadBaseControl b m => MonadBaseControl b (MyStack m) where
112+ type StM (MyStack m) a = ComposeSt MyStack m a
113+ liftBaseWith = defaultLiftBaseWith
114+ restoreM = defaultRestoreM
115+
92instance (MonadIO m) => Katip (MyStack m) where
116instance (MonadIO m) => Katip (MyStack m) where
93 getLogEnv = view msLogEnv
117 getLogEnv = view msLogEnv
94
118