src/Pure/Isar/element.ML
author wenzelm
Sun May 30 21:34:19 2010 +0200 (2010-05-30)
changeset 37198 3af985b10550
parent 36674 d95f39448121
child 38108 b4115423c049
permissions -rw-r--r--
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
ML_Context.eval/expression expect explicit ML_Lex source, which allows surrounding further text without loosing position information;
fall back on ML_Context.eval_text if there is no position or no surrounding text;
proper Args.name_source_position for method "tactic" and "raw_tactic";
tuned;
     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 eq_morphism: theory -> thm list -> morphism option
    59   val transfer_morphism: theory -> morphism
    60   val init: context_i -> Context.generic -> Context.generic
    61   val activate_i: context_i -> Proof.context -> context_i * Proof.context
    62   val activate: (typ, term, Facts.ref) ctxt -> Proof.context -> context_i * Proof.context
    63 end;
    64 
    65 structure Element: ELEMENT =
    66 struct
    67 
    68 (** language elements **)
    69 
    70 (* statement *)
    71 
    72 datatype ('typ, 'term) stmt =
    73   Shows of (Attrib.binding * ('term * 'term list) list) list |
    74   Obtains of (binding * ((binding * 'typ option) list * 'term list)) list;
    75 
    76 type statement = (string, string) stmt;
    77 type statement_i = (typ, term) stmt;
    78 
    79 
    80 (* context *)
    81 
    82 datatype ('typ, 'term, 'fact) ctxt =
    83   Fixes of (binding * 'typ option * mixfix) list |
    84   Constrains of (string * 'typ) list |
    85   Assumes of (Attrib.binding * ('term * 'term list) list) list |
    86   Defines of (Attrib.binding * ('term * 'term list)) list |
    87   Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list;
    88 
    89 type context = (string, string, Facts.ref) ctxt;
    90 type context_i = (typ, term, thm list) ctxt;
    91 
    92 fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts');
    93 
    94 fun map_ctxt {binding, typ, term, pattern, fact, attrib} =
    95   fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => (binding x, Option.map typ T, mx)))
    96    | Constrains xs => Constrains (xs |> map (fn (x, T) =>
    97       (Name.of_binding (binding (Binding.name x)), typ T)))
    98    | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
    99       ((binding a, map attrib atts), propps |> map (fn (t, ps) => (term t, map pattern ps)))))
   100    | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
   101       ((binding a, map attrib atts), (term t, map pattern ps))))
   102    | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
   103       ((binding a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
   104 
   105 fun map_ctxt_attrib attrib =
   106   map_ctxt {binding = I, typ = I, term = I, pattern = I, fact = I, attrib = attrib};
   107 
   108 fun morph_ctxt phi = map_ctxt
   109  {binding = Morphism.binding phi,
   110   typ = Morphism.typ phi,
   111   term = Morphism.term phi,
   112   pattern = Morphism.term phi,
   113   fact = Morphism.fact phi,
   114   attrib = Args.morph_values phi};
   115 
   116 
   117 
   118 (** pretty printing **)
   119 
   120 fun pretty_items _ _ [] = []
   121   | pretty_items keyword sep (x :: ys) =
   122       Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
   123         map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
   124 
   125 fun pretty_name_atts ctxt (b, atts) sep =
   126   if Binding.is_empty b andalso null atts then []
   127   else [Pretty.block (Pretty.breaks
   128     (Pretty.str (Binding.str_of b) :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
   129 
   130 
   131 (* pretty_stmt *)
   132 
   133 fun pretty_stmt ctxt =
   134   let
   135     val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
   136     val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
   137     val prt_terms = separate (Pretty.keyword "and") o map prt_term;
   138     val prt_name_atts = pretty_name_atts ctxt;
   139 
   140     fun prt_show (a, ts) =
   141       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
   142 
   143     fun prt_var (x, SOME T) = Pretty.block
   144           [Pretty.str (Binding.name_of x ^ " ::"), Pretty.brk 1, prt_typ T]
   145       | prt_var (x, NONE) = Pretty.str (Binding.name_of x);
   146     val prt_vars = separate (Pretty.keyword "and") o map prt_var;
   147 
   148     fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
   149       | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
   150           (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
   151   in
   152     fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
   153      | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
   154   end;
   155 
   156 
   157 (* pretty_ctxt *)
   158 
   159 fun pretty_ctxt ctxt =
   160   let
   161     val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
   162     val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
   163     val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
   164     val prt_name_atts = pretty_name_atts ctxt;
   165 
   166     fun prt_mixfix NoSyn = []
   167       | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
   168 
   169     fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (Binding.name_of x ^ " ::") ::
   170           Pretty.brk 1 :: prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
   171       | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str (Binding.name_of x) ::
   172           Pretty.brk 1 :: prt_mixfix mx);
   173     fun prt_constrain (x, T) = prt_fix (Binding.name x, SOME T, NoSyn);
   174 
   175     fun prt_asm (a, ts) =
   176       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
   177     fun prt_def (a, (t, _)) =
   178       Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
   179 
   180     fun prt_fact (ths, []) = map prt_thm ths
   181       | prt_fact (ths, atts) = Pretty.enclose "(" ")"
   182           (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
   183     fun prt_note (a, ths) =
   184       Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
   185   in
   186     fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
   187      | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
   188      | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
   189      | Defines defs => pretty_items "defines" "and" (map prt_def defs)
   190      | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
   191      | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
   192   end;
   193 
   194 
   195 (* pretty_statement *)
   196 
   197 local
   198 
   199 fun thm_name kind th prts =
   200   let val head =
   201     if Thm.has_name_hint th then
   202       Pretty.block [Pretty.command kind,
   203         Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
   204     else Pretty.command kind
   205   in Pretty.block (Pretty.fbreaks (head :: prts)) end;
   206 
   207 fun fix (x, T) = (Binding.name x, SOME T);
   208 
   209 fun obtain prop ctxt =
   210   let
   211     val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
   212     val As = Logic.strip_imp_prems (Thm.term_of prop');
   213   in ((Binding.empty, (map (fix o Term.dest_Free o Thm.term_of o #2) xs, As)), ctxt') end;
   214 
   215 in
   216 
   217 fun pretty_statement ctxt kind raw_th =
   218   let
   219     val thy = ProofContext.theory_of ctxt;
   220     val cert = Thm.cterm_of thy;
   221 
   222     val th = MetaSimplifier.norm_hhf raw_th;
   223     val is_elim = Object_Logic.is_elim th;
   224 
   225     val ((_, [th']), ctxt') = Variable.import true [th] (Variable.set_body false ctxt);
   226     val prop = Thm.prop_of th';
   227     val (prems, concl) = Logic.strip_horn prop;
   228     val concl_term = Object_Logic.drop_judgment thy concl;
   229 
   230     val fixes = fold_aterms (fn v as Free (x, T) =>
   231         if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
   232         then insert (op =) (x, T) else I | _ => I) prop [] |> rev;
   233     val (assumes, cases) = take_suffix (fn prem =>
   234       is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
   235   in
   236     pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @
   237     pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @
   238      (if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])])
   239       else
   240         let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt'
   241         in pretty_stmt ctxt'' (Obtains clauses) end)
   242   end |> thm_name kind raw_th;
   243 
   244 end;
   245 
   246 
   247 
   248 (** logical operations **)
   249 
   250 (* witnesses -- hypotheses as protected facts *)
   251 
   252 datatype witness = Witness of term * thm;
   253 
   254 val mark_witness = Logic.protect;
   255 fun witness_prop (Witness (t, _)) = t;
   256 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   257 fun map_witness f (Witness witn) = Witness (f witn);
   258 
   259 fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
   260 
   261 fun prove_witness ctxt t tac =
   262   Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ =>
   263     Tactic.rtac Drule.protectI 1 THEN tac)));
   264 
   265 
   266 local
   267 
   268 val refine_witness =
   269   Proof.refine (Method.Basic (K (RAW_METHOD
   270     (K (ALLGOALS
   271       (CONJUNCTS (ALLGOALS
   272         (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI))))))))));
   273 
   274 fun gen_witness_proof proof after_qed wit_propss eq_props =
   275   let
   276     val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss
   277       @ [map (rpair []) eq_props];
   278     fun after_qed' thmss =
   279       let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss);
   280       in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end;
   281   in proof after_qed' propss #> refine_witness #> Seq.hd end;
   282 
   283 in
   284 
   285 fun witness_proof after_qed wit_propss =
   286   gen_witness_proof (Proof.theorem NONE) (fn wits => fn _ => after_qed wits)
   287     wit_propss [];
   288 
   289 val witness_proof_eqs = gen_witness_proof (Proof.theorem NONE);
   290 
   291 fun witness_local_proof after_qed cmd wit_propss goal_ctxt int =
   292   gen_witness_proof (fn after_qed' => fn propss =>
   293     Proof.map_context (K goal_ctxt)
   294     #> Proof.local_goal (Proof_Display.print_results int) (K I) ProofContext.bind_propp_i
   295       cmd NONE after_qed' (map (pair Thm.empty_binding) propss))
   296     (fn wits => fn _ => after_qed wits) wit_propss [];
   297 
   298 end;
   299 
   300 
   301 fun compose_witness (Witness (_, th)) r =
   302   let
   303     val th' = Goal.conclude th;
   304     val A = Thm.cprem_of r 1;
   305   in
   306     Thm.implies_elim
   307       (Conv.gconv_rule Drule.beta_eta_conversion 1 r)
   308       (Conv.fconv_rule Drule.beta_eta_conversion
   309         (Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th'))
   310   end;
   311 
   312 fun conclude_witness (Witness (_, th)) =
   313   Thm.close_derivation (MetaSimplifier.norm_hhf_protect (Goal.conclude th));
   314 
   315 fun pretty_witness ctxt witn =
   316   let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in
   317     Pretty.block (prt_term (witness_prop witn) ::
   318       (if ! show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
   319          (map prt_term (witness_hyps witn))] else []))
   320   end;
   321 
   322 
   323 (* derived rules *)
   324 
   325 fun instantiate_tfrees thy subst th =
   326   let
   327     val certT = Thm.ctyp_of thy;
   328     val idx = Thm.maxidx_of th + 1;
   329     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   330 
   331     fun add_inst (a, S) insts =
   332       if AList.defined (op =) insts a then insts
   333       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   334     val insts =
   335       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   336         (Thm.full_prop_of th) [];
   337   in
   338     th
   339     |> Thm.generalize (map fst insts, []) idx
   340     |> Thm.instantiate (map cert_inst insts, [])
   341   end;
   342 
   343 fun instantiate_frees thy subst =
   344   let val cert = Thm.cterm_of thy in
   345     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   346     Drule.forall_elim_list (map (cert o snd) subst)
   347   end;
   348 
   349 fun hyps_rule rule th =
   350   let val {hyps, ...} = Thm.crep_thm th in
   351     Drule.implies_elim_list
   352       (rule (Drule.implies_intr_list hyps th))
   353       (map (Thm.assume o Drule.cterm_rule rule) hyps)
   354   end;
   355 
   356 
   357 (* instantiate types *)
   358 
   359 fun instT_type env =
   360   if Symtab.is_empty env then I
   361   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   362 
   363 fun instT_term env =
   364   if Symtab.is_empty env then I
   365   else Term.map_types (instT_type env);
   366 
   367 fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
   368   (fn T as TFree (a, _) =>
   369     let val T' = the_default T (Symtab.lookup env a)
   370     in if T = T' then I else insert (op =) (a, T') end
   371   | _ => I) th [];
   372 
   373 fun instT_thm thy env th =
   374   if Symtab.is_empty env then th
   375   else
   376     let val subst = instT_subst env th
   377     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   378 
   379 fun instT_morphism thy env =
   380   let val thy_ref = Theory.check_thy thy in
   381     Morphism.morphism
   382      {binding = I,
   383       typ = instT_type env,
   384       term = instT_term env,
   385       fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
   386   end;
   387 
   388 
   389 (* instantiate types and terms *)
   390 
   391 fun inst_term (envT, env) =
   392   if Symtab.is_empty env then instT_term envT
   393   else
   394     let
   395       val instT = instT_type envT;
   396       fun inst (Const (x, T)) = Const (x, instT T)
   397         | inst (Free (x, T)) =
   398             (case Symtab.lookup env x of
   399               NONE => Free (x, instT T)
   400             | SOME t => t)
   401         | inst (Var (xi, T)) = Var (xi, instT T)
   402         | inst (b as Bound _) = b
   403         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   404         | inst (t $ u) = inst t $ inst u;
   405     in Envir.beta_norm o inst end;
   406 
   407 fun inst_thm thy (envT, env) th =
   408   if Symtab.is_empty env then instT_thm thy envT th
   409   else
   410     let
   411       val substT = instT_subst envT th;
   412       val subst = (Thm.fold_terms o Term.fold_aterms)
   413        (fn Free (x, T) =>
   414           let
   415             val T' = instT_type envT T;
   416             val t = Free (x, T');
   417             val t' = the_default t (Symtab.lookup env x);
   418           in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
   419        | _ => I) th [];
   420     in
   421       if null substT andalso null subst then th
   422       else th |> hyps_rule
   423        (instantiate_tfrees thy substT #>
   424         instantiate_frees thy subst #>
   425         Conv.fconv_rule (Thm.beta_conversion true))
   426     end;
   427 
   428 fun inst_morphism thy envs =
   429   let val thy_ref = Theory.check_thy thy in
   430     Morphism.morphism
   431      {binding = I,
   432       typ = instT_type (#1 envs),
   433       term = inst_term envs,
   434       fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
   435   end;
   436 
   437 
   438 (* satisfy hypotheses *)
   439 
   440 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
   441     (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
   442       NONE => I
   443     | SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm));
   444 
   445 val satisfy_morphism = Morphism.thm_morphism o satisfy_thm;
   446 val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism;
   447 
   448 
   449 (* rewriting with equalities *)
   450 
   451 fun eq_morphism thy thms = if null thms then NONE else SOME (Morphism.morphism
   452  {binding = I,
   453   typ = I,
   454   term = MetaSimplifier.rewrite_term thy thms [],
   455   fact = map (MetaSimplifier.rewrite_rule thms)});
   456 
   457 
   458 (* transfer to theory using closure *)
   459 
   460 fun transfer_morphism thy =
   461   let val thy_ref = Theory.check_thy thy
   462   in Morphism.thm_morphism (fn th => transfer (Theory.deref thy_ref) th) end;
   463 
   464 
   465 
   466 (** activate in context **)
   467 
   468 (* init *)
   469 
   470 fun init (Fixes fixes) = Context.map_proof (ProofContext.add_fixes fixes #> #2)
   471   | init (Constrains _) = I
   472   | init (Assumes asms) = Context.map_proof (fn ctxt =>
   473       let
   474         val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
   475         val (_, ctxt') = ctxt
   476           |> fold Variable.auto_fixes (maps (map #1 o #2) asms')
   477           |> ProofContext.add_assms_i Assumption.assume_export asms';
   478       in ctxt' end)
   479   | init (Defines defs) = Context.map_proof (fn ctxt =>
   480       let
   481         val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs;
   482         val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
   483             let val ((c, _), t') = Local_Defs.cert_def ctxt t  (* FIXME adapt ps? *)
   484             in (t', ((Thm.def_binding_optional (Binding.name c) name, atts), [(t', ps)])) end);
   485         val (_, ctxt') = ctxt
   486           |> fold Variable.auto_fixes (map #1 asms)
   487           |> ProofContext.add_assms_i Local_Defs.def_export (map #2 asms);
   488       in ctxt' end)
   489   | init (Notes (kind, facts)) = (fn context =>
   490       let
   491         val facts' = Attrib.map_facts (Attrib.attribute_i (Context.theory_of context)) facts;
   492         val context' = context |> Context.mapping
   493           (PureThy.note_thmss kind facts' #> #2)
   494           (ProofContext.note_thmss kind facts' #> #2);
   495       in context' end);
   496 
   497 
   498 (* activate *)
   499 
   500 fun activate_i elem ctxt =
   501   let
   502     val elem' = map_ctxt_attrib Args.assignable elem;
   503     val ctxt' = Context.proof_map (init elem') ctxt;
   504   in (map_ctxt_attrib Args.closure elem', ctxt') end;
   505 
   506 fun activate raw_elem ctxt =
   507   let val elem = raw_elem |> map_ctxt
   508    {binding = tap Name.of_binding,
   509     typ = I,
   510     term = I,
   511     pattern = I,
   512     fact = ProofContext.get_fact ctxt,
   513     attrib = Attrib.intern_src (ProofContext.theory_of ctxt)}
   514   in activate_i elem ctxt end;
   515 
   516 end;