annotate lib/ur/top.ur @ 742:43553c93dd8c

Reading blobs from the database
author Adam Chlipala <adamc@hcoop.net>
date Sun, 26 Apr 2009 10:45:59 -0400
parents acb8537f58f0
children 87a7702d681d
rev   line source
adamc@623 1 (** Row folding *)
adamc@623 2
adamc@623 3 con folder = K ==> fn r :: {K} =>
adamc@623 4 tf :: ({K} -> Type)
adamc@653 5 -> (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
adamc@653 6 tf r -> tf ([nm = v] ++ r))
adamc@623 7 -> tf [] -> tf r
adamc@623 8
adamc@653 9 fun fold K (tf :: {K} -> Type)
adamc@653 10 (f : (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
adamc@653 11 tf r -> tf ([nm = v] ++ r)))
adamc@653 12 (i : tf []) (r :: {K}) (fl : folder r) = fl [tf] f i
adamc@653 13
adamc@627 14 structure Folder = struct
adamc@627 15 fun nil K (tf :: {K} -> Type)
adamc@653 16 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
adamc@653 17 tf r -> tf ([nm = v] ++ r))
adamc@627 18 (i : tf []) = i
adamc@627 19
adamc@627 20 fun cons K (r ::: {K}) (nm :: Name) (v :: K) [[nm] ~ r] (fold : folder r)
adamc@627 21 (tf :: {K} -> Type)
adamc@653 22 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
adamc@653 23 tf r -> tf ([nm = v] ++ r))
adamc@653 24 (i : tf []) = f [nm] [v] [r] ! (fold [tf] f i)
adamc@628 25
adamc@628 26 fun concat K (r1 ::: {K}) (r2 ::: {K}) [r1 ~ r2]
adamc@628 27 (f1 : folder r1) (f2 : folder r2)
adamc@628 28 (tf :: {K} -> Type)
adamc@653 29 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
adamc@653 30 tf r -> tf ([nm = v] ++ r))
adamc@628 31 (i : tf []) =
adamc@629 32 f1 [fn r1' => [r1' ~ r2] => tf (r1' ++ r2)]
adamc@653 33 (fn (nm :: Name) (v :: K) (r1' :: {K}) [[nm] ~ r1']
adamc@653 34 (acc : [r1' ~ r2] => tf (r1' ++ r2))
adamc@653 35 [[nm = v] ++ r1' ~ r2] =>
adamc@653 36 f [nm] [v] [r1' ++ r2] ! acc)
adamc@629 37 (fn [[] ~ r2] => f2 [tf] f i) !
adamc@630 38
adamc@630 39 fun mp K1 K2 (f ::: K1 -> K2) (r ::: {K1})
adamc@630 40 (fold : folder r)
adamc@630 41 (tf :: {K2} -> Type)
adamc@653 42 (f : nm :: Name -> v :: K2 -> r :: {K2} -> [[nm] ~ r] =>
adamc@653 43 tf r -> tf ([nm = v] ++ r))
adamc@630 44 (i : tf []) =
adamc@630 45 fold [fn r => tf (map f r)]
adamc@653 46 (fn (nm :: Name) (v :: K1) (rest :: {K1}) [[nm] ~ rest] (acc : tf (map f rest)) =>
adamc@653 47 f [nm] [f v] [map f rest] ! acc)
adamc@630 48 i
adamc@627 49 end
adamc@627 50
adamc@623 51
adamc@422 52 fun not b = if b then False else True
adamc@422 53
adamc@356 54 con idT (t :: Type) = t
adamc@356 55 con record (t :: {Type}) = $t
adamc@637 56 con fst = K1 ==> K2 ==> fn t :: (K1 * K2) => t.1
adamc@637 57 con snd = K1 ==> K2 ==> fn t :: (K1 * K2) => t.2
adamc@637 58 con fst3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.1
adamc@637 59 con snd3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.2
adamc@637 60 con thd3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.3
adamc@329 61
adamc@643 62 con mapU = K ==> fn f :: K => map (fn _ :: Unit => f)
adamc@445 63
adamc@339 64 con ex = fn tf :: (Type -> Type) =>
adamc@356 65 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
adamc@339 66
adamc@339 67 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
adamc@356 68 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
adamc@356 69 f [choice] body
adamc@339 70
adamc@356 71 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
adamc@356 72 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
adamc@325 73
adamc@720 74 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) =
adamc@564 75 cdata (show v)
adamc@328 76
adamc@411 77 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
adamc@411 78 (f : nm :: Name -> rest :: {Unit}
adamc@629 79 -> [[nm] ~ rest] =>
adamc@411 80 tf -> tr rest -> tr ([nm] ++ rest))
adamc@653 81 (i : tr []) (r :: {Unit}) (fl : folder r)=
adamc@653 82 fl [fn r :: {Unit} => $(mapU tf r) -> tr r]
adamc@653 83 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r =>
adamc@653 84 f [nm] [rest] ! r.nm (acc (r -- nm)))
adamc@653 85 (fn _ => i)
adamc@411 86
adamc@418 87 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
adamc@418 88 (f : nm :: Name -> rest :: {Unit}
adamc@629 89 -> [[nm] ~ rest] =>
adamc@418 90 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
adamc@653 91 (i : tr []) (r :: {Unit}) (fl : folder r) =
adamc@653 92 fl [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r]
adamc@653 93 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r1 r2 =>
adamc@653 94 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@653 95 (fn _ _ => i)
adamc@418 96
adamc@720 97 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
adamc@418 98 (f : nm :: Name -> rest :: {Unit}
adamc@629 99 -> [[nm] ~ rest] =>
adamc@720 100 tf1 -> tf2 -> xml ctx [] []) =
adamc@720 101 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@418 102 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
adamc@629 103 <xml>{f [nm] [rest] ! v1 v2}{acc}</xml>)
adamc@418 104 <xml/>
adamc@418 105
adamc@623 106 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type)
adamc@623 107 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@629 108 -> [[nm] ~ rest] =>
adamc@356 109 tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@653 110 (i : tr []) (r :: {K}) (fl : folder r) =
adamc@653 111 fl [fn r :: {K} => $(map tf r) -> tr r]
adamc@653 112 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] (acc : _ -> tr rest) r =>
adamc@653 113 f [nm] [t] [rest] ! r.nm (acc (r -- nm)))
adamc@653 114 (fn _ => i)
adamc@336 115
adamc@623 116 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type)
adamc@623 117 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@629 118 -> [[nm] ~ rest] =>
adamc@367 119 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@653 120 (i : tr []) (r :: {K}) (fl : folder r) =
adamc@653 121 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
adamc@653 122 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest]
adamc@653 123 (acc : _ -> _ -> tr rest) r1 r2 =>
adamc@653 124 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@653 125 (fn _ _ => i)
adamc@332 126
adamc@720 127 fun foldRX K (tf :: K -> Type) (ctx :: {Unit})
adamc@623 128 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@629 129 -> [[nm] ~ rest] =>
adamc@720 130 tf t -> xml ctx [] []) =
adamc@720 131 foldR [tf] [fn _ => xml ctx [] []]
adamc@623 132 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] r acc =>
adamc@629 133 <xml>{f [nm] [t] [rest] ! r}{acc}</xml>)
adamc@623 134 <xml/>
adamc@336 135
adamc@720 136 fun foldRX2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (ctx :: {Unit})
adamc@623 137 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@629 138 -> [[nm] ~ rest] =>
adamc@720 139 tf1 t -> tf2 t -> xml ctx [] []) =
adamc@720 140 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@623 141 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest]
adamc@623 142 r1 r2 acc =>
adamc@629 143 <xml>{f [nm] [t] [rest] ! r1 r2}{acc}</xml>)
adamc@623 144 <xml/>
adamc@445 145
adamc@682 146 fun queryI (tables ::: {{Type}}) (exps ::: {Type})
adamc@682 147 [tables ~ exps] (q : sql_query tables exps)
adamc@682 148 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
adamc@682 149 -> transaction unit) =
adamc@682 150 query q
adamc@682 151 (fn fs _ => f fs)
adamc@682 152 ()
adamc@682 153
adamc@720 154 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@632 155 [tables ~ exps] (q : sql_query tables exps)
adamc@621 156 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
adamc@720 157 -> xml ctx [] []) =
adamc@356 158 query q
adamc@356 159 (fn fs acc => return <xml>{acc}{f fs}</xml>)
adamc@360 160 <xml/>
adamc@341 161
adamc@720 162 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@632 163 [tables ~ exps] (q : sql_query tables exps)
adamc@621 164 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
adamc@720 165 -> transaction (xml ctx [] [])) =
adamc@469 166 query q
adamc@469 167 (fn fs acc =>
adamc@469 168 r <- f fs;
adamc@469 169 return <xml>{acc}{r}</xml>)
adamc@469 170 <xml/>
adamc@469 171
adamc@356 172 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
adamc@629 173 [tables ~ exps]
adamc@629 174 (q : sql_query tables exps) =
adamc@356 175 query q
adamc@356 176 (fn fs _ => return (Some fs))
adamc@356 177 None
adamc@440 178
adamc@440 179 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
adamc@629 180 [tables ~ exps] (q : sql_query tables exps) =
adamc@440 181 o <- oneOrNoRows q;
adamc@440 182 return (case o of
adamc@440 183 None => error <xml>Query returned no rows</xml>
adamc@440 184 | Some r => r)
adamc@440 185
adamc@470 186 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@470 187 (t ::: Type) (_ : sql_injectable (option t))
adamc@470 188 (e1 : sql_exp tables agg exps (option t))
adamc@470 189 (e2 : sql_exp tables agg exps (option t)) =
adamc@471 190 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
adamc@470 191
adamc@470 192 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@564 193 (t ::: Type) (_ : sql_injectable (option t))
adamc@470 194 (e1 : sql_exp tables agg exps (option t))
adamc@470 195 (e2 : option t) =
adamc@470 196 case e2 of
adamc@471 197 None => (SQL {e1} IS NULL)
adamc@564 198 | Some _ => sql_binary sql_eq e1 (sql_inject e2)