70 (* setup data *) |
70 (* setup data *) |
71 |
71 |
72 datatype data = Data of |
72 datatype data = Data of |
73 {axclasses: info Symtab.table, |
73 {axclasses: info Symtab.table, |
74 params: param list, |
74 params: param list, |
75 proven_classrels: (thm * proof) Symreltab.table, |
75 proven_classrels: thm Symreltab.table, |
76 proven_arities: ((class * sort list) * ((thm * string) * proof)) list Symtab.table, |
76 proven_arities: ((class * sort list) * (thm * string)) list Symtab.table, |
77 (*arity theorems with theory name*) |
77 (*arity theorems with theory name*) |
78 inst_params: |
78 inst_params: |
79 (string * thm) Symtab.table Symtab.table * |
79 (string * thm) Symtab.table Symtab.table * |
80 (*constant name ~> type constructor ~> (constant name, equation)*) |
80 (*constant name ~> type constructor ~> (constant name, equation)*) |
81 (string * string) Symtab.table (*constant name ~> (constant name, type constructor)*), |
81 (string * string) Symtab.table (*constant name ~> (constant name, type constructor)*), |
187 fun instance_name (a, c) = Long_Name.base_name c ^ "_" ^ Long_Name.base_name a; |
187 fun instance_name (a, c) = Long_Name.base_name c ^ "_" ^ Long_Name.base_name a; |
188 |
188 |
189 |
189 |
190 fun the_classrel thy (c1, c2) = |
190 fun the_classrel thy (c1, c2) = |
191 (case Symreltab.lookup (proven_classrels_of thy) (c1, c2) of |
191 (case Symreltab.lookup (proven_classrels_of thy) (c1, c2) of |
192 SOME classrel => classrel |
192 SOME thm => Thm.transfer thy thm |
193 | NONE => error ("Unproven class relation " ^ |
193 | NONE => error ("Unproven class relation " ^ |
194 Syntax.string_of_classrel (ProofContext.init thy) [c1, c2])); |
194 Syntax.string_of_classrel (ProofContext.init thy) [c1, c2])); |
195 |
|
196 fun the_classrel_thm thy = Thm.transfer thy o #1 o the_classrel thy; |
|
197 fun the_classrel_prf thy = #2 o the_classrel thy; |
|
198 |
195 |
199 fun put_trancl_classrel ((c1, c2), th) thy = |
196 fun put_trancl_classrel ((c1, c2), th) thy = |
200 let |
197 let |
201 val cert = Thm.cterm_of thy; |
198 val cert = Thm.cterm_of thy; |
202 val certT = Thm.ctyp_of thy; |
199 val certT = Thm.ctyp_of thy; |
205 val classrels = proven_classrels_of thy; |
202 val classrels = proven_classrels_of thy; |
206 |
203 |
207 fun reflcl_classrel (c1', c2') = |
204 fun reflcl_classrel (c1', c2') = |
208 if c1' = c2' |
205 if c1' = c2' |
209 then Thm.trivial (cert (Logic.mk_of_class (TVar ((Name.aT, 0), []), c1'))) |
206 then Thm.trivial (cert (Logic.mk_of_class (TVar ((Name.aT, 0), []), c1'))) |
210 else the_classrel_thm thy (c1', c2'); |
207 else the_classrel thy (c1', c2'); |
211 fun gen_classrel (c1_pred, c2_succ) = |
208 fun gen_classrel (c1_pred, c2_succ) = |
212 let |
209 let |
213 val th' = ((reflcl_classrel (c1_pred, c1) RS th) RS reflcl_classrel (c2, c2_succ)) |
210 val th' = ((reflcl_classrel (c1_pred, c1) RS th) RS reflcl_classrel (c2, c2_succ)) |
214 |> Drule.instantiate' [SOME (certT (TVar ((Name.aT, 0), [])))] [] |
211 |> Drule.instantiate' [SOME (certT (TVar ((Name.aT, 0), [])))] [] |
215 |> Thm.close_derivation; |
212 |> Thm.close_derivation; |
216 val prf' = Thm.proof_of th'; |
213 in ((c1_pred, c2_succ), th') end; |
217 in ((c1_pred, c2_succ), (th', prf')) end; |
|
218 |
214 |
219 val new_classrels = |
215 val new_classrels = |
220 Library.map_product pair (c1 :: Graph.imm_preds classes c1) (c2 :: Graph.imm_succs classes c2) |
216 Library.map_product pair (c1 :: Graph.imm_preds classes c1) (c2 :: Graph.imm_succs classes c2) |
221 |> filter_out (Symreltab.defined classrels) |
217 |> filter_out (Symreltab.defined classrels) |
222 |> map gen_classrel; |
218 |> map gen_classrel; |
231 let |
227 let |
232 val classrels = proven_classrels_of thy; |
228 val classrels = proven_classrels_of thy; |
233 val diff_merge_classrels = diff_merge_classrels_of thy; |
229 val diff_merge_classrels = diff_merge_classrels_of thy; |
234 val (needed, thy') = (false, thy) |> |
230 val (needed, thy') = (false, thy) |> |
235 fold (fn rel => fn (needed, thy) => |
231 fold (fn rel => fn (needed, thy) => |
236 put_trancl_classrel (rel, Symreltab.lookup classrels rel |> the |> #1) thy |
232 put_trancl_classrel (rel, Symreltab.lookup classrels rel |> the) thy |
237 |>> (fn b => needed orelse b)) |
233 |>> (fn b => needed orelse b)) |
238 diff_merge_classrels; |
234 diff_merge_classrels; |
239 in |
235 in |
240 if null diff_merge_classrels then NONE |
236 if null diff_merge_classrels then NONE |
241 else SOME (clear_diff_merge_classrels thy') |
237 else SOME (clear_diff_merge_classrels thy') |
242 end; |
238 end; |
243 |
239 |
244 |
240 |
245 fun the_arity thy a (c, Ss) = |
241 fun the_arity thy a (c, Ss) = |
246 (case AList.lookup (op =) (Symtab.lookup_list (proven_arities_of thy) a) (c, Ss) of |
242 (case AList.lookup (op =) (Symtab.lookup_list (proven_arities_of thy) a) (c, Ss) of |
247 SOME arity => arity |
243 SOME (thm, _) => Thm.transfer thy thm |
248 | NONE => error ("Unproven type arity " ^ |
244 | NONE => error ("Unproven type arity " ^ |
249 Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c]))); |
245 Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c]))); |
250 |
246 |
251 fun the_arity_thm thy a c_Ss = the_arity thy a c_Ss |> #1 |> #1 |> Thm.transfer thy; |
|
252 fun the_arity_prf thy a c_Ss = the_arity thy a c_Ss |> #2; |
|
253 |
|
254 fun thynames_of_arity thy (c, a) = |
247 fun thynames_of_arity thy (c, a) = |
255 Symtab.lookup_list (proven_arities_of thy) a |
248 Symtab.lookup_list (proven_arities_of thy) a |
256 |> map_filter (fn ((c', _), ((_, name), _)) => if c = c' then SOME name else NONE) |
249 |> map_filter (fn ((c', _), (_, name)) => if c = c' then SOME name else NONE) |
257 |> rev; |
250 |> rev; |
258 |
251 |
259 fun insert_arity_completions thy (t, ((c, Ss), ((th, thy_name), _))) arities = |
252 fun insert_arity_completions thy (t, ((c, Ss), ((th, thy_name)))) arities = |
260 let |
253 let |
261 val algebra = Sign.classes_of thy; |
254 val algebra = Sign.classes_of thy; |
262 val ars = Symtab.lookup_list arities t; |
255 val ars = Symtab.lookup_list arities t; |
263 val super_class_completions = |
256 val super_class_completions = |
264 Sign.super_classes thy c |
257 Sign.super_classes thy c |
268 val names = Name.invents Name.context Name.aT (length Ss); |
261 val names = Name.invents Name.context Name.aT (length Ss); |
269 val std_vars = map (fn a => SOME (ctyp_of thy (TVar ((a, 0), [])))) names; |
262 val std_vars = map (fn a => SOME (ctyp_of thy (TVar ((a, 0), [])))) names; |
270 |
263 |
271 val completions = super_class_completions |> map (fn c1 => |
264 val completions = super_class_completions |> map (fn c1 => |
272 let |
265 let |
273 val th1 = (th RS the_classrel_thm thy (c, c1)) |
266 val th1 = (th RS the_classrel thy (c, c1)) |
274 |> Drule.instantiate' std_vars [] |
267 |> Drule.instantiate' std_vars [] |
275 |> Thm.close_derivation; |
268 |> Thm.close_derivation; |
276 val prf1 = Thm.proof_of th1; |
269 in ((th1, thy_name), c1) end); |
277 in (((th1, thy_name), prf1), c1) end); |
270 val arities' = fold (fn (th, c1) => Symtab.cons_list (t, ((c1, Ss), th))) completions arities; |
278 val arities' = fold (fn (th_thy_prf1, c1) => Symtab.cons_list (t, ((c1, Ss), th_thy_prf1))) |
|
279 completions arities; |
|
280 in (null completions, arities') end; |
271 in (null completions, arities') end; |
281 |
272 |
282 fun put_arity ((t, Ss, c), th) thy = |
273 fun put_arity ((t, Ss, c), th) thy = |
283 let |
274 let val arity = (t, ((c, Ss), (th, Context.theory_name thy))) in |
284 val arity = (t, ((c, Ss), ((th, Context.theory_name thy), Thm.proof_of th))); |
|
285 in |
|
286 thy |
275 thy |
287 |> map_proven_arities |
276 |> map_proven_arities |
288 (Symtab.insert_list (eq_fst op =) arity #> insert_arity_completions thy arity #> #2) |
277 (Symtab.insert_list (eq_fst op =) arity #> insert_arity_completions thy arity #> #2) |
289 end; |
278 end; |
290 |
279 |
299 else SOME (map_proven_arities (K arities') thy) |
288 else SOME (map_proven_arities (K arities') thy) |
300 end; |
289 end; |
301 |
290 |
302 val _ = Context.>> (Context.map_theory |
291 val _ = Context.>> (Context.map_theory |
303 (Theory.at_begin complete_classrels #> Theory.at_begin complete_arities)); |
292 (Theory.at_begin complete_classrels #> Theory.at_begin complete_arities)); |
|
293 |
|
294 val the_classrel_prf = Thm.proof_of oo the_classrel; |
|
295 val the_arity_prf = Thm.proof_of ooo the_arity; |
304 |
296 |
305 |
297 |
306 (* maintain instance parameters *) |
298 (* maintain instance parameters *) |
307 |
299 |
308 fun get_inst_param thy (c, tyco) = |
300 fun get_inst_param thy (c, tyco) = |