adamc@422
|
1 fun not b = if b then False else True
|
adamc@422
|
2
|
adamc@356
|
3 con idT (t :: Type) = t
|
adamc@356
|
4 con record (t :: {Type}) = $t
|
adamc@356
|
5 con fstTT (t :: (Type * Type)) = t.1
|
adamc@356
|
6 con sndTT (t :: (Type * Type)) = t.2
|
adamc@445
|
7 con fstTTT (t :: (Type * Type * Type)) = t.1
|
adamc@445
|
8 con sndTTT (t :: (Type * Type * Type)) = t.2
|
adamc@445
|
9 con thdTTT (t :: (Type * Type * Type)) = t.3
|
adamc@329
|
10
|
adamc@621
|
11 con mapUT = fn f :: Type => map (fn _ :: Unit => f)
|
adamc@445
|
12
|
adamc@339
|
13 con ex = fn tf :: (Type -> Type) =>
|
adamc@356
|
14 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
|
adamc@339
|
15
|
adamc@339
|
16 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
|
adamc@356
|
17 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
|
adamc@356
|
18 f [choice] body
|
adamc@339
|
19
|
adamc@356
|
20 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
|
adamc@356
|
21 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
|
adamc@325
|
22
|
adamc@564
|
23 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) =
|
adamc@564
|
24 cdata (show v)
|
adamc@328
|
25
|
adamc@411
|
26 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
|
adamc@411
|
27 (f : nm :: Name -> rest :: {Unit}
|
adamc@411
|
28 -> fn [[nm] ~ rest] =>
|
adamc@411
|
29 tf -> tr rest -> tr ([nm] ++ rest))
|
adamc@411
|
30 (i : tr []) =
|
adamc@411
|
31 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
|
adamc@412
|
32 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@412
|
33 [[nm] ~ rest] r =>
|
adamc@411
|
34 f [nm] [rest] r.nm (acc (r -- nm)))
|
adamc@411
|
35 (fn _ => i)
|
adamc@411
|
36
|
adamc@418
|
37 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
|
adamc@418
|
38 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
39 -> fn [[nm] ~ rest] =>
|
adamc@418
|
40 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
|
adamc@418
|
41 (i : tr []) =
|
adamc@418
|
42 fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
|
adamc@418
|
43 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@418
|
44 [[nm] ~ rest] r1 r2 =>
|
adamc@418
|
45 f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@418
|
46 (fn _ _ => i)
|
adamc@418
|
47
|
adamc@418
|
48 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
|
adamc@418
|
49 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
50 -> fn [[nm] ~ rest] =>
|
adamc@418
|
51 tf1 -> tf2 -> xml ctx [] []) =
|
adamc@418
|
52 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@418
|
53 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
|
adamc@418
|
54 <xml>{f [nm] [rest] v1 v2}{acc}</xml>)
|
adamc@418
|
55 <xml/>
|
adamc@418
|
56
|
adamc@336
|
57 fun foldTR (tf :: Type -> Type) (tr :: {Type} -> Type)
|
adamc@356
|
58 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
59 -> fn [[nm] ~ rest] =>
|
adamc@356
|
60 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@356
|
61 (i : tr []) =
|
adamc@621
|
62 fold [fn r :: {Type} => $(map tf r) -> tr r]
|
adamc@356
|
63 (fn (nm :: Name) (t :: Type) (rest :: {Type}) (acc : _ -> tr rest)
|
adamc@356
|
64 [[nm] ~ rest] r =>
|
adamc@356
|
65 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@356
|
66 (fn _ => i)
|
adamc@336
|
67
|
adamc@339
|
68 fun foldT2R (tf :: (Type * Type) -> Type) (tr :: {(Type * Type)} -> Type)
|
adamc@367
|
69 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@367
|
70 -> fn [[nm] ~ rest] =>
|
adamc@367
|
71 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@367
|
72 (i : tr []) =
|
adamc@621
|
73 fold [fn r :: {(Type * Type)} => $(map tf r) -> tr r]
|
adamc@356
|
74 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
75 (acc : _ -> tr rest) [[nm] ~ rest] r =>
|
adamc@356
|
76 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@356
|
77 (fn _ => i)
|
adamc@339
|
78
|
adamc@445
|
79 fun foldT3R (tf :: (Type * Type * Type) -> Type) (tr :: {(Type * Type * Type)} -> Type)
|
adamc@445
|
80 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
|
adamc@445
|
81 -> fn [[nm] ~ rest] =>
|
adamc@445
|
82 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@445
|
83 (i : tr []) =
|
adamc@621
|
84 fold [fn r :: {(Type * Type * Type)} => $(map tf r) -> tr r]
|
adamc@445
|
85 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
|
adamc@445
|
86 (acc : _ -> tr rest) [[nm] ~ rest] r =>
|
adamc@445
|
87 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@445
|
88 (fn _ => i)
|
adamc@445
|
89
|
adamc@330
|
90 fun foldTR2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (tr :: {Type} -> Type)
|
adamc@367
|
91 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@367
|
92 -> fn [[nm] ~ rest] =>
|
adamc@367
|
93 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@367
|
94 (i : tr []) =
|
adamc@621
|
95 fold [fn r :: {Type} => $(map tf1 r) -> $(map tf2 r) -> tr r]
|
adamc@356
|
96 (fn (nm :: Name) (t :: Type) (rest :: {Type})
|
adamc@356
|
97 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@356
|
98 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@356
|
99 (fn _ _ => i)
|
adamc@332
|
100
|
adamc@356
|
101 fun foldT2R2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
|
adamc@356
|
102 (tr :: {(Type * Type)} -> Type)
|
adamc@356
|
103 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
104 -> fn [[nm] ~ rest] =>
|
adamc@356
|
105 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@356
|
106 (i : tr []) =
|
adamc@621
|
107 fold [fn r :: {(Type * Type)} => $(map tf1 r) -> $(map tf2 r) -> tr r]
|
adamc@356
|
108 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
109 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@356
|
110 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@356
|
111 (fn _ _ => i)
|
adamc@339
|
112
|
adamc@445
|
113 fun foldT3R2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
|
adamc@445
|
114 (tr :: {(Type * Type * Type)} -> Type)
|
adamc@445
|
115 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
|
adamc@445
|
116 -> fn [[nm] ~ rest] =>
|
adamc@445
|
117 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@445
|
118 (i : tr []) =
|
adamc@621
|
119 fold [fn r :: {(Type * Type * Type)} => $(map tf1 r) -> $(map tf2 r) -> tr r]
|
adamc@445
|
120 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
|
adamc@445
|
121 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@445
|
122 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@445
|
123 (fn _ _ => i)
|
adamc@445
|
124
|
adamc@336
|
125 fun foldTRX (tf :: Type -> Type) (ctx :: {Unit})
|
adamc@356
|
126 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
127 -> fn [[nm] ~ rest] =>
|
adamc@356
|
128 tf t -> xml ctx [] []) =
|
adamc@356
|
129 foldTR [tf] [fn _ => xml ctx [] []]
|
adamc@356
|
130 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest] r acc =>
|
adamc@356
|
131 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@360
|
132 <xml/>
|
adamc@336
|
133
|
adamc@339
|
134 fun foldT2RX (tf :: (Type * Type) -> Type) (ctx :: {Unit})
|
adamc@356
|
135 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
136 -> fn [[nm] ~ rest] =>
|
adamc@356
|
137 tf t -> xml ctx [] []) =
|
adamc@356
|
138 foldT2R [tf] [fn _ => xml ctx [] []]
|
adamc@356
|
139 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
140 [[nm] ~ rest] r acc =>
|
adamc@356
|
141 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@360
|
142 <xml/>
|
adamc@339
|
143
|
adamc@445
|
144 fun foldT3RX (tf :: (Type * Type * Type) -> Type) (ctx :: {Unit})
|
adamc@445
|
145 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
|
adamc@445
|
146 -> fn [[nm] ~ rest] =>
|
adamc@445
|
147 tf t -> xml ctx [] []) =
|
adamc@445
|
148 foldT3R [tf] [fn _ => xml ctx [] []]
|
adamc@445
|
149 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
|
adamc@445
|
150 [[nm] ~ rest] r acc =>
|
adamc@445
|
151 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@445
|
152 <xml/>
|
adamc@445
|
153
|
adamc@332
|
154 fun foldTRX2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (ctx :: {Unit})
|
adamc@356
|
155 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
156 -> fn [[nm] ~ rest] =>
|
adamc@356
|
157 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@356
|
158 foldTR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@356
|
159 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest]
|
adamc@356
|
160 r1 r2 acc =>
|
adamc@356
|
161 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@360
|
162 <xml/>
|
adamc@334
|
163
|
adamc@356
|
164 fun foldT2RX2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
|
adamc@356
|
165 (ctx :: {Unit})
|
adamc@356
|
166 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
167 -> fn [[nm] ~ rest] =>
|
adamc@356
|
168 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@356
|
169 foldT2R2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@356
|
170 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
171 [[nm] ~ rest] r1 r2 acc =>
|
adamc@356
|
172 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@360
|
173 <xml/>
|
adamc@339
|
174
|
adamc@445
|
175 fun foldT3RX2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
|
adamc@445
|
176 (ctx :: {Unit})
|
adamc@445
|
177 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
|
adamc@445
|
178 -> fn [[nm] ~ rest] =>
|
adamc@445
|
179 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@445
|
180 foldT3R2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@445
|
181 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
|
adamc@445
|
182 [[nm] ~ rest] r1 r2 acc =>
|
adamc@445
|
183 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@445
|
184 <xml/>
|
adamc@445
|
185
|
adamc@356
|
186 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@356
|
187 (q : sql_query tables exps) [tables ~ exps]
|
adamc@621
|
188 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@356
|
189 -> xml ctx [] []) =
|
adamc@356
|
190 query q
|
adamc@356
|
191 (fn fs acc => return <xml>{acc}{f fs}</xml>)
|
adamc@360
|
192 <xml/>
|
adamc@341
|
193
|
adamc@469
|
194 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@469
|
195 (q : sql_query tables exps) [tables ~ exps]
|
adamc@621
|
196 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@469
|
197 -> transaction (xml ctx [] [])) =
|
adamc@469
|
198 query q
|
adamc@469
|
199 (fn fs acc =>
|
adamc@469
|
200 r <- f fs;
|
adamc@469
|
201 return <xml>{acc}{r}</xml>)
|
adamc@469
|
202 <xml/>
|
adamc@469
|
203
|
adamc@356
|
204 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@356
|
205 (q : sql_query tables exps) [tables ~ exps] =
|
adamc@356
|
206 query q
|
adamc@356
|
207 (fn fs _ => return (Some fs))
|
adamc@356
|
208 None
|
adamc@440
|
209
|
adamc@440
|
210 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@440
|
211 (q : sql_query tables exps) [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@470
|
217 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@470
|
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@470
|
223 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@564
|
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)
|