@@ -24,6 +24,27 @@ Effect = (x : Type) -> Type -> (x -> Type) -> Type
24data EFFECT : Type where
24data EFFECT : Type where
25 MkEff : Type -> Effect -> EFFECT
25 MkEff : Type -> Effect -> EFFECT
27+-- 'sig' gives the signature for an effect. There are three versions
28+-- depending on whether there is no state change, a non-dependent change,
29+-- or a dependent change. These are easily disambiguated by type.
31+namespace NoResourceEffect
32+ sig : Effect -> Type -> Type
33+ sig e r = e r () (\v => ())
35+namespace NoUpdateEffect
36+ sig : Effect -> (ret : Type) -> (resource : Type) -> Type
37+ sig e r e_in = e r e_in (\v => e_in)
39+namespace UpdateEffect
40+ sig : Effect -> (ret : Type) -> (res_in : Type) -> (res_out : Type) -> Type
41+ sig e r e_in e_out = e r e_in (\v => e_out)
43+namespace DepUpdateEffect
45+ (ret : Type) -> (res_in : Type) -> (res_out : ret -> Type) -> Type
46+ sig e r e_in e_out = e r e_in e_out
27||| Handler classes describe how an effect `e` is translated to the
48||| Handler classes describe how an effect `e` is translated to the
28||| underlying computation context `m` for execution.
49||| underlying computation context `m` for execution.
29class Handler (e : Effect) (m : Type -> Type) where
50class Handler (e : Effect) (m : Type -> Type) where
@@ -60,6 +81,7 @@ data SubList : List a -> List a -> Type where
60 Keep : SubList xs ys -> SubList (x :: xs) (x :: ys)
81 Keep : SubList xs ys -> SubList (x :: xs) (x :: ys)
61 Drop : SubList xs ys -> SubList xs (x :: ys)
82 Drop : SubList xs ys -> SubList xs (x :: ys)
63subListId : SubList xs xs
85subListId : SubList xs xs
64subListId {xs = Nil} = SubNil
86subListId {xs = Nil} = SubNil
65subListId {xs = x :: xs} = Keep subListId
87subListId {xs = x :: xs} = Keep subListId
@@ -133,26 +155,56 @@ relabel {xs = (MkEff a e :: xs)} l (v :: vs) = (l := v) :: relabel l vs
133||| @ x The return type of the result.
155||| @ x The return type of the result.
134||| @ es The list of allowed side-effects.
156||| @ es The list of allowed side-effects.
135||| @ ce Function to compute a new list of allowed side-effects.
157||| @ ce Function to compute a new list of allowed side-effects.
136−data Eff : (x : Type)
158+data EffM : (m : Type -> Type) -> (x : Type)
137− -> (es : List EFFECT)
159+ -> (es : List EFFECT)
138− -> (ce : x -> List EFFECT) -> Type where
160+ -> (ce : x -> List EFFECT) -> Type where
139− value : (val : a) -> Eff a (xs val) xs
161+ value : (val : a) -> EffM m a (xs val) xs
140− ebind : Eff a xs xs' ->
162+ ebind : EffM m a xs xs' ->
141− ((val : a) -> Eff b (xs' val) xs'') -> Eff b xs xs''
163+ ((val : a) -> EffM m b (xs' val) xs'') -> EffM m b xs xs''
142 callP : (prf : EffElem e a xs) ->
164 callP : (prf : EffElem e a xs) ->
143 (eff : e t a b) ->
165 (eff : e t a b) ->
144− Eff t xs (\v => updateResTy v xs prf eff)
166+ EffM m t xs (\v => updateResTy v xs prf eff)
146 liftP : (prf : SubList ys xs) ->
168 liftP : (prf : SubList ys xs) ->
147− Eff t ys ys' -> Eff t xs (\v => updateWith (ys' v) xs prf)
169+ EffM m t ys ys' -> EffM m t xs (\v => updateWith (ys' v) xs prf)
149 (:-) : (l : ty) ->
171 (:-) : (l : ty) ->
150− Eff t [x] xs' -> -- [x] (\v => xs) ->
172+ EffM m t [x] xs' -> -- [x] (\v => xs) ->
151− Eff t [l ::: x] (\v => map (l :::) (xs' v))
173+ EffM m t [l ::: x] (\v => map (l :::) (xs' v))
175+-- Some type synonyms, so we don't always have to write EffM in full
178+ -- Simple effects, no updates
179+ Eff : (x : Type) -> (es : List EFFECT) -> Type
180+ Eff x es = {m : Type -> Type} -> EffM m x es (\v => es)
182+ EffT : (m : Type -> Type) -> (x : Type) -> (es : List EFFECT) -> Type
183+ EffT m x es = EffM m x es (\v => es)
186+ -- Dependent effects, updates not dependent on result
187+ Eff : (x : Type) -> (es : List EFFECT) -> (ce : List EFFECT) -> Type
188+ Eff x es ce = {m : Type -> Type} -> EffM m x es (\_ => ce)
190+ EffT : (m : Type -> Type) ->
191+ (x : Type) -> (es : List EFFECT) -> (ce : List EFFECT) -> Type
192+ EffT m x es ce = EffM m x es (\_ => ce)
195+ -- Dependent effects, updates dependent on result
196+ Eff : (x : Type) -> (es : List EFFECT)
197+ -> (ce : x -> List EFFECT) -> Type
198+ Eff x es ce = {m : Type -> Type} -> EffM m x es ce
200+ EffT : (m : Type -> Type) -> (x : Type) -> (es : List EFFECT)
201+ -> (ce : x -> List EFFECT) -> Type
202+ EffT m x es ce = EffM m x es ce
153%no_implicit
205%no_implicit
154−(>>=) : Eff a xs xs' ->
206+(>>=) : EffM m a xs xs' ->
155− ((val : a) -> Eff b (xs' val) xs'') -> Eff b xs xs''
207+ ((val : a) -> EffM m b (xs' val) xs'') -> EffM m b xs xs''
156(>>=) = ebind
208(>>=) = ebind
158-- namespace SimpleBind
210-- namespace SimpleBind
@@ -161,29 +213,29 @@ data Eff : (x : Type)
161-- (>>=) = ebind
213-- (>>=) = ebind
163||| Run a subprogram which results in an effect state the same as the input.
215||| Run a subprogram which results in an effect state the same as the input.
164−staticEff : Eff a xs (\v => xs) -> Eff a xs (\v => xs)
216+staticEff : EffM m a xs (\v => xs) -> EffM m a xs (\v => xs)
165staticEff = id
217staticEff = id
167||| Explicitly give the expected set of result effects for an effectful
219||| Explicitly give the expected set of result effects for an effectful
168||| operation.
220||| operation.
169−toEff : .(xs' : List EFFECT) -> Eff a xs (\v => xs') -> Eff a xs (\v => xs')
221+toEff : .(xs' : List EFFECT) -> EffM m a xs (\v => xs') -> EffM m a xs (\v => xs')
170toEff xs' = id
222toEff xs' = id
172−return : a -> Eff a xs (\v => xs)
224+return : a -> EffM m a xs (\v => xs)
173return x = value x
225return x = value x
175-- ------------------------------------------------------ [ for idiom brackets ]
227-- ------------------------------------------------------ [ for idiom brackets ]
177infixl 2 <*>
229infixl 2 <*>
179−pure : a -> Eff a xs (\v => xs)
231+pure : a -> EffM m a xs (\v => xs)
180pure = value
232pure = value
182−pureM : (val : a) -> Eff a (xs val) xs
234+pureM : (val : a) -> EffM m a (xs val) xs
183pureM = value
235pureM = value
185−(<*>) : Eff (a -> b) xs (\v => xs) ->
237+(<*>) : EffM m (a -> b) xs (\v => xs) ->
186− Eff a xs (\v => xs) -> Eff b xs (\v => xs)
238+ EffM m a xs (\v => xs) -> EffM m b xs (\v => xs)
187(<*>) prog v = do fn <- prog
239(<*>) prog v = do fn <- prog
189 return (fn arg)
241 return (fn arg)
@@ -203,7 +255,7 @@ execEff {e} {a} {res} {resk} (val :: env) (There p) eff k
203-- Q: Instead of m b, implement as StateT (Env m xs') m b, so that state
255-- Q: Instead of m b, implement as StateT (Env m xs') m b, so that state
204-- updates can be propagated even through failing computations?
256-- updates can be propagated even through failing computations?
206−eff : Env m xs -> Eff a xs xs' -> ((x : a) -> Env m (xs' x) -> m b) -> m b
258+eff : Env m xs -> EffM m a xs xs' -> ((x : a) -> Env m (xs' x) -> m b) -> m b
207eff env (value x) k = k x env
259eff env (value x) k = k x env
208eff env (prog `ebind` c) k
260eff env (prog `ebind` c) k
209 = eff env prog (\p', env' => eff env' (c p') k)
261 = eff env prog (\p', env' => eff env' (c p') k)
@@ -235,14 +287,14 @@ call : {a, b: _} -> {e : Effect} ->
235 (eff : e t a b) ->
287 (eff : e t a b) ->
236 {default tactics { search 100; }
288 {default tactics { search 100; }
237 prf : EffElem e a xs} ->
289 prf : EffElem e a xs} ->
238− Eff t xs (\v => updateResTy v xs prf eff)
290+ EffM m t xs (\v => updateResTy v xs prf eff)
239call e {prf} = callP prf e
291call e {prf} = callP prf e
242−lift : Eff t ys ys' ->
294+lift : EffM m t ys ys' ->
243 {default tactics { search 100; }
295 {default tactics { search 100; }
244 prf : SubList ys xs} ->
296 prf : SubList ys xs} ->
245− Eff t xs (\v => updateWith (ys' v) xs prf)
297+ EffM m t xs (\v => updateWith (ys' v) xs prf)
246lift e {prf} = liftP prf e
298lift e {prf} = liftP prf e
@@ -255,7 +307,7 @@ lift e {prf} = liftP prf e
256||| @prog The effectful program to run.
308||| @prog The effectful program to run.
257run : Applicative m => {default MkDefaultEnv env : Env m xs}
309run : Applicative m => {default MkDefaultEnv env : Env m xs}
258− -> (prog : Eff a xs xs') -> m a
310+ -> (prog : EffM m a xs xs') -> m a
259run {env} prog = eff env prog (\r, env => pure r)
311run {env} prog = eff env prog (\r, env => pure r)
261||| Run an effectful program in the identity context.
313||| Run an effectful program in the identity context.
@@ -264,7 +316,7 @@ run {env} prog = eff env prog (\r, env => pure r)
264||| The `env` argument is implicit and initialised automatically.
316||| The `env` argument is implicit and initialised automatically.
266||| @prog The effectful program to run.
318||| @prog The effectful program to run.
267−runPure : {default MkDefaultEnv env : Env id xs} -> (prog : Eff a xs xs') -> a
319+runPure : {default MkDefaultEnv env : Env id xs} -> (prog : EffM id a xs xs') -> a
268runPure {env} prog = eff env prog (\r, env => r)
320runPure {env} prog = eff env prog (\r, env => r)
270||| Run an effectful program in a given context `m` with a default value for the environment.
322||| Run an effectful program in a given context `m` with a default value for the environment.
@@ -273,7 +325,7 @@ runPure {env} prog = eff env prog (\r, env => r)
274||| @env The environment to use.
326||| @env The environment to use.
275||| @prog The effectful program to run.
327||| @prog The effectful program to run.
276−runInit : Applicative m => (env : Env m xs) -> (prog : Eff a xs xs') -> m a
328+runInit : Applicative m => (env : Env m xs) -> (prog : EffM m a xs xs') -> m a
277runInit env prog = eff env prog (\r, env => pure r)
329runInit env prog = eff env prog (\r, env => pure r)
279||| Run an effectful program with a given default value for the environment.
331||| Run an effectful program with a given default value for the environment.
@@ -282,31 +334,31 @@ runInit env prog = eff env prog (\r, env => pure r)
283||| @env The environment to use.
335||| @env The environment to use.
284||| @prog The effectful program to run.
336||| @prog The effectful program to run.
285−runPureInit : (env : Env id xs) -> (prog : Eff a xs xs') -> a
337+runPureInit : (env : Env id xs) -> (prog : EffM id a xs xs') -> a
286runPureInit env prog = eff env prog (\r, env => r)
338runPureInit env prog = eff env prog (\r, env => r)
288−runWith : (a -> m a) -> Env m xs -> Eff a xs xs' -> m a
340+runWith : (a -> m a) -> Env m xs -> EffM m a xs xs' -> m a
289runWith inj env prog = eff env prog (\r, env => inj r)
341runWith inj env prog = eff env prog (\r, env => inj r)
291−runEnv : Applicative m => Env m xs -> Eff a xs xs' ->
343+runEnv : Applicative m => Env m xs -> EffM m a xs xs' ->
292 m (x : a ** Env m (xs' x))
344 m (x : a ** Env m (xs' x))
293runEnv env prog = eff env prog (\r, env => pure (r ** env))
345runEnv env prog = eff env prog (\r, env => pure (r ** env))
295-- ----------------------------------------------- [ some higher order things ]
347-- ----------------------------------------------- [ some higher order things ]
297−mapE : (a -> {xs} Eff b) -> List a -> {xs} Eff (List b)
349+mapE : (a -> {xs} EffM m b) -> List a -> {xs} EffM m (List b)
298mapE f [] = pure []
350mapE f [] = pure []
299mapE f (x :: xs) = [| f x :: mapE f xs |]
351mapE f (x :: xs) = [| f x :: mapE f xs |]
302−mapVE : (a -> {xs} Eff b) ->
354+mapVE : (a -> {xs} EffM m b) ->
303 Vect n a ->
355 Vect n a ->
304− {xs} Eff (Vect n b)
356+ {xs} EffM m (Vect n b)
305mapVE f [] = pure []
357mapVE f [] = pure []
306mapVE f (x :: xs) = [| f x :: mapVE f xs |]
358mapVE f (x :: xs) = [| f x :: mapVE f xs |]
309−when : Bool -> Lazy ({xs} Eff ()) -> {xs} Eff ()
361+when : Bool -> Lazy ({xs} EffM m ()) -> {xs} EffM m ()
310when True e = Force e
362when True e = Force e
311when False e = pure ()
363when False e = pure ()