changeset 1175:79f487f51d9f

Monad.foldMapR
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Mar 2010 10:33:49 -0500
parents 9df124fcab3d
children 51e596feec37
files lib/ur/monad.ur lib/ur/monad.urs
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ur/monad.ur	Tue Mar 02 09:46:17 2010 -0500
+++ b/lib/ur/monad.ur	Tue Mar 02 10:33:49 2010 -0500
@@ -78,3 +78,17 @@
         v' <- f [nm] [t] v1 v2;
         return (acc ++ {nm = v'}))
     {}
+
+fun foldMapR [K] [m] (_ : monad m) [tf :: K -> Type] [tf' :: K -> Type] [tr :: {K} -> Type]
+             (f : nm :: Name -> t :: K -> rest :: {K}
+                  -> [[nm] ~ rest] =>
+              tf t -> tr rest -> m (tf' t * tr ([nm = t] ++ rest)))
+             (i : tr []) [r ::: {K}] (fl : folder r) =
+    @Top.fold [fn r :: {K} => $(map tf r) -> m ($(map tf' r) * tr r)]
+     (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] 
+                      (acc : _ -> m ($(map tf' rest) * tr rest)) r =>
+         p <- acc (r -- nm);
+         p' <- f [nm] [t] [rest] ! r.nm p.2;
+         return ({nm = p'.1} ++ p.1, p'.2))
+     (fn _ => return ({}, i))
+     fl
--- a/lib/ur/monad.urs	Tue Mar 02 09:46:17 2010 -0500
+++ b/lib/ur/monad.urs	Tue Mar 02 10:33:49 2010 -0500
@@ -50,3 +50,13 @@
             -> tr :: (K -> Type)
             -> (nm :: Name -> t :: K -> tf1 t -> tf2 t -> m (tr t))
             -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) -> m ($(map tr r))
+
+val foldMapR : K --> m ::: (Type -> Type) -> monad m
+               -> tf :: (K -> Type)
+               -> tf' :: (K -> Type)
+               -> tr :: ({K} -> Type)
+               -> (nm :: Name -> t :: K -> rest :: {K}
+                   -> [[nm] ~ rest] =>
+                   tf t -> tr rest -> m (tf' t * tr ([nm = t] ++ rest)))
+               -> tr []
+               -> r ::: {K} -> folder r -> $(map tf r) -> m ($(map tf' r) * tr r)