src/Pure/Isar/specification.ML
author wenzelm
Sun, 26 Nov 2006 18:07:29 +0100
changeset 21532 8c162b766cef
parent 21450 f16c9e6401d1
child 21602 cb13024d0e36
permissions -rw-r--r--
abbrevs: no result; Element.map_ctxt_attrib;
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 ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    14
    ((string * Attrib.src list) * string list) list -> local_theory ->
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    15
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    16
    local_theory
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    17
  val cert_specification: (string * typ option * mixfix) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    18
    ((string * Attrib.src list) * term list) list -> local_theory ->
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) *
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    20
    local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    21
  val axiomatization: (string * string option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    22
    ((bstring * Attrib.src list) * string list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    23
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    24
  val axiomatization_i: (string * typ option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    25
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    26
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    27
  val definition:
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    28
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    29
    local_theory -> (term * (bstring * thm)) list * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    30
  val definition_i:
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    31
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    32
    local_theory -> (term * (bstring * thm)) list * local_theory
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 19664
diff changeset
    33
  val abbreviation: Syntax.mode -> ((string * string option * mixfix) option * string) list ->
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
    34
    local_theory -> local_theory
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 19664
diff changeset
    35
  val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list ->
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
    36
    local_theory -> local_theory
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
    37
  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
    38
  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
    39
  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
    40
    -> local_theory -> (bstring * thm list) list * local_theory
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    41
  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
    42
    -> local_theory -> (bstring * thm list) list * local_theory
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    43
  val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    44
    string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    45
    local_theory -> Proof.state
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    46
  val theorem_i: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    47
    string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    48
    local_theory -> Proof.state
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    49
end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    50
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    51
structure Specification: SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    52
struct
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    53
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
    54
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    55
(* diagnostics *)
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    56
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    57
val quiet_mode = ref false;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    58
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    59
fun print_consts _ _ [] = ()
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    60
  | print_consts ctxt pred cs =
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    61
      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
    62
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    63
fun present_results ctxt res =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    64
  if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    65
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
    66
fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
    67
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
    68
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    69
(* prepare specification *)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    70
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    71
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    72
  let
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    73
    val thy = ProofContext.theory_of ctxt;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    74
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    75
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    76
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    77
    val ((specs, vs), specs_ctxt) =
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19544
diff changeset
    78
      prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    79
      |> swap |>> map (map fst)
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    80
      ||>> fold_map ProofContext.inferred_param xs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    81
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    82
    val params = vs ~~ map #3 vars;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    83
    val names = map (fst o fst) raw_specs;
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    84
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    85
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    86
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    87
fun read_specification x =
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    88
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
21370
d9dd7b4e5e69 replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents: 21359
diff changeset
    89
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    90
fun cert_specification x =
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    91
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    92
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    93
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    94
(* axiomatization *)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    95
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    96
fun gen_axioms prep raw_vars raw_specs lthy =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    97
  let
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
    98
    val (vars, specs) = fst (prep raw_vars raw_specs lthy);
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    99
    val cs = map fst vars;
18880
b8a1c3cdf739 axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents: 18828
diff changeset
   100
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   101
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   102
    val ((consts, axioms), lthy') = lthy
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   103
      |> LocalTheory.consts spec_frees vars
21370
d9dd7b4e5e69 replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents: 21359
diff changeset
   104
      ||> fold (fold Variable.auto_fixes o snd) specs   (* FIXME !? *)
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   105
      ||>> LocalTheory.axioms Thm.axiomK specs;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   106
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   107
    (* FIXME generic target!? *)
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   108
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   109
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   110
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   111
    val _ = print_consts lthy' spec_frees cs;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   112
  in ((map #1 consts, axioms), lthy'') end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   113
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   114
val axiomatization = gen_axioms read_specification;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   115
val axiomatization_i = gen_axioms cert_specification;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   116
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   117
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   118
(* definition *)
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   119
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   120
fun gen_defs prep args lthy =
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   121
  let
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   122
    fun define (raw_var, (raw_a, raw_prop)) lthy1 =
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   123
      let
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   124
        val (vars, [((raw_name, atts), [prop])]) =
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   125
          fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   126
        val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   127
        val name = Thm.def_name_optional x raw_name;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   128
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   129
          if x = x' then mx
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   130
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   131
        val ((lhs, (_, th)), lthy2) = lthy1
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   132
(*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   133
          |> LocalTheory.def Thm.definitionK ((x, mx), ((name, []), rhs));
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   134
        val ((b, [th']), lthy3) = lthy2
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   135
          |> LocalTheory.note Thm.definitionK ((name, atts), [prove lthy2 th]);
21275
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   136
      in (((x, T), (lhs, (b, th'))), LocalTheory.restore lthy3) end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   137
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   138
    val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
18880
b8a1c3cdf739 axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents: 18828
diff changeset
   139
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   140
    val _ = print_consts lthy' def_frees cs;
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   141
  in (defs, lthy') end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   142
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   143
val definition = gen_defs read_specification;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   144
val definition_i = gen_defs cert_specification;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   145
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   146
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   147
(* abbreviation *)
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   148
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   149
fun gen_abbrevs prep mode args lthy =
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   150
  let
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   151
    fun abbrev (raw_var, raw_prop) lthy1 =
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   152
      let
19372
3ff5f1777743 abbreviation(_i): do not expand abbreviations, do not use derived_def;
wenzelm
parents: 19080
diff changeset
   153
        val ((vars, [(_, [prop])]), _) =
3ff5f1777743 abbreviation(_i): do not expand abbreviations, do not use derived_def;
wenzelm
parents: 19080
diff changeset
   154
          prep (the_list raw_var) [(("", []), [raw_prop])]
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   155
            (lthy1 |> ProofContext.expand_abbrevs false);
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   156
        val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
21275
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   157
        val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   158
          if x = y then mx
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   159
          else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   160
        val lthy2 = lthy1 |> LocalTheory.abbrevs mode [((x, mx), rhs)];
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   161
      in ((x, T), LocalTheory.restore lthy2) end;
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   162
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   163
    val (cs, lthy') = lthy
19544
e3a39dae2004 abbreviation: observe local syntax mode;
wenzelm
parents: 19372
diff changeset
   164
      |> ProofContext.set_syntax_mode mode
e3a39dae2004 abbreviation: observe local syntax mode;
wenzelm
parents: 19372
diff changeset
   165
      |> fold_map abbrev args
20890
052bde912a51 moved pretty_consts to proof_display.ML;
wenzelm
parents: 20784
diff changeset
   166
      ||> ProofContext.restore_syntax_mode lthy;
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   167
    val _ = print_consts lthy' (K false) cs;
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   168
  in lthy' end;
19080
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   169
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   170
val abbreviation = gen_abbrevs read_specification;
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   171
val abbreviation_i = gen_abbrevs cert_specification;
46ba991e27d5 added abbreviation(_i);
wenzelm
parents: 18954
diff changeset
   172
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   173
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   174
(* notation *)
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   175
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   176
fun gen_notation prep_const mode args lthy =
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   177
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   178
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   179
val notation = gen_notation ProofContext.read_const;
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   180
val notation_i = gen_notation (K I);
19664
e1dc01a48a52 added const_syntax(_i);
wenzelm
parents: 19585
diff changeset
   181
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   182
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   183
(* fact statements *)
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   184
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   185
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   186
  let
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   187
    val attrib = prep_att (ProofContext.theory_of lthy);
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   188
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   189
      ((name, map attrib atts),
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   190
        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
   191
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   192
    val _ = present_results' lthy' kind res;
20914
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   193
  in (res, lthy') end;
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   194
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   195
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   196
val theorems_i = gen_theorems (K I) (K I);
3f065aa89792 added theorems(_i) -- with present_results;
wenzelm
parents: 20890
diff changeset
   197
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   198
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   199
(* complex goal statements *)
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   200
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   201
local
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   202
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   203
fun subtract_prems ctxt1 ctxt2 =
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   204
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   205
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   206
fun prep_statement prep_att prep_stmt elems concl ctxt =
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   207
  (case concl of
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   208
    Element.Shows shows =>
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   209
      let
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   210
        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
   211
        val prems = subtract_prems loc_ctxt elems_ctxt;
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   212
        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
   213
        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
   214
      in ((prems, stmt, []), goal_ctxt) end
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   215
  | Element.Obtains obtains =>
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   216
      let
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   217
        val case_names = obtains |> map_index
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   218
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   219
        val constraints = obtains |> map (fn (_, (vars, _)) =>
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   220
          Locale.Elem (Element.Constrains
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   221
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   222
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   223
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   224
        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
   225
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   226
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   227
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   228
        fun assume_case ((name, (vars, _)), asms) ctxt' =
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   229
          let
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   230
            val xs = map fst vars;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   231
            val props = map fst asms;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   232
            val (parms, _) = ctxt'
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   233
              |> fold Variable.declare_term props
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   234
              |> fold_map ProofContext.inferred_param xs;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   235
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   236
          in
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   237
            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
   238
            ctxt' |> Variable.auto_fixes asm
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   239
            |> ProofContext.add_assms_i Assumption.assume_export
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   240
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   241
            |>> (fn [(_, [th])] => th)
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   242
          end;
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   243
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   244
        val atts = map Attrib.internal
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   245
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   246
        val prems = subtract_prems loc_ctxt elems_ctxt;
21236
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   247
        val stmt = [(("", atts), [(thesis, [])])];
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   248
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   249
        val (facts, goal_ctxt) = elems_ctxt
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   250
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
890fafbcf8b0 complex goal statements: misc cleanup;
wenzelm
parents: 21230
diff changeset
   251
          |> fold_map assume_case (obtains ~~ propp)
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   252
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   253
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   254
      in ((prems, stmt, facts), goal_ctxt) end);
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   255
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   256
fun gen_theorem prep_att prep_stmt
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   257
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   258
  let
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   259
    val _ = LocalTheory.assert lthy0;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   260
    val thy = ProofContext.theory_of lthy0;
21206
2af4c7b3f7ef replaced const_syntax by notation, which operates on terms;
wenzelm
parents: 21036
diff changeset
   261
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   262
    val (loc, ctxt, lthy) =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   263
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   264
        (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
21275
e4cb9c7a7482 abbrevs: return result;
wenzelm
parents: 21236
diff changeset
   265
          (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   266
      | _ => (NONE, lthy0, lthy0));
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   267
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   268
    val attrib = prep_att thy;
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   269
    val atts = map attrib raw_atts;
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   270
21532
8c162b766cef abbrevs: no result;
wenzelm
parents: 21450
diff changeset
   271
    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
   272
    val ((prems, stmt, facts), goal_ctxt) =
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   273
      prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   274
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   275
    fun after_qed' results goal_ctxt' =
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   276
      let val results' = burrow (ProofContext.export_standard goal_ctxt' lthy) results in
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   277
        lthy
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   278
        |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   279
        |> (fn (res, lthy') =>
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   280
          (present_results lthy' ((kind, name), res);
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   281
            if name = "" andalso null atts then lthy'
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   282
            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   283
        |> after_qed results'
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   284
      end;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   285
  in
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   286
    goal_ctxt
21450
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   287
    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
f16c9e6401d1 theorem(_i): note assms of statement;
wenzelm
parents: 21435
diff changeset
   288
    |> snd
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21370
diff changeset
   289
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   290
    |> Proof.refine_insert facts
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   291
  end;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   292
21230
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   293
in
abfdce60b371 theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents: 21206
diff changeset
   294
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   295
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   296
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   297
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   298
end;
21036
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   299
0eed532acfca added theorem(_i);
wenzelm
parents: 20914
diff changeset
   300
end;