90 |
90 |
91 val classrel_prefix = "classrel_"; |
91 val classrel_prefix = "classrel_"; |
92 val arity_prefix = "arity_"; |
92 val arity_prefix = "arity_"; |
93 |
93 |
94 type instances = |
94 type instances = |
95 ((class * class) * thm) list * |
95 ((class * class) * thm) list * (*classrel theorems*) |
96 ((class * sort list) * thm) list Symtab.table; |
96 ((class * sort list) * (thm * string)) list Symtab.table; (*arity theorems with theory name*) |
97 |
97 |
98 fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) = |
98 fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) = |
99 (merge (eq_fst op =) (classrel1, classrel2), |
99 (merge (eq_fst op =) (classrel1, classrel2), |
100 Symtab.join (K (merge (eq_fst op =))) (arities1, arities2)); |
100 Symtab.join (K (merge (eq_fst op =))) (arities1, arities2)); |
101 |
101 |
173 (insert (eq_fst op =) arg classrel, arities)); |
173 (insert (eq_fst op =) arg classrel, arities)); |
174 |
174 |
175 |
175 |
176 fun the_arity thy a (c, Ss) = |
176 fun the_arity thy a (c, Ss) = |
177 (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of |
177 (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of |
178 SOME th => Thm.transfer thy th |
178 SOME (th, _) => Thm.transfer thy th |
179 | NONE => error ("Unproven type arity " ^ |
179 | NONE => error ("Unproven type arity " ^ |
180 Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c]))); |
180 Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c]))); |
181 |
181 |
182 fun arity_property thy (c, a) x = |
182 fun thynames_of_arity thy (c, a) = |
183 Symtab.lookup_list (snd (get_instances thy)) a |
183 Symtab.lookup_list (#2 (get_instances thy)) a |
184 |> map_filter (fn ((c', _), th) => if c = c' |
184 |> map_filter (fn ((c', _), (_, name)) => if c = c' then SOME name else NONE) |
185 then AList.lookup (op =) (Thm.get_tags th) x else NONE) |
|
186 |> rev; |
185 |> rev; |
187 |
186 |
188 fun insert_arity_completions thy (t, ((c, Ss), th)) arities = |
187 fun insert_arity_completions thy (t, ((c, Ss), (th, thy_name))) arities = |
189 let |
188 let |
190 val algebra = Sign.classes_of thy; |
189 val algebra = Sign.classes_of thy; |
191 val super_class_completions = Sign.super_classes thy c |
190 val super_class_completions = |
|
191 Sign.super_classes thy c |
192 |> filter_out (fn c1 => exists (fn ((c2, Ss2), _) => c1 = c2 |
192 |> filter_out (fn c1 => exists (fn ((c2, Ss2), _) => c1 = c2 |
193 andalso Sorts.sorts_le algebra (Ss2, Ss)) (Symtab.lookup_list arities t)) |
193 andalso Sorts.sorts_le algebra (Ss2, Ss)) (Symtab.lookup_list arities t)); |
194 val tags = Thm.get_tags th; |
|
195 val completions = map (fn c1 => (Sorts.weaken algebra |
194 val completions = map (fn c1 => (Sorts.weaken algebra |
196 (fn (th, c2) => fn c3 => th RS the_classrel thy (c2, c3)) (th, c) c1 |
195 (fn (th, c2) => fn c3 => th RS the_classrel thy (c2, c3)) (th, c) c1 |
197 |> Thm.map_tags (K tags) |> Thm.close_derivation, c1)) super_class_completions; |
196 |> Thm.close_derivation, c1)) super_class_completions; |
198 val arities' = fold (fn (th1, c1) => Symtab.cons_list (t, ((c1, Ss), th1))) |
197 val arities' = fold (fn (th1, c1) => Symtab.cons_list (t, ((c1, Ss), (th1, thy_name)))) |
199 completions arities; |
198 completions arities; |
200 in (completions, arities') end; |
199 in (null completions, arities') end; |
201 |
200 |
202 fun put_arity ((t, Ss, c), th) thy = |
201 fun put_arity ((t, Ss, c), th) thy = |
203 let |
202 let |
204 val th' = (Thm.map_tags o AList.default (op =)) |
203 val arity' = (t, ((c, Ss), (th, Context.theory_name thy))); |
205 (Markup.theory_nameN, Context.theory_name thy) th; |
|
206 val arity' = (t, ((c, Ss), th')); |
|
207 in |
204 in |
208 thy |
205 thy |
209 |> map_instances (fn (classrel, arities) => (classrel, |
206 |> map_instances (fn (classrel, arities) => (classrel, |
210 arities |
207 arities |
211 |> Symtab.insert_list (eq_fst op =) arity' |
208 |> Symtab.insert_list (eq_fst op =) arity' |
214 end; |
211 end; |
215 |
212 |
216 fun complete_arities thy = |
213 fun complete_arities thy = |
217 let |
214 let |
218 val arities = snd (get_instances thy); |
215 val arities = snd (get_instances thy); |
219 val (completions, arities') = arities |
216 val (finished, arities') = arities |
220 |> fold_map (insert_arity_completions thy) (Symtab.dest_list arities) |
217 |> fold_map (insert_arity_completions thy) (Symtab.dest_list arities); |
221 |>> flat; |
218 in |
222 in if null completions |
219 if forall I finished then NONE |
223 then NONE |
|
224 else SOME (thy |> map_instances (fn (classrel, _) => (classrel, arities'))) |
220 else SOME (thy |> map_instances (fn (classrel, _) => (classrel, arities'))) |
225 end; |
221 end; |
226 |
222 |
227 val _ = Context.>> (Context.map_theory (Theory.at_begin complete_arities)); |
223 val _ = Context.>> (Context.map_theory (Theory.at_begin complete_arities)); |
228 |
224 |