git.iko.soy/effects-benchmarks
on master help sign in
Add Oleg's extensible-effects library
f50cd09Patrick Thomson
instances/ExtEff/HTTP.hsnew file+40
@@ -0,0 +1,40 @@
1+{-# LANGUAGE DataKinds, TypeOperators, GADTs, FlexibleInstances, MultiParamTypeClasses #-}
2+
3+module ExtEff.HTTP where
4+
5+import Control.Eff
6+import Control.Eff.Extend
7+import Data.Function
8+
9+type HttpM = Eff '[HTTP, Lift IO]
10+
11+data HTTP a where
12+ Open :: String -> HTTP ()
13+ Close :: HTTP ()
14+ Post :: String -> HTTP String
15+ HGet :: HTTP String
16+
17+open' :: String -> HttpM ()
18+open' = send . Open
19+
20+close' :: HttpM ()
21+close' = send Close
22+
23+post' :: String -> HttpM String
24+post' = send . Post
25+
26+get' :: HttpM String
27+get' = send HGet
28+
29+instance Handle HTTP r b (Eff e b) where
30+ handle h q req = case req of
31+ Open _s -> h (q ^$ ())
32+ Close -> h (q ^$ ())
33+ Post s -> h (q ^$ ("posted " <> s))
34+ HGet -> h (q ^$ "contents")
35+
36+doHttp :: Eff (HTTP ': e) b -> Eff e b
37+doHttp = fix (handle_relay pure)
38+
39+runHttp :: HttpM a -> IO a
40+runHttp = runLift . doHttp
instances/ExtEff/Stateful.hsnew file+18
@@ -0,0 +1,18 @@
1+{-# LANGUAGE DataKinds, TypeApplications #-}
2+
3+module ExtEff.Stateful where
4+
5+import Control.Eff
6+import Control.Eff.State.Strict as State
7+import Data.Tuple
8+
9+type StateM = Eff '[State Int]
10+
11+get :: StateM Int
12+get = State.get @Int
13+
14+put :: Int -> StateM ()
15+put = State.put @Int
16+
17+runStateful :: Int -> StateM a -> (Int, a)
18+runStateful n x = swap . run $ State.runState n x
instances/ExtEff/StatefulExcept.hsnew file+21
@@ -0,0 +1,21 @@
1+{-# LANGUAGE TypeApplications, DataKinds #-}
2+module ExtEff.StatefulExcept where
3+
4+import Control.Eff
5+import Control.Eff.State.Strict as State
6+import Control.Eff.Exception
7+import Data.Tuple
8+
9+type StateM = Eff '[State Int, Exc String]
10+
11+get :: StateM Int
12+get = State.get @Int
13+
14+put :: Int -> StateM ()
15+put = State.put @Int
16+
17+throw :: String -> StateM a
18+throw = throwError
19+
20+runStatefulExcept :: Int -> StateM a -> Either String (Int, a)
21+runStatefulExcept n x = fmap swap . run . runError $ State.runState n x