summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2019-02-10 18:01:46 -0500
committerGravatar Adam Chlipala <adam@chlipala.net>2019-02-10 18:01:46 -0500
commitb9ddbe0ce5c700fef313788fda5c7ecb82c84638 (patch)
tree6242b196f0948c3fcb6509c0ab7b33243c739f6b /lib
parent0aab3f48920058051d61215f23440d1042659c92 (diff)
List.searchM and ListPair.unzip
Diffstat (limited to 'lib')
-rw-r--r--lib/ur/list.ur14
-rw-r--r--lib/ur/list.urs2
-rw-r--r--lib/ur/listPair.ur10
-rw-r--r--lib/ur/listPair.urs2
4 files changed, 28 insertions, 0 deletions
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