src/Pure/Isar/specification.ML
author wenzelm
Sun Mar 07 12:19:47 2010 +0100 (2010-03-07)
changeset 35625 9c818cab0dd0
parent 35624 c4e29a0bb8c1
child 35894 ab6dc4d86ea1
permissions -rw-r--r--
modernized structure Object_Logic;
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@20890
    10
  val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
wenzelm@30482
    11
  val check_spec:
wenzelm@30482
    12
    (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
wenzelm@30482
    13
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    14
  val read_spec:
wenzelm@30482
    15
    (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
wenzelm@30482
    16
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    17
  val check_free_spec:
wenzelm@30482
    18
    (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
wenzelm@30482
    19
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
wenzelm@30482
    20
  val read_free_spec:
wenzelm@30482
    21
    (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
wenzelm@30482
    22
    (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
haftmann@29581
    23
  val check_specification: (binding * typ option * mixfix) list ->
wenzelm@30482
    24
    (Attrib.binding * term list) list -> Proof.context ->
haftmann@29581
    25
    (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
haftmann@29581
    26
  val read_specification: (binding * string option * mixfix) list ->
wenzelm@28084
    27
    (Attrib.binding * string list) list -> Proof.context ->
haftmann@29581
    28
    (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
haftmann@29581
    29
  val axiomatization: (binding * typ option * mixfix) list ->
wenzelm@28114
    30
    (Attrib.binding * term list) list -> theory ->
wenzelm@33703
    31
    (term list * thm list list) * theory
haftmann@29581
    32
  val axiomatization_cmd: (binding * string option * mixfix) list ->
wenzelm@28114
    33
    (Attrib.binding * string list) list -> theory ->
wenzelm@33703
    34
    (term list * thm list list) * 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@24927
    38
  val definition_cmd:
haftmann@29581
    39
    (binding * string option * mixfix) option * (Attrib.binding * string) ->
wenzelm@28080
    40
    local_theory -> (term * (string * thm)) * local_theory
haftmann@29581
    41
  val abbreviation: Syntax.mode -> (binding * typ option * mixfix) option * term ->
wenzelm@21532
    42
    local_theory -> local_theory
haftmann@29581
    43
  val abbreviation_cmd: Syntax.mode -> (binding * string option * mixfix) option * string ->
wenzelm@24927
    44
    local_theory -> local_theory
wenzelm@35413
    45
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
wenzelm@35413
    46
  val type_notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    47
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    48
  val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
wenzelm@28080
    49
  val theorems: string ->
wenzelm@28084
    50
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28080
    51
    local_theory -> (string * thm list) list * local_theory
wenzelm@26336
    52
  val theorems_cmd: string ->
wenzelm@28084
    53
    (Attrib.binding * (Facts.ref * Attrib.src list) list) list ->
wenzelm@28080
    54
    local_theory -> (string * thm list) list * local_theory
wenzelm@24986
    55
  val theorem: string -> Method.text option ->
wenzelm@28084
    56
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
ballarin@28710
    57
    Element.context_i list -> Element.statement_i ->
berghofe@24452
    58
    bool -> local_theory -> Proof.state
wenzelm@24986
    59
  val theorem_cmd: string -> Method.text option ->
wenzelm@28084
    60
    (thm list list -> local_theory -> local_theory) -> Attrib.binding ->
ballarin@28710
    61
    Element.context list -> Element.statement ->
berghofe@24452
    62
    bool -> local_theory -> Proof.state
berghofe@24452
    63
  val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
wenzelm@18620
    64
end;
wenzelm@18620
    65
wenzelm@18620
    66
structure Specification: SPECIFICATION =
wenzelm@18620
    67
struct
wenzelm@18620
    68
wenzelm@20890
    69
(* diagnostics *)
wenzelm@20890
    70
wenzelm@20890
    71
fun print_consts _ _ [] = ()
wenzelm@33389
    72
  | print_consts ctxt pred cs = Pretty.writeln (Proof_Display.pretty_consts ctxt pred cs);
wenzelm@20914
    73
wenzelm@19664
    74
wenzelm@18620
    75
(* prepare specification *)
wenzelm@18620
    76
wenzelm@24724
    77
local
wenzelm@24724
    78
wenzelm@24724
    79
fun close_forms ctxt i xs As =
wenzelm@24724
    80
  let
wenzelm@24724
    81
    fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x
wenzelm@24724
    82
      | add_free _ = I;
wenzelm@24724
    83
wenzelm@24724
    84
    val commons = map #1 xs;
wenzelm@24724
    85
    val _ =
wenzelm@24724
    86
      (case duplicates (op =) commons of [] => ()
wenzelm@24724
    87
      | dups => error ("Duplicate local variables " ^ commas_quote dups));
wenzelm@24724
    88
    val frees = rev ((fold o fold_aterms) add_free As (rev commons));
wenzelm@24848
    89
    val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context Name.aT (length frees));
wenzelm@24724
    90
    val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
wenzelm@24724
    91
wenzelm@24724
    92
    fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
wenzelm@24724
    93
      | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
wenzelm@24724
    94
      | abs_body lev y (t as Free (x, T)) =
wenzelm@24724
    95
          if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev))
wenzelm@24724
    96
          else t
wenzelm@24724
    97
      | abs_body _ _ a = a;
wenzelm@24724
    98
    fun close (y, U) B =
wenzelm@24724
    99
      let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
wenzelm@24724
   100
      in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end;
wenzelm@24724
   101
    fun close_form A =
wenzelm@24724
   102
      let
wenzelm@24724
   103
        val occ_frees = rev (fold_aterms add_free A []);
wenzelm@24724
   104
        val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
wenzelm@24724
   105
      in fold_rev close bounds A end;
wenzelm@24724
   106
  in map close_form As end;
wenzelm@24724
   107
wenzelm@30482
   108
fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
wenzelm@18620
   109
  let
wenzelm@18670
   110
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
   111
wenzelm@18670
   112
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@30763
   113
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes vars;
wenzelm@18620
   114
wenzelm@24724
   115
    val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss;
wenzelm@24734
   116
    val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
wenzelm@24734
   117
      |> fold Name.declare xs;
wenzelm@24734
   118
    val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
wenzelm@24734
   119
    val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
wenzelm@24724
   120
    val specs =
wenzelm@24724
   121
      (if do_close then
wenzelm@24734
   122
        #1 (fold_map
wenzelm@24734
   123
            (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
wenzelm@24734
   124
      else Asss')
wenzelm@24724
   125
      |> flat |> burrow (Syntax.check_props params_ctxt);
wenzelm@24724
   126
    val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
wenzelm@24724
   127
wenzelm@28080
   128
    val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
wenzelm@28080
   129
    val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
wenzelm@24724
   130
    val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
wenzelm@24724
   131
  in ((params, name_atts ~~ specs), specs_ctxt) end;
wenzelm@24724
   132
wenzelm@30482
   133
wenzelm@30482
   134
fun single_spec (a, prop) = [(a, [prop])];
wenzelm@30482
   135
fun the_spec (a, [prop]) = (a, prop);
wenzelm@30482
   136
wenzelm@30482
   137
fun prep_spec prep_vars parse_prop prep_att do_close vars specs =
wenzelm@30482
   138
  prepare prep_vars parse_prop prep_att do_close
wenzelm@30482
   139
    vars (map single_spec specs) #>> apsnd (map the_spec);
wenzelm@30482
   140
wenzelm@24724
   141
in
wenzelm@21370
   142
wenzelm@30482
   143
fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) true x;
wenzelm@30482
   144
fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true x;
wenzelm@30482
   145
wenzelm@30482
   146
fun check_free_spec x = prep_spec ProofContext.cert_vars (K I) (K I) false x;
wenzelm@30482
   147
fun read_free_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src false x;
wenzelm@30482
   148
wenzelm@30482
   149
fun check_specification vars specs =
wenzelm@30482
   150
  prepare ProofContext.cert_vars (K I) (K I) true vars [specs];
wenzelm@30482
   151
wenzelm@30482
   152
fun read_specification vars specs =
wenzelm@30482
   153
  prepare ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
wenzelm@24724
   154
wenzelm@24724
   155
end;
wenzelm@18620
   156
wenzelm@18620
   157
wenzelm@28114
   158
(* axiomatization -- within global theory *)
wenzelm@18620
   159
wenzelm@28114
   160
fun gen_axioms do_print prep raw_vars raw_specs thy =
wenzelm@18620
   161
  let
wenzelm@30482
   162
    val ((vars, specs), _) = prep raw_vars raw_specs (ProofContext.init thy);
wenzelm@30585
   163
    val xs = map (fn ((b, T), _) => (Name.of_binding b, T)) vars;
wenzelm@28114
   164
wenzelm@28114
   165
    (*consts*)
wenzelm@33173
   166
    val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
wenzelm@28114
   167
    val subst = Term.subst_atomic (map Free xs ~~ consts);
wenzelm@28114
   168
wenzelm@28114
   169
    (*axioms*)
wenzelm@30470
   170
    val (axioms, axioms_thy) = (specs, consts_thy) |-> fold_map (fn ((b, atts), props) =>
wenzelm@30470
   171
        fold_map Thm.add_axiom
wenzelm@30470
   172
          (map (apfst (fn a => Binding.map_name (K a) b))
wenzelm@30585
   173
            (PureThy.name_multi (Name.of_binding b) (map subst props)))
wenzelm@33703
   174
        #>> (fn ths => ((b, atts), [(ths, [])])));
wenzelm@30470
   175
wenzelm@30470
   176
    (*facts*)
wenzelm@33703
   177
    val (facts, facts_lthy) = axioms_thy
wenzelm@33703
   178
      |> Theory_Target.init NONE
wenzelm@33703
   179
      |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
wenzelm@33703
   180
      |> Local_Theory.notes axioms;
wenzelm@30470
   181
wenzelm@26595
   182
    val _ =
wenzelm@26595
   183
      if not do_print then ()
wenzelm@33703
   184
      else print_consts facts_lthy (K false) xs;
wenzelm@33703
   185
  in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
wenzelm@18786
   186
wenzelm@26595
   187
val axiomatization = gen_axioms false check_specification;
wenzelm@26595
   188
val axiomatization_cmd = gen_axioms true read_specification;
wenzelm@18620
   189
wenzelm@18786
   190
wenzelm@18786
   191
(* definition *)
wenzelm@18786
   192
wenzelm@30482
   193
fun gen_def do_print prep (raw_var, raw_spec) lthy =
wenzelm@18786
   194
  let
wenzelm@30482
   195
    val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
wenzelm@35624
   196
    val (((x, T), rhs), prove) = Local_Defs.derived_def lthy true prop;
wenzelm@30434
   197
    val var as (b, _) =
wenzelm@28080
   198
      (case vars of
haftmann@28965
   199
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   200
      | [((b, _), mx)] =>
wenzelm@28080
   201
          let
wenzelm@30585
   202
            val y = Name.of_binding b;
wenzelm@28080
   203
            val _ = x = y orelse
wenzelm@28080
   204
              error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^
haftmann@28941
   205
                Position.str_of (Binding.pos_of b));
wenzelm@28080
   206
          in (b, mx) end);
wenzelm@30434
   207
    val name = Thm.def_binding_optional b raw_name;
wenzelm@33455
   208
    val ((lhs, (_, raw_th)), lthy2) = lthy
wenzelm@33765
   209
      |> Local_Theory.define (var, ((Binding.suffix_name "_raw" name, []), rhs));
wenzelm@33455
   210
wenzelm@33455
   211
    val th = prove lthy2 raw_th;
wenzelm@33455
   212
    val lthy3 = lthy2 |> Spec_Rules.add Spec_Rules.Equational ([lhs], [th]);
wenzelm@33455
   213
wenzelm@33670
   214
    val ([(def_name, [th'])], lthy4) = lthy3
wenzelm@33671
   215
      |> Local_Theory.notes_kind Thm.definitionK
bulwahn@33756
   216
        [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
wenzelm@18786
   217
wenzelm@33671
   218
    val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
wenzelm@26595
   219
    val _ =
wenzelm@26595
   220
      if not do_print then ()
wenzelm@33455
   221
      else print_consts lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
wenzelm@33455
   222
  in ((lhs, (def_name, th')), lthy4) end;
wenzelm@18786
   223
wenzelm@30482
   224
val definition = gen_def false check_free_spec;
wenzelm@30482
   225
val definition_cmd = gen_def true read_free_spec;
wenzelm@18786
   226
wenzelm@19080
   227
wenzelm@19080
   228
(* abbreviation *)
wenzelm@19080
   229
wenzelm@26595
   230
fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
wenzelm@19080
   231
  let
wenzelm@30482
   232
    val ((vars, [(_, prop)]), _) =
wenzelm@30482
   233
      prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
wenzelm@24676
   234
        (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
wenzelm@35624
   235
    val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
wenzelm@28080
   236
    val var =
wenzelm@28080
   237
      (case vars of
haftmann@28965
   238
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   239
      | [((b, _), mx)] =>
wenzelm@28080
   240
          let
wenzelm@30585
   241
            val y = Name.of_binding b;
wenzelm@28080
   242
            val _ = x = y orelse
wenzelm@28080
   243
              error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
haftmann@28941
   244
                Position.str_of (Binding.pos_of b));
wenzelm@28080
   245
          in (b, mx) end);
wenzelm@21705
   246
    val lthy' = lthy
wenzelm@24986
   247
      |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
wenzelm@33671
   248
      |> Local_Theory.abbrev mode (var, rhs) |> snd
wenzelm@21705
   249
      |> ProofContext.restore_syntax_mode lthy;
wenzelm@24724
   250
wenzelm@26595
   251
    val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
wenzelm@21532
   252
  in lthy' end;
wenzelm@19080
   253
wenzelm@30482
   254
val abbreviation = gen_abbrev false check_free_spec;
wenzelm@30482
   255
val abbreviation_cmd = gen_abbrev true read_free_spec;
wenzelm@19080
   256
wenzelm@19664
   257
wenzelm@21230
   258
(* notation *)
wenzelm@19664
   259
wenzelm@35413
   260
local
wenzelm@35413
   261
wenzelm@35413
   262
fun gen_type_notation prep_type add mode args lthy =
wenzelm@35413
   263
  lthy |> Local_Theory.type_notation add mode (map (apfst (prep_type lthy)) args);
wenzelm@35413
   264
wenzelm@24949
   265
fun gen_notation prep_const add mode args lthy =
wenzelm@33671
   266
  lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   267
wenzelm@35413
   268
in
wenzelm@35413
   269
wenzelm@35413
   270
val type_notation = gen_type_notation (K I);
wenzelm@35413
   271
val type_notation_cmd = gen_type_notation (fn ctxt => ProofContext.read_type_name ctxt false);
wenzelm@35413
   272
wenzelm@24927
   273
val notation = gen_notation (K I);
wenzelm@35399
   274
val notation_cmd = gen_notation (fn ctxt => ProofContext.read_const ctxt false);
wenzelm@19664
   275
wenzelm@35413
   276
end;
wenzelm@35413
   277
wenzelm@20914
   278
wenzelm@21036
   279
(* fact statements *)
wenzelm@20914
   280
wenzelm@26345
   281
fun gen_theorems prep_fact prep_att kind raw_facts lthy =
wenzelm@20914
   282
  let
wenzelm@20914
   283
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   284
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   285
      ((name, map attrib atts),
wenzelm@26345
   286
        bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
wenzelm@33671
   287
    val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
wenzelm@33389
   288
    val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
wenzelm@20914
   289
  in (res, lthy') end;
wenzelm@20914
   290
wenzelm@24927
   291
val theorems = gen_theorems (K I) (K I);
wenzelm@26345
   292
val theorems_cmd = gen_theorems ProofContext.get_fact Attrib.intern_src;
wenzelm@20914
   293
wenzelm@21036
   294
wenzelm@21230
   295
(* complex goal statements *)
wenzelm@21036
   296
wenzelm@21036
   297
local
wenzelm@21036
   298
wenzelm@21236
   299
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   300
  (case concl of
wenzelm@21236
   301
    Element.Shows shows =>
wenzelm@21230
   302
      let
ballarin@28880
   303
        val (propp, elems_ctxt) = prep_stmt elems (map snd shows) ctxt;
wenzelm@30472
   304
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
wenzelm@21450
   305
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   306
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@32856
   307
      in ((prems, stmt, NONE), goal_ctxt) end
wenzelm@21236
   308
  | Element.Obtains obtains =>
wenzelm@21230
   309
      let
haftmann@28862
   310
        val case_names = obtains |> map_index (fn (i, (b, _)) =>
wenzelm@30585
   311
          if Binding.is_empty b then string_of_int (i + 1) else Name.of_binding b);
wenzelm@21236
   312
        val constraints = obtains |> map (fn (_, (vars, _)) =>
ballarin@28710
   313
          Element.Constrains
wenzelm@30585
   314
            (vars |> map_filter (fn (x, SOME T) => SOME (Name.of_binding x, T) | _ => NONE)));
wenzelm@21036
   315
wenzelm@21236
   316
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
ballarin@28880
   317
        val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   318
wenzelm@35625
   319
        val thesis = Object_Logic.fixed_judgment (ProofContext.theory_of ctxt) Auto_Bind.thesisN;
wenzelm@21036
   320
wenzelm@21236
   321
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   322
          let
wenzelm@28080
   323
            val bs = map fst vars;
wenzelm@30585
   324
            val xs = map Name.of_binding bs;
wenzelm@21236
   325
            val props = map fst asms;
wenzelm@28080
   326
            val (Ts, _) = ctxt'
wenzelm@21236
   327
              |> fold Variable.declare_term props
wenzelm@21236
   328
              |> fold_map ProofContext.inferred_param xs;
wenzelm@28080
   329
            val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
wenzelm@21236
   330
          in
wenzelm@30763
   331
            ctxt' |> (snd o ProofContext.add_fixes (map (fn b => (b, NONE, NoSyn)) bs));
wenzelm@21370
   332
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   333
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@33369
   334
              [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   335
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   336
          end;
wenzelm@21236
   337
wenzelm@21658
   338
        val atts = map (Attrib.internal o K)
wenzelm@33368
   339
          [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
wenzelm@30472
   340
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
haftmann@28965
   341
        val stmt = [((Binding.empty, atts), [(thesis, [])])];
wenzelm@21236
   342
wenzelm@21236
   343
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@33386
   344
          |> (snd o ProofContext.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
wenzelm@21236
   345
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@33644
   346
          |-> (fn ths =>
wenzelm@33644
   347
            ProofContext.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
wenzelm@33644
   348
            #2 #> pair ths);
wenzelm@32856
   349
      in ((prems, stmt, SOME facts), goal_ctxt) end);
wenzelm@21036
   350
wenzelm@33519
   351
structure TheoremHook = Generic_Data
berghofe@24452
   352
(
berghofe@24452
   353
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
berghofe@24452
   354
  val empty = [];
berghofe@24452
   355
  val extend = I;
wenzelm@33519
   356
  fun merge hooks : T = Library.merge (eq_snd op =) hooks;
berghofe@24452
   357
);
berghofe@24452
   358
wenzelm@21036
   359
fun gen_theorem prep_att prep_stmt
wenzelm@28858
   360
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy =
wenzelm@21036
   361
  let
wenzelm@33671
   362
    val _ = Local_Theory.affirm lthy;
wenzelm@28858
   363
    val thy = ProofContext.theory_of lthy;
wenzelm@21036
   364
wenzelm@21435
   365
    val attrib = prep_att thy;
wenzelm@21435
   366
    val atts = map attrib raw_atts;
ballarin@28710
   367
    val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
wenzelm@21450
   368
    val ((prems, stmt, facts), goal_ctxt) =
ballarin@28880
   369
      prep_statement attrib prep_stmt elems raw_concl lthy;
wenzelm@21036
   370
wenzelm@21036
   371
    fun after_qed' results goal_ctxt' =
wenzelm@21602
   372
      let val results' =
wenzelm@21602
   373
        burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
wenzelm@21602
   374
      in
wenzelm@21036
   375
        lthy
wenzelm@33671
   376
        |> Local_Theory.notes_kind kind
wenzelm@33670
   377
          (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
wenzelm@21036
   378
        |> (fn (res, lthy') =>
haftmann@28965
   379
          if Binding.is_empty name andalso null atts then
wenzelm@33389
   380
            (Proof_Display.print_results true lthy' ((kind, ""), res); lthy')
wenzelm@28080
   381
          else
wenzelm@28080
   382
            let
wenzelm@28080
   383
              val ([(res_name, _)], lthy'') = lthy'
wenzelm@33671
   384
                |> Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])];
wenzelm@33389
   385
              val _ = Proof_Display.print_results true lthy' ((kind, res_name), res);
wenzelm@28080
   386
            in lthy'' end)
wenzelm@21036
   387
        |> after_qed results'
wenzelm@21036
   388
      end;
wenzelm@21036
   389
  in
wenzelm@21036
   390
    goal_ctxt
wenzelm@33644
   391
    |> ProofContext.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
wenzelm@21450
   392
    |> snd
wenzelm@21435
   393
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
wenzelm@32856
   394
    |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
wenzelm@24542
   395
    |> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt))))
wenzelm@21036
   396
  end;
wenzelm@21036
   397
wenzelm@21230
   398
in
wenzelm@21230
   399
ballarin@29249
   400
val theorem = gen_theorem (K I) Expression.cert_statement;
ballarin@29249
   401
val theorem_cmd = gen_theorem Attrib.intern_src Expression.read_statement;
wenzelm@21036
   402
berghofe@24452
   403
fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
berghofe@24452
   404
wenzelm@18620
   405
end;
wenzelm@21036
   406
wenzelm@21036
   407
end;