comparison lib/ur/top.ur @ 822:d4e811beb8eb

fn-pattern code in but not tested yet; hello compiles
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 May 2009 10:16:50 -0400
parents 87a7702d681d
children d1d0b18afd3d
comparison
equal deleted inserted replaced
821:395a5d450cc0 822:d4e811beb8eb
4 tf :: ({K} -> Type) 4 tf :: ({K} -> Type)
5 -> (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => 5 -> (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
6 tf r -> tf ([nm = v] ++ r)) 6 tf r -> tf ([nm = v] ++ r))
7 -> tf [] -> tf r 7 -> tf [] -> tf r
8 8
9 fun fold K (tf :: {K} -> Type) 9 fun fold [K] [tf :: {K} -> Type]
10 (f : (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => 10 (f : (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
11 tf r -> tf ([nm = v] ++ r))) 11 tf r -> tf ([nm = v] ++ r)))
12 (i : tf []) (r :: {K}) (fl : folder r) = fl [tf] f i 12 (i : tf []) [r :: {K}] (fl : folder r) = fl [tf] f i
13 13
14 structure Folder = struct 14 structure Folder = struct
15 fun nil K (tf :: {K} -> Type) 15 fun nil [K] [tf :: {K} -> Type]
16 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => 16 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
17 tf r -> tf ([nm = v] ++ r)) 17 tf r -> tf ([nm = v] ++ r))
18 (i : tf []) = i 18 (i : tf []) = i
19 19
20 fun cons K (r ::: {K}) (nm :: Name) (v :: K) [[nm] ~ r] (fold : folder r) 20 fun cons [K] [r ::: {K}] [nm :: Name] [v :: K] [[nm] ~ r] (fold : folder r)
21 (tf :: {K} -> Type) 21 [tf :: {K} -> Type]
22 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => 22 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
23 tf r -> tf ([nm = v] ++ r)) 23 tf r -> tf ([nm = v] ++ r))
24 (i : tf []) = f [nm] [v] [r] ! (fold [tf] f i) 24 (i : tf []) = f [nm] [v] [r] ! (fold [tf] f i)
25 25
26 fun concat K (r1 ::: {K}) (r2 ::: {K}) [r1 ~ r2] 26 fun concat [K] [r1 ::: {K}] [r2 ::: {K}] [r1 ~ r2]
27 (f1 : folder r1) (f2 : folder r2) 27 (f1 : folder r1) (f2 : folder r2)
28 (tf :: {K} -> Type) 28 [tf :: {K} -> Type]
29 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => 29 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
30 tf r -> tf ([nm = v] ++ r)) 30 tf r -> tf ([nm = v] ++ r))
31 (i : tf []) = 31 (i : tf []) =
32 f1 [fn r1' => [r1' ~ r2] => tf (r1' ++ r2)] 32 f1 [fn r1' => [r1' ~ r2] => tf (r1' ++ r2)]
33 (fn (nm :: Name) (v :: K) (r1' :: {K}) [[nm] ~ r1'] 33 (fn [nm :: Name] [v :: K] [r1' :: {K}] [[nm] ~ r1']
34 (acc : [r1' ~ r2] => tf (r1' ++ r2)) 34 (acc : [r1' ~ r2] => tf (r1' ++ r2))
35 [[nm = v] ++ r1' ~ r2] => 35 [[nm = v] ++ r1' ~ r2] =>
36 f [nm] [v] [r1' ++ r2] ! acc) 36 f [nm] [v] [r1' ++ r2] ! acc)
37 (fn [[] ~ r2] => f2 [tf] f i) ! 37 (fn [[] ~ r2] => f2 [tf] f i) !
38 38
39 fun mp K1 K2 (f ::: K1 -> K2) (r ::: {K1}) 39 fun mp [K1] [K2] [f ::: K1 -> K2] [r ::: {K1}]
40 (fold : folder r) 40 (fold : folder r)
41 (tf :: {K2} -> Type) 41 [tf :: {K2} -> Type]
42 (f : nm :: Name -> v :: K2 -> r :: {K2} -> [[nm] ~ r] => 42 (f : nm :: Name -> v :: K2 -> r :: {K2} -> [[nm] ~ r] =>
43 tf r -> tf ([nm = v] ++ r)) 43 tf r -> tf ([nm = v] ++ r))
44 (i : tf []) = 44 (i : tf []) =
45 fold [fn r => tf (map f r)] 45 fold [fn r => tf (map f r)]
46 (fn (nm :: Name) (v :: K1) (rest :: {K1}) [[nm] ~ rest] (acc : tf (map f rest)) => 46 (fn [nm :: Name] [v :: K1] [rest :: {K1}] [[nm] ~ rest] (acc : tf (map f rest)) =>
47 f [nm] [f v] [map f rest] ! acc) 47 f [nm] [f v] [map f rest] ! acc)
48 i 48 i
49 end 49 end
50 50
51 51
62 con mapU = K ==> fn f :: K => map (fn _ :: Unit => f) 62 con mapU = K ==> fn f :: K => map (fn _ :: Unit => f)
63 63
64 con ex = fn tf :: (Type -> Type) => 64 con ex = fn tf :: (Type -> Type) =>
65 res ::: Type -> (choice :: Type -> tf choice -> res) -> res 65 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
66 66
67 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf = 67 fun ex [tf :: (Type -> Type)] [choice :: Type] (body : tf choice) : ex tf =
68 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) => 68 fn [res] (f : choice :: Type -> tf choice -> res) =>
69 f [choice] body 69 f [choice] body
70 70
71 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type) 71 fun compose [t1 ::: Type] [t2 ::: Type] [t3 ::: Type]
72 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x) 72 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
73 73
74 fun show_option (t ::: Type) (_ : show t) = 74 fun show_option [t ::: Type] (_ : show t) =
75 mkShow (fn opt : option t => 75 mkShow (fn opt : option t =>
76 case opt of 76 case opt of
77 None => "" 77 None => ""
78 | Some x => show x) 78 | Some x => show x)
79 79
80 fun read_option (t ::: Type) (_ : read t) = 80 fun read_option [t ::: Type] (_ : read t) =
81 mkRead (fn s => 81 mkRead (fn s =>
82 case s of 82 case s of
83 "" => None 83 "" => None
84 | _ => Some (readError s : t)) 84 | _ => Some (readError s : t))
85 (fn s => 85 (fn s =>
87 "" => Some None 87 "" => Some None
88 | _ => case read s of 88 | _ => case read s of
89 None => None 89 None => None
90 | v => Some v) 90 | v => Some v)
91 91
92 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) = 92 fun txt [t] [ctx ::: {Unit}] [use ::: {Type}] (_ : show t) (v : t) =
93 cdata (show v) 93 cdata (show v)
94 94
95 fun foldUR (tf :: Type) (tr :: {Unit} -> Type) 95 fun foldUR [tf :: Type] [tr :: {Unit} -> Type]
96 (f : nm :: Name -> rest :: {Unit} 96 (f : nm :: Name -> rest :: {Unit}
97 -> [[nm] ~ rest] => 97 -> [[nm] ~ rest] =>
98 tf -> tr rest -> tr ([nm] ++ rest)) 98 tf -> tr rest -> tr ([nm] ++ rest))
99 (i : tr []) (r :: {Unit}) (fl : folder r)= 99 (i : tr []) [r :: {Unit}] (fl : folder r)=
100 fl [fn r :: {Unit} => $(mapU tf r) -> tr r] 100 fl [fn r :: {Unit} => $(mapU tf r) -> tr r]
101 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r => 101 (fn [nm :: Name] [t :: Unit] [rest :: {Unit}] [[nm] ~ rest] acc r =>
102 f [nm] [rest] ! r.nm (acc (r -- nm))) 102 f [nm] [rest] ! r.nm (acc (r -- nm)))
103 (fn _ => i) 103 (fn _ => i)
104 104
105 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type) 105 fun foldUR2 [tf1 :: Type] [tf2 :: Type] [tr :: {Unit} -> Type]
106 (f : nm :: Name -> rest :: {Unit} 106 (f : nm :: Name -> rest :: {Unit}
107 -> [[nm] ~ rest] => 107 -> [[nm] ~ rest] =>
108 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest)) 108 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
109 (i : tr []) (r :: {Unit}) (fl : folder r) = 109 (i : tr []) [r :: {Unit}] (fl : folder r) =
110 fl [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r] 110 fl [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r]
111 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r1 r2 => 111 (fn [nm :: Name] [t :: Unit] [rest :: {Unit}] [[nm] ~ rest] acc r1 r2 =>
112 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm))) 112 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
113 (fn _ _ => i) 113 (fn _ _ => i)
114 114
115 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit}) 115 fun foldURX2 [tf1 :: Type] [tf2 :: Type] [ctx :: {Unit}]
116 (f : nm :: Name -> rest :: {Unit} 116 (f : nm :: Name -> rest :: {Unit}
117 -> [[nm] ~ rest] => 117 -> [[nm] ~ rest] =>
118 tf1 -> tf2 -> xml ctx [] []) = 118 tf1 -> tf2 -> xml ctx [] []) =
119 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []] 119 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
120 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc => 120 (fn [nm :: Name] [rest :: {Unit}] [[nm] ~ rest] v1 v2 acc =>
121 <xml>{f [nm] [rest] ! v1 v2}{acc}</xml>) 121 <xml>{f [nm] [rest] ! v1 v2}{acc}</xml>)
122 <xml/> 122 <xml/>
123 123
124 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type) 124 fun foldR [K] [tf :: K -> Type] [tr :: {K} -> Type]
125 (f : nm :: Name -> t :: K -> rest :: {K} 125 (f : nm :: Name -> t :: K -> rest :: {K}
126 -> [[nm] ~ rest] => 126 -> [[nm] ~ rest] =>
127 tf t -> tr rest -> tr ([nm = t] ++ rest)) 127 tf t -> tr rest -> tr ([nm = t] ++ rest))
128 (i : tr []) (r :: {K}) (fl : folder r) = 128 (i : tr []) [r :: {K}] (fl : folder r) =
129 fl [fn r :: {K} => $(map tf r) -> tr r] 129 fl [fn r :: {K} => $(map tf r) -> tr r]
130 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] (acc : _ -> tr rest) r => 130 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] (acc : _ -> tr rest) r =>
131 f [nm] [t] [rest] ! r.nm (acc (r -- nm))) 131 f [nm] [t] [rest] ! r.nm (acc (r -- nm)))
132 (fn _ => i) 132 (fn _ => i)
133 133
134 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type) 134 fun foldR2 [K] [tf1 :: K -> Type] [tf2 :: K -> Type] [tr :: {K} -> Type]
135 (f : nm :: Name -> t :: K -> rest :: {K} 135 (f : nm :: Name -> t :: K -> rest :: {K}
136 -> [[nm] ~ rest] => 136 -> [[nm] ~ rest] =>
137 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest)) 137 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
138 (i : tr []) (r :: {K}) (fl : folder r) = 138 (i : tr []) [r :: {K}] (fl : folder r) =
139 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r] 139 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
140 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] 140 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
141 (acc : _ -> _ -> tr rest) r1 r2 => 141 (acc : _ -> _ -> tr rest) r1 r2 =>
142 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm))) 142 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
143 (fn _ _ => i) 143 (fn _ _ => i)
144 144
145 fun foldRX K (tf :: K -> Type) (ctx :: {Unit}) 145 fun foldRX [K] [tf :: K -> Type] [ctx :: {Unit}]
146 (f : nm :: Name -> t :: K -> rest :: {K} 146 (f : nm :: Name -> t :: K -> rest :: {K}
147 -> [[nm] ~ rest] => 147 -> [[nm] ~ rest] =>
148 tf t -> xml ctx [] []) = 148 tf t -> xml ctx [] []) =
149 foldR [tf] [fn _ => xml ctx [] []] 149 foldR [tf] [fn _ => xml ctx [] []]
150 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] r acc => 150 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest] r acc =>
151 <xml>{f [nm] [t] [rest] ! r}{acc}</xml>) 151 <xml>{f [nm] [t] [rest] ! r}{acc}</xml>)
152 <xml/> 152 <xml/>
153 153
154 fun foldRX2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (ctx :: {Unit}) 154 fun foldRX2 [K] [tf1 :: K -> Type] [tf2 :: K -> Type] [ctx :: {Unit}]
155 (f : nm :: Name -> t :: K -> rest :: {K} 155 (f : nm :: Name -> t :: K -> rest :: {K}
156 -> [[nm] ~ rest] => 156 -> [[nm] ~ rest] =>
157 tf1 t -> tf2 t -> xml ctx [] []) = 157 tf1 t -> tf2 t -> xml ctx [] []) =
158 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []] 158 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []]
159 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] 159 (fn [nm :: Name] [t :: K] [rest :: {K}] [[nm] ~ rest]
160 r1 r2 acc => 160 r1 r2 acc =>
161 <xml>{f [nm] [t] [rest] ! r1 r2}{acc}</xml>) 161 <xml>{f [nm] [t] [rest] ! r1 r2}{acc}</xml>)
162 <xml/> 162 <xml/>
163 163
164 fun queryI (tables ::: {{Type}}) (exps ::: {Type}) 164 fun queryI [tables ::: {{Type}}] [exps ::: {Type}]
165 [tables ~ exps] (q : sql_query tables exps) 165 [tables ~ exps] (q : sql_query tables exps)
166 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables) 166 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
167 -> transaction unit) = 167 -> transaction unit) =
168 query q 168 query q
169 (fn fs _ => f fs) 169 (fn fs _ => f fs)
170 () 170 ()
171 171
172 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit}) 172 fun queryX [tables ::: {{Type}}] [exps ::: {Type}] [ctx ::: {Unit}]
173 [tables ~ exps] (q : sql_query tables exps) 173 [tables ~ exps] (q : sql_query tables exps)
174 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables) 174 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
175 -> xml ctx [] []) = 175 -> xml ctx [] []) =
176 query q 176 query q
177 (fn fs acc => return <xml>{acc}{f fs}</xml>) 177 (fn fs acc => return <xml>{acc}{f fs}</xml>)
178 <xml/> 178 <xml/>
179 179
180 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit}) 180 fun queryX' [tables ::: {{Type}}] [exps ::: {Type}] [ctx ::: {Unit}]
181 [tables ~ exps] (q : sql_query tables exps) 181 [tables ~ exps] (q : sql_query tables exps)
182 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables) 182 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
183 -> transaction (xml ctx [] [])) = 183 -> transaction (xml ctx [] [])) =
184 query q 184 query q
185 (fn fs acc => 185 (fn fs acc =>
186 r <- f fs; 186 r <- f fs;
187 return <xml>{acc}{r}</xml>) 187 return <xml>{acc}{r}</xml>)
188 <xml/> 188 <xml/>
189 189
190 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type}) 190 fun oneOrNoRows [tables ::: {{Type}}] [exps ::: {Type}]
191 [tables ~ exps] 191 [tables ~ exps]
192 (q : sql_query tables exps) = 192 (q : sql_query tables exps) =
193 query q 193 query q
194 (fn fs _ => return (Some fs)) 194 (fn fs _ => return (Some fs))
195 None 195 None
196 196
197 fun oneRow (tables ::: {{Type}}) (exps ::: {Type}) 197 fun oneRow [tables ::: {{Type}}] [exps ::: {Type}]
198 [tables ~ exps] (q : sql_query tables exps) = 198 [tables ~ exps] (q : sql_query tables exps) =
199 o <- oneOrNoRows q; 199 o <- oneOrNoRows q;
200 return (case o of 200 return (case o of
201 None => error <xml>Query returned no rows</xml> 201 None => error <xml>Query returned no rows</xml>
202 | Some r => r) 202 | Some r => r)
203 203
204 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type}) 204 fun eqNullable [tables ::: {{Type}}] [agg ::: {{Type}}] [exps ::: {Type}]
205 (t ::: Type) (_ : sql_injectable (option t)) 205 [t ::: Type] (_ : sql_injectable (option t))
206 (e1 : sql_exp tables agg exps (option t)) 206 (e1 : sql_exp tables agg exps (option t))
207 (e2 : sql_exp tables agg exps (option t)) = 207 (e2 : sql_exp tables agg exps (option t)) =
208 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2}) 208 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
209 209
210 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type}) 210 fun eqNullable' [tables ::: {{Type}}] [agg ::: {{Type}}] [exps ::: {Type}]
211 (t ::: Type) (_ : sql_injectable (option t)) 211 [t ::: Type] (_ : sql_injectable (option t))
212 (e1 : sql_exp tables agg exps (option t)) 212 (e1 : sql_exp tables agg exps (option t))
213 (e2 : option t) = 213 (e2 : option t) =
214 case e2 of 214 case e2 of
215 None => (SQL {e1} IS NULL) 215 None => (SQL {e1} IS NULL)
216 | Some _ => sql_binary sql_eq e1 (sql_inject e2) 216 | Some _ => sql_binary sql_eq e1 (sql_inject e2)