src/Pure/Isar/element.ML
author wenzelm
Sun Mar 08 17:26:14 2009 +0100 (2009-03-08)
changeset 30364 577edc39b501
parent 30280 eb98b49ef835
child 30434 9b94b1358b95
permissions -rw-r--r--
moved basic algebra of long names from structure NameSpace to Long_Name;
     1 (*  Title:      Pure/Isar/element.ML
     2     Author:     Makarius
     3 
     4 Explicit data structures for some Isar language elements, with derived
     5 logical operations.
     6 *)
     7 
     8 signature ELEMENT =
     9 sig
    10   datatype ('typ, 'term) stmt =
    11     Shows of (Attrib.binding * ('term * 'term list) list) list |
    12     Obtains of (binding * ((binding * 'typ option) list * 'term list)) list
    13   type statement = (string, string) stmt
    14   type statement_i = (typ, term) stmt
    15   datatype ('typ, 'term, 'fact) ctxt =
    16     Fixes of (binding * 'typ option * mixfix) list |
    17     Constrains of (string * 'typ) list |
    18     Assumes of (Attrib.binding * ('term * 'term list) list) list |
    19     Defines of (Attrib.binding * ('term * 'term list)) list |
    20     Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list
    21   type context = (string, string, Facts.ref) ctxt
    22   type context_i = (typ, term, thm list) ctxt
    23   val facts_map: (('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt) ->
    24    (Attrib.binding * ('fact * Attrib.src list) list) list ->
    25    (Attrib.binding * ('c * Attrib.src list) list) list
    26   val map_ctxt: {binding: binding -> binding, typ: 'typ -> 'a, term: 'term -> 'b,
    27     pattern: 'term -> 'b, fact: 'fact -> 'c, attrib: Attrib.src -> Attrib.src} ->
    28     ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
    29   val map_ctxt_attrib: (Attrib.src -> Attrib.src) ->
    30     ('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt
    31   val morph_ctxt: morphism -> context_i -> context_i
    32   val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
    33   val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
    34   val pretty_statement: Proof.context -> string -> thm -> Pretty.T
    35   type witness
    36   val prove_witness: Proof.context -> term -> tactic -> witness
    37   val witness_proof: (witness list list -> Proof.context -> Proof.context) ->
    38     term list list -> Proof.context -> Proof.state
    39   val witness_proof_eqs: (witness list list -> thm list -> Proof.context -> Proof.context) ->
    40     term list list -> term list -> Proof.context -> Proof.state
    41   val witness_local_proof: (witness list list -> Proof.state -> Proof.state) ->
    42     string -> term list list -> Proof.context -> bool -> Proof.state -> Proof.state
    43   val morph_witness: morphism -> witness -> witness
    44   val conclude_witness: witness -> thm
    45   val pretty_witness: Proof.context -> witness -> Pretty.T
    46   val instT_type: typ Symtab.table -> typ -> typ
    47   val instT_term: typ Symtab.table -> term -> term
    48   val instT_thm: theory -> typ Symtab.table -> thm -> thm
    49   val instT_morphism: theory -> typ Symtab.table -> morphism
    50   val inst_term: typ Symtab.table * term Symtab.table -> term -> term
    51   val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
    52   val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism
    53   val satisfy_thm: witness list -> thm -> thm
    54   val satisfy_morphism: witness list -> morphism
    55   val satisfy_facts: witness list ->
    56     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    57     (Attrib.binding * (thm list * Attrib.src list) list) list
    58   val generalize_facts: Proof.context -> Proof.context ->
    59     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    60     (Attrib.binding * (thm list * Attrib.src list) list) list
    61   val eq_morphism: theory -> thm list -> morphism
    62   val transfer_morphism: theory -> morphism
    63   val activate: (typ, term, Facts.ref) ctxt list -> Proof.context -> context_i list * Proof.context
    64   val activate_i: context_i list -> Proof.context -> context_i list * Proof.context
    65 end;
    66 
    67 structure Element: ELEMENT =
    68 struct
    69 
    70 (** language elements **)
    71 
    72 (* statement *)
    73 
    74 datatype ('typ, 'term) stmt =
    75   Shows of (Attrib.binding * ('term * 'term list) list) list |
    76   Obtains of (binding * ((binding * 'typ option) list * 'term list)) list;
    77 
    78 type statement = (string, string) stmt;
    79 type statement_i = (typ, term) stmt;
    80 
    81 
    82 (* context *)
    83 
    84 datatype ('typ, 'term, 'fact) ctxt =
    85   Fixes of (binding * 'typ option * mixfix) list |
    86   Constrains of (string * 'typ) list |
    87   Assumes of (Attrib.binding * ('term * 'term list) list) list |
    88   Defines of (Attrib.binding * ('term * 'term list)) list |
    89   Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list;
    90 
    91 type context = (string, string, Facts.ref) ctxt;
    92 type context_i = (typ, term, thm list) ctxt;
    93 
    94 fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts');
    95 
    96 fun map_ctxt {binding, typ, term, pattern, fact, attrib} =
    97   fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => (binding x, Option.map typ T, mx)))
    98    | Constrains xs => Constrains (xs |> map (fn (x, T) =>
    99       (Binding.name_of (binding (Binding.name x)), typ T)))
   100    | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
   101       ((binding a, map attrib atts), propps |> map (fn (t, ps) => (term t, map pattern ps)))))
   102    | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
   103       ((binding a, map attrib atts), (term t, map pattern ps))))
   104    | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
   105       ((binding a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
   106 
   107 fun map_ctxt_attrib attrib =
   108   map_ctxt {binding = I, typ = I, term = I, pattern = I, fact = I, attrib = attrib};
   109 
   110 fun morph_ctxt phi = map_ctxt
   111  {binding = Morphism.binding phi,
   112   typ = Morphism.typ phi,
   113   term = Morphism.term phi,
   114   pattern = Morphism.term phi,
   115   fact = Morphism.fact phi,
   116   attrib = Args.morph_values phi};
   117 
   118 
   119 
   120 (** pretty printing **)
   121 
   122 fun pretty_items _ _ [] = []
   123   | pretty_items keyword sep (x :: ys) =
   124       Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
   125         map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
   126 
   127 fun pretty_name_atts ctxt (b, atts) sep =
   128   if Binding.is_empty b andalso null atts then []
   129   else [Pretty.block (Pretty.breaks
   130     (Pretty.str (Binding.str_of b) :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
   131 
   132 
   133 (* pretty_stmt *)
   134 
   135 fun pretty_stmt ctxt =
   136   let
   137     val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
   138     val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
   139     val prt_terms = separate (Pretty.keyword "and") o map prt_term;
   140     val prt_name_atts = pretty_name_atts ctxt;
   141 
   142     fun prt_show (a, ts) =
   143       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
   144 
   145     fun prt_var (x, SOME T) = Pretty.block
   146           [Pretty.str (Binding.name_of x ^ " ::"), Pretty.brk 1, prt_typ T]
   147       | prt_var (x, NONE) = Pretty.str (Binding.name_of x);
   148     val prt_vars = separate (Pretty.keyword "and") o map prt_var;
   149 
   150     fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
   151       | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
   152           (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
   153   in
   154     fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
   155      | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
   156   end;
   157 
   158 
   159 (* pretty_ctxt *)
   160 
   161 fun pretty_ctxt ctxt =
   162   let
   163     val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
   164     val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
   165     val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
   166     val prt_name_atts = pretty_name_atts ctxt;
   167 
   168     fun prt_mixfix NoSyn = []
   169       | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
   170 
   171     fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (Binding.name_of x ^ " ::") ::
   172           Pretty.brk 1 :: prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
   173       | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str (Binding.name_of x) ::
   174           Pretty.brk 1 :: prt_mixfix mx);
   175     fun prt_constrain (x, T) = prt_fix (Binding.name x, SOME T, NoSyn);
   176 
   177     fun prt_asm (a, ts) =
   178       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
   179     fun prt_def (a, (t, _)) =
   180       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
   181 
   182     fun prt_fact (ths, []) = map prt_thm ths
   183       | prt_fact (ths, atts) = Pretty.enclose "(" ")"
   184           (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
   185     fun prt_note (a, ths) =
   186       Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
   187   in
   188     fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
   189      | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
   190      | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
   191      | Defines defs => pretty_items "defines" "and" (map prt_def defs)
   192      | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
   193      | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
   194   end;
   195 
   196 
   197 (* pretty_statement *)
   198 
   199 local
   200 
   201 fun thm_name kind th prts =
   202   let val head =
   203     if Thm.has_name_hint th then
   204       Pretty.block [Pretty.command kind,
   205         Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
   206     else Pretty.command kind
   207   in Pretty.block (Pretty.fbreaks (head :: prts)) end;
   208 
   209 fun fix (x, T) = (Binding.name x, SOME T);
   210 
   211 fun obtain prop ctxt =
   212   let
   213     val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
   214     val As = Logic.strip_imp_prems (Thm.term_of prop');
   215   in ((Binding.empty, (map (fix o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end;
   216 
   217 in
   218 
   219 fun pretty_statement ctxt kind raw_th =
   220   let
   221     val thy = ProofContext.theory_of ctxt;
   222     val cert = Thm.cterm_of thy;
   223 
   224     val th = MetaSimplifier.norm_hhf raw_th;
   225     val is_elim = ObjectLogic.is_elim th;
   226 
   227     val ((_, [th']), ctxt') = Variable.import_thms true [th] (Variable.set_body false ctxt);
   228     val prop = Thm.prop_of th';
   229     val (prems, concl) = Logic.strip_horn prop;
   230     val concl_term = ObjectLogic.drop_judgment thy concl;
   231 
   232     val fixes = fold_aterms (fn v as Free (x, T) =>
   233         if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
   234         then insert (op =) (x, T) else I | _ => I) prop [] |> rev;
   235     val (assumes, cases) = take_suffix (fn prem =>
   236       is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
   237   in
   238     pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @
   239     pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @
   240      (if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])])
   241       else
   242         let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt'
   243         in pretty_stmt ctxt'' (Obtains clauses) end)
   244   end |> thm_name kind raw_th;
   245 
   246 end;
   247 
   248 
   249 
   250 (** logical operations **)
   251 
   252 (* witnesses -- hypotheses as protected facts *)
   253 
   254 datatype witness = Witness of term * thm;
   255 
   256 val mark_witness = Logic.protect;
   257 fun witness_prop (Witness (t, _)) = t;
   258 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   259 fun map_witness f (Witness witn) = Witness (f witn);
   260 
   261 fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
   262 
   263 fun prove_witness ctxt t tac =
   264   Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ =>
   265     Tactic.rtac Drule.protectI 1 THEN tac)));
   266 
   267 
   268 local
   269 
   270 val refine_witness =
   271   Proof.refine (Method.Basic (K (Method.RAW_METHOD
   272     (K (ALLGOALS
   273       (CONJUNCTS (ALLGOALS
   274         (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI)))))))), Position.none));
   275 
   276 fun gen_witness_proof proof after_qed wit_propss eq_props =
   277   let
   278     val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss
   279       @ [map (rpair []) eq_props];
   280     fun after_qed' thmss =
   281       let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss);
   282       in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end;
   283   in proof after_qed' propss #> refine_witness #> Seq.hd end;
   284 
   285 in
   286 
   287 fun witness_proof after_qed wit_propss =
   288   gen_witness_proof (Proof.theorem_i NONE) (fn wits => fn _ => after_qed wits)
   289     wit_propss [];
   290 
   291 val witness_proof_eqs = gen_witness_proof (Proof.theorem_i NONE);
   292 
   293 fun witness_local_proof after_qed cmd wit_propss goal_ctxt int =
   294   gen_witness_proof (fn after_qed' => fn propss =>
   295     Proof.map_context (K goal_ctxt)
   296     #> Proof.local_goal (ProofDisplay.print_results int) (K I) ProofContext.bind_propp_i
   297       cmd NONE after_qed' (map (pair Thm.empty_binding) propss))
   298     (fn wits => fn _ => after_qed wits) wit_propss [];
   299 
   300 end;
   301 
   302 
   303 fun compose_witness (Witness (_, th)) r =
   304   let
   305     val th' = Goal.conclude th;
   306     val A = Thm.cprem_of r 1;
   307   in
   308     Thm.implies_elim
   309       (Conv.gconv_rule Drule.beta_eta_conversion 1 r)
   310       (Conv.fconv_rule Drule.beta_eta_conversion
   311         (Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th'))
   312   end;
   313 
   314 fun conclude_witness (Witness (_, th)) =
   315   Thm.close_derivation (MetaSimplifier.norm_hhf_protect (Goal.conclude th));
   316 
   317 fun pretty_witness ctxt witn =
   318   let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in
   319     Pretty.block (prt_term (witness_prop witn) ::
   320       (if ! show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
   321          (map prt_term (witness_hyps witn))] else []))
   322   end;
   323 
   324 
   325 (* derived rules *)
   326 
   327 fun instantiate_tfrees thy subst th =
   328   let
   329     val certT = Thm.ctyp_of thy;
   330     val idx = Thm.maxidx_of th + 1;
   331     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   332 
   333     fun add_inst (a, S) insts =
   334       if AList.defined (op =) insts a then insts
   335       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   336     val insts =
   337       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   338         (Thm.full_prop_of th) [];
   339   in
   340     th
   341     |> Thm.generalize (map fst insts, []) idx
   342     |> Thm.instantiate (map cert_inst insts, [])
   343   end;
   344 
   345 fun instantiate_frees thy subst =
   346   let val cert = Thm.cterm_of thy in
   347     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   348     Drule.forall_elim_list (map (cert o snd) subst)
   349   end;
   350 
   351 fun hyps_rule rule th =
   352   let val {hyps, ...} = Thm.crep_thm th in
   353     Drule.implies_elim_list
   354       (rule (Drule.implies_intr_list hyps th))
   355       (map (Thm.assume o Drule.cterm_rule rule) hyps)
   356   end;
   357 
   358 
   359 (* instantiate types *)
   360 
   361 fun instT_type env =
   362   if Symtab.is_empty env then I
   363   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   364 
   365 fun instT_term env =
   366   if Symtab.is_empty env then I
   367   else Term.map_types (instT_type env);
   368 
   369 fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
   370   (fn T as TFree (a, _) =>
   371     let val T' = the_default T (Symtab.lookup env a)
   372     in if T = T' then I else insert (op =) (a, T') end
   373   | _ => I) th [];
   374 
   375 fun instT_thm thy env th =
   376   if Symtab.is_empty env then th
   377   else
   378     let val subst = instT_subst env th
   379     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   380 
   381 fun instT_morphism thy env =
   382   let val thy_ref = Theory.check_thy thy in
   383     Morphism.morphism
   384      {binding = I,
   385       typ = instT_type env,
   386       term = instT_term env,
   387       fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
   388   end;
   389 
   390 
   391 (* instantiate types and terms *)
   392 
   393 fun inst_term (envT, env) =
   394   if Symtab.is_empty env then instT_term envT
   395   else
   396     let
   397       val instT = instT_type envT;
   398       fun inst (Const (x, T)) = Const (x, instT T)
   399         | inst (Free (x, T)) =
   400             (case Symtab.lookup env x of
   401               NONE => Free (x, instT T)
   402             | SOME t => t)
   403         | inst (Var (xi, T)) = Var (xi, instT T)
   404         | inst (b as Bound _) = b
   405         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   406         | inst (t $ u) = inst t $ inst u;
   407     in Envir.beta_norm o inst end;
   408 
   409 fun inst_thm thy (envT, env) th =
   410   if Symtab.is_empty env then instT_thm thy envT th
   411   else
   412     let
   413       val substT = instT_subst envT th;
   414       val subst = (Thm.fold_terms o Term.fold_aterms)
   415        (fn Free (x, T) =>
   416           let
   417             val T' = instT_type envT T;
   418             val t = Free (x, T');
   419             val t' = the_default t (Symtab.lookup env x);
   420           in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
   421        | _ => I) th [];
   422     in
   423       if null substT andalso null subst then th
   424       else th |> hyps_rule
   425        (instantiate_tfrees thy substT #>
   426         instantiate_frees thy subst #>
   427         Conv.fconv_rule (Thm.beta_conversion true))
   428     end;
   429 
   430 fun inst_morphism thy envs =
   431   let val thy_ref = Theory.check_thy thy in
   432     Morphism.morphism
   433      {binding = I,
   434       typ = instT_type (#1 envs),
   435       term = inst_term envs,
   436       fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
   437   end;
   438 
   439 
   440 (* satisfy hypotheses *)
   441 
   442 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
   443     (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
   444       NONE => I
   445     | SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm));
   446 
   447 val satisfy_morphism = Morphism.thm_morphism o satisfy_thm;
   448 val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism;
   449 
   450 
   451 (* rewriting with equalities *)
   452 
   453 fun eq_morphism thy thms = Morphism.morphism
   454  {binding = I,
   455   typ = I,
   456   term = MetaSimplifier.rewrite_term thy thms [],
   457   fact = map (MetaSimplifier.rewrite_rule thms)};
   458 
   459 
   460 (* generalize type/term parameters *)
   461 
   462 val maxidx_atts = fold Args.maxidx_values;
   463 
   464 fun generalize_facts inner outer facts =
   465   let
   466     val thy = ProofContext.theory_of inner;
   467     val maxidx =
   468       fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
   469     val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> Variable.export inner outer;
   470     val exp_term = Drule.term_rule thy (singleton exp_fact);
   471     val exp_typ = Logic.type_map exp_term;
   472     val morphism = Morphism.morphism {binding = I, typ = exp_typ, term = exp_term, fact = exp_fact};
   473   in facts_map (morph_ctxt morphism) facts end;
   474 
   475 
   476 (* transfer to theory using closure *)
   477 
   478 fun transfer_morphism thy =
   479   let val thy_ref = Theory.check_thy thy
   480   in Morphism.thm_morphism (fn th => transfer (Theory.deref thy_ref) th) end;
   481 
   482 
   483 
   484 (** activate in context, return elements and facts **)
   485 
   486 local
   487 
   488 fun activate_elem (Fixes fixes) ctxt =
   489       ctxt |> ProofContext.add_fixes_i fixes |> snd
   490   | activate_elem (Constrains _) ctxt =
   491       ctxt
   492   | activate_elem (Assumes asms) ctxt =
   493       let
   494         val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
   495         val ts = maps (map #1 o #2) asms';
   496         val (_, ctxt') =
   497           ctxt |> fold Variable.auto_fixes ts
   498           |> ProofContext.add_assms_i Assumption.presume_export asms';
   499       in ctxt' end
   500   | activate_elem (Defines defs) ctxt =
   501       let
   502         val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs;
   503         val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
   504             let val ((c, _), t') = LocalDefs.cert_def ctxt t
   505             in (t', ((Binding.map_name (Thm.def_name_optional c) name, atts), [(t', ps)])) end);
   506         val (_, ctxt') =
   507           ctxt |> fold (Variable.auto_fixes o #1) asms
   508           |> ProofContext.add_assms_i LocalDefs.def_export (map #2 asms);
   509       in ctxt' end
   510   | activate_elem (Notes (kind, facts)) ctxt =
   511       let
   512         val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts;
   513         val (res, ctxt') = ctxt |> ProofContext.note_thmss_i kind facts';
   514       in ctxt' end;
   515 
   516 fun gen_activate prep_facts raw_elems ctxt =
   517   let
   518     fun activate elem ctxt =
   519       let val elem' = (map_ctxt_attrib Args.assignable o prep_facts ctxt) elem
   520       in (elem', activate_elem elem' ctxt) end
   521     val (elems, ctxt') = fold_map activate raw_elems (ProofContext.qualified_names ctxt);
   522   in (elems |> map (map_ctxt_attrib Args.closure), ProofContext.restore_naming ctxt ctxt') end;
   523 
   524 fun check_name name =
   525   if Long_Name.is_qualified name then error ("Illegal qualified name: " ^ quote name)
   526   else name;
   527 
   528 fun prep_facts prep_name get intern ctxt =
   529   map_ctxt
   530    {binding = Binding.map_name prep_name,
   531     typ = I,
   532     term = I,
   533     pattern = I,
   534     fact = get ctxt,
   535     attrib = intern (ProofContext.theory_of ctxt)};
   536 
   537 in
   538 
   539 fun activate x = gen_activate (prep_facts check_name ProofContext.get_fact Attrib.intern_src) x;
   540 fun activate_i x = gen_activate (K I) x;
   541 
   542 end;
   543 
   544 end;