src/Pure/Isar/specification.ML
author wenzelm
Thu Mar 06 13:44:01 2014 +0100 (2014-03-06)
changeset 55954 a29aefc88c8d
parent 55952 2f85cc6c27d4
child 55955 e8f1bf005661
permissions -rw-r--r--
more uniform check_const/read_const;
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    Author:     Makarius
wenzelm@18620
     3
wenzelm@21036
     4
Derived local theory specifications --- with type-inference and
wenzelm@18810
     5
toplevel polymorphism.
wenzelm@18620
     6
*)
wenzelm@18620
     7
wenzelm@18620
     8
signature SPECIFICATION =
wenzelm@18620
     9
sig
wenzelm@30482
    10
  val check_spec:
wenzelm@30482
    11
    (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
wenzelm@30482
    12
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    13
  val read_spec:
wenzelm@30482
    14
    (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
wenzelm@30482
    15
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    16
  val check_free_spec:
wenzelm@30482
    17
    (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
wenzelm@30482
    18
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    19
  val read_free_spec:
wenzelm@30482
    20
    (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
wenzelm@30482
    21
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
haftmann@29581
    22
  val check_specification: (binding * typ option * mixfix) list ->
wenzelm@30482
    23
    (Attrib.binding * term list) list -> Proof.context ->
haftmann@29581
    24
    (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
haftmann@29581
    25
  val read_specification: (binding * string option * mixfix) list ->
wenzelm@28084
    26
    (Attrib.binding * string list) list -> Proof.context ->
haftmann@29581
    27
    (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
haftmann@29581
    28
  val axiomatization: (binding * typ option * mixfix) list ->
wenzelm@28114
    29
    (Attrib.binding * term list) list -> theory ->
wenzelm@33703
    30
    (term list * thm list list) * theory
haftmann@29581
    31
  val axiomatization_cmd: (binding * string option * mixfix) list ->
wenzelm@28114
    32
    (Attrib.binding * string list) list -> theory ->
wenzelm@33703
    33
    (term list * thm list list) * theory
wenzelm@35894
    34
  val axiom: Attrib.binding * term -> theory -> thm * theory
wenzelm@18954
    35
  val definition:
haftmann@29581
    36
    (binding * typ option * mixfix) option * (Attrib.binding * term) ->
wenzelm@28080
    37
    local_theory -> (term * (string * thm)) * local_theory
wenzelm@44192
    38
  val definition':
wenzelm@44192
    39
    (binding * typ option * mixfix) option * (Attrib.binding * term) ->
wenzelm@44192
    40
    bool -> local_theory -> (term * (string * thm)) * local_theory
wenzelm@24927
    41
  val definition_cmd:
haftmann@29581
    42
    (binding * string option * mixfix) option * (Attrib.binding * string) ->
wenzelm@44192
    43
    bool -> local_theory -> (term * (string * thm)) * local_theory
haftmann@29581
    44
  val abbreviation: Syntax.mode -> (binding * typ option * mixfix) option * term ->
wenzelm@44192
    45
    bool -> local_theory -> local_theory
haftmann@29581
    46
  val abbreviation_cmd: Syntax.mode -> (binding * string option * mixfix) option * string ->
wenzelm@44192
    47
    bool -> local_theory -> local_theory
wenzelm@35413
    48
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
wenzelm@55639
    49
  val type_notation_cmd: bool -> Syntax.mode -> (string * mixfix) list ->
wenzelm@55639
    50
    local_theory -> local_theory
wenzelm@24949
    51
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    52
  val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
wenzelm@28080
    53
  val theorems: string ->
wenzelm@28084
    54
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@45600
    55
    (binding * typ option * mixfix) list ->
wenzelm@44192
    56
    bool -> local_theory -> (string * thm list) list * local_theory
wenzelm@26336
    57
  val theorems_cmd: string ->
wenzelm@28084
    58
    (Attrib.binding * (Facts.ref * Attrib.src list) list) list ->
wenzelm@45600
    59
    (binding * string option * mixfix) list ->
wenzelm@44192
    60
    bool -> local_theory -> (string * thm list) list * local_theory
wenzelm@24986
    61
  val theorem: string -> Method.text option ->
wenzelm@28084
    62
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
wenzelm@47067
    63
    string list -> Element.context_i list -> Element.statement_i ->
berghofe@24452
    64
    bool -> local_theory -> Proof.state
wenzelm@24986
    65
  val theorem_cmd: string -> Method.text option ->
wenzelm@28084
    66
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
wenzelm@47067
    67
    (xstring * Position.T) list -> Element.context list -> Element.statement ->
berghofe@24452
    68
    bool -> local_theory -> Proof.state
wenzelm@36317
    69
  val schematic_theorem: string -> Method.text option ->
wenzelm@36317
    70
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
wenzelm@47067
    71
    string list -> Element.context_i list -> Element.statement_i ->
wenzelm@36317
    72
    bool -> local_theory -> Proof.state
wenzelm@36317
    73
  val schematic_theorem_cmd: string -> Method.text option ->
wenzelm@36317
    74
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
wenzelm@47067
    75
    (xstring * Position.T) list -> Element.context list -> Element.statement ->
wenzelm@36317
    76
    bool -> local_theory -> Proof.state
berghofe@24452
    77
  val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
wenzelm@18620
    78
end;
wenzelm@18620
    79
wenzelm@18620
    80
structure Specification: SPECIFICATION =
wenzelm@18620
    81
struct
wenzelm@18620
    82
wenzelm@18620
    83
(* prepare specification *)
wenzelm@18620
    84
wenzelm@24724
    85
local
wenzelm@24724
    86
wenzelm@24724
    87
fun close_forms ctxt i xs As =
wenzelm@24724
    88
  let
wenzelm@24724
    89
    val commons = map #1 xs;
wenzelm@24724
    90
    val _ =
wenzelm@24724
    91
      (case duplicates (op =) commons of [] => ()
wenzelm@24724
    92
      | dups => error ("Duplicate local variables " ^ commas_quote dups));
wenzelm@42482
    93
    val frees = rev (fold (Variable.add_free_names ctxt) As (rev commons));
wenzelm@55639
    94
    val types =
wenzelm@55639
    95
      map (Type_Infer.param i o rpair []) (Name.invent Name.context Name.aT (length frees));
wenzelm@24724
    96
    val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
wenzelm@24724
    97
wenzelm@24724
    98
    fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
wenzelm@24724
    99
      | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
wenzelm@24724
   100
      | abs_body lev y (t as Free (x, T)) =
wenzelm@39288
   101
          if x = y then Type.constraint (uniform_typing x) (Type.constraint T (Bound lev))
wenzelm@24724
   102
          else t
wenzelm@24724
   103
      | abs_body _ _ a = a;
wenzelm@24724
   104
    fun close (y, U) B =
wenzelm@24724
   105
      let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
wenzelm@46217
   106
      in if Term.is_dependent B' then Logic.all_const dummyT $ Abs (y, U, B') else B end;
wenzelm@24724
   107
    fun close_form A =
wenzelm@24724
   108
      let
wenzelm@42482
   109
        val occ_frees = rev (Variable.add_free_names ctxt A []);
wenzelm@24724
   110
        val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
wenzelm@24724
   111
      in fold_rev close bounds A end;
wenzelm@24724
   112
  in map close_form As end;
wenzelm@24724
   113
wenzelm@30482
   114
fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
wenzelm@18620
   115
  let
wenzelm@42360
   116
    val thy = Proof_Context.theory_of ctxt;
wenzelm@18620
   117
wenzelm@18670
   118
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@42360
   119
    val (xs, params_ctxt) = vars_ctxt |> Proof_Context.add_fixes vars;
wenzelm@18620
   120
wenzelm@42265
   121
    val Asss =
wenzelm@42265
   122
      (map o map) snd raw_specss
wenzelm@46989
   123
      |> (burrow o burrow)
wenzelm@46989
   124
        (grouped 10 (Par_List.map_name "Specification.parse_prop") (parse_prop params_ctxt));
wenzelm@24734
   125
    val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
wenzelm@24734
   126
      |> fold Name.declare xs;
wenzelm@24734
   127
    val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
wenzelm@24734
   128
    val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
wenzelm@24724
   129
    val specs =
wenzelm@24724
   130
      (if do_close then
wenzelm@24734
   131
        #1 (fold_map
wenzelm@24734
   132
            (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
wenzelm@24734
   133
      else Asss')
wenzelm@24724
   134
      |> flat |> burrow (Syntax.check_props params_ctxt);
wenzelm@24724
   135
    val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
wenzelm@24724
   136
wenzelm@42360
   137
    val Ts = specs_ctxt |> fold_map Proof_Context.inferred_param xs |> fst;
wenzelm@28080
   138
    val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
wenzelm@24724
   139
    val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
wenzelm@24724
   140
  in ((params, name_atts ~~ specs), specs_ctxt) end;
wenzelm@24724
   141
wenzelm@30482
   142
wenzelm@30482
   143
fun single_spec (a, prop) = [(a, [prop])];
wenzelm@30482
   144
fun the_spec (a, [prop]) = (a, prop);
wenzelm@30482
   145
wenzelm@30482
   146
fun prep_spec prep_vars parse_prop prep_att do_close vars specs =
wenzelm@30482
   147
  prepare prep_vars parse_prop prep_att do_close
wenzelm@30482
   148
    vars (map single_spec specs) #>> apsnd (map the_spec);
wenzelm@30482
   149
wenzelm@24724
   150
in
wenzelm@21370
   151
wenzelm@42360
   152
fun check_spec x = prep_spec Proof_Context.cert_vars (K I) (K I) true x;
wenzelm@42360
   153
fun read_spec x = prep_spec Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src true x;
wenzelm@30482
   154
wenzelm@42360
   155
fun check_free_spec x = prep_spec Proof_Context.cert_vars (K I) (K I) false x;
wenzelm@42360
   156
fun read_free_spec x = prep_spec Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src false x;
wenzelm@30482
   157
wenzelm@30482
   158
fun check_specification vars specs =
wenzelm@42360
   159
  prepare Proof_Context.cert_vars (K I) (K I) true vars [specs];
wenzelm@30482
   160
wenzelm@30482
   161
fun read_specification vars specs =
wenzelm@42360
   162
  prepare Proof_Context.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
wenzelm@24724
   163
wenzelm@24724
   164
end;
wenzelm@18620
   165
wenzelm@18620
   166
wenzelm@28114
   167
(* axiomatization -- within global theory *)
wenzelm@18620
   168
wenzelm@44192
   169
fun gen_axioms prep raw_vars raw_specs thy =
wenzelm@18620
   170
  let
wenzelm@42360
   171
    val ((vars, specs), _) = prep raw_vars raw_specs (Proof_Context.init_global thy);
wenzelm@42494
   172
    val xs = map (fn ((b, T), _) => (Variable.check_name b, T)) vars;
wenzelm@28114
   173
wenzelm@28114
   174
    (*consts*)
wenzelm@33173
   175
    val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
wenzelm@28114
   176
    val subst = Term.subst_atomic (map Free xs ~~ consts);
wenzelm@28114
   177
wenzelm@28114
   178
    (*axioms*)
wenzelm@30470
   179
    val (axioms, axioms_thy) = (specs, consts_thy) |-> fold_map (fn ((b, atts), props) =>
wenzelm@42375
   180
        fold_map Thm.add_axiom_global
wenzelm@30470
   181
          (map (apfst (fn a => Binding.map_name (K a) b))
wenzelm@47021
   182
            (Global_Theory.name_multi (Binding.name_of b) (map subst props)))
wenzelm@36106
   183
        #>> (fn ths => ((b, atts), [(map #2 ths, [])])));
wenzelm@30470
   184
wenzelm@30470
   185
    (*facts*)
wenzelm@33703
   186
    val (facts, facts_lthy) = axioms_thy
haftmann@38388
   187
      |> Named_Target.theory_init
wenzelm@33703
   188
      |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
wenzelm@33703
   189
      |> Local_Theory.notes axioms;
wenzelm@30470
   190
wenzelm@33703
   191
  in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
wenzelm@18786
   192
wenzelm@44192
   193
val axiomatization = gen_axioms check_specification;
wenzelm@44192
   194
val axiomatization_cmd = gen_axioms read_specification;
wenzelm@18620
   195
wenzelm@35894
   196
fun axiom (b, ax) = axiomatization [] [(b, [ax])] #>> (hd o hd o snd);
wenzelm@35894
   197
wenzelm@18786
   198
wenzelm@18786
   199
(* definition *)
wenzelm@18786
   200
wenzelm@44192
   201
fun gen_def prep (raw_var, raw_spec) int lthy =
wenzelm@18786
   202
  let
wenzelm@30482
   203
    val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
wenzelm@35624
   204
    val (((x, T), rhs), prove) = Local_Defs.derived_def lthy true prop;
wenzelm@30434
   205
    val var as (b, _) =
wenzelm@28080
   206
      (case vars of
haftmann@28965
   207
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   208
      | [((b, _), mx)] =>
wenzelm@28080
   209
          let
wenzelm@42494
   210
            val y = Variable.check_name b;
wenzelm@28080
   211
            val _ = x = y orelse
wenzelm@28080
   212
              error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^
wenzelm@48992
   213
                Position.here (Binding.pos_of b));
wenzelm@28080
   214
          in (b, mx) end);
wenzelm@30434
   215
    val name = Thm.def_binding_optional b raw_name;
wenzelm@33455
   216
    val ((lhs, (_, raw_th)), lthy2) = lthy
wenzelm@46992
   217
      |> Local_Theory.define_internal (var, ((Binding.suffix_name "_raw" name, []), rhs));
wenzelm@33455
   218
wenzelm@33455
   219
    val th = prove lthy2 raw_th;
wenzelm@33455
   220
    val lthy3 = lthy2 |> Spec_Rules.add Spec_Rules.Equational ([lhs], [th]);
wenzelm@33455
   221
wenzelm@33670
   222
    val ([(def_name, [th'])], lthy4) = lthy3
wenzelm@47080
   223
      |> Local_Theory.notes [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
wenzelm@18786
   224
wenzelm@33671
   225
    val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
wenzelm@44192
   226
wenzelm@44192
   227
    val _ = Proof_Display.print_consts int lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
wenzelm@33455
   228
  in ((lhs, (def_name, th')), lthy4) end;
wenzelm@18786
   229
wenzelm@44192
   230
val definition' = gen_def check_free_spec;
wenzelm@44192
   231
fun definition spec = definition' spec false;
wenzelm@44192
   232
val definition_cmd = gen_def read_free_spec;
wenzelm@18786
   233
wenzelm@19080
   234
wenzelm@19080
   235
(* abbreviation *)
wenzelm@19080
   236
wenzelm@44192
   237
fun gen_abbrev prep mode (raw_var, raw_prop) int lthy =
wenzelm@19080
   238
  let
wenzelm@55119
   239
    val lthy1 = lthy
wenzelm@55119
   240
      |> Proof_Context.set_syntax_mode mode;
wenzelm@30482
   241
    val ((vars, [(_, prop)]), _) =
wenzelm@30482
   242
      prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
wenzelm@55119
   243
        (lthy1 |> Proof_Context.set_mode Proof_Context.mode_abbrev);
wenzelm@55119
   244
    val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy1 prop));
wenzelm@28080
   245
    val var =
wenzelm@28080
   246
      (case vars of
haftmann@28965
   247
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   248
      | [((b, _), mx)] =>
wenzelm@28080
   249
          let
wenzelm@42494
   250
            val y = Variable.check_name b;
wenzelm@28080
   251
            val _ = x = y orelse
wenzelm@28080
   252
              error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
wenzelm@48992
   253
                Position.here (Binding.pos_of b));
wenzelm@28080
   254
          in (b, mx) end);
wenzelm@55119
   255
    val lthy2 = lthy1
wenzelm@33671
   256
      |> Local_Theory.abbrev mode (var, rhs) |> snd
wenzelm@42360
   257
      |> Proof_Context.restore_syntax_mode lthy;
wenzelm@24724
   258
wenzelm@55119
   259
    val _ = Proof_Display.print_consts int lthy2 (K false) [(x, T)];
wenzelm@55119
   260
  in lthy2 end;
wenzelm@19080
   261
wenzelm@44192
   262
val abbreviation = gen_abbrev check_free_spec;
wenzelm@44192
   263
val abbreviation_cmd = gen_abbrev read_free_spec;
wenzelm@19080
   264
wenzelm@19664
   265
wenzelm@21230
   266
(* notation *)
wenzelm@19664
   267
wenzelm@35413
   268
local
wenzelm@35413
   269
wenzelm@35413
   270
fun gen_type_notation prep_type add mode args lthy =
wenzelm@35413
   271
  lthy |> Local_Theory.type_notation add mode (map (apfst (prep_type lthy)) args);
wenzelm@35413
   272
wenzelm@24949
   273
fun gen_notation prep_const add mode args lthy =
wenzelm@33671
   274
  lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   275
wenzelm@35413
   276
in
wenzelm@35413
   277
wenzelm@35413
   278
val type_notation = gen_type_notation (K I);
wenzelm@55951
   279
val type_notation_cmd =
wenzelm@55952
   280
  gen_type_notation (fn ctxt => Proof_Context.read_type_name ctxt {proper = true, strict = false});
wenzelm@35413
   281
wenzelm@24927
   282
val notation = gen_notation (K I);
wenzelm@55954
   283
val notation_cmd =
wenzelm@55954
   284
  gen_notation (fn ctxt => Proof_Context.read_const ctxt {proper = false, strict = false} dummyT);
wenzelm@19664
   285
wenzelm@35413
   286
end;
wenzelm@35413
   287
wenzelm@20914
   288
wenzelm@21036
   289
(* fact statements *)
wenzelm@20914
   290
wenzelm@45600
   291
local
wenzelm@45600
   292
wenzelm@45600
   293
fun gen_theorems prep_fact prep_att prep_vars
wenzelm@45600
   294
    kind raw_facts raw_fixes int lthy =
wenzelm@20914
   295
  let
wenzelm@42360
   296
    val attrib = prep_att (Proof_Context.theory_of lthy);
wenzelm@20914
   297
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   298
      ((name, map attrib atts),
wenzelm@26345
   299
        bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
wenzelm@45600
   300
    val (_, ctxt') = lthy |> prep_vars raw_fixes |-> Proof_Context.add_fixes;
wenzelm@45600
   301
wenzelm@45600
   302
    val facts' = facts
wenzelm@45600
   303
      |> Attrib.partial_evaluation ctxt'
wenzelm@45601
   304
      |> Element.transform_facts (Proof_Context.export_morphism ctxt' lthy);
wenzelm@45600
   305
    val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts';
wenzelm@46728
   306
    val _ = Proof_Display.print_results Markup.empty int lthy' ((kind, ""), res);
wenzelm@20914
   307
  in (res, lthy') end;
wenzelm@20914
   308
wenzelm@45600
   309
in
wenzelm@45600
   310
wenzelm@45600
   311
val theorems = gen_theorems (K I) (K I) Proof_Context.cert_vars;
wenzelm@45600
   312
val theorems_cmd = gen_theorems Proof_Context.get_fact Attrib.intern_src Proof_Context.read_vars;
wenzelm@45600
   313
wenzelm@45600
   314
end;
wenzelm@20914
   315
wenzelm@21036
   316
wenzelm@21230
   317
(* complex goal statements *)
wenzelm@21036
   318
wenzelm@21036
   319
local
wenzelm@21036
   320
wenzelm@21236
   321
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   322
  (case concl of
wenzelm@21236
   323
    Element.Shows shows =>
wenzelm@21230
   324
      let
ballarin@28880
   325
        val (propp, elems_ctxt) = prep_stmt elems (map snd shows) ctxt;
wenzelm@30472
   326
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
wenzelm@45390
   327
        val stmt = Attrib.map_specs (map prep_att) (map fst shows ~~ propp);
wenzelm@45327
   328
        val goal_ctxt = (fold o fold) (Variable.auto_fixes o fst) propp elems_ctxt;
wenzelm@45583
   329
      in (([], prems, stmt, NONE), goal_ctxt) end
wenzelm@21236
   330
  | Element.Obtains obtains =>
wenzelm@21230
   331
      let
haftmann@28862
   332
        val case_names = obtains |> map_index (fn (i, (b, _)) =>
wenzelm@47021
   333
          if Binding.is_empty b then string_of_int (i + 1) else Name_Space.base_name b);
wenzelm@21236
   334
        val constraints = obtains |> map (fn (_, (vars, _)) =>
ballarin@28710
   335
          Element.Constrains
wenzelm@42494
   336
            (vars |> map_filter (fn (x, SOME T) => SOME (Variable.check_name x, T) | _ => NONE)));
wenzelm@21036
   337
wenzelm@21236
   338
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
ballarin@28880
   339
        val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   340
wenzelm@42360
   341
        val thesis = Object_Logic.fixed_judgment (Proof_Context.theory_of ctxt) Auto_Bind.thesisN;
wenzelm@21036
   342
wenzelm@21236
   343
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   344
          let
wenzelm@28080
   345
            val bs = map fst vars;
wenzelm@42494
   346
            val xs = map Variable.check_name bs;
wenzelm@21236
   347
            val props = map fst asms;
wenzelm@28080
   348
            val (Ts, _) = ctxt'
wenzelm@21236
   349
              |> fold Variable.declare_term props
wenzelm@42360
   350
              |> fold_map Proof_Context.inferred_param xs;
wenzelm@46215
   351
            val params = map Free (xs ~~ Ts);
wenzelm@46215
   352
            val asm = fold_rev Logic.all params (Logic.list_implies (props, thesis));
wenzelm@45583
   353
            val _ = ctxt' |> Proof_Context.add_fixes (map (fn b => (b, NONE, NoSyn)) bs);
wenzelm@21236
   354
          in
wenzelm@45583
   355
            ctxt'
wenzelm@45583
   356
            |> Variable.auto_fixes asm
wenzelm@42360
   357
            |> Proof_Context.add_assms_i Assumption.assume_export
wenzelm@33369
   358
              [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   359
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   360
          end;
wenzelm@21236
   361
wenzelm@45583
   362
        val more_atts = map (Attrib.internal o K)
wenzelm@33368
   363
          [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
wenzelm@30472
   364
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
wenzelm@45583
   365
        val stmt = [((Binding.empty, []), [(thesis, [])])];
wenzelm@21236
   366
wenzelm@21236
   367
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@42360
   368
          |> (snd o Proof_Context.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
wenzelm@21236
   369
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@33644
   370
          |-> (fn ths =>
wenzelm@42360
   371
            Proof_Context.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
wenzelm@33644
   372
            #2 #> pair ths);
wenzelm@45583
   373
      in ((more_atts, prems, stmt, SOME facts), goal_ctxt) end);
wenzelm@21036
   374
wenzelm@37216
   375
structure Theorem_Hook = Generic_Data
berghofe@24452
   376
(
berghofe@24452
   377
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
berghofe@24452
   378
  val empty = [];
berghofe@24452
   379
  val extend = I;
wenzelm@41472
   380
  fun merge data : T = Library.merge (eq_snd op =) data;
berghofe@24452
   381
);
berghofe@24452
   382
wenzelm@47067
   383
fun gen_theorem schematic prep_bundle prep_att prep_stmt
wenzelm@47067
   384
    kind before_qed after_qed (name, raw_atts) raw_includes raw_elems raw_concl int lthy =
wenzelm@21036
   385
  let
wenzelm@47066
   386
    val _ = Local_Theory.assert lthy;
wenzelm@42360
   387
    val thy = Proof_Context.theory_of lthy;
wenzelm@21036
   388
wenzelm@21435
   389
    val attrib = prep_att thy;
wenzelm@47067
   390
ballarin@28710
   391
    val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
wenzelm@47067
   392
    val ((more_atts, prems, stmt, facts), goal_ctxt) = lthy
wenzelm@47067
   393
      |> Bundle.includes (map (prep_bundle lthy) raw_includes)
wenzelm@47067
   394
      |> prep_statement attrib prep_stmt elems raw_concl;
wenzelm@45583
   395
    val atts = more_atts @ map attrib raw_atts;
wenzelm@21036
   396
wenzelm@21036
   397
    fun after_qed' results goal_ctxt' =
wenzelm@45584
   398
      let
wenzelm@54883
   399
        val results' =
wenzelm@54883
   400
          burrow (map (Goal.norm_result lthy) o Proof_Context.export goal_ctxt' lthy) results;
wenzelm@45584
   401
        val (res, lthy') =
wenzelm@49062
   402
          if forall (Attrib.is_empty_binding o fst) stmt then (map (pair "") results', lthy)
wenzelm@45584
   403
          else
wenzelm@45584
   404
            Local_Theory.notes_kind kind
wenzelm@45584
   405
              (map2 (fn (b, _) => fn ths => (b, [(ths, [])])) stmt results') lthy;
wenzelm@45584
   406
        val lthy'' =
wenzelm@45584
   407
          if Attrib.is_empty_binding (name, atts) then
wenzelm@46728
   408
            (Proof_Display.print_results Markup.empty int lthy' ((kind, ""), res); lthy')
wenzelm@28080
   409
          else
wenzelm@28080
   410
            let
wenzelm@45584
   411
              val ([(res_name, _)], lthy'') =
wenzelm@45584
   412
                Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])] lthy';
wenzelm@46728
   413
              val _ = Proof_Display.print_results Markup.empty int lthy' ((kind, res_name), res);
wenzelm@45584
   414
            in lthy'' end;
wenzelm@45584
   415
      in after_qed results' lthy'' end;
wenzelm@21036
   416
  in
wenzelm@21036
   417
    goal_ctxt
wenzelm@42360
   418
    |> Proof_Context.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
wenzelm@21450
   419
    |> snd
wenzelm@36323
   420
    |> Proof.theorem before_qed after_qed' (map snd stmt)
wenzelm@32856
   421
    |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
wenzelm@36317
   422
    |> tap (fn state => not schematic andalso Proof.schematic_goal state andalso
wenzelm@36317
   423
        error "Illegal schematic goal statement")
wenzelm@46776
   424
    |> fold_rev (fn (f, _) => f int) (Theorem_Hook.get (Context.Proof goal_ctxt))
wenzelm@21036
   425
  end;
wenzelm@21036
   426
wenzelm@21230
   427
in
wenzelm@21230
   428
wenzelm@47067
   429
val theorem = gen_theorem false (K I) (K I) Expression.cert_statement;
wenzelm@47067
   430
val theorem_cmd =
wenzelm@47067
   431
  gen_theorem false Bundle.check Attrib.intern_src Expression.read_statement;
wenzelm@36317
   432
wenzelm@47067
   433
val schematic_theorem = gen_theorem true (K I) (K I) Expression.cert_statement;
wenzelm@47067
   434
val schematic_theorem_cmd =
wenzelm@47067
   435
  gen_theorem true Bundle.check Attrib.intern_src Expression.read_statement;
wenzelm@21036
   436
wenzelm@37216
   437
fun add_theorem_hook f = Theorem_Hook.map (cons (f, stamp ()));
berghofe@24452
   438
wenzelm@18620
   439
end;
wenzelm@21036
   440
wenzelm@21036
   441
end;