src/Pure/Isar/specification.ML
author wenzelm
Fri Oct 26 22:10:43 2007 +0200 (2007-10-26)
changeset 25211 aec1cbdbca71
parent 25016 2bcac52d7abc
child 25371 26d349416c4f
permissions -rw-r--r--
notation: associate syntax to checked-unchecked term;
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    ID:         $Id$
wenzelm@18620
     3
    Author:     Makarius
wenzelm@18620
     4
wenzelm@21036
     5
Derived local theory specifications --- with type-inference and
wenzelm@18810
     6
toplevel polymorphism.
wenzelm@18620
     7
*)
wenzelm@18620
     8
wenzelm@18620
     9
signature SPECIFICATION =
wenzelm@18620
    10
sig
wenzelm@20890
    11
  val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
wenzelm@24724
    12
  val check_specification: (string * typ option * mixfix) list ->
wenzelm@24724
    13
    ((string * Attrib.src list) * term list) list list -> Proof.context ->
wenzelm@18771
    14
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@24724
    15
    Proof.context
wenzelm@24927
    16
  val read_specification: (string * string option * mixfix) list ->
wenzelm@24927
    17
    ((string * Attrib.src list) * string list) list list -> Proof.context ->
wenzelm@18771
    18
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@24724
    19
    Proof.context
wenzelm@24724
    20
  val check_free_specification: (string * typ option * mixfix) list ->
wenzelm@24724
    21
    ((string * Attrib.src list) * term list) list -> Proof.context ->
wenzelm@24724
    22
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@24724
    23
    Proof.context
wenzelm@24927
    24
  val read_free_specification: (string * string option * mixfix) list ->
wenzelm@24927
    25
    ((string * Attrib.src list) * string list) list -> Proof.context ->
wenzelm@24927
    26
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@24927
    27
    Proof.context
wenzelm@24927
    28
  val axiomatization: (string * typ option * mixfix) list ->
wenzelm@24927
    29
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@24927
    30
    (term list * (bstring * thm list) list) * local_theory
wenzelm@24927
    31
  val axiomatization_cmd: (string * string option * mixfix) list ->
wenzelm@18954
    32
    ((bstring * Attrib.src list) * string list) list -> local_theory ->
wenzelm@18954
    33
    (term list * (bstring * thm list) list) * local_theory
wenzelm@18954
    34
  val definition:
wenzelm@21705
    35
    (string * typ option * mixfix) option * ((string * Attrib.src list) * term) ->
wenzelm@21705
    36
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@24927
    37
  val definition_cmd:
wenzelm@24927
    38
    (string * string option * mixfix) option * ((string * Attrib.src list) * string) ->
wenzelm@24927
    39
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@24927
    40
  val abbreviation: Syntax.mode -> (string * typ option * mixfix) option * term ->
wenzelm@21532
    41
    local_theory -> local_theory
wenzelm@24927
    42
  val abbreviation_cmd: Syntax.mode -> (string * string option * mixfix) option * string ->
wenzelm@24927
    43
    local_theory -> local_theory
wenzelm@24949
    44
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    45
  val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
wenzelm@24927
    46
  val theorems: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list
wenzelm@20914
    47
    -> local_theory -> (bstring * thm list) list * local_theory
wenzelm@24927
    48
  val theorems_cmd: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list
wenzelm@20914
    49
    -> local_theory -> (bstring * thm list) list * local_theory
wenzelm@24986
    50
  val theorem: string -> Method.text option ->
wenzelm@24986
    51
    (thm list list -> local_theory -> local_theory) ->
wenzelm@24927
    52
    string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
berghofe@24452
    53
    bool -> local_theory -> Proof.state
wenzelm@24986
    54
  val theorem_cmd: string -> Method.text option ->
wenzelm@24986
    55
    (thm list list -> local_theory -> local_theory) ->
wenzelm@24927
    56
    string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
berghofe@24452
    57
    bool -> local_theory -> Proof.state
berghofe@24452
    58
  val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
wenzelm@18620
    59
end;
wenzelm@18620
    60
wenzelm@18620
    61
structure Specification: SPECIFICATION =
wenzelm@18620
    62
struct
wenzelm@18620
    63
wenzelm@21230
    64
wenzelm@20890
    65
(* diagnostics *)
wenzelm@20890
    66
wenzelm@20890
    67
fun print_consts _ _ [] = ()
wenzelm@24986
    68
  | print_consts ctxt pred cs = Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
wenzelm@20914
    69
wenzelm@19664
    70
wenzelm@18620
    71
(* prepare specification *)
wenzelm@18620
    72
wenzelm@24724
    73
local
wenzelm@24724
    74
wenzelm@24724
    75
fun close_forms ctxt i xs As =
wenzelm@24724
    76
  let
wenzelm@24724
    77
    fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x
wenzelm@24724
    78
      | add_free _ = I;
wenzelm@24724
    79
wenzelm@24724
    80
    val commons = map #1 xs;
wenzelm@24724
    81
    val _ =
wenzelm@24724
    82
      (case duplicates (op =) commons of [] => ()
wenzelm@24724
    83
      | dups => error ("Duplicate local variables " ^ commas_quote dups));
wenzelm@24724
    84
    val frees = rev ((fold o fold_aterms) add_free As (rev commons));
wenzelm@24848
    85
    val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context Name.aT (length frees));
wenzelm@24724
    86
    val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
wenzelm@24724
    87
wenzelm@24724
    88
    fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
wenzelm@24724
    89
      | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
wenzelm@24724
    90
      | abs_body lev y (t as Free (x, T)) =
wenzelm@24724
    91
          if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev))
wenzelm@24724
    92
          else t
wenzelm@24724
    93
      | abs_body _ _ a = a;
wenzelm@24724
    94
    fun close (y, U) B =
wenzelm@24724
    95
      let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
wenzelm@24724
    96
      in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end;
wenzelm@24724
    97
    fun close_form A =
wenzelm@24724
    98
      let
wenzelm@24724
    99
        val occ_frees = rev (fold_aterms add_free A []);
wenzelm@24724
   100
        val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
wenzelm@24724
   101
      in fold_rev close bounds A end;
wenzelm@24724
   102
  in map close_form As end;
wenzelm@24724
   103
wenzelm@24724
   104
fun prep_spec prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
wenzelm@18620
   105
  let
wenzelm@18670
   106
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
   107
wenzelm@18670
   108
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
   109
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18620
   110
wenzelm@24724
   111
    val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss;
wenzelm@24734
   112
    val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
wenzelm@24734
   113
      |> fold Name.declare xs;
wenzelm@24734
   114
    val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
wenzelm@24734
   115
    val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
wenzelm@24724
   116
    val specs =
wenzelm@24724
   117
      (if do_close then
wenzelm@24734
   118
        #1 (fold_map
wenzelm@24734
   119
            (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
wenzelm@24734
   120
      else Asss')
wenzelm@24724
   121
      |> flat |> burrow (Syntax.check_props params_ctxt);
wenzelm@24724
   122
    val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
wenzelm@24724
   123
wenzelm@24724
   124
    val vs = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
wenzelm@18771
   125
    val params = vs ~~ map #3 vars;
wenzelm@24724
   126
    val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
wenzelm@24724
   127
  in ((params, name_atts ~~ specs), specs_ctxt) end;
wenzelm@24724
   128
wenzelm@24724
   129
fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src x;
wenzelm@24724
   130
fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) x;
wenzelm@18620
   131
wenzelm@24724
   132
in
wenzelm@21370
   133
wenzelm@24724
   134
fun read_specification x = read_spec true x;
wenzelm@24724
   135
fun check_specification x = check_spec true x;
wenzelm@24724
   136
fun read_free_specification vars specs = read_spec false vars [specs];
wenzelm@24724
   137
fun check_free_specification vars specs = check_spec false vars [specs];
wenzelm@24724
   138
wenzelm@24724
   139
end;
wenzelm@18620
   140
wenzelm@18620
   141
wenzelm@18771
   142
(* axiomatization *)
wenzelm@18620
   143
wenzelm@20890
   144
fun gen_axioms prep raw_vars raw_specs lthy =
wenzelm@18620
   145
  let
wenzelm@24986
   146
    val ((vars, specs), _) = prep raw_vars [raw_specs] lthy;
wenzelm@24986
   147
    val ((consts, axioms), lthy') = LocalTheory.axioms Thm.axiomK (vars, specs) lthy;
wenzelm@24724
   148
    val consts' = map (Morphism.term (LocalTheory.target_morphism lthy')) consts;
wenzelm@24986
   149
    val _ = print_consts lthy' (member (op =) (fold Term.add_frees consts' [])) (map fst vars);
wenzelm@24986
   150
  in ((consts, axioms), lthy') end;
wenzelm@18786
   151
wenzelm@24927
   152
val axiomatization = gen_axioms check_specification;
wenzelm@24927
   153
val axiomatization_cmd = gen_axioms read_specification;
wenzelm@18620
   154
wenzelm@18786
   155
wenzelm@18786
   156
(* definition *)
wenzelm@18786
   157
wenzelm@21705
   158
fun gen_def prep (raw_var, (raw_a, raw_prop)) lthy =
wenzelm@18786
   159
  let
wenzelm@21705
   160
    val (vars, [((raw_name, atts), [prop])]) =
wenzelm@21705
   161
      fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
wenzelm@21705
   162
    val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
wenzelm@21705
   163
    val name = Thm.def_name_optional x raw_name;
wenzelm@21705
   164
    val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@21705
   165
      if x = x' then mx
wenzelm@21705
   166
      else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@21705
   167
    val ((lhs, (_, th)), lthy2) = lthy
wenzelm@25016
   168
      |> LocalTheory.define Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs));
wenzelm@21705
   169
    val ((b, [th']), lthy3) = lthy2
haftmann@22744
   170
      |> LocalTheory.note Thm.definitionK
haftmann@24624
   171
          ((name, Code.add_default_func_attr :: atts), [prove lthy2 th]);
wenzelm@18786
   172
wenzelm@21705
   173
    val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs;
wenzelm@21705
   174
    val _ = print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
wenzelm@21705
   175
  in ((lhs, (b, th')), lthy3) end;
wenzelm@18786
   176
wenzelm@24927
   177
val definition = gen_def check_free_specification;
wenzelm@24927
   178
val definition_cmd = gen_def read_free_specification;
wenzelm@18786
   179
wenzelm@19080
   180
wenzelm@19080
   181
(* abbreviation *)
wenzelm@19080
   182
wenzelm@21705
   183
fun gen_abbrev prep mode (raw_var, raw_prop) lthy =
wenzelm@19080
   184
  let
wenzelm@21705
   185
    val ((vars, [(_, [prop])]), _) =
wenzelm@21705
   186
      prep (the_list raw_var) [(("", []), [raw_prop])]
wenzelm@24676
   187
        (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
wenzelm@21705
   188
    val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
wenzelm@21705
   189
    val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
wenzelm@21705
   190
      if x = y then mx
wenzelm@21705
   191
      else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
wenzelm@21705
   192
    val lthy' = lthy
wenzelm@24986
   193
      |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
wenzelm@21793
   194
      |> LocalTheory.abbrev mode ((x, mx), rhs) |> snd
wenzelm@21705
   195
      |> ProofContext.restore_syntax_mode lthy;
wenzelm@24724
   196
wenzelm@21705
   197
    val _ = print_consts lthy' (K false) [(x, T)]
wenzelm@21532
   198
  in lthy' end;
wenzelm@19080
   199
wenzelm@24927
   200
val abbreviation = gen_abbrev check_free_specification;
wenzelm@24927
   201
val abbreviation_cmd = gen_abbrev read_free_specification;
wenzelm@19080
   202
wenzelm@19664
   203
wenzelm@21230
   204
(* notation *)
wenzelm@19664
   205
wenzelm@24949
   206
fun gen_notation prep_const add mode args lthy =
wenzelm@25211
   207
  let
wenzelm@25211
   208
    val ctxt = ProofContext.set_mode ProofContext.mode_abbrev lthy;
wenzelm@25211
   209
    fun prep_arg (s, mx) =
wenzelm@25211
   210
      let
wenzelm@25211
   211
        val t = Syntax.check_term ctxt
wenzelm@25211
   212
          (case prep_const ctxt s of Const (c, _) => Const (c, dummyT) | a => a);
wenzelm@25211
   213
        val [t'] = Syntax.uncheck_terms ctxt [t];
wenzelm@25211
   214
        val u = if Term.is_Const t' orelse Term.is_Free t' then t' else t;
wenzelm@25211
   215
      in (u, mx) end;
wenzelm@25211
   216
  in lthy |> LocalTheory.notation add mode (map prep_arg args) end;
wenzelm@19664
   217
wenzelm@24927
   218
val notation = gen_notation (K I);
wenzelm@24927
   219
val notation_cmd = gen_notation ProofContext.read_const;
wenzelm@19664
   220
wenzelm@20914
   221
wenzelm@21036
   222
(* fact statements *)
wenzelm@20914
   223
wenzelm@20914
   224
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   225
  let
wenzelm@20914
   226
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   227
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   228
      ((name, map attrib atts),
wenzelm@21435
   229
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
wenzelm@21435
   230
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
wenzelm@24986
   231
    val _ = ProofDisplay.present_results lthy' ((kind, ""), res);
wenzelm@20914
   232
  in (res, lthy') end;
wenzelm@20914
   233
wenzelm@24927
   234
val theorems = gen_theorems (K I) (K I);
wenzelm@24927
   235
val theorems_cmd = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   236
wenzelm@21036
   237
wenzelm@21230
   238
(* complex goal statements *)
wenzelm@21036
   239
wenzelm@21036
   240
local
wenzelm@21036
   241
wenzelm@21450
   242
fun subtract_prems ctxt1 ctxt2 =
wenzelm@21450
   243
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
wenzelm@21450
   244
wenzelm@21236
   245
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   246
  (case concl of
wenzelm@21236
   247
    Element.Shows shows =>
wenzelm@21230
   248
      let
wenzelm@21450
   249
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
wenzelm@21450
   250
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21450
   251
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   252
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@21450
   253
      in ((prems, stmt, []), goal_ctxt) end
wenzelm@21236
   254
  | Element.Obtains obtains =>
wenzelm@21230
   255
      let
wenzelm@21236
   256
        val case_names = obtains |> map_index
wenzelm@21236
   257
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@21236
   258
        val constraints = obtains |> map (fn (_, (vars, _)) =>
wenzelm@21236
   259
          Locale.Elem (Element.Constrains
wenzelm@21236
   260
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
wenzelm@21036
   261
wenzelm@21236
   262
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
wenzelm@21450
   263
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   264
wenzelm@21236
   265
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@21036
   266
wenzelm@21236
   267
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   268
          let
wenzelm@21236
   269
            val xs = map fst vars;
wenzelm@21236
   270
            val props = map fst asms;
wenzelm@21236
   271
            val (parms, _) = ctxt'
wenzelm@21236
   272
              |> fold Variable.declare_term props
wenzelm@21236
   273
              |> fold_map ProofContext.inferred_param xs;
wenzelm@21236
   274
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@21236
   275
          in
wenzelm@21236
   276
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@21370
   277
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   278
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@21236
   279
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   280
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   281
          end;
wenzelm@21236
   282
wenzelm@21658
   283
        val atts = map (Attrib.internal o K)
wenzelm@21236
   284
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
wenzelm@21450
   285
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21236
   286
        val stmt = [(("", atts), [(thesis, [])])];
wenzelm@21236
   287
wenzelm@21236
   288
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@21236
   289
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@21236
   290
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@21435
   291
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
wenzelm@21435
   292
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@21450
   293
      in ((prems, stmt, facts), goal_ctxt) end);
wenzelm@21036
   294
berghofe@24452
   295
structure TheoremHook = GenericDataFun
berghofe@24452
   296
(
berghofe@24452
   297
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
berghofe@24452
   298
  val empty = [];
berghofe@24452
   299
  val extend = I;
wenzelm@24464
   300
  fun merge _ hooks : T = Library.merge (eq_snd op =) hooks;
berghofe@24452
   301
);
berghofe@24452
   302
wenzelm@21036
   303
fun gen_theorem prep_att prep_stmt
berghofe@24452
   304
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy0 =
wenzelm@21036
   305
  let
wenzelm@21860
   306
    val _ = LocalTheory.affirm lthy0;
wenzelm@21036
   307
    val thy = ProofContext.theory_of lthy0;
wenzelm@21206
   308
wenzelm@21036
   309
    val (loc, ctxt, lthy) =
wenzelm@21036
   310
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
wenzelm@25011
   311
        ({target, is_locale = true, ...}, true) =>
wenzelm@25011
   312
          (*temporary workaround for non-modularity of in/includes*)  (* FIXME *)
wenzelm@25011
   313
          (SOME target, ProofContext.init thy, LocalTheory.restore lthy0)
wenzelm@21036
   314
      | _ => (NONE, lthy0, lthy0));
wenzelm@21036
   315
wenzelm@21435
   316
    val attrib = prep_att thy;
wenzelm@21435
   317
    val atts = map attrib raw_atts;
wenzelm@21435
   318
wenzelm@21532
   319
    val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib));
wenzelm@21450
   320
    val ((prems, stmt, facts), goal_ctxt) =
wenzelm@21450
   321
      prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
wenzelm@21036
   322
wenzelm@21036
   323
    fun after_qed' results goal_ctxt' =
wenzelm@21602
   324
      let val results' =
wenzelm@21602
   325
        burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
wenzelm@21602
   326
      in
wenzelm@21036
   327
        lthy
wenzelm@21435
   328
        |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
wenzelm@21036
   329
        |> (fn (res, lthy') =>
wenzelm@24986
   330
          (ProofDisplay.present_results lthy' ((kind, name), res);
wenzelm@21435
   331
            if name = "" andalso null atts then lthy'
wenzelm@21435
   332
            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
wenzelm@21036
   333
        |> after_qed results'
wenzelm@21036
   334
      end;
wenzelm@21036
   335
  in
wenzelm@21036
   336
    goal_ctxt
wenzelm@21450
   337
    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
wenzelm@21450
   338
    |> snd
wenzelm@21435
   339
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
wenzelm@24557
   340
    |> Proof.refine_insert facts
wenzelm@24542
   341
    |> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt))))
wenzelm@21036
   342
  end;
wenzelm@21036
   343
wenzelm@21230
   344
in
wenzelm@21230
   345
wenzelm@24927
   346
val theorem = gen_theorem (K I) Locale.cert_context_statement;
wenzelm@24927
   347
val theorem_cmd = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
wenzelm@21036
   348
berghofe@24452
   349
fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
berghofe@24452
   350
wenzelm@18620
   351
end;
wenzelm@21036
   352
wenzelm@21036
   353
end;