From 4ada57be570bfbe18137c5b37ed5e0d327de82db Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Mon, 31 Dec 2018 14:48:16 -0500 Subject: ListPair.mapM --- lib/ur/listPair.ur | 16 +++++++++++++++- lib/ur/listPair.urs | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ur/listPair.ur b/lib/ur/listPair.ur index 94b92872..c5e70708 100644 --- a/lib/ur/listPair.ur +++ b/lib/ur/listPair.ur @@ -40,7 +40,21 @@ fun mp [a] [b] [c] (f : a -> b -> c) = case (ls1, ls2) of ([], []) => [] | (x1 :: ls1, x2 :: ls2) => f x1 x2 :: map' ls1 ls2 - | _ => error ListPair.map2: Unequal list lengths + | _ => error ListPair.mp: Unequal list lengths in map' end + +fun mapM [m] (_ : monad m) [a] [b] [c] (f : a -> b -> m c) = + let + fun mapM' ls1 ls2 = + case (ls1, ls2) of + ([], []) => return [] + | (x1 :: ls1, x2 :: ls2) => + y <- f x1 x2; + ls <- mapM' ls1 ls2; + return (y :: ls) + | _ => error ListPair.mapM: Unequal list lengths + in + mapM' + end diff --git a/lib/ur/listPair.urs b/lib/ur/listPair.urs index b473e226..9efff405 100644 --- a/lib/ur/listPair.urs +++ b/lib/ur/listPair.urs @@ -8,3 +8,6 @@ val all : a ::: Type -> b ::: Type -> (a -> b -> bool) -> list a -> list b -> bo val mp : a ::: Type -> b ::: Type -> c ::: Type -> (a -> b -> c) -> list a -> list b -> list c + +val mapM : m ::: (Type -> Type) -> monad m -> a ::: Type -> b ::: Type -> c ::: Type + -> (a -> b -> m c) -> list a -> list b -> m (list c) -- cgit v1.2.3