src/Pure/Isar/specification.ML
author paulson
Fri, 05 Oct 2007 09:59:03 +0200
changeset 24854 0ebcd575d3c6
parent 24848 5dbbd33c3236
child 24927 48e08f37ce92
permissions -rw-r--r--
filtering out some package theorems
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/specification.ML
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     4
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
     5
Derived local theory specifications --- with type-inference and
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
     6
toplevel polymorphism.
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     7
*)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     8
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     9
signature SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    10
sig
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    11
  val quiet_mode: bool ref
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    12
  val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    13
  val read_specification: (string * string option * mixfix) list ->
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    14
    ((string * Attrib.src list) * string list) list list -> Proof.context ->
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    15
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    16
    Proof.context
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    17
  val check_specification: (string * typ option * mixfix) list ->
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    18
    ((string * Attrib.src list) * term list) list list -> Proof.context ->
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    19
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    20
    Proof.context
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    21
  val read_free_specification: (string * string option * mixfix) list ->
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    22
    ((string * Attrib.src list) * string list) list -> Proof.context ->
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    23
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    24
    Proof.context
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    25
  val check_free_specification: (string * typ option * mixfix) list ->
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    26
    ((string * Attrib.src list) * term list) list -> Proof.context ->
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    27
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    28
    Proof.context
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    29
  val axiomatization: (string * string option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    30
    ((bstring * Attrib.src list) * string list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    31
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    32
  val axiomatization_i: (string * typ option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    33
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    34
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    35
  val definition:
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    36
    (string * string option * mixfix) option * ((string * Attrib.src list) * string) ->
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    37
    local_theory -> (term * (bstring * thm)) * local_theory
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    38
  val definition_i:
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    39
    (string * typ option * mixfix) option * ((string * Attrib.src list) * term) ->
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    40
    local_theory -> (term * (bstring * thm)) * local_theory
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    41
  val abbreviation: Syntax.mode -> (string * string option * mixfix) option * string ->
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
    42
    local_theory -> local_theory
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
    43
  val abbreviation_i: Syntax.mode -> (string * typ option * mixfix) option * term ->
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
    44
    local_theory -> local_theory
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
    45
  val notation: Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
    46
  val notation_i: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    47
  val theorems: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    48
    -> local_theory -> (bstring * thm list) list * local_theory
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    49
  val theorems_i: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    50
    -> local_theory -> (bstring * thm list) list * local_theory
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    51
  val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    52
    string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
    53
    bool -> local_theory -> Proof.state
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    54
  val theorem_i: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    55
    string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
    56
    bool -> local_theory -> Proof.state
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
    57
  val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    58
end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    59
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    60
structure Specification: SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    61
struct
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    62
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
    63
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    64
(* diagnostics *)
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    65
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    66
val quiet_mode = ref false;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    67
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    68
fun print_consts _ _ [] = ()
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    69
  | print_consts ctxt pred cs =
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    70
      if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    71
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    72
fun present_results ctxt res =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    73
  if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    74
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    75
fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    76
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
    77
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    78
(* prepare specification *)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    79
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    80
local
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    81
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    82
fun close_forms ctxt i xs As =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    83
  let
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    84
    fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    85
      | add_free _ = I;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    86
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    87
    val commons = map #1 xs;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    88
    val _ =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    89
      (case duplicates (op =) commons of [] => ()
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    90
      | dups => error ("Duplicate local variables " ^ commas_quote dups));
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    91
    val frees = rev ((fold o fold_aterms) add_free As (rev commons));
24848
5dbbd33c3236 replaced literal 'a by Name.aT;
wenzelm
parents: 24734
diff changeset
    92
    val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context Name.aT (length frees));
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    93
    val uniform_typing = the o AList.lookup (op =) (frees ~~ types);
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    94
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    95
    fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b)
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    96
      | abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    97
      | abs_body lev y (t as Free (x, T)) =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    98
          if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev))
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
    99
          else t
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   100
      | abs_body _ _ a = a;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   101
    fun close (y, U) B =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   102
      let val B' = abs_body 0 y (Term.incr_boundvars 1 B)
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   103
      in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   104
    fun close_form A =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   105
      let
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   106
        val occ_frees = rev (fold_aterms add_free A []);
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   107
        val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   108
      in fold_rev close bounds A end;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   109
  in map close_form As end;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   110
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   111
fun prep_spec prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   112
  let
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
   113
    val thy = ProofContext.theory_of ctxt;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   114
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
   115
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
   116
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   117
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   118
    val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss;
24734
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   119
    val names = Variable.names_of (params_ctxt |> (fold o fold o fold) Variable.declare_term Asss)
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   120
      |> fold Name.declare xs;
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   121
    val Asss' = #1 ((fold_map o fold_map o fold_map) Term.free_dummy_patterns Asss names);
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   122
    val idx = (fold o fold o fold) Term.maxidx_term Asss' ~1 + 1;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   123
    val specs =
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   124
      (if do_close then
24734
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   125
        #1 (fold_map
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   126
            (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss' idx)
ff617b6711f4 read/check_specification: free_dummy_patterns;
wenzelm
parents: 24724
diff changeset
   127
      else Asss')
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   128
      |> flat |> burrow (Syntax.check_props params_ctxt);
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   129
    val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   130
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   131
    val vs = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
   132
    val params = vs ~~ map #3 vars;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   133
    val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   134
  in ((params, name_atts ~~ specs), specs_ctxt) end;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   135
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   136
fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src x;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   137
fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   138
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   139
in
21370
d9dd7b4e5e69 replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents: 21359
diff changeset
   140
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   141
fun read_specification x = read_spec true x;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   142
fun check_specification x = check_spec true x;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   143
fun read_free_specification vars specs = read_spec false vars [specs];
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   144
fun check_free_specification vars specs = check_spec false vars [specs];
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   145
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   146
end;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   147
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   148
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
   149
(* axiomatization *)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   150
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   151
fun gen_axioms prep raw_vars raw_specs lthy =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   152
  let
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   153
    val (vars, specs) = fst (prep raw_vars [raw_specs] lthy);
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
   154
    val cs = map fst vars;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   155
    val spec_frees = member (op =) ((fold (fold Term.add_frees o snd) specs []));
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   156
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   157
    val ((constants, axioms), lthy') = lthy
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   158
      |> LocalTheory.consts spec_frees vars
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   159
      ||>> LocalTheory.axioms Thm.axiomK specs;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   160
    val consts = map #1 constants;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   161
    val consts' = map (Morphism.term (LocalTheory.target_morphism lthy')) consts;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   162
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   163
    (* FIXME generic target!? *)
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   164
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) constants;
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   165
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   166
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   167
    val _ = print_consts lthy' (member (op =) (fold Term.add_frees consts' [])) cs;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   168
  in ((consts, axioms), lthy'') end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   169
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   170
val axiomatization = gen_axioms read_specification;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   171
val axiomatization_i = gen_axioms check_specification;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   172
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   173
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   174
(* definition *)
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   175
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   176
fun gen_def prep (raw_var, (raw_a, raw_prop)) lthy =
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   177
  let
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   178
    val (vars, [((raw_name, atts), [prop])]) =
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   179
      fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   180
    val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   181
    val name = Thm.def_name_optional x raw_name;
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   182
    val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   183
      if x = x' then mx
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   184
      else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   185
    val ((lhs, (_, th)), lthy2) = lthy
22923
6384c43da028 Changed name of raw definition.
berghofe
parents: 22744
diff changeset
   186
      |> LocalTheory.def Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs));
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   187
    val ((b, [th']), lthy3) = lthy2
22744
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 21860
diff changeset
   188
      |> LocalTheory.note Thm.definitionK
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24557
diff changeset
   189
          ((name, Code.add_default_func_attr :: atts), [prove lthy2 th]);
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   190
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   191
    val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs;
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   192
    val _ = print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   193
  in ((lhs, (b, th')), lthy3) end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   194
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   195
val definition = gen_def read_free_specification;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   196
val definition_i = gen_def check_free_specification;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   197
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   198
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   199
(* abbreviation *)
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   200
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   201
fun gen_abbrev prep mode (raw_var, raw_prop) lthy =
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   202
  let
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   203
    val ((vars, [(_, [prop])]), _) =
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   204
      prep (the_list raw_var) [(("", []), [raw_prop])]
24676
63eaef3207e1 ProofContext.mode_abbrev;
wenzelm
parents: 24624
diff changeset
   205
        (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   206
    val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   207
    val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   208
      if x = y then mx
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   209
      else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   210
    val lthy' = lthy
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   211
      |> ProofContext.set_syntax_mode mode    (* FIXME !? *)
21793
74409847e349 LocalTheory.abbrev;
wenzelm
parents: 21746
diff changeset
   212
      |> LocalTheory.abbrev mode ((x, mx), rhs) |> snd
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   213
      |> ProofContext.restore_syntax_mode lthy;
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   214
21705
0f3ad56548bc definition/abbreviation: single argument;
wenzelm
parents: 21691
diff changeset
   215
    val _ = print_consts lthy' (K false) [(x, T)]
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   216
  in lthy' end;
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   217
24724
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   218
val abbreviation = gen_abbrev read_free_specification;
0df74bb87a13 read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents: 24676
diff changeset
   219
val abbreviation_i = gen_abbrev check_free_specification;
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   220
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   221
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   222
(* notation *)
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   223
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   224
fun gen_notation prep_const mode args lthy =
21746
9d0652354513 LocalTheory.notation/abbrev;
wenzelm
parents: 21729
diff changeset
   225
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   226
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   227
val notation = gen_notation ProofContext.read_const;
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   228
val notation_i = gen_notation (K I);
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   229
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   230
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   231
(* fact statements *)
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   232
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   233
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   234
  let
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   235
    val attrib = prep_att (ProofContext.theory_of lthy);
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   236
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   237
      ((name, map attrib atts),
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   238
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   239
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   240
    val _ = present_results' lthy' kind res;
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   241
  in (res, lthy') end;
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   242
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   243
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   244
val theorems_i = gen_theorems (K I) (K I);
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   245
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   246
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   247
(* complex goal statements *)
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   248
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   249
local
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   250
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   251
fun subtract_prems ctxt1 ctxt2 =
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   252
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   253
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   254
fun prep_statement prep_att prep_stmt elems concl ctxt =
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   255
  (case concl of
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   256
    Element.Shows shows =>
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   257
      let
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   258
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   259
        val prems = subtract_prems loc_ctxt elems_ctxt;
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   260
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
21370
d9dd7b4e5e69 replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents: 21359
diff changeset
   261
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   262
      in ((prems, stmt, []), goal_ctxt) end
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   263
  | Element.Obtains obtains =>
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   264
      let
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   265
        val case_names = obtains |> map_index
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   266
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   267
        val constraints = obtains |> map (fn (_, (vars, _)) =>
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   268
          Locale.Elem (Element.Constrains
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   269
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   270
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   271
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   272
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   273
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   274
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   275
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   276
        fun assume_case ((name, (vars, _)), asms) ctxt' =
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   277
          let
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   278
            val xs = map fst vars;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   279
            val props = map fst asms;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   280
            val (parms, _) = ctxt'
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   281
              |> fold Variable.declare_term props
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   282
              |> fold_map ProofContext.inferred_param xs;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   283
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   284
          in
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   285
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
21370
d9dd7b4e5e69 replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents: 21359
diff changeset
   286
            ctxt' |> Variable.auto_fixes asm
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   287
            |> ProofContext.add_assms_i Assumption.assume_export
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   288
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   289
            |>> (fn [(_, [th])] => th)
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   290
          end;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   291
21658
5e31241e1e3c Attrib.internal: morphism;
wenzelm
parents: 21602
diff changeset
   292
        val atts = map (Attrib.internal o K)
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   293
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   294
        val prems = subtract_prems loc_ctxt elems_ctxt;
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   295
        val stmt = [(("", atts), [(thesis, [])])];
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   296
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   297
        val (facts, goal_ctxt) = elems_ctxt
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   298
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   299
          |> fold_map assume_case (obtains ~~ propp)
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   300
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   301
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   302
      in ((prems, stmt, facts), goal_ctxt) end);
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   303
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   304
structure TheoremHook = GenericDataFun
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   305
(
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   306
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   307
  val empty = [];
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   308
  val extend = I;
24464
58d24cbe5fa6 TheoremHook: fixed copy-paste mistake;
wenzelm
parents: 24452
diff changeset
   309
  fun merge _ hooks : T = Library.merge (eq_snd op =) hooks;
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   310
);
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   311
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   312
fun gen_theorem prep_att prep_stmt
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   313
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy0 =
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   314
  let
21860
c4492c6bf450 renamed LocalTheory.assert to affirm;
wenzelm
parents: 21793
diff changeset
   315
    val _ = LocalTheory.affirm lthy0;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   316
    val thy = ProofContext.theory_of lthy0;
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   317
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   318
    val (loc, ctxt, lthy) =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   319
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   320
        (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
21275
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   321
          (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   322
      | _ => (NONE, lthy0, lthy0));
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   323
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   324
    val attrib = prep_att thy;
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   325
    val atts = map attrib raw_atts;
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   326
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   327
    val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib));
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   328
    val ((prems, stmt, facts), goal_ctxt) =
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   329
      prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   330
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   331
    fun after_qed' results goal_ctxt' =
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21532
diff changeset
   332
      let val results' =
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21532
diff changeset
   333
        burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21532
diff changeset
   334
      in
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   335
        lthy
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   336
        |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   337
        |> (fn (res, lthy') =>
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   338
          (present_results lthy' ((kind, name), res);
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   339
            if name = "" andalso null atts then lthy'
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   340
            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   341
        |> after_qed results'
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   342
      end;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   343
  in
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   344
    goal_ctxt
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   345
    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   346
    |> snd
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   347
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
24557
57e2a5fbde86 theorem: apply hook last;
wenzelm
parents: 24542
diff changeset
   348
    |> Proof.refine_insert facts
24542
f13c59e40617 theorem hooks: apply in declaration order;
wenzelm
parents: 24464
diff changeset
   349
    |> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt))))
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   350
  end;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   351
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   352
in
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   353
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   354
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   355
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   356
24452
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   357
fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
93b113c5ac33 - theorem(_i) now also takes "interactive" flag as argument
berghofe
parents: 24219
diff changeset
   358
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   359
end;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   360
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   361
end;