src/Pure/Isar/isar_thy.ML
author wenzelm
Thu Nov 19 11:49:09 1998 +0100 (1998-11-19)
changeset 5938 fe7640933a47
parent 5925 669d0bc621e1
child 5959 7af99477751b
permissions -rw-r--r--
match_bind: 'as' patterns;
statements: 'is' patterns;
wenzelm@5830
     1
(*  Title:      Pure/Isar/isar_thy.ML
wenzelm@5830
     2
    ID:         $Id$
wenzelm@5830
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5830
     4
wenzelm@5830
     5
Derived theory operations.
wenzelm@5830
     6
wenzelm@5830
     7
TODO:
wenzelm@5925
     8
  - pure_thy.ML: add_constdefs (atomic!);
wenzelm@5830
     9
  - 'methods' section (proof macros, ML method defs) (!?);
wenzelm@5882
    10
  - next_block: ProofHistory open / close (!?);
wenzelm@5830
    11
*)
wenzelm@5830
    12
wenzelm@5830
    13
signature ISAR_THY =
wenzelm@5830
    14
sig
wenzelm@5830
    15
  val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@5830
    16
  val add_defs: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@5915
    17
  val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    18
    -> theory -> theory
wenzelm@5915
    19
  val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    20
    -> theory -> theory
wenzelm@5915
    21
  val have_facts: (string * Args.src list) * (string * Args.src list) list
wenzelm@5915
    22
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    23
  val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    24
  val match_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    25
  val theorem: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@5938
    26
  val lemma: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@5938
    27
  val assume: string -> Args.src list -> (string * string list) list
wenzelm@5938
    28
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    29
  val show: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    30
  val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    31
  val chain: ProofHistory.T -> ProofHistory.T
wenzelm@5915
    32
  val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    33
  val begin_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    34
  val next_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    35
  val end_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    36
  val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
wenzelm@5830
    37
  val qed: ProofHistory.T -> theory
wenzelm@5830
    38
  val kill_proof: ProofHistory.T -> theory
wenzelm@5830
    39
  val tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5882
    40
  val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    41
  val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    42
  val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    43
  val trivial_proof: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    44
  val default_proof: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    45
  val use_mltext: string -> theory option -> theory option
wenzelm@5830
    46
  val use_mltext_theory: string -> theory -> theory
wenzelm@5830
    47
  val use_setup: string -> theory -> theory
wenzelm@5830
    48
  val parse_ast_translation: string -> theory -> theory
wenzelm@5830
    49
  val parse_translation: string -> theory -> theory
wenzelm@5830
    50
  val print_translation: string -> theory -> theory
wenzelm@5830
    51
  val typed_print_translation: string -> theory -> theory
wenzelm@5830
    52
  val print_ast_translation: string -> theory -> theory
wenzelm@5830
    53
  val token_translation: string -> theory -> theory
wenzelm@5830
    54
  val add_oracle: bstring * string -> theory -> theory
wenzelm@5830
    55
  val the_theory: string -> unit -> theory
wenzelm@5830
    56
  val begin_theory: string * string list -> unit -> theory
wenzelm@5830
    57
  val end_theory: theory -> unit
wenzelm@5830
    58
end;
wenzelm@5830
    59
wenzelm@5830
    60
structure IsarThy: ISAR_THY =
wenzelm@5830
    61
struct
wenzelm@5830
    62
wenzelm@5830
    63
wenzelm@5830
    64
(** derived theory and proof operations **)
wenzelm@5830
    65
wenzelm@5830
    66
(* axioms and defs *)
wenzelm@5830
    67
wenzelm@5915
    68
fun add_axms f args thy =
wenzelm@5915
    69
  f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
wenzelm@5915
    70
wenzelm@5915
    71
val add_axioms = add_axms PureThy.add_axioms;
wenzelm@5915
    72
val add_defs = add_axms PureThy.add_defs;
wenzelm@5915
    73
wenzelm@5915
    74
wenzelm@5915
    75
(* theorems *)
wenzelm@5915
    76
wenzelm@5915
    77
fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
wenzelm@5915
    78
  f name (map (attrib x) more_srcs)
wenzelm@5915
    79
    (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
wenzelm@5915
    80
wenzelm@5830
    81
wenzelm@5915
    82
val have_theorems =
wenzelm@5915
    83
  #1 oo gen_have_thmss PureThy.get_tthms Attrib.global_attribute PureThy.have_tthmss;
wenzelm@5915
    84
wenzelm@5915
    85
val have_lemmas =
wenzelm@5915
    86
  #1 oo gen_have_thmss PureThy.get_tthms Attrib.global_attribute
wenzelm@5915
    87
    (fn name => fn more_atts => PureThy.have_tthmss name (more_atts @ [Attribute.tag_lemma]));
wenzelm@5915
    88
wenzelm@5915
    89
wenzelm@5915
    90
val have_thmss =
wenzelm@5915
    91
  gen_have_thmss (ProofContext.get_tthms o Proof.context_of)
wenzelm@5915
    92
    (Attrib.local_attribute o Proof.theory_of) Proof.have_tthmss;
wenzelm@5915
    93
wenzelm@5915
    94
val have_facts = ProofHistory.apply o have_thmss;
wenzelm@5915
    95
val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
wenzelm@5830
    96
wenzelm@5830
    97
wenzelm@5830
    98
(* context *)
wenzelm@5830
    99
wenzelm@5830
   100
val fix = ProofHistory.apply o Proof.fix;
wenzelm@5830
   101
wenzelm@5830
   102
val match_bind = ProofHistory.apply o Proof.match_bind;
wenzelm@5830
   103
wenzelm@5830
   104
wenzelm@5830
   105
(* statements *)
wenzelm@5830
   106
wenzelm@5830
   107
fun global_statement f name tags s thy =
wenzelm@5830
   108
  ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
wenzelm@5830
   109
wenzelm@5830
   110
fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
wenzelm@5830
   111
  f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
wenzelm@5830
   112
wenzelm@5830
   113
val theorem = global_statement Proof.theorem;
wenzelm@5830
   114
val lemma = global_statement Proof.lemma;
wenzelm@5830
   115
val assume = local_statement Proof.assume;
wenzelm@5830
   116
val show = local_statement Proof.show;
wenzelm@5830
   117
val have = local_statement Proof.have;
wenzelm@5830
   118
wenzelm@5830
   119
wenzelm@5830
   120
(* forward chaining *)
wenzelm@5830
   121
wenzelm@5830
   122
val chain = ProofHistory.apply Proof.chain;
wenzelm@5830
   123
wenzelm@5830
   124
wenzelm@5830
   125
(* end proof *)
wenzelm@5830
   126
wenzelm@5830
   127
fun qed_with (alt_name, alt_tags) prf =
wenzelm@5830
   128
  let
wenzelm@5830
   129
    val state = ProofHistory.current prf;
wenzelm@5830
   130
    val thy = Proof.theory_of state;
wenzelm@5830
   131
    val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
wenzelm@5830
   132
    val (thy', (kind, name, tthm)) = Method.qed alt_name alt_atts state;
wenzelm@5830
   133
wenzelm@5830
   134
    val prt_result = Pretty.block
wenzelm@5830
   135
      [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Attribute.pretty_tthm tthm];
wenzelm@5830
   136
  in Pretty.writeln prt_result; thy end;
wenzelm@5830
   137
wenzelm@5830
   138
val qed = qed_with (None, None);
wenzelm@5830
   139
wenzelm@5830
   140
val kill_proof = Proof.theory_of o ProofHistory.current;
wenzelm@5830
   141
wenzelm@5830
   142
wenzelm@5830
   143
(* blocks *)
wenzelm@5830
   144
wenzelm@5830
   145
val begin_block = ProofHistory.apply_open Proof.begin_block;
wenzelm@5830
   146
val next_block = ProofHistory.apply Proof.next_block;
wenzelm@5830
   147
wenzelm@5830
   148
wenzelm@5830
   149
(* backward steps *)
wenzelm@5830
   150
wenzelm@5830
   151
val tac = ProofHistory.applys o Method.tac;
wenzelm@5882
   152
val then_tac = ProofHistory.applys o Method.then_tac;
wenzelm@5830
   153
val proof = ProofHistory.applys o Method.proof;
wenzelm@5830
   154
val end_block = ProofHistory.applys_close Method.end_block;
wenzelm@5830
   155
val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
wenzelm@5830
   156
val trivial_proof = ProofHistory.applys_close Method.trivial_proof;
wenzelm@5830
   157
val default_proof = ProofHistory.applys_close Method.default_proof;
wenzelm@5830
   158
wenzelm@5830
   159
wenzelm@5830
   160
(* use ML text *)
wenzelm@5830
   161
wenzelm@5830
   162
fun use_mltext txt opt_thy =
wenzelm@5830
   163
  let
wenzelm@5830
   164
    val save_context = Context.get_context ();
wenzelm@5830
   165
    val _ = Context.set_context opt_thy;
wenzelm@5830
   166
    val opt_exn = (transform_error (use_text false) txt; None) handle exn => Some exn;
wenzelm@5830
   167
    val opt_thy' = Context.get_context ();
wenzelm@5830
   168
  in
wenzelm@5830
   169
    Context.set_context save_context;
wenzelm@5830
   170
    (case opt_exn of
wenzelm@5830
   171
      None => opt_thy'
wenzelm@5830
   172
    | Some exn => raise exn)
wenzelm@5830
   173
  end;
wenzelm@5830
   174
wenzelm@5830
   175
fun use_mltext_theory txt thy =
wenzelm@5830
   176
  (case use_mltext txt (Some thy) of
wenzelm@5830
   177
    Some thy' => thy'
wenzelm@5830
   178
  | None => error "Missing result ML theory context");
wenzelm@5830
   179
wenzelm@5830
   180
wenzelm@5830
   181
fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
wenzelm@5830
   182
wenzelm@5830
   183
fun use_let name body txt =
wenzelm@5830
   184
  use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@5830
   185
wenzelm@5830
   186
val use_setup =
wenzelm@5915
   187
  use_let "setup: (theory -> theory) list" "Library.apply setup";
wenzelm@5830
   188
wenzelm@5830
   189
wenzelm@5830
   190
(* translation functions *)
wenzelm@5830
   191
wenzelm@5830
   192
val parse_ast_translation =
wenzelm@5830
   193
  use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   194
    "Theory.add_trfuns (parse_ast_translation, [], [], [])";
wenzelm@5830
   195
wenzelm@5830
   196
val parse_translation =
wenzelm@5830
   197
  use_let "parse_translation: (string * (term list -> term)) list"
wenzelm@5830
   198
    "Theory.add_trfuns ([], parse_translation, [], [])";
wenzelm@5830
   199
wenzelm@5830
   200
val print_translation =
wenzelm@5830
   201
  use_let "print_translation: (string * (term list -> term)) list"
wenzelm@5830
   202
    "Theory.add_trfuns ([], [], print_translation, [])";
wenzelm@5830
   203
wenzelm@5830
   204
val print_ast_translation =
wenzelm@5830
   205
  use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   206
    "Theory.add_trfuns ([], [], [], print_ast_translation)";
wenzelm@5830
   207
wenzelm@5830
   208
val typed_print_translation =
wenzelm@5830
   209
  use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
wenzelm@5830
   210
    "Theory.add_trfunsT typed_print_translation";
wenzelm@5830
   211
wenzelm@5830
   212
val token_translation =
wenzelm@5830
   213
  use_let "token_translation: (string * string * (string -> string * int)) list"
wenzelm@5830
   214
    "Theory.add_tokentrfuns token_translation";
wenzelm@5830
   215
wenzelm@5830
   216
wenzelm@5830
   217
(* add_oracle *)
wenzelm@5830
   218
wenzelm@5830
   219
fun add_oracle (name, txt) =
wenzelm@5830
   220
  use_let
wenzelm@5830
   221
    "oracle: bstring * (Sign.sg * Object.T -> term)"
wenzelm@5830
   222
    "Theory.add_oracle oracle"
wenzelm@5830
   223
    ("(" ^ quote name ^ ", " ^ txt ^ ")");
wenzelm@5830
   224
wenzelm@5830
   225
wenzelm@5830
   226
(* theory init and exit *)
wenzelm@5830
   227
wenzelm@5830
   228
fun the_theory name () = ThyInfo.theory name;
wenzelm@5830
   229
wenzelm@5830
   230
fun begin_theory (name, names) () =
wenzelm@5830
   231
  PureThy.begin_theory name (map ThyInfo.theory names);
wenzelm@5830
   232
wenzelm@5830
   233
wenzelm@5830
   234
fun end_theory thy =
wenzelm@5830
   235
  let val thy' = PureThy.end_theory thy in
wenzelm@5830
   236
    transform_error ThyInfo.store_theory thy'
wenzelm@5830
   237
      handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
wenzelm@5830
   238
  end;
wenzelm@5830
   239
wenzelm@5830
   240
wenzelm@5830
   241
end;