46 |
46 |
47 fun pretty_thy ctxt target is_locale is_class = |
47 fun pretty_thy ctxt target is_locale is_class = |
48 let |
48 let |
49 val thy = ProofContext.theory_of ctxt; |
49 val thy = ProofContext.theory_of ctxt; |
50 val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target; |
50 val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target; |
51 val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt)); |
51 val fixes = map (fn (x, T) => |
52 val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt)); |
52 (Name.binding x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt)); |
|
53 val assumes = map (fn A => |
|
54 ((Name.no_binding, []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt)); |
53 val elems = |
55 val elems = |
54 (if null fixes then [] else [Element.Fixes fixes]) @ |
56 (if null fixes then [] else [Element.Fixes fixes]) @ |
55 (if null assumes then [] else [Element.Assumes assumes]); |
57 (if null assumes then [] else [Element.Assumes assumes]); |
56 in |
58 in |
57 if target = "" then [] |
59 if target = "" then [] |
136 val result'' = |
138 val result'' = |
137 (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of |
139 (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of |
138 NONE => raise THM ("Failed to re-import result", 0, [result']) |
140 NONE => raise THM ("Failed to re-import result", 0, [result']) |
139 | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv]) |
141 | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv]) |
140 |> Goal.norm_result |
142 |> Goal.norm_result |
141 |> PureThy.name_thm false false name; |
143 |> PureThy.name_thm false false Position.none name; |
142 |
144 |
143 in (result'', result) end; |
145 in (result'', result) end; |
144 |
146 |
145 fun note_local kind facts ctxt = |
147 fun note_local kind facts ctxt = |
146 ctxt |
148 ctxt |
152 let |
154 let |
153 val thy = ProofContext.theory_of lthy; |
155 val thy = ProofContext.theory_of lthy; |
154 val full = LocalTheory.full_name lthy; |
156 val full = LocalTheory.full_name lthy; |
155 |
157 |
156 val facts' = facts |
158 val facts' = facts |
157 |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs)) |
159 |> map (fn (a, bs) => |
|
160 (a, PureThy.burrow_fact (PureThy.name_multi (full (Name.name_of (fst a)))) bs)) |
158 |> PureThy.map_facts (import_export_proof lthy); |
161 |> PureThy.map_facts (import_export_proof lthy); |
159 val local_facts = PureThy.map_facts #1 facts' |
162 val local_facts = PureThy.map_facts #1 facts' |
160 |> Attrib.map_facts (Attrib.attribute_i thy); |
163 |> Attrib.map_facts (Attrib.attribute_i thy); |
161 val target_facts = PureThy.map_facts #1 facts' |
164 val target_facts = PureThy.map_facts #1 facts' |
162 |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy)); |
165 |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy)); |
183 |
186 |
184 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) tags ((c, mx), rhs) phi = |
187 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) tags ((c, mx), rhs) phi = |
185 let |
188 let |
186 val c' = Morphism.name phi c; |
189 val c' = Morphism.name phi c; |
187 val rhs' = Morphism.term phi rhs; |
190 val rhs' = Morphism.term phi rhs; |
188 val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs')); |
191 val name = Name.name_of c; |
189 val arg = (c', Term.close_schematic_term rhs'); |
192 val name' = Name.name_of c' |
|
193 val legacy_arg = (name', Term.close_schematic_term (Logic.legacy_varify rhs')); |
|
194 val arg = (name', Term.close_schematic_term rhs'); |
190 val similar_body = Type.similar_types (rhs, rhs'); |
195 val similar_body = Type.similar_types (rhs, rhs'); |
191 (* FIXME workaround based on educated guess *) |
196 (* FIXME workaround based on educated guess *) |
192 val class_global = c' = NameSpace.qualified (Class.class_prefix target) c; |
197 val class_global = name' = NameSpace.qualified (Class.class_prefix target) name; |
193 in |
198 in |
194 not (is_class andalso (similar_body orelse class_global)) ? |
199 not (is_class andalso (similar_body orelse class_global)) ? |
195 (Context.mapping_result |
200 (Context.mapping_result |
196 (Sign.add_abbrev PrintMode.internal tags legacy_arg) |
201 (Sign.add_abbrev PrintMode.internal tags legacy_arg) |
197 (ProofContext.add_abbrev PrintMode.internal tags arg) |
202 (ProofContext.add_abbrev PrintMode.internal tags arg) |
199 similar_body ? |
204 similar_body ? |
200 (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #> |
205 (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #> |
201 Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)])))) |
206 Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)])))) |
202 end; |
207 end; |
203 |
208 |
204 fun declare_const (ta as Target {target, is_locale, is_class, ...}) depends ((c, T), mx) lthy = |
209 fun declare_const (ta as Target {target, is_locale, is_class, ...}) depends ((b, T), mx) lthy = |
205 let |
210 let |
|
211 val c = Name.name_of b; |
206 val tags = LocalTheory.group_position_of lthy; |
212 val tags = LocalTheory.group_position_of lthy; |
207 val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy))); |
213 val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy))); |
208 val U = map #2 xs ---> T; |
214 val U = map #2 xs ---> T; |
209 val (mx1, mx2, mx3) = fork_mixfix ta mx; |
215 val (mx1, mx2, mx3) = fork_mixfix ta mx; |
210 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c); |
216 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c); |
223 | NONE => LocalTheory.theory_result (Sign.declare_const tags (c, U, mx3)))); |
229 | NONE => LocalTheory.theory_result (Sign.declare_const tags (c, U, mx3)))); |
224 val (const, lthy') = lthy |> declare_const; |
230 val (const, lthy') = lthy |> declare_const; |
225 val t = Term.list_comb (const, map Free xs); |
231 val t = Term.list_comb (const, map Free xs); |
226 in |
232 in |
227 lthy' |
233 lthy' |
228 |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default tags ((c, mx2), t)) |
234 |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default tags ((b, mx2), t)) |
229 |> is_class ? class_target ta (Class.declare target tags ((c, mx1), t)) |
235 |> is_class ? class_target ta (Class.declare target tags ((c, mx1), t)) |
230 |> LocalDefs.add_def ((c, NoSyn), t) |
236 |> LocalDefs.add_def ((b, NoSyn), t) |
231 end; |
237 end; |
232 |
238 |
233 |
239 |
234 (* abbrev *) |
240 (* abbrev *) |
235 |
241 |
236 fun abbrev (ta as Target {target, is_locale, is_class, ...}) prmode ((c, mx), t) lthy = |
242 fun abbrev (ta as Target {target, is_locale, is_class, ...}) prmode ((b, mx), t) lthy = |
237 let |
243 let |
|
244 val c = Name.name_of b; |
238 val tags = LocalTheory.group_position_of lthy; |
245 val tags = LocalTheory.group_position_of lthy; |
239 val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy); |
246 val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy); |
240 val target_ctxt = LocalTheory.target_of lthy; |
247 val target_ctxt = LocalTheory.target_of lthy; |
241 |
248 |
242 val (mx1, mx2, mx3) = fork_mixfix ta mx; |
249 val (mx1, mx2, mx3) = fork_mixfix ta mx; |
249 lthy |> |
256 lthy |> |
250 (if is_locale then |
257 (if is_locale then |
251 LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal tags (c, global_rhs)) |
258 LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal tags (c, global_rhs)) |
252 #-> (fn (lhs, _) => |
259 #-> (fn (lhs, _) => |
253 let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in |
260 let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in |
254 term_syntax ta (locale_const ta prmode tags ((c, mx2), lhs')) #> |
261 term_syntax ta (locale_const ta prmode tags ((b, mx2), lhs')) #> |
255 is_class ? class_target ta (Class.abbrev target prmode tags ((c, mx1), t')) |
262 is_class ? class_target ta (Class.abbrev target prmode tags ((c, mx1), t')) |
256 end) |
263 end) |
257 else |
264 else |
258 LocalTheory.theory |
265 LocalTheory.theory |
259 (Sign.add_abbrev (#1 prmode) tags (c, global_rhs) #-> (fn (lhs, _) => |
266 (Sign.add_abbrev (#1 prmode) tags (c, global_rhs) #-> (fn (lhs, _) => |
260 Sign.notation true prmode [(lhs, mx3)]))) |
267 Sign.notation true prmode [(lhs, mx3)]))) |
261 |> ProofContext.add_abbrev PrintMode.internal tags (c, t) |> snd |
268 |> ProofContext.add_abbrev PrintMode.internal tags (c, t) |> snd |
262 |> LocalDefs.fixed_abbrev ((c, NoSyn), t) |
269 |> LocalDefs.fixed_abbrev ((b, NoSyn), t) |
263 end; |
270 end; |
264 |
271 |
265 |
272 |
266 (* define *) |
273 (* define *) |
267 |
274 |
268 fun define (ta as Target {target, is_locale, is_class, ...}) |
275 fun define (ta as Target {target, is_locale, is_class, ...}) |
269 kind ((c, mx), ((name, atts), rhs)) lthy = |
276 kind ((b, mx), ((name, atts), rhs)) lthy = |
270 let |
277 let |
271 val thy = ProofContext.theory_of lthy; |
278 val thy = ProofContext.theory_of lthy; |
272 val thy_ctxt = ProofContext.init thy; |
279 val thy_ctxt = ProofContext.init thy; |
273 |
280 |
274 val name' = Thm.def_name_optional c name; |
281 val c = Name.name_of b; |
|
282 val name' = Name.map_name (Thm.def_name_optional c) name; |
275 val (rhs', rhs_conv) = |
283 val (rhs', rhs_conv) = |
276 LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of; |
284 LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of; |
277 val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' []; |
285 val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' []; |
278 val T = Term.fastype_of rhs; |
286 val T = Term.fastype_of rhs; |
279 |
287 |
280 (*const*) |
288 (*const*) |
281 val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((c, T), mx); |
289 val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((b, T), mx); |
282 val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def); |
290 val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def); |
283 |
291 |
284 (*def*) |
292 (*def*) |
285 val define_const = |
293 val define_const = |
286 (case Overloading.operation lthy c of |
294 (case Overloading.operation lthy c of |
289 | NONE => |
297 | NONE => |
290 if is_none (Class.instantiation_param lthy c) |
298 if is_none (Class.instantiation_param lthy c) |
291 then (fn name => fn eq => Thm.add_def false false (name, Logic.mk_equals eq)) |
299 then (fn name => fn eq => Thm.add_def false false (name, Logic.mk_equals eq)) |
292 else (fn name => fn (Const (c, _), rhs) => AxClass.define_overloaded name (c, rhs))); |
300 else (fn name => fn (Const (c, _), rhs) => AxClass.define_overloaded name (c, rhs))); |
293 val (global_def, lthy3) = lthy2 |
301 val (global_def, lthy3) = lthy2 |
294 |> LocalTheory.theory_result (define_const name' (lhs', rhs')); |
302 |> LocalTheory.theory_result (define_const (Name.name_of name') (lhs', rhs')); |
295 val def = LocalDefs.trans_terms lthy3 |
303 val def = LocalDefs.trans_terms lthy3 |
296 [(*c == global.c xs*) local_def, |
304 [(*c == global.c xs*) local_def, |
297 (*global.c xs == rhs'*) global_def, |
305 (*global.c xs == rhs'*) global_def, |
298 (*rhs' == rhs*) Thm.symmetric rhs_conv]; |
306 (*rhs' == rhs*) Thm.symmetric rhs_conv]; |
299 |
307 |
316 val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts; |
324 val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts; |
317 |
325 |
318 (*axioms*) |
326 (*axioms*) |
319 val hyps = map Thm.term_of (Assumption.assms_of lthy'); |
327 val hyps = map Thm.term_of (Assumption.assms_of lthy'); |
320 fun axiom ((name, atts), props) thy = thy |
328 fun axiom ((name, atts), props) thy = thy |
321 |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props) |
329 |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi (Name.name_of name) props) |
322 |-> (fn ths => pair ((name, atts), [(ths, [])])); |
330 |-> (fn ths => pair ((name, atts), [(ths, [])])); |
323 in |
331 in |
324 lthy' |
332 lthy' |
325 |> fold Variable.declare_term expanded_props |
333 |> fold Variable.declare_term expanded_props |
326 |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts) |
334 |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts) |