src/Pure/Isar/element.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 42287 d98eb048a2e4
child 42357 3305f573294e
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
     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, Mixfix.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 standard_elim th =
   205   (case Object_Logic.elim_concl th of
   206     SOME C =>
   207       let
   208         val cert = Thm.cterm_of (Thm.theory_of_thm th);
   209         val thesis = Var ((Auto_Bind.thesisN, Thm.maxidx_of th + 1), fastype_of C);
   210         val th' = Thm.instantiate ([], [(cert C, cert thesis)]) th;
   211       in (th', true) end
   212   | NONE => (th, false));
   213 
   214 fun thm_name kind th prts =
   215   let val head =
   216     if Thm.has_name_hint th then
   217       Pretty.block [Pretty.command kind,
   218         Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
   219     else Pretty.command kind
   220   in Pretty.block (Pretty.fbreaks (head :: prts)) end;
   221 
   222 fun obtain prop ctxt =
   223   let
   224     val ((ps, prop'), ctxt') = Variable.focus prop ctxt;
   225     fun fix (x, T) = (Binding.name (ProofContext.revert_skolem ctxt' x), SOME T);
   226     val xs = map (fix o Term.dest_Free o Thm.term_of o #2) ps;
   227     val As = Logic.strip_imp_prems (Thm.term_of prop');
   228   in ((Binding.empty, (xs, As)), ctxt') end;
   229 
   230 in
   231 
   232 fun pretty_statement ctxt kind raw_th =
   233   let
   234     val thy = ProofContext.theory_of ctxt;
   235     val cert = Thm.cterm_of thy;
   236 
   237     val (th, is_elim) = standard_elim (Raw_Simplifier.norm_hhf raw_th);
   238     val ((_, [th']), ctxt') = Variable.import true [th] (Variable.set_body true ctxt);
   239     val prop = Thm.prop_of th';
   240     val (prems, concl) = Logic.strip_horn prop;
   241     val concl_term = Object_Logic.drop_judgment thy concl;
   242 
   243     val fixes = fold_aterms (fn v as Free (x, T) =>
   244         if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
   245         then insert (op =) (ProofContext.revert_skolem ctxt' x, T) else I | _ => I) prop [] |> rev;
   246     val (assumes, cases) = take_suffix (fn prem =>
   247       is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
   248   in
   249     pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @
   250     pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @
   251      (if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])])
   252       else
   253         let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt'
   254         in pretty_stmt ctxt'' (Obtains clauses) end)
   255   end |> thm_name kind raw_th;
   256 
   257 end;
   258 
   259 
   260 
   261 (** logical operations **)
   262 
   263 (* witnesses -- hypotheses as protected facts *)
   264 
   265 datatype witness = Witness of term * thm;
   266 
   267 val mark_witness = Logic.protect;
   268 fun witness_prop (Witness (t, _)) = t;
   269 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
   270 fun map_witness f (Witness witn) = Witness (f witn);
   271 
   272 fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
   273 
   274 fun prove_witness ctxt t tac =
   275   Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ =>
   276     Tactic.rtac Drule.protectI 1 THEN tac)));
   277 
   278 
   279 local
   280 
   281 val refine_witness =
   282   Proof.refine (Method.Basic (K (RAW_METHOD
   283     (K (ALLGOALS
   284       (CONJUNCTS (ALLGOALS
   285         (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI))))))))));
   286 
   287 fun gen_witness_proof proof after_qed wit_propss eq_props =
   288   let
   289     val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss
   290       @ [map (rpair []) eq_props];
   291     fun after_qed' thmss =
   292       let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss);
   293       in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end;
   294   in proof after_qed' propss #> refine_witness #> Seq.hd end;
   295 
   296 fun proof_local cmd goal_ctxt int after_qed' propss =
   297     Proof.map_context (K goal_ctxt)
   298     #> Proof.local_goal (Proof_Display.print_results int) (K I) ProofContext.bind_propp_i
   299       cmd NONE after_qed' (map (pair Thm.empty_binding) propss);
   300 
   301 in
   302 
   303 fun witness_proof after_qed wit_propss =
   304   gen_witness_proof (Proof.theorem NONE) (fn wits => fn _ => after_qed wits)
   305     wit_propss [];
   306 
   307 val witness_proof_eqs = gen_witness_proof (Proof.theorem NONE);
   308 
   309 fun witness_local_proof after_qed cmd wit_propss goal_ctxt int =
   310   gen_witness_proof (proof_local cmd goal_ctxt int)
   311     (fn wits => fn _ => after_qed wits) wit_propss [];
   312 
   313 fun witness_local_proof_eqs after_qed cmd wit_propss eq_props goal_ctxt int =
   314   gen_witness_proof (proof_local cmd goal_ctxt int) after_qed wit_propss eq_props;
   315 
   316 end;
   317 
   318 
   319 fun compose_witness (Witness (_, th)) r =
   320   let
   321     val th' = Goal.conclude th;
   322     val A = Thm.cprem_of r 1;
   323   in
   324     Thm.implies_elim
   325       (Conv.gconv_rule Drule.beta_eta_conversion 1 r)
   326       (Conv.fconv_rule Drule.beta_eta_conversion
   327         (Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th'))
   328   end;
   329 
   330 fun conclude_witness (Witness (_, th)) =
   331   Thm.close_derivation (Raw_Simplifier.norm_hhf_protect (Goal.conclude th));
   332 
   333 fun pretty_witness ctxt witn =
   334   let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in
   335     Pretty.block (prt_term (witness_prop witn) ::
   336       (if Config.get ctxt show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
   337          (map prt_term (witness_hyps witn))] else []))
   338   end;
   339 
   340 
   341 (* derived rules *)
   342 
   343 fun instantiate_tfrees thy subst th =
   344   let
   345     val certT = Thm.ctyp_of thy;
   346     val idx = Thm.maxidx_of th + 1;
   347     fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
   348 
   349     fun add_inst (a, S) insts =
   350       if AList.defined (op =) insts a then insts
   351       else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
   352     val insts =
   353       Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
   354         (Thm.full_prop_of th) [];
   355   in
   356     th
   357     |> Thm.generalize (map fst insts, []) idx
   358     |> Thm.instantiate (map cert_inst insts, [])
   359   end;
   360 
   361 fun instantiate_frees thy subst =
   362   let val cert = Thm.cterm_of thy in
   363     Drule.forall_intr_list (map (cert o Free o fst) subst) #>
   364     Drule.forall_elim_list (map (cert o snd) subst)
   365   end;
   366 
   367 fun hyps_rule rule th =
   368   let val {hyps, ...} = Thm.crep_thm th in
   369     Drule.implies_elim_list
   370       (rule (Drule.implies_intr_list hyps th))
   371       (map (Thm.assume o Drule.cterm_rule rule) hyps)
   372   end;
   373 
   374 
   375 (* instantiate types *)
   376 
   377 fun instT_type env =
   378   if Symtab.is_empty env then I
   379   else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
   380 
   381 fun instT_term env =
   382   if Symtab.is_empty env then I
   383   else Term.map_types (instT_type env);
   384 
   385 fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
   386   (fn T as TFree (a, _) =>
   387     let val T' = the_default T (Symtab.lookup env a)
   388     in if T = T' then I else insert (op =) (a, T') end
   389   | _ => I) th [];
   390 
   391 fun instT_thm thy env th =
   392   if Symtab.is_empty env then th
   393   else
   394     let val subst = instT_subst env th
   395     in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
   396 
   397 fun instT_morphism thy env =
   398   let val thy_ref = Theory.check_thy thy in
   399     Morphism.morphism
   400      {binding = I,
   401       typ = instT_type env,
   402       term = instT_term env,
   403       fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
   404   end;
   405 
   406 
   407 (* instantiate types and terms *)
   408 
   409 fun inst_term (envT, env) =
   410   if Symtab.is_empty env then instT_term envT
   411   else
   412     let
   413       val instT = instT_type envT;
   414       fun inst (Const (x, T)) = Const (x, instT T)
   415         | inst (Free (x, T)) =
   416             (case Symtab.lookup env x of
   417               NONE => Free (x, instT T)
   418             | SOME t => t)
   419         | inst (Var (xi, T)) = Var (xi, instT T)
   420         | inst (b as Bound _) = b
   421         | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
   422         | inst (t $ u) = inst t $ inst u;
   423     in Envir.beta_norm o inst end;
   424 
   425 fun inst_thm thy (envT, env) th =
   426   if Symtab.is_empty env then instT_thm thy envT th
   427   else
   428     let
   429       val substT = instT_subst envT th;
   430       val subst = (Thm.fold_terms o Term.fold_aterms)
   431        (fn Free (x, T) =>
   432           let
   433             val T' = instT_type envT T;
   434             val t = Free (x, T');
   435             val t' = the_default t (Symtab.lookup env x);
   436           in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
   437        | _ => I) th [];
   438     in
   439       if null substT andalso null subst then th
   440       else th |> hyps_rule
   441        (instantiate_tfrees thy substT #>
   442         instantiate_frees thy subst #>
   443         Conv.fconv_rule (Thm.beta_conversion true))
   444     end;
   445 
   446 fun inst_morphism thy envs =
   447   let val thy_ref = Theory.check_thy thy in
   448     Morphism.morphism
   449      {binding = I,
   450       typ = instT_type (#1 envs),
   451       term = inst_term envs,
   452       fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
   453   end;
   454 
   455 
   456 (* satisfy hypotheses *)
   457 
   458 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
   459     (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
   460       NONE => I
   461     | SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm));
   462 
   463 val satisfy_morphism = Morphism.thm_morphism o satisfy_thm;
   464 val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism;
   465 
   466 
   467 (* rewriting with equalities *)
   468 
   469 fun eq_morphism thy thms = if null thms then NONE else SOME (Morphism.morphism
   470  {binding = I,
   471   typ = I,
   472   term = Raw_Simplifier.rewrite_term thy thms [],
   473   fact = map (Raw_Simplifier.rewrite_rule thms)});
   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 => Thm.transfer (Theory.deref thy_ref) th) end;
   481 
   482 
   483 
   484 (** activate in context **)
   485 
   486 (* init *)
   487 
   488 fun generic_note_thmss kind facts context =
   489   let
   490     val facts' = Attrib.map_facts (Attrib.attribute_i (Context.theory_of context)) facts;
   491   in
   492     context |> Context.mapping_result
   493       (Global_Theory.note_thmss kind facts')
   494       (ProofContext.note_thmss kind facts')
   495   end;
   496 
   497 fun init (Fixes fixes) = Context.map_proof (ProofContext.add_fixes fixes #> #2)
   498   | init (Constrains _) = I
   499   | init (Assumes asms) = Context.map_proof (fn ctxt =>
   500       let
   501         val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
   502         val (_, ctxt') = ctxt
   503           |> fold Variable.auto_fixes (maps (map #1 o #2) asms')
   504           |> ProofContext.add_assms_i Assumption.assume_export asms';
   505       in ctxt' end)
   506   | init (Defines defs) = Context.map_proof (fn ctxt =>
   507       let
   508         val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs;
   509         val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
   510             let val ((c, _), t') = Local_Defs.cert_def ctxt t  (* FIXME adapt ps? *)
   511             in (t', ((Thm.def_binding_optional (Binding.name c) name, atts), [(t', ps)])) end);
   512         val (_, ctxt') = ctxt
   513           |> fold Variable.auto_fixes (map #1 asms)
   514           |> ProofContext.add_assms_i Local_Defs.def_export (map #2 asms);
   515       in ctxt' end)
   516   | init (Notes (kind, facts)) = generic_note_thmss kind facts #> #2;
   517 
   518 
   519 (* activate *)
   520 
   521 fun activate_i elem ctxt =
   522   let
   523     val elem' = map_ctxt_attrib Args.assignable elem;
   524     val ctxt' = Context.proof_map (init elem') ctxt;
   525   in (map_ctxt_attrib Args.closure elem', ctxt') end;
   526 
   527 fun activate raw_elem ctxt =
   528   let val elem = raw_elem |> map_ctxt
   529    {binding = tap Name.of_binding,
   530     typ = I,
   531     term = I,
   532     pattern = I,
   533     fact = ProofContext.get_fact ctxt,
   534     attrib = Attrib.intern_src (ProofContext.theory_of ctxt)}
   535   in activate_i elem ctxt end;
   536 
   537 end;