-
Notifications
You must be signed in to change notification settings - Fork 9
/
FreeHandlers.hs
32 lines (26 loc) · 915 Bytes
/
FreeHandlers.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
{- Handlers using a free monad -}
{-# LANGUAGE TypeFamilies,
MultiParamTypeClasses,
GADTs,
TypeOperators,
RankNTypes,
FunctionalDependencies,
PolyKinds
#-}
module FreeHandlers where
type family Return (opApp :: *) :: *
type family Result (h :: *) :: *
class ((h :: *) `Handles` (op :: j -> k -> *)) (e :: j) | h op -> e where
clause :: op e u -> (Return (op e u) -> h -> Result h) -> h -> Result h
data Comp h a where
Ret :: a -> Comp h a
Do :: (h `Handles` op) e => op e u -> (Return (op e u) -> Comp h a) -> Comp h a
instance Monad (Comp h) where
return = Ret
Ret v >>= f = f v
Do op k >>= f = Do op (\x -> k x >>= f)
doOp :: (h `Handles` op) e => op e u -> Comp h (Return (op e u))
doOp op = Do op return
handle :: Comp h a -> (a -> h -> Result h) -> h -> Result h
handle (Ret v) r h = r v h
handle (Do op k) r h = clause op (\v h' -> handle (k v) r h') h