changeset 1321:4172863d049d

queryL1 and List.sort
author Adam Chlipala <adam@chlipala.net>
date Sat, 20 Nov 2010 10:45:22 -0500
parents add5ae41969e
children 80bff6449f41
files lib/ur/list.ur lib/ur/list.urs lib/ur/top.ur lib/ur/top.urs
diffstat 4 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ur/list.ur	Sat Nov 20 09:48:03 2010 -0500
+++ b/lib/ur/list.ur	Sat Nov 20 10:45:22 2010 -0500
@@ -280,6 +280,34 @@
                 [];
     return (rev ls)
 
+fun sort [a] (gt : a -> a -> bool) (ls : t a) : t a =
+    let
+        fun split ls acc1 acc2 =
+            case ls of
+                [] => (rev acc1, rev acc2)
+              | x :: [] => (rev (x :: acc1), rev acc2)
+              | x1 :: x2 :: ls' => split ls' (x1 :: acc1) (x2 :: acc2)
+
+        fun merge ls1 ls2 acc =
+            case (ls1, ls2) of
+                ([], _) => revAppend acc ls2
+              | (_, []) => revAppend acc ls1
+              | (x1 :: ls1', x2 :: ls2') => if gt x1 x2 then merge ls1 ls2' (x2 :: acc) else merge ls1' ls2 (x1 :: acc)
+
+        fun sort' ls =
+            case ls of
+                [] => ls
+              | _ :: [] => ls
+              | _ =>
+                let
+                    val (ls1, ls2) = split ls [] []
+                in
+                    merge (sort' ls1) (sort' ls2) []
+                end
+    in
+        sort' ls
+    end
+
 fun assoc [a] [b] (_ : eq a) (x : a) =
     let
         fun assoc' (ls : list (a * b)) =
--- a/lib/ur/list.urs	Sat Nov 20 09:48:03 2010 -0500
+++ b/lib/ur/list.urs	Sat Nov 20 10:45:22 2010 -0500
@@ -69,6 +69,8 @@
     -> ($(exps ++ map (fn fields :: {Type} => $fields) tables) -> transaction (option t))
     -> transaction (list t)
 
+val sort : a ::: Type -> (a -> a -> bool) (* > predicate *) -> t a -> t a
+
 (** Association lists *)
 
 val assoc : a ::: Type -> b ::: Type -> eq a -> a -> t (a * b) -> option b
--- a/lib/ur/top.ur	Sat Nov 20 09:48:03 2010 -0500
+++ b/lib/ur/top.ur	Sat Nov 20 10:45:22 2010 -0500
@@ -228,6 +228,11 @@
     (fn r ls => return (r :: ls))
     []
 
+fun queryL1 [t ::: Name] [fs ::: {Type}] (q : sql_query [] [t = fs] []) =
+    query q
+    (fn r ls => return (r.t :: ls))
+    []
+
 fun queryI [tables ::: {{Type}}] [exps ::: {Type}]
            [tables ~ exps] (q : sql_query [] tables exps)
            (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
--- a/lib/ur/top.urs	Sat Nov 20 09:48:03 2010 -0500
+++ b/lib/ur/top.urs	Sat Nov 20 10:45:22 2010 -0500
@@ -129,6 +129,10 @@
                   sql_query [] tables exps
                   -> transaction (list $(exps ++ map (fn fields :: {Type} => $fields) tables))
 
+val queryL1 : t ::: Name -> fs ::: {Type}
+              -> sql_query [] [t = fs] []
+              -> transaction (list $fs)
+
 val query1 : t ::: Name -> fs ::: {Type} -> state ::: Type
              -> sql_query [] [t = fs] []
              -> ($fs -> state -> transaction state)