src/Pure/Isar/specification.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 42265 ffdaa07cf6cf
child 42357 3305f573294e
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
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@39288
   105
          if x = y then Type.constraint (uniform_typing x) (Type.constraint 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@42083
   110
      in if Term.is_dependent B' 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@42265
   125
    val Asss =
wenzelm@42265
   126
      (map o map) snd raw_specss
wenzelm@42265
   127
      |> (burrow o burrow) (Par_List.map_name "Specification.parse_prop" (parse_prop params_ctxt));
wenzelm@24734
   128
    val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
wenzelm@24734
   129
      |> fold Name.declare xs;
wenzelm@24734
   130
    val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
wenzelm@24734
   131
    val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
wenzelm@24724
   132
    val specs =
wenzelm@24724
   133
      (if do_close then
wenzelm@24734
   134
        #1 (fold_map
wenzelm@24734
   135
            (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
wenzelm@24734
   136
      else Asss')
wenzelm@24724
   137
      |> flat |> burrow (Syntax.check_props params_ctxt);
wenzelm@24724
   138
    val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
wenzelm@24724
   139
wenzelm@28080
   140
    val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
wenzelm@28080
   141
    val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
wenzelm@24724
   142
    val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
wenzelm@24724
   143
  in ((params, name_atts ~~ specs), specs_ctxt) end;
wenzelm@24724
   144
wenzelm@30482
   145
wenzelm@30482
   146
fun single_spec (a, prop) = [(a, [prop])];
wenzelm@30482
   147
fun the_spec (a, [prop]) = (a, prop);
wenzelm@30482
   148
wenzelm@30482
   149
fun prep_spec prep_vars parse_prop prep_att do_close vars specs =
wenzelm@30482
   150
  prepare prep_vars parse_prop prep_att do_close
wenzelm@30482
   151
    vars (map single_spec specs) #>> apsnd (map the_spec);
wenzelm@30482
   152
wenzelm@24724
   153
in
wenzelm@21370
   154
wenzelm@30482
   155
fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) true x;
wenzelm@30482
   156
fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true x;
wenzelm@30482
   157
wenzelm@30482
   158
fun check_free_spec x = prep_spec ProofContext.cert_vars (K I) (K I) false x;
wenzelm@30482
   159
fun read_free_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src false x;
wenzelm@30482
   160
wenzelm@30482
   161
fun check_specification vars specs =
wenzelm@30482
   162
  prepare ProofContext.cert_vars (K I) (K I) true vars [specs];
wenzelm@30482
   163
wenzelm@30482
   164
fun read_specification vars specs =
wenzelm@30482
   165
  prepare ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
wenzelm@24724
   166
wenzelm@24724
   167
end;
wenzelm@18620
   168
wenzelm@18620
   169
wenzelm@28114
   170
(* axiomatization -- within global theory *)
wenzelm@18620
   171
wenzelm@28114
   172
fun gen_axioms do_print prep raw_vars raw_specs thy =
wenzelm@18620
   173
  let
wenzelm@36610
   174
    val ((vars, specs), _) = prep raw_vars raw_specs (ProofContext.init_global thy);
wenzelm@30585
   175
    val xs = map (fn ((b, T), _) => (Name.of_binding b, T)) vars;
wenzelm@28114
   176
wenzelm@28114
   177
    (*consts*)
wenzelm@33173
   178
    val (consts, consts_thy) = thy |> fold_map Theory.specify_const vars;
wenzelm@28114
   179
    val subst = Term.subst_atomic (map Free xs ~~ consts);
wenzelm@28114
   180
wenzelm@28114
   181
    (*axioms*)
wenzelm@30470
   182
    val (axioms, axioms_thy) = (specs, consts_thy) |-> fold_map (fn ((b, atts), props) =>
wenzelm@30470
   183
        fold_map Thm.add_axiom
wenzelm@30470
   184
          (map (apfst (fn a => Binding.map_name (K a) b))
wenzelm@39557
   185
            (Global_Theory.name_multi (Name.of_binding b) (map subst props)))
wenzelm@36106
   186
        #>> (fn ths => ((b, atts), [(map #2 ths, [])])));
wenzelm@30470
   187
wenzelm@30470
   188
    (*facts*)
wenzelm@33703
   189
    val (facts, facts_lthy) = axioms_thy
haftmann@38388
   190
      |> Named_Target.theory_init
wenzelm@33703
   191
      |> Spec_Rules.add Spec_Rules.Unknown (consts, maps (maps #1 o #2) axioms)
wenzelm@33703
   192
      |> Local_Theory.notes axioms;
wenzelm@30470
   193
wenzelm@26595
   194
    val _ =
wenzelm@26595
   195
      if not do_print then ()
wenzelm@33703
   196
      else print_consts facts_lthy (K false) xs;
wenzelm@33703
   197
  in ((consts, map #2 facts), Local_Theory.exit_global facts_lthy) end;
wenzelm@18786
   198
wenzelm@26595
   199
val axiomatization = gen_axioms false check_specification;
wenzelm@26595
   200
val axiomatization_cmd = gen_axioms true read_specification;
wenzelm@18620
   201
wenzelm@35894
   202
fun axiom (b, ax) = axiomatization [] [(b, [ax])] #>> (hd o hd o snd);
wenzelm@35894
   203
fun axiom_cmd (b, ax) = axiomatization_cmd [] [(b, [ax])] #>> (hd o hd o snd);
wenzelm@35894
   204
wenzelm@18786
   205
wenzelm@18786
   206
(* definition *)
wenzelm@18786
   207
wenzelm@30482
   208
fun gen_def do_print prep (raw_var, raw_spec) lthy =
wenzelm@18786
   209
  let
wenzelm@30482
   210
    val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
wenzelm@35624
   211
    val (((x, T), rhs), prove) = Local_Defs.derived_def lthy true prop;
wenzelm@30434
   212
    val var as (b, _) =
wenzelm@28080
   213
      (case vars of
haftmann@28965
   214
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   215
      | [((b, _), mx)] =>
wenzelm@28080
   216
          let
wenzelm@30585
   217
            val y = Name.of_binding b;
wenzelm@28080
   218
            val _ = x = y orelse
wenzelm@28080
   219
              error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^
haftmann@28941
   220
                Position.str_of (Binding.pos_of b));
wenzelm@28080
   221
          in (b, mx) end);
wenzelm@30434
   222
    val name = Thm.def_binding_optional b raw_name;
wenzelm@33455
   223
    val ((lhs, (_, raw_th)), lthy2) = lthy
wenzelm@33765
   224
      |> Local_Theory.define (var, ((Binding.suffix_name "_raw" name, []), rhs));
wenzelm@33455
   225
wenzelm@33455
   226
    val th = prove lthy2 raw_th;
wenzelm@33455
   227
    val lthy3 = lthy2 |> Spec_Rules.add Spec_Rules.Equational ([lhs], [th]);
wenzelm@33455
   228
wenzelm@33670
   229
    val ([(def_name, [th'])], lthy4) = lthy3
wenzelm@33671
   230
      |> Local_Theory.notes_kind Thm.definitionK
bulwahn@33756
   231
        [((name, Code.add_default_eqn_attrib :: atts), [([th], [])])];
wenzelm@18786
   232
wenzelm@33671
   233
    val lhs' = Morphism.term (Local_Theory.target_morphism lthy4) lhs;
wenzelm@26595
   234
    val _ =
wenzelm@26595
   235
      if not do_print then ()
wenzelm@33455
   236
      else print_consts lthy4 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
wenzelm@33455
   237
  in ((lhs, (def_name, th')), lthy4) end;
wenzelm@18786
   238
wenzelm@30482
   239
val definition = gen_def false check_free_spec;
wenzelm@30482
   240
val definition_cmd = gen_def true read_free_spec;
wenzelm@18786
   241
wenzelm@19080
   242
wenzelm@19080
   243
(* abbreviation *)
wenzelm@19080
   244
wenzelm@26595
   245
fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
wenzelm@19080
   246
  let
wenzelm@30482
   247
    val ((vars, [(_, prop)]), _) =
wenzelm@30482
   248
      prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
wenzelm@24676
   249
        (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
wenzelm@35624
   250
    val ((x, T), rhs) = Local_Defs.abs_def (#2 (Local_Defs.cert_def lthy prop));
wenzelm@28080
   251
    val var =
wenzelm@28080
   252
      (case vars of
haftmann@28965
   253
        [] => (Binding.name x, NoSyn)
wenzelm@28080
   254
      | [((b, _), mx)] =>
wenzelm@28080
   255
          let
wenzelm@30585
   256
            val y = Name.of_binding b;
wenzelm@28080
   257
            val _ = x = y orelse
wenzelm@28080
   258
              error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
haftmann@28941
   259
                Position.str_of (Binding.pos_of b));
wenzelm@28080
   260
          in (b, mx) end);
wenzelm@21705
   261
    val lthy' = lthy
wenzelm@24986
   262
      |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
wenzelm@33671
   263
      |> Local_Theory.abbrev mode (var, rhs) |> snd
wenzelm@21705
   264
      |> ProofContext.restore_syntax_mode lthy;
wenzelm@24724
   265
wenzelm@26595
   266
    val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
wenzelm@21532
   267
  in lthy' end;
wenzelm@19080
   268
wenzelm@30482
   269
val abbreviation = gen_abbrev false check_free_spec;
wenzelm@30482
   270
val abbreviation_cmd = gen_abbrev true read_free_spec;
wenzelm@19080
   271
wenzelm@19664
   272
wenzelm@21230
   273
(* notation *)
wenzelm@19664
   274
wenzelm@35413
   275
local
wenzelm@35413
   276
wenzelm@35413
   277
fun gen_type_notation prep_type add mode args lthy =
wenzelm@35413
   278
  lthy |> Local_Theory.type_notation add mode (map (apfst (prep_type lthy)) args);
wenzelm@35413
   279
wenzelm@24949
   280
fun gen_notation prep_const add mode args lthy =
wenzelm@33671
   281
  lthy |> Local_Theory.notation add mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   282
wenzelm@35413
   283
in
wenzelm@35413
   284
wenzelm@35413
   285
val type_notation = gen_type_notation (K I);
wenzelm@35413
   286
val type_notation_cmd = gen_type_notation (fn ctxt => ProofContext.read_type_name ctxt false);
wenzelm@35413
   287
wenzelm@24927
   288
val notation = gen_notation (K I);
wenzelm@36505
   289
val notation_cmd = gen_notation (fn ctxt => ProofContext.read_const ctxt false dummyT);
wenzelm@19664
   290
wenzelm@35413
   291
end;
wenzelm@35413
   292
wenzelm@20914
   293
wenzelm@21036
   294
(* fact statements *)
wenzelm@20914
   295
wenzelm@26345
   296
fun gen_theorems prep_fact prep_att kind raw_facts lthy =
wenzelm@20914
   297
  let
wenzelm@20914
   298
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   299
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   300
      ((name, map attrib atts),
wenzelm@26345
   301
        bs |> map (fn (b, more_atts) => (prep_fact lthy b, map attrib more_atts))));
wenzelm@33671
   302
    val (res, lthy') = lthy |> Local_Theory.notes_kind kind facts;
wenzelm@33389
   303
    val _ = Proof_Display.print_results true lthy' ((kind, ""), res);
wenzelm@20914
   304
  in (res, lthy') end;
wenzelm@20914
   305
wenzelm@24927
   306
val theorems = gen_theorems (K I) (K I);
wenzelm@26345
   307
val theorems_cmd = gen_theorems ProofContext.get_fact Attrib.intern_src;
wenzelm@20914
   308
wenzelm@21036
   309
wenzelm@21230
   310
(* complex goal statements *)
wenzelm@21036
   311
wenzelm@21036
   312
local
wenzelm@21036
   313
wenzelm@21236
   314
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   315
  (case concl of
wenzelm@21236
   316
    Element.Shows shows =>
wenzelm@21230
   317
      let
ballarin@28880
   318
        val (propp, elems_ctxt) = prep_stmt elems (map snd shows) ctxt;
wenzelm@30472
   319
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
wenzelm@21450
   320
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   321
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@32856
   322
      in ((prems, stmt, NONE), goal_ctxt) end
wenzelm@21236
   323
  | Element.Obtains obtains =>
wenzelm@21230
   324
      let
haftmann@28862
   325
        val case_names = obtains |> map_index (fn (i, (b, _)) =>
wenzelm@30585
   326
          if Binding.is_empty b then string_of_int (i + 1) else Name.of_binding b);
wenzelm@21236
   327
        val constraints = obtains |> map (fn (_, (vars, _)) =>
ballarin@28710
   328
          Element.Constrains
wenzelm@30585
   329
            (vars |> map_filter (fn (x, SOME T) => SOME (Name.of_binding x, T) | _ => NONE)));
wenzelm@21036
   330
wenzelm@21236
   331
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
ballarin@28880
   332
        val (propp, elems_ctxt) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   333
wenzelm@35625
   334
        val thesis = Object_Logic.fixed_judgment (ProofContext.theory_of ctxt) Auto_Bind.thesisN;
wenzelm@21036
   335
wenzelm@21236
   336
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   337
          let
wenzelm@28080
   338
            val bs = map fst vars;
wenzelm@30585
   339
            val xs = map Name.of_binding bs;
wenzelm@21236
   340
            val props = map fst asms;
wenzelm@28080
   341
            val (Ts, _) = ctxt'
wenzelm@21236
   342
              |> fold Variable.declare_term props
wenzelm@21236
   343
              |> fold_map ProofContext.inferred_param xs;
wenzelm@28080
   344
            val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
wenzelm@21236
   345
          in
wenzelm@30763
   346
            ctxt' |> (snd o ProofContext.add_fixes (map (fn b => (b, NONE, NoSyn)) bs));
wenzelm@21370
   347
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   348
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@33369
   349
              [((name, [Context_Rules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   350
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   351
          end;
wenzelm@21236
   352
wenzelm@21658
   353
        val atts = map (Attrib.internal o K)
wenzelm@33368
   354
          [Rule_Cases.consumes (~ (length obtains)), Rule_Cases.case_names case_names];
wenzelm@30472
   355
        val prems = Assumption.local_prems_of elems_ctxt ctxt;
haftmann@28965
   356
        val stmt = [((Binding.empty, atts), [(thesis, [])])];
wenzelm@21236
   357
wenzelm@21236
   358
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@33386
   359
          |> (snd o ProofContext.add_fixes [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)])
wenzelm@21236
   360
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@33644
   361
          |-> (fn ths =>
wenzelm@33644
   362
            ProofContext.note_thmss "" [((Binding.name Obtain.thatN, []), [(ths, [])])] #>
wenzelm@33644
   363
            #2 #> pair ths);
wenzelm@32856
   364
      in ((prems, stmt, SOME facts), goal_ctxt) end);
wenzelm@21036
   365
wenzelm@37216
   366
structure Theorem_Hook = Generic_Data
berghofe@24452
   367
(
berghofe@24452
   368
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
berghofe@24452
   369
  val empty = [];
berghofe@24452
   370
  val extend = I;
wenzelm@41472
   371
  fun merge data : T = Library.merge (eq_snd op =) data;
berghofe@24452
   372
);
berghofe@24452
   373
wenzelm@36317
   374
fun gen_theorem schematic prep_att prep_stmt
wenzelm@28858
   375
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy =
wenzelm@21036
   376
  let
wenzelm@33671
   377
    val _ = Local_Theory.affirm lthy;
wenzelm@28858
   378
    val thy = ProofContext.theory_of lthy;
wenzelm@21036
   379
wenzelm@21435
   380
    val attrib = prep_att thy;
wenzelm@21435
   381
    val atts = map attrib raw_atts;
ballarin@28710
   382
    val elems = raw_elems |> map (Element.map_ctxt_attrib attrib);
wenzelm@21450
   383
    val ((prems, stmt, facts), goal_ctxt) =
ballarin@28880
   384
      prep_statement attrib prep_stmt elems raw_concl lthy;
wenzelm@21036
   385
wenzelm@21036
   386
    fun after_qed' results goal_ctxt' =
wenzelm@21602
   387
      let val results' =
wenzelm@21602
   388
        burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
wenzelm@21602
   389
      in
wenzelm@21036
   390
        lthy
wenzelm@33671
   391
        |> Local_Theory.notes_kind kind
wenzelm@33670
   392
          (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
wenzelm@21036
   393
        |> (fn (res, lthy') =>
haftmann@28965
   394
          if Binding.is_empty name andalso null atts then
wenzelm@33389
   395
            (Proof_Display.print_results true lthy' ((kind, ""), res); lthy')
wenzelm@28080
   396
          else
wenzelm@28080
   397
            let
wenzelm@28080
   398
              val ([(res_name, _)], lthy'') = lthy'
wenzelm@33671
   399
                |> Local_Theory.notes_kind kind [((name, atts), [(maps #2 res, [])])];
wenzelm@33389
   400
              val _ = Proof_Display.print_results true lthy' ((kind, res_name), res);
wenzelm@28080
   401
            in lthy'' end)
wenzelm@21036
   402
        |> after_qed results'
wenzelm@21036
   403
      end;
wenzelm@21036
   404
  in
wenzelm@21036
   405
    goal_ctxt
wenzelm@33644
   406
    |> ProofContext.note_thmss "" [((Binding.name Auto_Bind.assmsN, []), [(prems, [])])]
wenzelm@21450
   407
    |> snd
wenzelm@36323
   408
    |> Proof.theorem before_qed after_qed' (map snd stmt)
wenzelm@32856
   409
    |> (case facts of NONE => I | SOME ths => Proof.refine_insert ths)
wenzelm@36317
   410
    |> tap (fn state => not schematic andalso Proof.schematic_goal state andalso
wenzelm@36317
   411
        error "Illegal schematic goal statement")
wenzelm@37216
   412
    |> Library.apply (map (fn (f, _) => f int) (rev (Theorem_Hook.get (Context.Proof goal_ctxt))))
wenzelm@21036
   413
  end;
wenzelm@21036
   414
wenzelm@21230
   415
in
wenzelm@21230
   416
wenzelm@36317
   417
val theorem = gen_theorem false (K I) Expression.cert_statement;
wenzelm@36317
   418
val theorem_cmd = gen_theorem false Attrib.intern_src Expression.read_statement;
wenzelm@36317
   419
wenzelm@36317
   420
val schematic_theorem = gen_theorem true (K I) Expression.cert_statement;
wenzelm@36317
   421
val schematic_theorem_cmd = gen_theorem true Attrib.intern_src Expression.read_statement;
wenzelm@21036
   422
wenzelm@37216
   423
fun add_theorem_hook f = Theorem_Hook.map (cons (f, stamp ()));
berghofe@24452
   424
wenzelm@18620
   425
end;
wenzelm@21036
   426
wenzelm@21036
   427
end;