adamc@834
|
1 fun exec [m ::: Type -> Type] (_ : monad m) [ts ::: {Type}] r (fd : folder ts) =
|
adamc@834
|
2 foldR [m] [fn ts => m $ts]
|
adamc@834
|
3 (fn [nm :: Name] [v :: Type] [rest :: {Type}] [[nm] ~ rest] action acc =>
|
adamc@834
|
4 this <- action;
|
adamc@834
|
5 others <- acc;
|
adamc@834
|
6 return ({nm = this} ++ others))
|
adamc@834
|
7 (return {}) [ts] fd r
|
adamc@898
|
8
|
adamc@898
|
9 fun ignore [m ::: Type -> Type] (_ : monad m) [t] (v : m t) = x <- v; return ()
|
adamc@905
|
10
|
adamc@905
|
11 fun foldR [K] [m] (_ : monad m) [tf :: K -> Type] [tr :: {K} -> Type]
|
adamc@905
|
12 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@905
|
13 -> [[nm] ~ rest] =>
|
adamc@905
|
14 tf t -> tr rest -> m (tr ([nm = t] ++ rest)))
|
adamc@905
|
15 (i : tr []) [r :: {K}] (fl : folder r) =
|
adamc@905
|
16 Top.fold [fn r :: {K} => $(map tf r) -> m (tr r)]
|
adamc@905
|
17 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
|
adamc@905
|
18 (acc : _ -> m (tr rest)) r =>
|
adamc@905
|
19 acc' <- acc (r -- nm);
|
adamc@905
|
20 f [nm] [t] [rest] ! r.nm acc')
|
adamc@905
|
21 (fn _ => return i)
|
adamc@905
|
22 [_] fl
|
adamc@905
|
23
|
adamc@905
|
24 fun foldR2 [K] [m] (_ : monad m) [tf1 :: K -> Type] [tf2 :: K -> Type] [tr :: {K} -> Type]
|
adamc@905
|
25 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@905
|
26 -> [[nm] ~ rest] =>
|
adamc@905
|
27 tf1 t -> tf2 t -> tr rest -> m (tr ([nm = t] ++ rest)))
|
adamc@905
|
28 (i : tr []) [r :: {K}] (fl : folder r) =
|
adamc@905
|
29 Top.fold [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> m (tr r)]
|
adamc@905
|
30 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
|
adamc@905
|
31 (acc : _ -> _ -> m (tr rest)) r1 r2 =>
|
adamc@905
|
32 acc' <- acc (r1 -- nm) (r2 -- nm);
|
adamc@905
|
33 f [nm] [t] [rest] ! r1.nm r2.nm acc')
|
adamc@905
|
34 (fn _ _ => return i)
|
adamc@905
|
35 [_] fl
|
adamc@905
|
36
|
adamc@910
|
37 fun foldR3 [K] [m] (_ : monad m) [tf1 :: K -> Type] [tf2 :: K -> Type] [tf3 :: K -> Type] [tr :: {K} -> Type]
|
adamc@910
|
38 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@910
|
39 -> [[nm] ~ rest] =>
|
adamc@910
|
40 tf1 t -> tf2 t -> tf3 t -> tr rest -> m (tr ([nm = t] ++ rest)))
|
adamc@910
|
41 (i : tr []) [r :: {K}] (fl : folder r) =
|
adamc@910
|
42 Top.fold [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> $(map tf3 r) -> m (tr r)]
|
adamc@910
|
43 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
|
adamc@910
|
44 (acc : _ -> _ -> _ -> m (tr rest)) r1 r2 r3 =>
|
adamc@910
|
45 acc' <- acc (r1 -- nm) (r2 -- nm) (r3 -- nm);
|
adamc@910
|
46 f [nm] [t] [rest] ! r1.nm r2.nm r3.nm acc')
|
adamc@910
|
47 (fn _ _ _ => return i)
|
adamc@910
|
48 [_] fl
|
adamc@910
|
49
|
adamc@905
|
50 fun mapR [K] [m] (_ : monad m) [tf :: K -> Type] [tr :: K -> Type]
|
adamc@905
|
51 (f : nm :: Name -> t :: K -> tf t -> m (tr t)) =
|
adamc@905
|
52 @@foldR [m] _ [tf] [fn r => $(map tr r)]
|
adamc@905
|
53 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] (v : tf t)
|
adamc@905
|
54 (acc : $(map tr rest)) =>
|
adamc@905
|
55 v' <- f [nm] [t] v;
|
adamc@905
|
56 return (acc ++ {nm = v'}))
|
adamc@905
|
57 {}
|