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