src/Pure/Isar/locale.ML
changeset 28739 bbb5f83ce602
parent 28734 19277c7a160c
child 28792 1d80cee865de
equal deleted inserted replaced
28738:677312de6608 28739:bbb5f83ce602
    96   val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
    96   val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
    97   val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
    97   val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
    98   val add_declaration: string -> declaration -> Proof.context -> Proof.context
    98   val add_declaration: string -> declaration -> Proof.context -> Proof.context
    99 
    99 
   100   (* Interpretation *)
   100   (* Interpretation *)
   101   val get_interpret_morph: theory -> string -> string ->
   101   val get_interpret_morph: theory -> string -> bool * string -> string ->
   102     (Morphism.morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list))) ->
   102     (Morphism.morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list))) ->
   103     string -> term list -> Morphism.morphism
   103     string -> term list -> Morphism.morphism
   104   val interpretation_i: (Proof.context -> Proof.context) ->
   104   val interpretation_i: (Proof.context -> Proof.context) ->
   105     bool * string -> expr ->
   105     bool * string -> expr ->
   106     term option list * (Attrib.binding * term) list ->
   106     term option list * (Attrib.binding * term) list ->
   133 fun merge_lists _ xs [] = xs
   133 fun merge_lists _ xs [] = xs
   134   | merge_lists _ [] ys = ys
   134   | merge_lists _ [] ys = ys
   135   | merge_lists eq xs ys = xs @ filter_out (member eq xs) ys;
   135   | merge_lists eq xs ys = xs @ filter_out (member eq xs) ys;
   136 
   136 
   137 fun merge_alists eq xs = merge_lists (eq_fst eq) xs;
   137 fun merge_alists eq xs = merge_lists (eq_fst eq) xs;
       
   138 
       
   139 
       
   140 (* auxiliary: noting with structured name bindings *)
       
   141 
       
   142 fun global_note_prefix kind ((bnd, atts), facts_atts_s) thy =
       
   143   (*FIXME belongs to theory_target.ML*)
       
   144   let
       
   145     val prfx = Name.prefix_of bnd;
       
   146     val name = Name.name_of bnd;
       
   147   in
       
   148     thy
       
   149     |> Sign.qualified_names
       
   150     |> fold (fn (prfx_seg, sticky) =>
       
   151         (if sticky then Sign.sticky_prefix else Sign.add_path) prfx_seg) prfx
       
   152     |> yield_singleton (PureThy.note_thmss kind) ((Name.binding name, atts), facts_atts_s)
       
   153     ||> Sign.restore_naming thy
       
   154   end;
       
   155 
       
   156 fun local_note_prefix kind ((bnd, atts), facts_atts_s) ctxt =
       
   157   (*FIXME belongs to theory_target.ML ?*)
       
   158   let
       
   159     val prfx = Name.prefix_of bnd;
       
   160     val name = Name.name_of bnd;
       
   161   in
       
   162     ctxt
       
   163     |> ProofContext.qualified_names
       
   164     |> fold (fn (prfx_seg, sticky) =>
       
   165         (if sticky then ProofContext.sticky_prefix else ProofContext.add_path) prfx_seg) prfx
       
   166     |> yield_singleton (ProofContext.note_thmss_i kind) ((Name.binding name, atts), facts_atts_s)
       
   167     ||> ProofContext.restore_naming ctxt
       
   168   end;
   138 
   169 
   139 
   170 
   140 (** locale elements and expressions **)
   171 (** locale elements and expressions **)
   141 
   172 
   142 datatype ctxt = datatype Element.ctxt;
   173 datatype ctxt = datatype Element.ctxt;
   547             Pretty.brk 1, (Pretty.quote o Syntax.pretty_typ ctxt) (type_of t)]
   578             Pretty.brk 1, (Pretty.quote o Syntax.pretty_typ ctxt) (type_of t)]
   548           else prt_term t;
   579           else prt_term t;
   549     val prt_thm = prt_term o prop_of;
   580     val prt_thm = prt_term o prop_of;
   550     fun prt_inst ts =
   581     fun prt_inst ts =
   551         Pretty.enclose "(" ")" (Pretty.breaks (map prt_term' ts));
   582         Pretty.enclose "(" ")" (Pretty.breaks (map prt_term' ts));
   552     fun prt_prfx ((false, prfx), pprfx) = [Pretty.str prfx, Pretty.brk 1, Pretty.str "(optional)", Pretty.brk 1, Pretty.str pprfx]
   583     fun prt_prfx ((false, prfx), param_prfx) = [Pretty.str prfx, Pretty.brk 1, Pretty.str "(optional)", Pretty.brk 1, Pretty.str param_prfx]
   553       | prt_prfx ((true, prfx), pprfx) = [Pretty.str prfx, Pretty.brk 1, Pretty.str pprfx];
   584       | prt_prfx ((true, prfx), param_prfx) = [Pretty.str prfx, Pretty.brk 1, Pretty.str param_prfx];
   554     fun prt_eqns [] = Pretty.str "no equations."
   585     fun prt_eqns [] = Pretty.str "no equations."
   555       | prt_eqns eqns = Pretty.block (Pretty.str "equations:" :: Pretty.brk 1 ::
   586       | prt_eqns eqns = Pretty.block (Pretty.str "equations:" :: Pretty.brk 1 ::
   556           Pretty.breaks (map prt_thm eqns));
   587           Pretty.breaks (map prt_thm eqns));
   557     fun prt_core ts eqns =
   588     fun prt_core ts eqns =
   558           [prt_inst ts, Pretty.fbrk, prt_eqns (Termtab.dest eqns |> map snd)];
   589           [prt_inst ts, Pretty.fbrk, prt_eqns (Termtab.dest eqns |> map snd)];
   943         val mode' = map_mode (map (Element.map_witness (inst_wit all_params))) mode;
   974         val mode' = map_mode (map (Element.map_witness (inst_wit all_params))) mode;
   944         val ren = map fst ps ~~ map (fn p => (p, lookup_syn p)) params;
   975         val ren = map fst ps ~~ map (fn p => (p, lookup_syn p)) params;
   945         val [env] = unify_parms ctxt all_params [map (apfst (Element.rename ren) o apsnd SOME) ps];
   976         val [env] = unify_parms ctxt all_params [map (apfst (Element.rename ren) o apsnd SOME) ps];
   946         val elem_morphism =
   977         val elem_morphism =
   947           Element.rename_morphism ren $>
   978           Element.rename_morphism ren $>
   948           Morphism.name_morphism
   979           Morphism.name_morphism (Name.add_prefix false (param_prefix params)) $>
   949             (Name.map_name (NameSpace.qualified (param_prefix params))) $>
       
   950           Element.instT_morphism thy env;
   980           Element.instT_morphism thy env;
   951         val elems' = map (Element.morph_ctxt elem_morphism) elems;
   981         val elems' = map (Element.morph_ctxt elem_morphism) elems;
   952       in (((name, map (apsnd SOME) locale_params'), mode'), elems') end;
   982       in (((name, map (apsnd SOME) locale_params'), mode'), elems') end;
   953 
   983 
   954     (* parameters, their types and syntax *)
   984     (* parameters, their types and syntax *)
  1003   | activate_elem _ _ (Defines defs) (ctxt, Derived ths) =
  1033   | activate_elem _ _ (Defines defs) (ctxt, Derived ths) =
  1004       ([], (ctxt, Derived ths))
  1034       ([], (ctxt, Derived ths))
  1005   | activate_elem _ is_ext (Notes (kind, facts)) (ctxt, mode) =
  1035   | activate_elem _ is_ext (Notes (kind, facts)) (ctxt, mode) =
  1006       let
  1036       let
  1007         val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts;
  1037         val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts;
  1008         val (res, ctxt') = ctxt |> ProofContext.note_thmss_i kind facts';
  1038         val (res, ctxt') = ctxt |> fold_map (local_note_prefix kind) facts';
  1009       in (if is_ext then (map (#1 o #1) facts' ~~ map #2 res) else [], (ctxt', mode)) end;
  1039       in (if is_ext then (map (#1 o #1) facts' ~~ map #2 res) else [], (ctxt', mode)) end;
  1010 
  1040 
  1011 fun activate_elems ax_in_ctxt (((name, ps), mode), elems) ctxt =
  1041 fun activate_elems ax_in_ctxt (((name, ps), mode), elems) ctxt =
  1012   let
  1042   let
  1013     val thy = ProofContext.theory_of ctxt;
  1043     val thy = ProofContext.theory_of ctxt;
  1584 
  1614 
  1585 
  1615 
  1586 
  1616 
  1587 (** store results **)
  1617 (** store results **)
  1588 
  1618 
  1589 (* naming of interpreted theorems *)
       
  1590 
       
  1591 fun global_note_prefix kind ((bnd, atts), facts_atts_s) thy =
       
  1592   (*FIXME belongs to theory_target.ML*)
       
  1593   let
       
  1594     val prfx = Name.prefix_of bnd;
       
  1595     val name = Name.name_of bnd;
       
  1596   in
       
  1597     thy
       
  1598     |> Sign.qualified_names
       
  1599     |> fold (fn (prfx_seg, fully_qualified) =>
       
  1600         (if fully_qualified then Sign.sticky_prefix else Sign.add_path) prfx_seg) prfx
       
  1601     |> yield_singleton (PureThy.note_thmss kind) ((Name.binding name, atts), facts_atts_s)
       
  1602     ||> Sign.restore_naming thy
       
  1603   end;
       
  1604 
       
  1605 fun local_note_prefix kind ((bnd, atts), facts_atts_s) ctxt =
       
  1606   (*FIXME belongs to theory_target.ML ?*)
       
  1607   let
       
  1608     val prfx = Name.prefix_of bnd;
       
  1609     val name = Name.name_of bnd;
       
  1610   in
       
  1611     ctxt
       
  1612     |> ProofContext.qualified_names
       
  1613     |> fold (fn (prfx_seg, fully_qualified) =>
       
  1614         (if fully_qualified then ProofContext.sticky_prefix else ProofContext.add_path) prfx_seg) prfx
       
  1615     |> yield_singleton (ProofContext.note_thmss_i kind) ((Name.binding name, atts), facts_atts_s)
       
  1616     ||> ProofContext.restore_naming ctxt
       
  1617   end;
       
  1618 
       
  1619 
       
  1620 (* join equations of an id with already accumulated ones *)
  1619 (* join equations of an id with already accumulated ones *)
  1621 
  1620 
  1622 fun join_eqns get_reg id eqns =
  1621 fun join_eqns get_reg id eqns =
  1623   let
  1622   let
  1624     val eqns' = case get_reg id
  1623     val eqns' = case get_reg id
  1654       fold_map (join_eqns (get_local_registration ctxt imprt))
  1653       fold_map (join_eqns (get_local_registration ctxt imprt))
  1655         (map fst inst_ids) Termtab.empty |> snd |> Termtab.dest |> map snd;
  1654         (map fst inst_ids) Termtab.empty |> snd |> Termtab.dest |> map snd;
  1656   in ((tinst, inst), wits, eqns) end;
  1655   in ((tinst, inst), wits, eqns) end;
  1657 
  1656 
  1658 
  1657 
  1659 (* compute morphism and apply to args *)
  1658 (* compute and apply morphism *)
  1660 
  1659 
  1661 fun inst_morph thy prfx param_prfx insts prems eqns export =
  1660 fun add_prefixes loc (sticky, interp_prfx) param_prfx bnd =
       
  1661   bnd
       
  1662   |> (if sticky andalso Name.name_of bnd <> "" andalso param_prfx <> ""
       
  1663         then Name.add_prefix false param_prfx else I)
       
  1664   |> Name.add_prefix sticky interp_prfx
       
  1665   |> Name.add_prefix false (NameSpace.base loc ^ "_locale");
       
  1666 
       
  1667 fun inst_morph thy loc (sticky, interp_prfx) param_prfx insts prems eqns export =
  1662   let
  1668   let
  1663     (* standardise export morphism *)
  1669     (* standardise export morphism *)
  1664     val exp_fact = Drule.zero_var_indexes_list o map Thm.strip_shyps o Morphism.fact export;
  1670     val exp_fact = Drule.zero_var_indexes_list o map Thm.strip_shyps o Morphism.fact export;
  1665     val exp_term = TermSubst.zero_var_indexes o Morphism.term export;
  1671     val exp_term = TermSubst.zero_var_indexes o Morphism.term export;
  1666       (* FIXME sync with exp_fact *)
  1672       (* FIXME sync with exp_fact *)
  1667     val exp_typ = Logic.type_map exp_term;
  1673     val exp_typ = Logic.type_map exp_term;
  1668     val export' =
  1674     val export' =
  1669       Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
  1675       Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
  1670   in
  1676   in
  1671     Morphism.name_morphism (Name.qualified prfx o Name.qualified param_prfx) $>
  1677     Morphism.name_morphism (add_prefixes loc (sticky, interp_prfx) param_prfx) $>
  1672       Element.inst_morphism thy insts $> Element.satisfy_morphism prems $>
  1678       Element.inst_morphism thy insts $> Element.satisfy_morphism prems $>
  1673       Morphism.term_morphism (MetaSimplifier.rewrite_term thy eqns []) $>
  1679       Morphism.term_morphism (MetaSimplifier.rewrite_term thy eqns []) $>
  1674       Morphism.thm_morphism (MetaSimplifier.rewrite_rule eqns) $>
  1680       Morphism.thm_morphism (MetaSimplifier.rewrite_rule eqns) $>
  1675       export'
  1681       export'
  1676   end;
  1682   end;
  1677 
  1683 
  1678 fun morph_ctxt' phi = Element.map_ctxt
  1684 fun activate_note thy loc (sticky, interp_prfx) param_prfx attrib insts prems eqns exp =
  1679   {name = I,
  1685   (Element.facts_map o Element.morph_ctxt)
  1680    var = Morphism.var phi,
  1686       (inst_morph thy loc (sticky, interp_prfx) param_prfx insts prems eqns exp)
  1681    typ = Morphism.typ phi,
  1687   #> Attrib.map_facts attrib (*o Args.morph_values (Morphism.name_morphism (add_prefixes loc (sticky, interp_prfx) param_prfx))*);
  1682    term = Morphism.term phi,
       
  1683    fact = Morphism.fact phi,
       
  1684    attrib = Args.morph_values phi};
       
  1685 
       
  1686 fun add_prefixes loc (fully_qualified, interp_prfx) pprfx bnd =
       
  1687   bnd
       
  1688   |> (if fully_qualified andalso Name.name_of bnd <> "" andalso pprfx <> ""
       
  1689         then Name.add_prefix false pprfx else I)
       
  1690   |> Name.add_prefix fully_qualified interp_prfx
       
  1691   |> Name.add_prefix false (NameSpace.base loc ^ "_locale");
       
  1692 
       
  1693 fun interpret_args thy loc (fully_qualified, interp_prfx) pprfx inst attrib args =
       
  1694   args
       
  1695   |> Element.facts_map (morph_ctxt' inst)
       
  1696        (*FIXME: morph_ctxt' suppresses application of name morphism.*)
       
  1697   |> Attrib.map_facts attrib
       
  1698   |> (map o apfst o apfst) (add_prefixes loc (fully_qualified, interp_prfx) pprfx);
       
  1699 
  1688 
  1700 
  1689 
  1701 (* public interface to interpretation morphism *)
  1690 (* public interface to interpretation morphism *)
  1702 
  1691 
  1703 fun get_interpret_morph thy prfx param_prfx (exp, imp) target ext_ts =
  1692 fun get_interpret_morph thy loc (sticky, interp_prfx) param_prfx (exp, imp) target ext_ts =
  1704   let
  1693   let
  1705     val parms = the_locale thy target |> #params |> map fst;
  1694     val parms = the_locale thy target |> #params |> map fst;
  1706     val ids = flatten (ProofContext.init thy, intern_expr thy)
  1695     val ids = flatten (ProofContext.init thy, intern_expr thy)
  1707       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst;
  1696       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst;
  1708     val (insts, prems, eqns) = collect_witnesses (ProofContext.init thy) imp parms ids ext_ts;
  1697     val (insts, prems, eqns) = collect_witnesses (ProofContext.init thy) imp parms ids ext_ts;
  1709   in
  1698   in
  1710     inst_morph thy prfx param_prfx insts prems eqns exp
  1699     inst_morph thy loc (sticky, interp_prfx) param_prfx insts prems eqns exp
  1711   end;
  1700   end;
  1712 
  1701 
  1713 (* store instantiations of args for all registered interpretations
  1702 (* store instantiations of args for all registered interpretations
  1714    of the theory *)
  1703    of the theory *)
  1715 
  1704 
  1720       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst;
  1709       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst;
  1721 
  1710 
  1722     val regs = get_global_registrations thy target;
  1711     val regs = get_global_registrations thy target;
  1723     (* add args to thy for all registrations *)
  1712     (* add args to thy for all registrations *)
  1724 
  1713 
  1725     fun activate (ext_ts, (((fully_qualified, interp_prfx), pprfx), (exp, imp), _, _)) thy =
  1714     fun activate (ext_ts, (((sticky, interp_prfx), param_prfx), (exp, imp), _, _)) thy =
  1726       let
  1715       let
  1727         val (insts, prems, eqns) = collect_witnesses (ProofContext.init thy) imp parms ids ext_ts;
  1716         val (insts, prems, eqns) = collect_witnesses (ProofContext.init thy) imp parms ids ext_ts;
  1728         val attrib = Attrib.attribute_i thy;
       
  1729         val args' = args
  1717         val args' = args
  1730           |> interpret_args thy target (fully_qualified, interp_prfx) pprfx
  1718           |> activate_note thy target (sticky, interp_prfx) param_prfx
  1731                (inst_morph thy interp_prfx pprfx insts prems eqns exp) attrib;
  1719                (Attrib.attribute_i thy) insts prems eqns exp;
  1732       in
  1720       in
  1733         thy
  1721         thy
  1734         |> fold (snd oo global_note_prefix kind) args'
  1722         |> fold (snd oo global_note_prefix kind) args'
  1735       end;
  1723       end;
  1736   in fold activate regs thy end;
  1724   in fold activate regs thy end;
  2100   | extract_asms_elems ((id, Derived _), _) = (id, []);
  2088   | extract_asms_elems ((id, Derived _), _) = (id, []);
  2101 
  2089 
  2102 
  2090 
  2103 (* activate instantiated facts in theory or context *)
  2091 (* activate instantiated facts in theory or context *)
  2104 
  2092 
  2105 fun gen_activate_facts_elemss mk_ctxt note note_interp attrib put_reg add_wit add_eqn
  2093 fun gen_activate_facts_elemss mk_ctxt note attrib put_reg add_wit add_eqn
  2106         prfx all_elemss pss propss eq_attns (exp, imp) thmss thy_ctxt =
  2094         prfx all_elemss pss propss eq_attns (exp, imp) thmss thy_ctxt =
  2107   let
  2095   let
  2108     val ctxt = mk_ctxt thy_ctxt;
  2096     val ctxt = mk_ctxt thy_ctxt;
  2109     fun get_reg thy_ctxt = get_local_registration (mk_ctxt thy_ctxt);
  2097     fun get_reg thy_ctxt = get_local_registration (mk_ctxt thy_ctxt);
  2110     fun test_reg thy_ctxt = test_local_registration (mk_ctxt thy_ctxt);
  2098     fun test_reg thy_ctxt = test_local_registration (mk_ctxt thy_ctxt);
  2137       (* add witnesses of Derived elements *)
  2125       (* add witnesses of Derived elements *)
  2138       |> fold (fn (id, thms) => fold (add_wit id o Element.morph_witness (Element.satisfy_morphism prems)) thms)
  2126       |> fold (fn (id, thms) => fold (add_wit id o Element.morph_witness (Element.satisfy_morphism prems)) thms)
  2139          (map_filter (fn ((_, Assumed _), _) => NONE
  2127          (map_filter (fn ((_, Assumed _), _) => NONE
  2140             | ((id, Derived thms), _) => SOME (id, thms)) new_elemss)
  2128             | ((id, Derived thms), _) => SOME (id, thms)) new_elemss)
  2141 
  2129 
  2142     fun activate_elem loc (fully_qualified, interp_prfx) pprfx insts prems eqns exp (Notes (kind, facts)) thy_ctxt =
  2130     fun activate_elem loc (sticky, interp_prfx) param_prfx insts prems eqns exp (Notes (kind, facts)) thy_ctxt =
  2143         let
  2131         let
  2144           val ctxt = mk_ctxt thy_ctxt;
  2132           val ctxt = mk_ctxt thy_ctxt;
  2145           val thy = ProofContext.theory_of ctxt;
  2133           val thy = ProofContext.theory_of ctxt;
  2146           val facts' = facts
  2134           val facts' = facts
  2147             |> interpret_args (ProofContext.theory_of ctxt) loc
  2135             |> activate_note thy loc (sticky, interp_prfx) param_prfx
  2148                  (fully_qualified, interp_prfx) pprfx
  2136                  (attrib thy_ctxt) insts prems eqns exp;
  2149                    (inst_morph thy interp_prfx pprfx insts prems eqns exp) (attrib thy_ctxt);
       
  2150         in 
  2137         in 
  2151           thy_ctxt
  2138           thy_ctxt
  2152           |> fold (snd oo note_interp kind) facts'
  2139           |> fold (snd oo note kind) facts'
  2153         end
  2140         end
  2154       | activate_elem _ _ _ _ _ _ _ _ thy_ctxt = thy_ctxt;
  2141       | activate_elem _ _ _ _ _ _ _ _ thy_ctxt = thy_ctxt;
  2155 
  2142 
  2156     fun activate_elems ((((loc, ext_ts), _), _), ps) thy_ctxt =
  2143     fun activate_elems (((loc, ext_ts), _), _) ps thy_ctxt =
  2157       let
  2144       let
  2158         val ctxt = mk_ctxt thy_ctxt;
  2145         val ctxt = mk_ctxt thy_ctxt;
  2159         val thy = ProofContext.theory_of ctxt;
  2146         val thy = ProofContext.theory_of ctxt;
  2160         val {params, elems, ...} = the_locale thy loc;
  2147         val {params, elems, ...} = the_locale thy loc;
  2161         val parms = map fst params;
  2148         val parms = map fst params;
  2162         val pprfx = param_prefix ps;
  2149         val param_prfx = param_prefix ps;
  2163         val ids = flatten (ProofContext.init thy, intern_expr thy)
  2150         val ids = flatten (ProofContext.init thy, intern_expr thy)
  2164           (([], Symtab.empty), Expr (Locale loc)) |> fst |> fst;
  2151           (([], Symtab.empty), Expr (Locale loc)) |> fst |> fst;
  2165         val (insts, prems, eqns) = collect_witnesses ctxt imp parms ids ext_ts;
  2152         val (insts, prems, eqns) = collect_witnesses ctxt imp parms ids ext_ts;
  2166       in
  2153       in
  2167         thy_ctxt
  2154         thy_ctxt
  2168         |> fold (activate_elem loc prfx pprfx insts prems eqns exp o fst) elems
  2155         |> fold (activate_elem loc prfx param_prfx insts prems eqns exp o fst) elems
  2169       end;
  2156       end;
  2170 
  2157 
  2171   in
  2158   in
  2172     thy_ctxt''
  2159     thy_ctxt''
  2173     (* add equations as lemmas to context *)
  2160     (* add equations as lemmas to context *)
  2174     |> fold (fn (attns, thms) =>
  2161     |> (fold2 o fold2) (fn attn => fn thm => snd o note Thm.lemmaK
  2175          fold (fn (attn, thm) => note "lemma"
  2162          ((apsnd o map) (attrib thy_ctxt'') attn, [([Element.conclude_witness thm], [])]))
  2176            [(apsnd (map (attrib thy_ctxt'')) attn,
  2163             (unflat eq_thms eq_attns) eq_thms
  2177              [([Element.conclude_witness thm], [])])] #> snd)
       
  2178            (attns ~~ thms)) (unflat eq_thms eq_attns ~~ eq_thms)
       
  2179     (* add interpreted facts *)
  2164     (* add interpreted facts *)
  2180     |> fold activate_elems (new_elemss ~~ new_pss)
  2165     |> fold2 activate_elems new_elemss new_pss
  2181   end;
  2166   end;
  2182 
  2167 
  2183 fun global_activate_facts_elemss x = gen_activate_facts_elemss
  2168 fun global_activate_facts_elemss x = gen_activate_facts_elemss
  2184   ProofContext.init
  2169   ProofContext.init
  2185   PureThy.note_thmss
       
  2186   global_note_prefix
  2170   global_note_prefix
  2187   Attrib.attribute_i
  2171   Attrib.attribute_i
  2188   put_global_registration
  2172   put_global_registration
  2189   add_global_witness
  2173   add_global_witness
  2190   add_global_equation
  2174   add_global_equation
  2191   x;
  2175   x;
  2192 
  2176 
  2193 fun local_activate_facts_elemss x = gen_activate_facts_elemss
  2177 fun local_activate_facts_elemss x = gen_activate_facts_elemss
  2194   I
  2178   I
  2195   ProofContext.note_thmss_i
       
  2196   local_note_prefix
  2179   local_note_prefix
  2197   (Attrib.attribute_i o ProofContext.theory_of)
  2180   (Attrib.attribute_i o ProofContext.theory_of)
  2198   put_local_registration
  2181   put_local_registration
  2199   add_local_witness
  2182   add_local_witness
  2200   add_local_equation
  2183   add_local_equation
  2371         - add registrations to the target locale
  2354         - add registrations to the target locale
  2372         - add induced registrations for all global registrations of
  2355         - add induced registrations for all global registrations of
  2373           the target, unless already present
  2356           the target, unless already present
  2374         - add facts of induced registrations to theory **)
  2357         - add facts of induced registrations to theory **)
  2375 
  2358 
  2376     fun activate thmss thy = let
  2359     fun activate thmss thy =
       
  2360       let
  2377         val satisfy = Element.satisfy_thm (flat thmss);
  2361         val satisfy = Element.satisfy_thm (flat thmss);
  2378         val ids_elemss_thmss = ids_elemss ~~ thmss;
  2362         val ids_elemss_thmss = ids_elemss ~~ thmss;
  2379         val regs = get_global_registrations thy target;
  2363         val regs = get_global_registrations thy target;
  2380 
  2364 
  2381         fun activate_id (((id, Assumed _), _), thms) thy =
  2365         fun activate_id (((id, Assumed _), _), thms) thy =
  2382             thy |> put_registration_in_locale target id
  2366             thy |> put_registration_in_locale target id
  2383                 |> fold (add_witness_in_locale target id) thms
  2367                 |> fold (add_witness_in_locale target id) thms
  2384           | activate_id _ thy = thy;
  2368           | activate_id _ thy = thy;
  2385 
  2369 
  2386         fun activate_reg (ext_ts, (((fully_qualified, interp_prfx), pprfx), (exp, imp), _, _)) thy =
  2370         fun activate_reg (ext_ts, (((sticky, interp_prfx), param_prfx), (exp, imp), _, _)) thy =
  2387           let
  2371           let
  2388             val (insts, wits, _) = collect_witnesses (ProofContext.init thy) imp fixed target_ids ext_ts;
  2372             val (insts, wits, _) = collect_witnesses (ProofContext.init thy) imp fixed target_ids ext_ts;
  2389             fun inst_parms ps = map
  2373             val inst_parms = map (the o AList.lookup (op =) (map #1 fixed ~~ ext_ts));
  2390                   (the o AList.lookup (op =) (map #1 fixed ~~ ext_ts)) ps;
       
  2391             val disch = Element.satisfy_thm wits;
  2374             val disch = Element.satisfy_thm wits;
  2392             val new_elemss = filter (fn (((name, ps), _), _) =>
  2375             val new_elemss = filter (fn (((name, ps), _), _) =>
  2393                 not (test_global_registration thy (name, inst_parms ps))) (ids_elemss);
  2376                 not (test_global_registration thy (name, inst_parms ps))) (ids_elemss);
  2394             fun activate_assumed_id (((_, Derived _), _), _) thy = thy
  2377             fun activate_assumed_id (((_, Derived _), _), _) thy = thy
  2395               | activate_assumed_id ((((name, ps), Assumed _), _), thms) thy = let
  2378               | activate_assumed_id ((((name, ps), Assumed _), _), thms) thy = let
  2396                 val ps' = inst_parms ps;
  2379                 val ps' = inst_parms ps;
  2397               in
  2380               in
  2398                 if test_global_registration thy (name, ps')
  2381                 if test_global_registration thy (name, ps')
  2399                 then thy
  2382                 then thy
  2400                 else thy
  2383                 else thy
  2401                   |> put_global_registration (name, ps') ((fully_qualified, interp_prfx), param_prefix ps) (exp, imp)
  2384                   |> put_global_registration (name, ps') ((sticky, interp_prfx), param_prefix ps) (exp, imp)
  2402                   |> fold (fn witn => fn thy => add_global_witness (name, ps')
  2385                   |> fold (fn witn => fn thy => add_global_witness (name, ps')
  2403                      (Element.morph_witness (Element.inst_morphism thy insts) witn) thy) thms
  2386                      (Element.morph_witness (Element.inst_morphism thy insts) witn) thy) thms
  2404               end;
  2387               end;
  2405 
  2388 
  2406             fun activate_derived_id ((_, Assumed _), _) thy = thy
  2389             fun activate_derived_id ((_, Assumed _), _) thy = thy
  2408                 val ps' = inst_parms ps;
  2391                 val ps' = inst_parms ps;
  2409               in
  2392               in
  2410                 if test_global_registration thy (name, ps')
  2393                 if test_global_registration thy (name, ps')
  2411                 then thy
  2394                 then thy
  2412                 else thy
  2395                 else thy
  2413                   |> put_global_registration (name, ps') ((fully_qualified, interp_prfx), param_prefix ps) (exp, imp)
  2396                   |> put_global_registration (name, ps') ((sticky, interp_prfx), param_prefix ps) (exp, imp)
  2414                   |> fold (fn witn => fn thy => add_global_witness (name, ps')
  2397                   |> fold (fn witn => fn thy => add_global_witness (name, ps')
  2415                        (witn |> Element.map_witness (fn (t, th) =>  (* FIXME *)
  2398                        (witn |> Element.map_witness (fn (t, th) =>  (* FIXME *)
  2416                        (Element.inst_term insts t,
  2399                        (Element.inst_term insts t,
  2417                         disch (Element.inst_thm thy insts (satisfy th))))) thy) ths
  2400                         disch (Element.inst_thm thy insts (satisfy th))))) thy) ths
  2418               end;
  2401               end;
  2419 
  2402 
  2420             fun apply_prefix loc bnd =
       
  2421               let
       
  2422                 val param_prfx_name = Name.name_of bnd;
       
  2423                 val (param_prfx, name) = case NameSpace.explode param_prfx_name
       
  2424                  of [] => ([], "")
       
  2425                   | [name] => ([], name) (*heuristic for locales with no parameter*)
       
  2426                   | prfx :: name_segs => (if fully_qualified then [(prfx, false)] else [],
       
  2427                        NameSpace.implode name_segs);
       
  2428               in
       
  2429                 Name.binding name
       
  2430                 |> fold (uncurry Name.add_prefix o swap) param_prfx
       
  2431                 |> Name.add_prefix fully_qualified interp_prfx
       
  2432                 |> Name.add_prefix false (NameSpace.base loc ^ "_locale")
       
  2433               end;
       
  2434 
       
  2435             fun activate_elem (loc, ps) (Notes (kind, facts)) thy =
  2403             fun activate_elem (loc, ps) (Notes (kind, facts)) thy =
  2436                 let
  2404                 let
  2437                   val att_morphism =
  2405                   val att_morphism =
  2438                     Morphism.name_morphism (Name.qualified interp_prfx) $>
  2406                     Morphism.name_morphism (add_prefixes loc (sticky, interp_prfx) param_prfx) $>
  2439                       (* FIXME? treatment of parameter prefix *)
       
  2440                     Morphism.thm_morphism satisfy $>
  2407                     Morphism.thm_morphism satisfy $>
  2441                     Element.inst_morphism thy insts $>
  2408                     Element.inst_morphism thy insts $>
  2442                     Morphism.thm_morphism disch;
  2409                     Morphism.thm_morphism disch;
  2443                   val facts' = facts
  2410                   val facts' = facts
  2444                     |> Attrib.map_facts (Attrib.attribute_i thy o Args.morph_values att_morphism)
  2411                     |> Attrib.map_facts (Attrib.attribute_i thy o Args.morph_values att_morphism)
  2445                     |> (map o apsnd o map o apfst o map) (disch o Element.inst_thm thy insts o satisfy)
  2412                     |> (map o apsnd o map o apfst o map) (disch o Element.inst_thm thy insts o satisfy)
  2446                     |> (map o apfst o apfst) (apply_prefix loc);
  2413                     |> (map o apfst o apfst) (add_prefixes loc (sticky, interp_prfx) param_prfx);
  2447                 in
  2414                 in
  2448                   thy
  2415                   thy
  2449                   |> fold (snd oo global_note_prefix kind) facts'
  2416                   |> fold (snd oo global_note_prefix kind) facts'
  2450                 end
  2417                 end
  2451               | activate_elem _ _ thy = thy;
  2418               | activate_elem _ _ thy = thy;