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