-
Notifications
You must be signed in to change notification settings - Fork 28
/
2013-12-04-extensible-effects.hs
45 lines (37 loc) · 1.2 KB
/
2013-12-04-extensible-effects.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
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
import Prelude hiding (log)
import Control.Eff
import Control.Eff.Lift
import Data.Typeable (Typeable)
data Log v = Log String v deriving (Functor, Typeable)
log :: Member Log r => String -> Eff r ()
log txt = send $ \next -> inj (Log txt (next ()))
verboseAddition :: Member Log r => Eff r Int
verboseAddition = do
log "I'm starting with 1..."
x <- return 1
log "and I'm adding 2..."
y <- return 2
let r = x + y
log $ "Looks like the result is " ++ show r
return r
runLogger :: Eff (Log :> r) a -> Eff r (a, [String])
runLogger logAction = go (admin logAction)
where
go (Val v) = return (v, [])
go (E request) =
let prefixLogWith txt (v, l) = (v, txt:l)
performLog (Log txt next) =
fmap (prefixLogWith txt) (go next)
in handleRelay request go performLog
runIOLogger :: SetMember Lift (Lift IO) r => Eff (Log :> r) a -> Eff r a
runIOLogger logAction = go (admin logAction)
where
go (Val v) = return v
go (E request) =
let performLog (Log txt next) =
lift (putStrLn txt) >> go next
in handleRelay request go performLog