From b9ddbe0ce5c700fef313788fda5c7ecb82c84638 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sun, 10 Feb 2019 18:01:46 -0500 Subject: List.searchM and ListPair.unzip --- lib/ur/list.ur | 14 ++++++++++++++ lib/ur/list.urs | 2 ++ lib/ur/listPair.ur | 10 ++++++++++ lib/ur/listPair.urs | 2 ++ 4 files changed, 28 insertions(+) (limited to 'lib') diff --git a/lib/ur/list.ur b/lib/ur/list.ur index 10877420..b38097b7 100644 --- a/lib/ur/list.ur +++ b/lib/ur/list.ur @@ -293,6 +293,20 @@ fun search [a] [b] f = search' end +fun searchM [m] (_ : monad m) [a] [b] f = + let + fun search' ls = + case ls of + [] => return None + | x :: ls => + o <- f x; + case o of + None => search' ls + | v => return v + in + search' + end + fun foldlM [m] (_ : monad m) [a] [b] f = let fun foldlM' acc ls = diff --git a/lib/ur/list.urs b/lib/ur/list.urs index c1d46022..7e3cf205 100644 --- a/lib/ur/list.urs +++ b/lib/ur/list.urs @@ -67,6 +67,8 @@ val findM : m ::: (Type -> Type) -> monad m -> a ::: Type -> (a -> m bool) -> t val search : a ::: Type -> b ::: Type -> (a -> option b) -> t a -> option b +val searchM : m ::: (Type -> Type) -> monad m -> a ::: Type -> b ::: Type -> (a -> m (option b)) -> t a -> m (option b) + val all : a ::: Type -> (a -> bool) -> t a -> bool val allM : m ::: (Type -> Type) -> monad m -> a ::: Type -> (a -> m bool) -> t a -> m bool diff --git a/lib/ur/listPair.ur b/lib/ur/listPair.ur index c5e70708..52c73cd8 100644 --- a/lib/ur/listPair.ur +++ b/lib/ur/listPair.ur @@ -58,3 +58,13 @@ fun mapM [m] (_ : monad m) [a] [b] [c] (f : a -> b -> m c) = in mapM' end + +fun unzip [a] [b] (ls : list (a * b)) : list a * list b = + let + fun unzip' ls ls1 ls2 = + case ls of + [] => (List.rev ls1, List.rev ls2) + | (x1, x2) :: ls => unzip' ls (x1 :: ls1) (x2 :: ls2) + in + unzip' ls [] [] + end diff --git a/lib/ur/listPair.urs b/lib/ur/listPair.urs index 9efff405..91d8807d 100644 --- a/lib/ur/listPair.urs +++ b/lib/ur/listPair.urs @@ -11,3 +11,5 @@ val mp : a ::: Type -> b ::: Type -> c ::: Type val mapM : m ::: (Type -> Type) -> monad m -> a ::: Type -> b ::: Type -> c ::: Type -> (a -> b -> m c) -> list a -> list b -> m (list c) + +val unzip : a ::: Type -> b ::: Type -> list (a * b) -> list a * list b -- cgit v1.2.3