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@822
|
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@822
|
12 (i : tf []) [r :: {K}] (fl : folder r) = fl [tf] f i
|
adamc@653
|
13
|
adamc@627
|
14 structure Folder = struct
|
adamc@822
|
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@822
|
20 fun cons [K] [r ::: {K}] [nm :: Name] [v :: K] [[nm] ~ r] (fold : folder r)
|
adamc@822
|
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@822
|
26 fun concat [K] [r1 ::: {K}] [r2 ::: {K}] [r1 ~ r2]
|
adamc@628
|
27 (f1 : folder r1) (f2 : folder r2)
|
adamc@822
|
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@822
|
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@822
|
39 fun mp [K1] [K2] [f ::: K1 -> K2] [r ::: {K1}]
|
adamc@630
|
40 (fold : folder r)
|
adamc@822
|
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@822
|
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@898
|
54 con id = K ==> fn t :: K => 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@822
|
67 fun ex [tf :: (Type -> Type)] [choice :: Type] (body : tf choice) : ex tf =
|
adamc@822
|
68 fn [res] (f : choice :: Type -> tf choice -> res) =>
|
adamc@356
|
69 f [choice] body
|
adamc@339
|
70
|
adamc@822
|
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@822
|
74 fun show_option [t ::: Type] (_ : show t) =
|
adamc@777
|
75 mkShow (fn opt : option t =>
|
adamc@777
|
76 case opt of
|
adamc@777
|
77 None => ""
|
adamc@777
|
78 | Some x => show x)
|
adamc@777
|
79
|
adamc@822
|
80 fun read_option [t ::: Type] (_ : read t) =
|
adamc@777
|
81 mkRead (fn s =>
|
adamc@777
|
82 case s of
|
adamc@777
|
83 "" => None
|
adamc@777
|
84 | _ => Some (readError s : t))
|
adamc@777
|
85 (fn s =>
|
adamc@777
|
86 case s of
|
adamc@777
|
87 "" => Some None
|
adamc@777
|
88 | _ => case read s of
|
adamc@777
|
89 None => None
|
adamc@777
|
90 | v => Some v)
|
adamc@777
|
91
|
adamc@822
|
92 fun txt [t] [ctx ::: {Unit}] [use ::: {Type}] (_ : show t) (v : t) =
|
adamc@564
|
93 cdata (show v)
|
adamc@328
|
94
|
adamc@898
|
95 fun mp [K] [tf1 :: K -> Type] [tf2 :: K -> Type] (f : t ::: K -> tf1 t -> tf2 t) [r :: {K}] (fl : folder r) =
|
adamc@898
|
96 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r)]
|
adamc@898
|
97 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] acc r =>
|
adamc@898
|
98 acc (r -- nm) ++ {nm = f r.nm})
|
adamc@898
|
99 (fn _ => {})
|
adamc@898
|
100
|
adamc@898
|
101 fun map2 [K1] [K2] [tf1 :: K1 -> Type] [tf2 :: K2 -> Type] [tf :: K1 -> K2]
|
adamc@898
|
102 (f : t ::: K1 -> tf1 t -> tf2 (tf t)) [r :: {K1}] (fl : folder r) =
|
adamc@898
|
103 fl [fn r :: {K1} => $(map tf1 r) -> $(map tf2 (map tf r))]
|
adamc@898
|
104 (fn [nm :: Name] [t :: K1] [rest :: {K1}] [[nm] ~ rest] acc r =>
|
adamc@898
|
105 acc (r -- nm) ++ {nm = f r.nm})
|
adamc@898
|
106 (fn _ => {})
|
adamc@898
|
107
|
adamc@822
|
108 fun foldUR [tf :: Type] [tr :: {Unit} -> Type]
|
adamc@411
|
109 (f : nm :: Name -> rest :: {Unit}
|
adamc@629
|
110 -> [[nm] ~ rest] =>
|
adamc@411
|
111 tf -> tr rest -> tr ([nm] ++ rest))
|
adamc@898
|
112 (i : tr []) [r :: {Unit}] (fl : folder r) =
|
adamc@653
|
113 fl [fn r :: {Unit} => $(mapU tf r) -> tr r]
|
adamc@822
|
114 (fn [nm :: Name] [t :: Unit] [rest :: {Unit}] [[nm] ~ rest] acc r =>
|
adamc@653
|
115 f [nm] [rest] ! r.nm (acc (r -- nm)))
|
adamc@653
|
116 (fn _ => i)
|
adamc@411
|
117
|
adamc@822
|
118 fun foldUR2 [tf1 :: Type] [tf2 :: Type] [tr :: {Unit} -> Type]
|
adamc@418
|
119 (f : nm :: Name -> rest :: {Unit}
|
adamc@629
|
120 -> [[nm] ~ rest] =>
|
adamc@418
|
121 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
|
adamc@822
|
122 (i : tr []) [r :: {Unit}] (fl : folder r) =
|
adamc@653
|
123 fl [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r]
|
adamc@822
|
124 (fn [nm :: Name] [t :: Unit] [rest :: {Unit}] [[nm] ~ rest] acc r1 r2 =>
|
adamc@653
|
125 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@653
|
126 (fn _ _ => i)
|
adamc@418
|
127
|
adamc@822
|
128 fun foldURX2 [tf1 :: Type] [tf2 :: Type] [ctx :: {Unit}]
|
adamc@418
|
129 (f : nm :: Name -> rest :: {Unit}
|
adamc@629
|
130 -> [[nm] ~ rest] =>
|
adamc@720
|
131 tf1 -> tf2 -> xml ctx [] []) =
|
adamc@720
|
132 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@822
|
133 (fn [nm :: Name] [rest :: {Unit}] [[nm] ~ rest] v1 v2 acc =>
|
adamc@629
|
134 <xml>{f [nm] [rest] ! v1 v2}{acc}</xml>)
|
adamc@418
|
135 <xml/>
|
adamc@418
|
136
|
adamc@822
|
137 fun foldR [K] [tf :: K -> Type] [tr :: {K} -> Type]
|
adamc@623
|
138 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@629
|
139 -> [[nm] ~ rest] =>
|
adamc@356
|
140 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@822
|
141 (i : tr []) [r :: {K}] (fl : folder r) =
|
adamc@653
|
142 fl [fn r :: {K} => $(map tf r) -> tr r]
|
adamc@822
|
143 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] (acc : _ -> tr rest) r =>
|
adamc@653
|
144 f [nm] [t] [rest] ! r.nm (acc (r -- nm)))
|
adamc@653
|
145 (fn _ => i)
|
adamc@336
|
146
|
adamc@822
|
147 fun foldR2 [K] [tf1 :: K -> Type] [tf2 :: K -> Type] [tr :: {K} -> Type]
|
adamc@623
|
148 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@629
|
149 -> [[nm] ~ rest] =>
|
adamc@367
|
150 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@822
|
151 (i : tr []) [r :: {K}] (fl : folder r) =
|
adamc@653
|
152 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
|
adamc@822
|
153 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
|
adamc@653
|
154 (acc : _ -> _ -> tr rest) r1 r2 =>
|
adamc@653
|
155 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@653
|
156 (fn _ _ => i)
|
adamc@332
|
157
|
adamc@822
|
158 fun foldRX [K] [tf :: K -> Type] [ctx :: {Unit}]
|
adamc@623
|
159 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@629
|
160 -> [[nm] ~ rest] =>
|
adamc@720
|
161 tf t -> xml ctx [] []) =
|
adamc@720
|
162 foldR [tf] [fn _ => xml ctx [] []]
|
adamc@822
|
163 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] r acc =>
|
adamc@629
|
164 <xml>{f [nm] [t] [rest] ! r}{acc}</xml>)
|
adamc@623
|
165 <xml/>
|
adamc@336
|
166
|
adamc@822
|
167 fun foldRX2 [K] [tf1 :: K -> Type] [tf2 :: K -> Type] [ctx :: {Unit}]
|
adamc@623
|
168 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@629
|
169 -> [[nm] ~ rest] =>
|
adamc@720
|
170 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@720
|
171 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@822
|
172 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
|
adamc@623
|
173 r1 r2 acc =>
|
adamc@629
|
174 <xml>{f [nm] [t] [rest] ! r1 r2}{acc}</xml>)
|
adamc@623
|
175 <xml/>
|
adamc@445
|
176
|
adamc@822
|
177 fun queryI [tables ::: {{Type}}] [exps ::: {Type}]
|
adamc@682
|
178 [tables ~ exps] (q : sql_query tables exps)
|
adamc@682
|
179 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@682
|
180 -> transaction unit) =
|
adamc@682
|
181 query q
|
adamc@682
|
182 (fn fs _ => f fs)
|
adamc@682
|
183 ()
|
adamc@682
|
184
|
adamc@822
|
185 fun queryX [tables ::: {{Type}}] [exps ::: {Type}] [ctx ::: {Unit}]
|
adamc@632
|
186 [tables ~ exps] (q : sql_query tables exps)
|
adamc@621
|
187 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@720
|
188 -> xml ctx [] []) =
|
adamc@356
|
189 query q
|
adamc@356
|
190 (fn fs acc => return <xml>{acc}{f fs}</xml>)
|
adamc@360
|
191 <xml/>
|
adamc@341
|
192
|
adamc@822
|
193 fun queryX' [tables ::: {{Type}}] [exps ::: {Type}] [ctx ::: {Unit}]
|
adamc@632
|
194 [tables ~ exps] (q : sql_query tables exps)
|
adamc@621
|
195 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@720
|
196 -> transaction (xml ctx [] [])) =
|
adamc@469
|
197 query q
|
adamc@469
|
198 (fn fs acc =>
|
adamc@469
|
199 r <- f fs;
|
adamc@469
|
200 return <xml>{acc}{r}</xml>)
|
adamc@469
|
201 <xml/>
|
adamc@469
|
202
|
adamc@822
|
203 fun oneOrNoRows [tables ::: {{Type}}] [exps ::: {Type}]
|
adamc@629
|
204 [tables ~ exps]
|
adamc@629
|
205 (q : sql_query tables exps) =
|
adamc@356
|
206 query q
|
adamc@356
|
207 (fn fs _ => return (Some fs))
|
adamc@356
|
208 None
|
adamc@440
|
209
|
adamc@822
|
210 fun oneRow [tables ::: {{Type}}] [exps ::: {Type}]
|
adamc@822
|
211 [tables ~ exps] (q : sql_query tables exps) =
|
adamc@440
|
212 o <- oneOrNoRows q;
|
adamc@440
|
213 return (case o of
|
adamc@440
|
214 None => error <xml>Query returned no rows</xml>
|
adamc@440
|
215 | Some r => r)
|
adamc@440
|
216
|
adamc@822
|
217 fun eqNullable [tables ::: {{Type}}] [agg ::: {{Type}}] [exps ::: {Type}]
|
adamc@822
|
218 [t ::: Type] (_ : sql_injectable (option t))
|
adamc@470
|
219 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
220 (e2 : sql_exp tables agg exps (option t)) =
|
adamc@471
|
221 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
|
adamc@470
|
222
|
adamc@822
|
223 fun eqNullable' [tables ::: {{Type}}] [agg ::: {{Type}}] [exps ::: {Type}]
|
adamc@822
|
224 [t ::: Type] (_ : sql_injectable (option t))
|
adamc@470
|
225 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
226 (e2 : option t) =
|
adamc@470
|
227 case e2 of
|
adamc@471
|
228 None => (SQL {e1} IS NULL)
|
adamc@564
|
229 | Some _ => sql_binary sql_eq e1 (sql_inject e2)
|