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