annotate lib/ur/top.ur @ 623:588b9d16b00a

Start of kind polymorphism, up to the point where demo/hello elaborates with updated Basis/Top
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Feb 2009 16:10:25 -0500
parents 8998114760c1
children f4f2b09a533a
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@623 5 -> (nm :: Name -> v :: K -> r :: {K} -> tf r
adamc@623 6 -> fn [[nm] ~ r] => tf ([nm = v] ++ r))
adamc@623 7 -> tf [] -> tf r
adamc@623 8
adamc@623 9
adamc@422 10 fun not b = if b then False else True
adamc@422 11
adamc@356 12 con idT (t :: Type) = t
adamc@356 13 con record (t :: {Type}) = $t
adamc@356 14 con fstTT (t :: (Type * Type)) = t.1
adamc@356 15 con sndTT (t :: (Type * Type)) = t.2
adamc@445 16 con fstTTT (t :: (Type * Type * Type)) = t.1
adamc@445 17 con sndTTT (t :: (Type * Type * Type)) = t.2
adamc@445 18 con thdTTT (t :: (Type * Type * Type)) = t.3
adamc@329 19
adamc@621 20 con mapUT = fn f :: Type => map (fn _ :: Unit => f)
adamc@445 21
adamc@339 22 con ex = fn tf :: (Type -> Type) =>
adamc@356 23 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
adamc@339 24
adamc@339 25 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
adamc@356 26 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
adamc@356 27 f [choice] body
adamc@339 28
adamc@356 29 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
adamc@356 30 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
adamc@325 31
adamc@564 32 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) =
adamc@564 33 cdata (show v)
adamc@328 34
adamc@411 35 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
adamc@411 36 (f : nm :: Name -> rest :: {Unit}
adamc@411 37 -> fn [[nm] ~ rest] =>
adamc@411 38 tf -> tr rest -> tr ([nm] ++ rest))
adamc@623 39 (i : tr []) (r ::: {Unit}) (fold : folder r)=
adamc@411 40 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
adamc@623 41 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
adamc@623 42 [[nm] ~ rest] r =>
adamc@623 43 f [nm] [rest] r.nm (acc (r -- nm)))
adamc@623 44 (fn _ => i)
adamc@411 45
adamc@418 46 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
adamc@418 47 (f : nm :: Name -> rest :: {Unit}
adamc@418 48 -> fn [[nm] ~ rest] =>
adamc@418 49 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
adamc@623 50 (i : tr []) (r ::: {Unit}) (fold : folder r) =
adamc@418 51 fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
adamc@623 52 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
adamc@623 53 [[nm] ~ rest] r1 r2 =>
adamc@623 54 f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@623 55 (fn _ _ => i)
adamc@418 56
adamc@418 57 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
adamc@418 58 (f : nm :: Name -> rest :: {Unit}
adamc@418 59 -> fn [[nm] ~ rest] =>
adamc@418 60 tf1 -> tf2 -> xml ctx [] []) =
adamc@418 61 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@418 62 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
adamc@418 63 <xml>{f [nm] [rest] v1 v2}{acc}</xml>)
adamc@418 64 <xml/>
adamc@418 65
adamc@623 66 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type)
adamc@623 67 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@356 68 -> fn [[nm] ~ rest] =>
adamc@356 69 tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@623 70 (i : tr []) (r ::: {K}) (fold : folder r) =
adamc@623 71 fold [fn r :: {K} => $(map tf r) -> tr r]
adamc@623 72 (fn (nm :: Name) (t :: K) (rest :: {K}) (acc : _ -> tr rest)
adamc@356 73 [[nm] ~ rest] r =>
adamc@356 74 f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@356 75 (fn _ => i)
adamc@336 76
adamc@623 77 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type)
adamc@623 78 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@367 79 -> fn [[nm] ~ rest] =>
adamc@367 80 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@623 81 (i : tr []) (r ::: {K}) (fold : folder r) =
adamc@623 82 fold [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
adamc@623 83 (fn (nm :: Name) (t :: K) (rest :: {K})
adamc@623 84 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@623 85 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@623 86 (fn _ _ => i)
adamc@332 87
adamc@623 88 fun foldRX K (tf :: K -> Type) (ctx :: {Unit})
adamc@623 89 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@356 90 -> fn [[nm] ~ rest] =>
adamc@356 91 tf t -> xml ctx [] []) =
adamc@623 92 foldR [tf] [fn _ => xml ctx [] []]
adamc@623 93 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] r acc =>
adamc@623 94 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
adamc@623 95 <xml/>
adamc@336 96
adamc@623 97 fun foldRX2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (ctx :: {Unit})
adamc@623 98 (f : nm :: Name -> t :: K -> rest :: {K}
adamc@356 99 -> fn [[nm] ~ rest] =>
adamc@356 100 tf1 t -> tf2 t -> xml ctx [] []) =
adamc@623 101 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@623 102 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest]
adamc@623 103 r1 r2 acc =>
adamc@623 104 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
adamc@623 105 <xml/>
adamc@445 106
adamc@356 107 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@356 108 (q : sql_query tables exps) [tables ~ exps]
adamc@621 109 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
adamc@356 110 -> xml ctx [] []) =
adamc@356 111 query q
adamc@356 112 (fn fs acc => return <xml>{acc}{f fs}</xml>)
adamc@360 113 <xml/>
adamc@341 114
adamc@469 115 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@469 116 (q : sql_query tables exps) [tables ~ exps]
adamc@621 117 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
adamc@469 118 -> transaction (xml ctx [] [])) =
adamc@469 119 query q
adamc@469 120 (fn fs acc =>
adamc@469 121 r <- f fs;
adamc@469 122 return <xml>{acc}{r}</xml>)
adamc@469 123 <xml/>
adamc@469 124
adamc@356 125 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
adamc@356 126 (q : sql_query tables exps) [tables ~ exps] =
adamc@356 127 query q
adamc@356 128 (fn fs _ => return (Some fs))
adamc@356 129 None
adamc@440 130
adamc@440 131 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
adamc@440 132 (q : sql_query tables exps) [tables ~ exps] =
adamc@440 133 o <- oneOrNoRows q;
adamc@440 134 return (case o of
adamc@440 135 None => error <xml>Query returned no rows</xml>
adamc@440 136 | Some r => r)
adamc@440 137
adamc@470 138 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@470 139 (t ::: Type) (_ : sql_injectable (option t))
adamc@470 140 (e1 : sql_exp tables agg exps (option t))
adamc@470 141 (e2 : sql_exp tables agg exps (option t)) =
adamc@471 142 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
adamc@470 143
adamc@470 144 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@564 145 (t ::: Type) (_ : sql_injectable (option t))
adamc@470 146 (e1 : sql_exp tables agg exps (option t))
adamc@470 147 (e2 : option t) =
adamc@470 148 case e2 of
adamc@471 149 None => (SQL {e1} IS NULL)
adamc@564 150 | Some _ => sql_binary sql_eq e1 (sql_inject e2)