comparison lib/ur/top.ur @ 602:1d34d916c206

Combine lib* directories
author Adam Chlipala <adamc@hcoop.net>
date Tue, 13 Jan 2009 15:23:48 -0500
parents lib/top.ur@803b2f3bb86b
children 8998114760c1
comparison
equal deleted inserted replaced
601:7c3c21eb5b4c 602:1d34d916c206
1 fun not b = if b then False else True
2
3 con idT (t :: Type) = t
4 con record (t :: {Type}) = $t
5 con fstTT (t :: (Type * Type)) = t.1
6 con sndTT (t :: (Type * Type)) = t.2
7 con fstTTT (t :: (Type * Type * Type)) = t.1
8 con sndTTT (t :: (Type * Type * Type)) = t.2
9 con thdTTT (t :: (Type * Type * Type)) = t.3
10
11 con mapTT (f :: Type -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
12 [nm = f t] ++ acc) []
13
14 con mapUT = fn f :: Type => fold (fn nm t acc [[nm] ~ acc] =>
15 [nm = f] ++ acc) []
16
17 con mapT2T (f :: (Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
18 [nm = f t] ++ acc) []
19
20 con mapT3T (f :: (Type * Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
21 [nm = f t] ++ acc) []
22
23 con ex = fn tf :: (Type -> Type) =>
24 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
25
26 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
27 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
28 f [choice] body
29
30 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
31 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
32
33 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) =
34 cdata (show v)
35
36 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
37 (f : nm :: Name -> rest :: {Unit}
38 -> fn [[nm] ~ rest] =>
39 tf -> tr rest -> tr ([nm] ++ rest))
40 (i : tr []) =
41 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
42 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
43 [[nm] ~ rest] r =>
44 f [nm] [rest] r.nm (acc (r -- nm)))
45 (fn _ => i)
46
47 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
48 (f : nm :: Name -> rest :: {Unit}
49 -> fn [[nm] ~ rest] =>
50 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
51 (i : tr []) =
52 fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
53 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
54 [[nm] ~ rest] r1 r2 =>
55 f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
56 (fn _ _ => i)
57
58 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
59 (f : nm :: Name -> rest :: {Unit}
60 -> fn [[nm] ~ rest] =>
61 tf1 -> tf2 -> xml ctx [] []) =
62 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
63 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
64 <xml>{f [nm] [rest] v1 v2}{acc}</xml>)
65 <xml/>
66
67 fun foldTR (tf :: Type -> Type) (tr :: {Type} -> Type)
68 (f : nm :: Name -> t :: Type -> rest :: {Type}
69 -> fn [[nm] ~ rest] =>
70 tf t -> tr rest -> tr ([nm = t] ++ rest))
71 (i : tr []) =
72 fold [fn r :: {Type} => $(mapTT tf r) -> tr r]
73 (fn (nm :: Name) (t :: Type) (rest :: {Type}) (acc : _ -> tr rest)
74 [[nm] ~ rest] r =>
75 f [nm] [t] [rest] r.nm (acc (r -- nm)))
76 (fn _ => i)
77
78 fun foldT2R (tf :: (Type * Type) -> Type) (tr :: {(Type * Type)} -> Type)
79 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
80 -> fn [[nm] ~ rest] =>
81 tf t -> tr rest -> tr ([nm = t] ++ rest))
82 (i : tr []) =
83 fold [fn r :: {(Type * Type)} => $(mapT2T tf r) -> tr r]
84 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
85 (acc : _ -> tr rest) [[nm] ~ rest] r =>
86 f [nm] [t] [rest] r.nm (acc (r -- nm)))
87 (fn _ => i)
88
89 fun foldT3R (tf :: (Type * Type * Type) -> Type) (tr :: {(Type * Type * Type)} -> Type)
90 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
91 -> fn [[nm] ~ rest] =>
92 tf t -> tr rest -> tr ([nm = t] ++ rest))
93 (i : tr []) =
94 fold [fn r :: {(Type * Type * Type)} => $(mapT3T tf r) -> tr r]
95 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
96 (acc : _ -> tr rest) [[nm] ~ rest] r =>
97 f [nm] [t] [rest] r.nm (acc (r -- nm)))
98 (fn _ => i)
99
100 fun foldTR2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (tr :: {Type} -> Type)
101 (f : nm :: Name -> t :: Type -> rest :: {Type}
102 -> fn [[nm] ~ rest] =>
103 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
104 (i : tr []) =
105 fold [fn r :: {Type} => $(mapTT tf1 r) -> $(mapTT tf2 r) -> tr r]
106 (fn (nm :: Name) (t :: Type) (rest :: {Type})
107 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
108 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
109 (fn _ _ => i)
110
111 fun foldT2R2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
112 (tr :: {(Type * Type)} -> Type)
113 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
114 -> fn [[nm] ~ rest] =>
115 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
116 (i : tr []) =
117 fold [fn r :: {(Type * Type)} => $(mapT2T tf1 r) -> $(mapT2T tf2 r) -> tr r]
118 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
119 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
120 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
121 (fn _ _ => i)
122
123 fun foldT3R2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
124 (tr :: {(Type * Type * Type)} -> Type)
125 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
126 -> fn [[nm] ~ rest] =>
127 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
128 (i : tr []) =
129 fold [fn r :: {(Type * Type * Type)} => $(mapT3T tf1 r) -> $(mapT3T tf2 r) -> tr r]
130 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
131 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
132 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
133 (fn _ _ => i)
134
135 fun foldTRX (tf :: Type -> Type) (ctx :: {Unit})
136 (f : nm :: Name -> t :: Type -> rest :: {Type}
137 -> fn [[nm] ~ rest] =>
138 tf t -> xml ctx [] []) =
139 foldTR [tf] [fn _ => xml ctx [] []]
140 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest] r acc =>
141 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
142 <xml/>
143
144 fun foldT2RX (tf :: (Type * Type) -> Type) (ctx :: {Unit})
145 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
146 -> fn [[nm] ~ rest] =>
147 tf t -> xml ctx [] []) =
148 foldT2R [tf] [fn _ => xml ctx [] []]
149 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
150 [[nm] ~ rest] r acc =>
151 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
152 <xml/>
153
154 fun foldT3RX (tf :: (Type * Type * Type) -> Type) (ctx :: {Unit})
155 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
156 -> fn [[nm] ~ rest] =>
157 tf t -> xml ctx [] []) =
158 foldT3R [tf] [fn _ => xml ctx [] []]
159 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
160 [[nm] ~ rest] r acc =>
161 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
162 <xml/>
163
164 fun foldTRX2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (ctx :: {Unit})
165 (f : nm :: Name -> t :: Type -> rest :: {Type}
166 -> fn [[nm] ~ rest] =>
167 tf1 t -> tf2 t -> xml ctx [] []) =
168 foldTR2 [tf1] [tf2] [fn _ => xml ctx [] []]
169 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest]
170 r1 r2 acc =>
171 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
172 <xml/>
173
174 fun foldT2RX2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
175 (ctx :: {Unit})
176 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
177 -> fn [[nm] ~ rest] =>
178 tf1 t -> tf2 t -> xml ctx [] []) =
179 foldT2R2 [tf1] [tf2] [fn _ => xml ctx [] []]
180 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
181 [[nm] ~ rest] r1 r2 acc =>
182 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
183 <xml/>
184
185 fun foldT3RX2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
186 (ctx :: {Unit})
187 (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
188 -> fn [[nm] ~ rest] =>
189 tf1 t -> tf2 t -> xml ctx [] []) =
190 foldT3R2 [tf1] [tf2] [fn _ => xml ctx [] []]
191 (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
192 [[nm] ~ rest] r1 r2 acc =>
193 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
194 <xml/>
195
196 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
197 (q : sql_query tables exps) [tables ~ exps]
198 (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
199 [nm = $fields] ++ acc) [] tables)
200 -> xml ctx [] []) =
201 query q
202 (fn fs acc => return <xml>{acc}{f fs}</xml>)
203 <xml/>
204
205 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
206 (q : sql_query tables exps) [tables ~ exps]
207 (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
208 [nm = $fields] ++ acc) [] tables)
209 -> transaction (xml ctx [] [])) =
210 query q
211 (fn fs acc =>
212 r <- f fs;
213 return <xml>{acc}{r}</xml>)
214 <xml/>
215
216 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
217 (q : sql_query tables exps) [tables ~ exps] =
218 query q
219 (fn fs _ => return (Some fs))
220 None
221
222 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
223 (q : sql_query tables exps) [tables ~ exps] =
224 o <- oneOrNoRows q;
225 return (case o of
226 None => error <xml>Query returned no rows</xml>
227 | Some r => r)
228
229 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
230 (t ::: Type) (_ : sql_injectable (option t))
231 (e1 : sql_exp tables agg exps (option t))
232 (e2 : sql_exp tables agg exps (option t)) =
233 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
234
235 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
236 (t ::: Type) (_ : sql_injectable (option t))
237 (e1 : sql_exp tables agg exps (option t))
238 (e2 : option t) =
239 case e2 of
240 None => (SQL {e1} IS NULL)
241 | Some _ => sql_binary sql_eq e1 (sql_inject e2)