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