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@329
|
7
|
adamc@356
|
8 con mapTT (f :: Type -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
|
adamc@356
|
9 [nm = f t] ++ acc) []
|
adamc@325
|
10
|
adamc@411
|
11 con mapUT = fn f :: Type => fold (fn nm t acc [[nm] ~ acc] =>
|
adamc@411
|
12 [nm = f] ++ acc) []
|
adamc@411
|
13
|
adamc@356
|
14 con mapT2T (f :: (Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
|
adamc@356
|
15 [nm = f t] ++ acc) []
|
adamc@339
|
16
|
adamc@339
|
17 con ex = fn tf :: (Type -> Type) =>
|
adamc@356
|
18 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
|
adamc@339
|
19
|
adamc@339
|
20 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
|
adamc@356
|
21 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
|
adamc@356
|
22 f [choice] body
|
adamc@339
|
23
|
adamc@356
|
24 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
|
adamc@356
|
25 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
|
adamc@325
|
26
|
adamc@356
|
27 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (sh : show t) (v : t) =
|
adamc@403
|
28 cdata (@show sh v)
|
adamc@328
|
29
|
adamc@411
|
30 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
|
adamc@411
|
31 (f : nm :: Name -> rest :: {Unit}
|
adamc@411
|
32 -> fn [[nm] ~ rest] =>
|
adamc@411
|
33 tf -> tr rest -> tr ([nm] ++ rest))
|
adamc@411
|
34 (i : tr []) =
|
adamc@411
|
35 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
|
adamc@412
|
36 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@412
|
37 [[nm] ~ rest] r =>
|
adamc@411
|
38 f [nm] [rest] r.nm (acc (r -- nm)))
|
adamc@411
|
39 (fn _ => i)
|
adamc@411
|
40
|
adamc@418
|
41 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
|
adamc@418
|
42 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
43 -> fn [[nm] ~ rest] =>
|
adamc@418
|
44 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
|
adamc@418
|
45 (i : tr []) =
|
adamc@418
|
46 fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
|
adamc@418
|
47 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@418
|
48 [[nm] ~ rest] r1 r2 =>
|
adamc@418
|
49 f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@418
|
50 (fn _ _ => i)
|
adamc@418
|
51
|
adamc@418
|
52 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
|
adamc@418
|
53 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
54 -> fn [[nm] ~ rest] =>
|
adamc@418
|
55 tf1 -> tf2 -> xml ctx [] []) =
|
adamc@418
|
56 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@418
|
57 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
|
adamc@418
|
58 <xml>{f [nm] [rest] v1 v2}{acc}</xml>)
|
adamc@418
|
59 <xml/>
|
adamc@418
|
60
|
adamc@336
|
61 fun foldTR (tf :: Type -> Type) (tr :: {Type} -> Type)
|
adamc@356
|
62 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
63 -> fn [[nm] ~ rest] =>
|
adamc@356
|
64 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@356
|
65 (i : tr []) =
|
adamc@356
|
66 fold [fn r :: {Type} => $(mapTT tf r) -> tr r]
|
adamc@356
|
67 (fn (nm :: Name) (t :: Type) (rest :: {Type}) (acc : _ -> tr rest)
|
adamc@356
|
68 [[nm] ~ rest] r =>
|
adamc@356
|
69 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@356
|
70 (fn _ => i)
|
adamc@336
|
71
|
adamc@339
|
72 fun foldT2R (tf :: (Type * Type) -> Type) (tr :: {(Type * Type)} -> Type)
|
adamc@367
|
73 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@367
|
74 -> fn [[nm] ~ rest] =>
|
adamc@367
|
75 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@367
|
76 (i : tr []) =
|
adamc@356
|
77 fold [fn r :: {(Type * Type)} => $(mapT2T tf r) -> tr r]
|
adamc@356
|
78 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
79 (acc : _ -> tr rest) [[nm] ~ rest] r =>
|
adamc@356
|
80 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@356
|
81 (fn _ => i)
|
adamc@339
|
82
|
adamc@330
|
83 fun foldTR2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (tr :: {Type} -> Type)
|
adamc@367
|
84 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@367
|
85 -> fn [[nm] ~ rest] =>
|
adamc@367
|
86 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@367
|
87 (i : tr []) =
|
adamc@356
|
88 fold [fn r :: {Type} => $(mapTT tf1 r) -> $(mapTT tf2 r) -> tr r]
|
adamc@356
|
89 (fn (nm :: Name) (t :: Type) (rest :: {Type})
|
adamc@356
|
90 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@356
|
91 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@356
|
92 (fn _ _ => i)
|
adamc@332
|
93
|
adamc@356
|
94 fun foldT2R2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
|
adamc@356
|
95 (tr :: {(Type * Type)} -> Type)
|
adamc@356
|
96 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
97 -> fn [[nm] ~ rest] =>
|
adamc@356
|
98 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@356
|
99 (i : tr []) =
|
adamc@356
|
100 fold [fn r :: {(Type * Type)} => $(mapT2T tf1 r) -> $(mapT2T tf2 r) -> tr r]
|
adamc@356
|
101 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
102 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@356
|
103 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@356
|
104 (fn _ _ => i)
|
adamc@339
|
105
|
adamc@336
|
106 fun foldTRX (tf :: Type -> Type) (ctx :: {Unit})
|
adamc@356
|
107 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
108 -> fn [[nm] ~ rest] =>
|
adamc@356
|
109 tf t -> xml ctx [] []) =
|
adamc@356
|
110 foldTR [tf] [fn _ => xml ctx [] []]
|
adamc@356
|
111 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest] r acc =>
|
adamc@356
|
112 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@360
|
113 <xml/>
|
adamc@336
|
114
|
adamc@339
|
115 fun foldT2RX (tf :: (Type * Type) -> Type) (ctx :: {Unit})
|
adamc@356
|
116 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
117 -> fn [[nm] ~ rest] =>
|
adamc@356
|
118 tf t -> xml ctx [] []) =
|
adamc@356
|
119 foldT2R [tf] [fn _ => xml ctx [] []]
|
adamc@356
|
120 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
121 [[nm] ~ rest] r acc =>
|
adamc@356
|
122 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@360
|
123 <xml/>
|
adamc@339
|
124
|
adamc@332
|
125 fun foldTRX2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (ctx :: {Unit})
|
adamc@356
|
126 (f : nm :: Name -> t :: Type -> rest :: {Type}
|
adamc@356
|
127 -> fn [[nm] ~ rest] =>
|
adamc@356
|
128 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@356
|
129 foldTR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@356
|
130 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest]
|
adamc@356
|
131 r1 r2 acc =>
|
adamc@356
|
132 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@360
|
133 <xml/>
|
adamc@334
|
134
|
adamc@356
|
135 fun foldT2RX2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
|
adamc@356
|
136 (ctx :: {Unit})
|
adamc@356
|
137 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
|
adamc@356
|
138 -> fn [[nm] ~ rest] =>
|
adamc@356
|
139 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@356
|
140 foldT2R2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@356
|
141 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@356
|
142 [[nm] ~ rest] r1 r2 acc =>
|
adamc@356
|
143 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@360
|
144 <xml/>
|
adamc@339
|
145
|
adamc@356
|
146 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@356
|
147 (q : sql_query tables exps) [tables ~ exps]
|
adamc@356
|
148 (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
|
adamc@356
|
149 [nm = $fields] ++ acc) [] tables)
|
adamc@356
|
150 -> xml ctx [] []) =
|
adamc@356
|
151 query q
|
adamc@356
|
152 (fn fs acc => return <xml>{acc}{f fs}</xml>)
|
adamc@360
|
153 <xml/>
|
adamc@341
|
154
|
adamc@356
|
155 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@356
|
156 (q : sql_query tables exps) [tables ~ exps] =
|
adamc@356
|
157 query q
|
adamc@356
|
158 (fn fs _ => return (Some fs))
|
adamc@356
|
159 None
|
adamc@440
|
160
|
adamc@440
|
161 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@440
|
162 (q : sql_query tables exps) [tables ~ exps] =
|
adamc@440
|
163 o <- oneOrNoRows q;
|
adamc@440
|
164 return (case o of
|
adamc@440
|
165 None => error <xml>Query returned no rows</xml>
|
adamc@440
|
166 | Some r => r)
|
adamc@440
|
167
|