wenzelm@20894: (* Title: Pure/Isar/theory_target.ML wenzelm@20894: ID: $Id$ wenzelm@20894: Author: Makarius wenzelm@20894: wenzelm@21594: Common theory/locale targets. wenzelm@20894: *) wenzelm@20894: wenzelm@20894: signature THEORY_TARGET = wenzelm@20894: sig wenzelm@21006: val peek: local_theory -> string option wenzelm@20962: val begin: bstring -> Proof.context -> local_theory wenzelm@20894: val init: xstring option -> theory -> local_theory wenzelm@20894: val init_i: string option -> theory -> local_theory wenzelm@20894: end; wenzelm@20894: wenzelm@20894: structure TheoryTarget: THEORY_TARGET = wenzelm@20894: struct wenzelm@20894: wenzelm@21594: wenzelm@20894: (** locale targets **) wenzelm@20894: wenzelm@21006: (* context data *) wenzelm@21006: wenzelm@21006: structure Data = ProofDataFun wenzelm@21006: ( wenzelm@21006: val name = "Isar/theory_target"; wenzelm@21006: type T = string option; wenzelm@21006: fun init _ = NONE; wenzelm@21006: fun print _ _ = (); wenzelm@21006: ); wenzelm@21006: wenzelm@21006: val _ = Context.add_setup Data.init; wenzelm@21006: val peek = Data.get; wenzelm@21006: wenzelm@21006: wenzelm@20894: (* pretty *) wenzelm@20894: wenzelm@20894: fun pretty loc ctxt = wenzelm@20894: let wenzelm@20894: val thy = ProofContext.theory_of ctxt; wenzelm@20894: val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt)); wenzelm@21585: val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt)); wenzelm@20894: val elems = wenzelm@20894: (if null fixes then [] else [Element.Fixes fixes]) @ wenzelm@20894: (if null assumes then [] else [Element.Assumes assumes]); wenzelm@20894: in wenzelm@20984: if loc = "" then wenzelm@20984: [Pretty.block wenzelm@20984: [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy), wenzelm@20984: Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]] wenzelm@20984: else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)] wenzelm@20984: else wenzelm@20984: [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =") wenzelm@20984: (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)] wenzelm@20894: end; wenzelm@20894: wenzelm@20894: wenzelm@20894: (* consts *) wenzelm@20894: wenzelm@21594: fun consts is_loc depends decls lthy = wenzelm@20894: let wenzelm@21533: val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy))); wenzelm@20894: wenzelm@20894: fun const ((c, T), mx) thy = wenzelm@20894: let wenzelm@20894: val U = map #2 xs ---> T; wenzelm@21533: val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs); wenzelm@21533: val thy' = Sign.add_consts_authentic [(c, U, Syntax.unlocalize_mixfix is_loc mx)] thy; wenzelm@21533: in (((c, mx), t), thy') end; wenzelm@20894: wenzelm@21533: val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls); wenzelm@21533: val defs = abbrs |> map (fn (x, t) => (x, (("", []), t))); wenzelm@20894: in wenzelm@20894: lthy' wenzelm@21570: |> is_loc ? LocalTheory.abbrevs Syntax.default_mode abbrs wenzelm@21611: |> LocalDefs.add_defs defs |>> map (apsnd snd) wenzelm@20894: end; wenzelm@20894: wenzelm@20894: wenzelm@20894: (* defs *) wenzelm@20894: wenzelm@21594: local wenzelm@21594: wenzelm@21570: infix also; wenzelm@21611: wenzelm@21570: fun eq1 also eq2 = wenzelm@21570: eq2 COMP (eq1 COMP (Drule.incr_indexes2 eq1 eq2 transitive_thm)); wenzelm@21570: wenzelm@21594: fun expand_term ctxt t = wenzelm@21570: let wenzelm@21570: val thy = ProofContext.theory_of ctxt; wenzelm@21570: val thy_ctxt = ProofContext.init thy; wenzelm@21570: val ct = Thm.cterm_of thy t; wenzelm@21594: val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term; wenzelm@21570: in (Thm.term_of ct', Tactic.rewrite true defs ct) end; wenzelm@21570: wenzelm@20894: fun add_def (name, prop) = wenzelm@20894: Theory.add_defs_i false false [(name, prop)] #> wenzelm@20894: (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy)); wenzelm@20894: wenzelm@20894: in wenzelm@20894: wenzelm@21570: fun defs kind args lthy0 = wenzelm@20894: let wenzelm@21533: fun def ((c, mx), ((name, atts), rhs)) lthy1 = wenzelm@20894: let wenzelm@21594: val (rhs', rhs_conv) = expand_term lthy0 rhs; wenzelm@21570: val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' []; wenzelm@20894: wenzelm@21570: val ([(lhs, lhs_def)], lthy2) = lthy1 wenzelm@21570: |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)]; wenzelm@21570: val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def)); wenzelm@21533: wenzelm@21570: val name' = Thm.def_name_optional c name; wenzelm@21570: val (def, lthy3) = lthy2 wenzelm@20894: |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs'))); wenzelm@20894: wenzelm@21533: val eq = wenzelm@21570: (*c == loc.c xs*) lhs_def wenzelm@21570: (*lhs' == rhs'*) also def wenzelm@21570: (*rhs' == rhs*) also Thm.symmetric rhs_conv; wenzelm@20894: in ((lhs, ((name', atts), [([eq], [])])), lthy3) end; wenzelm@20894: wenzelm@21570: val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list; wenzelm@21433: val (res, lthy'') = lthy' |> LocalTheory.notes kind facts; wenzelm@20894: in (lhss ~~ map (apsnd the_single) res, lthy'') end; wenzelm@20894: wenzelm@20894: end; wenzelm@20894: wenzelm@20894: wenzelm@21611: (* axioms *) wenzelm@21611: wenzelm@21611: local wenzelm@21611: wenzelm@21611: fun add_axiom hyps (name, prop) thy = wenzelm@21611: let wenzelm@21611: val name' = if name = "" then "axiom_" ^ serial_string () else name; wenzelm@21611: val prop' = Logic.list_implies (hyps, prop); wenzelm@21611: val thy' = thy |> Theory.add_axioms_i [(name', prop')]; wenzelm@21611: val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name')); wenzelm@21611: val prems = map (Thm.assume o Thm.cterm_of thy') hyps; wenzelm@21611: in (Drule.implies_elim_list axm prems, thy') end; wenzelm@21611: wenzelm@21611: in wenzelm@21611: wenzelm@21611: fun axioms kind specs lthy = wenzelm@21611: let wenzelm@21611: val hyps = map Thm.term_of (Assumption.assms_of lthy); wenzelm@21611: fun axiom ((name, atts), props) thy = thy wenzelm@21611: |> fold_map (add_axiom hyps) (PureThy.name_multi name props) wenzelm@21611: |-> (fn ths => pair ((name, atts), [(ths, [])])); wenzelm@21611: in wenzelm@21611: lthy wenzelm@21611: |> fold (fold Variable.declare_term o snd) specs wenzelm@21611: |> LocalTheory.theory_result (fold_map axiom specs) wenzelm@21611: |-> LocalTheory.notes kind wenzelm@21611: end; wenzelm@21611: wenzelm@21611: end; wenzelm@21611: wenzelm@21611: wenzelm@20894: (* notes *) wenzelm@20894: wenzelm@21611: fun import_export_proof ctxt (name, raw_th) = wenzelm@21594: let wenzelm@21611: val thy = ProofContext.theory_of ctxt; wenzelm@21611: val thy_ctxt = ProofContext.init thy; wenzelm@21611: val certT = Thm.ctyp_of thy; wenzelm@21611: val cert = Thm.cterm_of thy; wenzelm@21594: wenzelm@21611: (*export assumes/defines*) wenzelm@21611: val th = Goal.norm_result raw_th; wenzelm@21611: val (defs, th') = LocalDefs.export ctxt thy_ctxt th; wenzelm@21594: val concl_conv = Tactic.rewrite true defs (Thm.cprop_of th); wenzelm@21611: val assms = map (Tactic.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt); wenzelm@21611: val nprems = Thm.nprems_of th' - Thm.nprems_of th; wenzelm@21611: wenzelm@21611: (*export fixes*) wenzelm@21611: val tfrees = map TFree (Drule.fold_terms Term.add_tfrees th' []); wenzelm@21611: val frees = map Free (Drule.fold_terms Term.add_frees th' []); wenzelm@21611: val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees)) wenzelm@21611: |> Variable.export ctxt thy_ctxt wenzelm@21611: |> Drule.zero_var_indexes_list; wenzelm@21611: wenzelm@21611: (*thm definition*) wenzelm@21613: val result = PureThy.name_thm true (name, Goal.close_result th''); wenzelm@21611: wenzelm@21611: (*import fixes*) wenzelm@21611: val (tvars, vars) = wenzelm@21611: chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs) wenzelm@21611: |>> map Logic.dest_type; wenzelm@21611: wenzelm@21611: val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees); wenzelm@21611: val inst = filter (is_Var o fst) (vars ~~ frees); wenzelm@21611: val cinstT = map (pairself certT o apfst TVar) instT; wenzelm@21611: val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst; wenzelm@21611: val result' = Thm.instantiate (cinstT, cinst) result; wenzelm@21611: wenzelm@21611: (*import assumes/defines*) wenzelm@21594: val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms); wenzelm@21611: val result'' = wenzelm@21611: (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of wenzelm@21611: NONE => raise THM ("Failed to re-import result", 0, [result']) wenzelm@21611: | SOME res => res) COMP (concl_conv COMP_INCR Drule.equal_elim_rule2) wenzelm@21613: |> Goal.norm_result; wenzelm@21611: wenzelm@21611: in (result'', result) end; wenzelm@21611: wenzelm@21594: fun notes loc kind facts lthy = wenzelm@21585: let wenzelm@21594: val is_loc = loc <> ""; wenzelm@21594: val thy = ProofContext.theory_of lthy; wenzelm@21615: val full = LocalTheory.full_name lthy; wenzelm@21585: wenzelm@21594: val facts' = facts wenzelm@21615: |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs)) wenzelm@21615: |> PureThy.map_facts (import_export_proof lthy); wenzelm@21611: val local_facts = PureThy.map_facts #1 facts' wenzelm@21611: |> Attrib.map_facts (Attrib.attribute_i thy); wenzelm@21611: val target_facts = PureThy.map_facts #1 facts' wenzelm@21611: |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy)); wenzelm@21611: val global_facts = PureThy.map_facts #2 facts' wenzelm@21594: |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy); wenzelm@21594: in wenzelm@21611: lthy |> LocalTheory.theory wenzelm@21611: (Sign.qualified_names wenzelm@21611: #> PureThy.note_thmss_i kind global_facts #> snd wenzelm@21611: #> Sign.restore_naming thy) wenzelm@21585: wenzelm@21611: |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts) wenzelm@21594: wenzelm@21433: |> ProofContext.set_stmt true wenzelm@20894: |> ProofContext.qualified_names wenzelm@21611: |> ProofContext.note_thmss_i kind local_facts wenzelm@20894: ||> ProofContext.restore_naming lthy wenzelm@21433: ||> ProofContext.restore_stmt lthy wenzelm@20894: end; wenzelm@20894: wenzelm@20894: wenzelm@21533: (* target declarations *) wenzelm@20894: wenzelm@21611: fun target_decl _ "" f = wenzelm@21533: LocalTheory.theory (Context.theory_map (f Morphism.identity)) #> wenzelm@21533: LocalTheory.target (Context.proof_map (f Morphism.identity)) wenzelm@21611: | target_decl add loc f = wenzelm@21533: LocalTheory.target (add loc (Context.proof_map o f)); wenzelm@20894: wenzelm@21611: val type_syntax = target_decl Locale.add_type_syntax; wenzelm@21611: val term_syntax = target_decl Locale.add_term_syntax; wenzelm@21611: val declaration = target_decl Locale.add_declaration; wenzelm@20894: wenzelm@21533: fun target_morphism loc lthy = wenzelm@21594: ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $> wenzelm@21613: Morphism.thm_morphism Goal.norm_result; wenzelm@21533: wenzelm@21533: fun target_name "" lthy = Sign.full_name (ProofContext.theory_of lthy) wenzelm@21533: | target_name _ lthy = ProofContext.full_name (LocalTheory.target_of lthy); wenzelm@21533: wenzelm@20894: wenzelm@20894: (* init and exit *) wenzelm@20894: wenzelm@21293: fun begin loc = wenzelm@21293: Data.put (if loc = "" then NONE else SOME loc) #> wenzelm@21533: LocalTheory.init (NameSpace.base loc) wenzelm@21293: {pretty = pretty loc, wenzelm@21594: consts = consts (loc <> ""), wenzelm@21293: axioms = axioms, wenzelm@21293: defs = defs, wenzelm@21293: notes = notes loc, wenzelm@21498: type_syntax = type_syntax loc, wenzelm@21293: term_syntax = term_syntax loc, wenzelm@21293: declaration = declaration loc, wenzelm@21533: target_morphism = target_morphism loc, wenzelm@21533: target_name = target_name loc, wenzelm@21293: reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc), wenzelm@21533: exit = LocalTheory.target_of}; wenzelm@20894: wenzelm@20962: fun init_i NONE thy = begin "" (ProofContext.init thy) wenzelm@20962: | init_i (SOME loc) thy = begin loc (Locale.init loc thy); wenzelm@20894: wenzelm@21276: fun init (SOME "-") thy = init_i NONE thy wenzelm@21276: | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy; wenzelm@20894: wenzelm@20894: end;