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@564
|
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@418
|
97 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
|
adamc@418
|
98 (f : nm :: Name -> rest :: {Unit}
|
adamc@629
|
99 -> [[nm] ~ rest] =>
|
adamc@418
|
100 tf1 -> tf2 -> xml ctx [] []) =
|
adamc@418
|
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@623
|
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@356
|
130 tf t -> xml ctx [] []) =
|
adamc@623
|
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@623
|
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@356
|
139 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@623
|
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@356
|
146 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@632
|
147 [tables ~ exps] (q : sql_query tables exps)
|
adamc@621
|
148 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@356
|
149 -> xml ctx [] []) =
|
adamc@356
|
150 query q
|
adamc@356
|
151 (fn fs acc => return <xml>{acc}{f fs}</xml>)
|
adamc@360
|
152 <xml/>
|
adamc@341
|
153
|
adamc@469
|
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@469
|
157 -> transaction (xml ctx [] [])) =
|
adamc@469
|
158 query q
|
adamc@469
|
159 (fn fs acc =>
|
adamc@469
|
160 r <- f fs;
|
adamc@469
|
161 return <xml>{acc}{r}</xml>)
|
adamc@469
|
162 <xml/>
|
adamc@469
|
163
|
adamc@356
|
164 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@629
|
165 [tables ~ exps]
|
adamc@629
|
166 (q : sql_query tables exps) =
|
adamc@356
|
167 query q
|
adamc@356
|
168 (fn fs _ => return (Some fs))
|
adamc@356
|
169 None
|
adamc@440
|
170
|
adamc@440
|
171 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@629
|
172 [tables ~ exps] (q : sql_query tables exps) =
|
adamc@440
|
173 o <- oneOrNoRows q;
|
adamc@440
|
174 return (case o of
|
adamc@440
|
175 None => error <xml>Query returned no rows</xml>
|
adamc@440
|
176 | Some r => r)
|
adamc@440
|
177
|
adamc@470
|
178 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@470
|
179 (t ::: Type) (_ : sql_injectable (option t))
|
adamc@470
|
180 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
181 (e2 : sql_exp tables agg exps (option t)) =
|
adamc@471
|
182 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
|
adamc@470
|
183
|
adamc@470
|
184 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@564
|
185 (t ::: Type) (_ : sql_injectable (option t))
|
adamc@470
|
186 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
187 (e2 : option t) =
|
adamc@470
|
188 case e2 of
|
adamc@471
|
189 None => (SQL {e1} IS NULL)
|
adamc@564
|
190 | Some _ => sql_binary sql_eq e1 (sql_inject e2)
|