src/Pure/Isar/isar_thy.ML
author wenzelm
Thu Mar 11 21:53:50 1999 +0100 (1999-03-11 ago)
changeset 6354 a4c75cbd2fbf
parent 6331 fb7b8d6c2bd1
child 6371 8469852acbc0
permissions -rw-r--r--
add_title;
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@5959
    15
  val add_text: string -> theory -> theory
wenzelm@6354
    16
  val add_title: string -> string -> string -> theory -> theory
wenzelm@5959
    17
  val add_chapter: string -> theory -> theory
wenzelm@5959
    18
  val add_section: string -> theory -> theory
wenzelm@5959
    19
  val add_subsection: string -> theory -> theory
wenzelm@5959
    20
  val add_subsubsection: string -> theory -> theory
wenzelm@5830
    21
  val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@5830
    22
  val add_defs: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@5915
    23
  val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    24
    -> theory -> theory
wenzelm@5915
    25
  val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    26
    -> theory -> theory
wenzelm@5915
    27
  val have_facts: (string * Args.src list) * (string * Args.src list) list
wenzelm@5915
    28
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    29
  val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    30
  val match_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    31
  val theorem: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@5938
    32
  val lemma: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@5938
    33
  val assume: string -> Args.src list -> (string * string list) list
wenzelm@5938
    34
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    35
  val show: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    36
  val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    37
  val chain: ProofHistory.T -> ProofHistory.T
wenzelm@5915
    38
  val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    39
  val begin_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    40
  val next_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    41
  val end_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    42
  val qed_with: bstring option * Args.src list option -> ProofHistory.T -> theory
wenzelm@5830
    43
  val qed: ProofHistory.T -> theory
wenzelm@5830
    44
  val kill_proof: ProofHistory.T -> theory
wenzelm@5830
    45
  val tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5882
    46
  val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    47
  val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    48
  val terminal_proof: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6108
    49
  val immediate_proof: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    50
  val default_proof: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    51
  val use_mltext: string -> theory option -> theory option
wenzelm@5830
    52
  val use_mltext_theory: string -> theory -> theory
wenzelm@5830
    53
  val use_setup: string -> theory -> theory
wenzelm@5830
    54
  val parse_ast_translation: string -> theory -> theory
wenzelm@5830
    55
  val parse_translation: string -> theory -> theory
wenzelm@5830
    56
  val print_translation: string -> theory -> theory
wenzelm@5830
    57
  val typed_print_translation: string -> theory -> theory
wenzelm@5830
    58
  val print_ast_translation: string -> theory -> theory
wenzelm@5830
    59
  val token_translation: string -> theory -> theory
wenzelm@5830
    60
  val add_oracle: bstring * string -> theory -> theory
wenzelm@6331
    61
  val begin_theory: string -> string list -> (string * bool) list -> theory
wenzelm@6331
    62
  val end_theory: theory -> theory
wenzelm@6246
    63
  val theory: string * string list * (string * bool) list
wenzelm@6246
    64
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
    65
  val context: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
    66
  val update_context: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5830
    67
end;
wenzelm@5830
    68
wenzelm@5830
    69
structure IsarThy: ISAR_THY =
wenzelm@5830
    70
struct
wenzelm@5830
    71
wenzelm@5830
    72
wenzelm@5830
    73
(** derived theory and proof operations **)
wenzelm@5830
    74
wenzelm@5959
    75
(* formal comments *)	(* FIXME dummy *)
wenzelm@5959
    76
wenzelm@5959
    77
fun add_text (txt:string) (thy:theory) = thy;
wenzelm@6354
    78
fun add_title title author date thy = thy;
wenzelm@5959
    79
val add_chapter = add_text;
wenzelm@5959
    80
val add_section = add_text;
wenzelm@5959
    81
val add_subsection = add_text;
wenzelm@5959
    82
val add_subsubsection = add_text;
wenzelm@5959
    83
wenzelm@5959
    84
wenzelm@5830
    85
(* axioms and defs *)
wenzelm@5830
    86
wenzelm@5915
    87
fun add_axms f args thy =
wenzelm@5915
    88
  f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
wenzelm@5915
    89
wenzelm@5915
    90
val add_axioms = add_axms PureThy.add_axioms;
wenzelm@5915
    91
val add_defs = add_axms PureThy.add_defs;
wenzelm@5915
    92
wenzelm@5915
    93
wenzelm@5915
    94
(* theorems *)
wenzelm@5915
    95
wenzelm@5915
    96
fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
wenzelm@5915
    97
  f name (map (attrib x) more_srcs)
wenzelm@5915
    98
    (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
wenzelm@5915
    99
wenzelm@5830
   100
wenzelm@5915
   101
val have_theorems =
wenzelm@6091
   102
  #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute PureThy.have_thmss;
wenzelm@5915
   103
wenzelm@5915
   104
val have_lemmas =
wenzelm@6091
   105
  #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute
wenzelm@6091
   106
    (fn name => fn more_atts => PureThy.have_thmss name (more_atts @ [Drule.tag_lemma]));
wenzelm@5915
   107
wenzelm@5915
   108
wenzelm@5915
   109
val have_thmss =
wenzelm@6091
   110
  gen_have_thmss (ProofContext.get_thms o Proof.context_of)
wenzelm@6091
   111
    (Attrib.local_attribute o Proof.theory_of) Proof.have_thmss;
wenzelm@5915
   112
wenzelm@5915
   113
val have_facts = ProofHistory.apply o have_thmss;
wenzelm@5915
   114
val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
wenzelm@5830
   115
wenzelm@5830
   116
wenzelm@5830
   117
(* context *)
wenzelm@5830
   118
wenzelm@5830
   119
val fix = ProofHistory.apply o Proof.fix;
wenzelm@5830
   120
wenzelm@5830
   121
val match_bind = ProofHistory.apply o Proof.match_bind;
wenzelm@5830
   122
wenzelm@5830
   123
wenzelm@5830
   124
(* statements *)
wenzelm@5830
   125
wenzelm@5830
   126
fun global_statement f name tags s thy =
wenzelm@5830
   127
  ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
wenzelm@5830
   128
wenzelm@5830
   129
fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
wenzelm@5830
   130
  f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
wenzelm@5830
   131
wenzelm@5830
   132
val theorem = global_statement Proof.theorem;
wenzelm@5830
   133
val lemma = global_statement Proof.lemma;
wenzelm@5830
   134
val assume = local_statement Proof.assume;
wenzelm@5830
   135
val show = local_statement Proof.show;
wenzelm@5830
   136
val have = local_statement Proof.have;
wenzelm@5830
   137
wenzelm@5830
   138
wenzelm@5830
   139
(* forward chaining *)
wenzelm@5830
   140
wenzelm@5830
   141
val chain = ProofHistory.apply Proof.chain;
wenzelm@5830
   142
wenzelm@5830
   143
wenzelm@5830
   144
(* end proof *)
wenzelm@5830
   145
wenzelm@5830
   146
fun qed_with (alt_name, alt_tags) prf =
wenzelm@5830
   147
  let
wenzelm@5830
   148
    val state = ProofHistory.current prf;
wenzelm@5830
   149
    val thy = Proof.theory_of state;
wenzelm@5830
   150
    val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
wenzelm@6091
   151
    val (thy', (kind, name, thm)) = Method.qed alt_name alt_atts state;
wenzelm@5830
   152
wenzelm@5830
   153
    val prt_result = Pretty.block
wenzelm@6091
   154
      [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
wenzelm@5830
   155
  in Pretty.writeln prt_result; thy end;
wenzelm@5830
   156
wenzelm@5830
   157
val qed = qed_with (None, None);
wenzelm@5830
   158
wenzelm@5830
   159
val kill_proof = Proof.theory_of o ProofHistory.current;
wenzelm@5830
   160
wenzelm@5830
   161
wenzelm@5830
   162
(* blocks *)
wenzelm@5830
   163
wenzelm@5830
   164
val begin_block = ProofHistory.apply_open Proof.begin_block;
wenzelm@5830
   165
val next_block = ProofHistory.apply Proof.next_block;
wenzelm@5830
   166
wenzelm@5830
   167
wenzelm@5830
   168
(* backward steps *)
wenzelm@5830
   169
wenzelm@5830
   170
val tac = ProofHistory.applys o Method.tac;
wenzelm@5882
   171
val then_tac = ProofHistory.applys o Method.then_tac;
wenzelm@5830
   172
val proof = ProofHistory.applys o Method.proof;
wenzelm@5830
   173
val end_block = ProofHistory.applys_close Method.end_block;
wenzelm@5830
   174
val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
wenzelm@6108
   175
val immediate_proof = ProofHistory.applys_close Method.immediate_proof;
wenzelm@5830
   176
val default_proof = ProofHistory.applys_close Method.default_proof;
wenzelm@5830
   177
wenzelm@5830
   178
wenzelm@5830
   179
(* use ML text *)
wenzelm@5830
   180
wenzelm@6331
   181
fun use_mltext txt opt_thy = #2 (Context.pass opt_thy (use_text false) txt);
wenzelm@6331
   182
fun use_mltext_theory txt thy = #2 (Context.pass_theory thy (use_text false) txt);
wenzelm@5830
   183
wenzelm@5830
   184
fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
wenzelm@5830
   185
wenzelm@5830
   186
fun use_let name body txt =
wenzelm@5830
   187
  use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@5830
   188
wenzelm@5830
   189
val use_setup =
wenzelm@5915
   190
  use_let "setup: (theory -> theory) list" "Library.apply setup";
wenzelm@5830
   191
wenzelm@5830
   192
wenzelm@5830
   193
(* translation functions *)
wenzelm@5830
   194
wenzelm@5830
   195
val parse_ast_translation =
wenzelm@5830
   196
  use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   197
    "Theory.add_trfuns (parse_ast_translation, [], [], [])";
wenzelm@5830
   198
wenzelm@5830
   199
val parse_translation =
wenzelm@5830
   200
  use_let "parse_translation: (string * (term list -> term)) list"
wenzelm@5830
   201
    "Theory.add_trfuns ([], parse_translation, [], [])";
wenzelm@5830
   202
wenzelm@5830
   203
val print_translation =
wenzelm@5830
   204
  use_let "print_translation: (string * (term list -> term)) list"
wenzelm@5830
   205
    "Theory.add_trfuns ([], [], print_translation, [])";
wenzelm@5830
   206
wenzelm@5830
   207
val print_ast_translation =
wenzelm@5830
   208
  use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   209
    "Theory.add_trfuns ([], [], [], print_ast_translation)";
wenzelm@5830
   210
wenzelm@5830
   211
val typed_print_translation =
wenzelm@5830
   212
  use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
wenzelm@5830
   213
    "Theory.add_trfunsT typed_print_translation";
wenzelm@5830
   214
wenzelm@5830
   215
val token_translation =
wenzelm@5830
   216
  use_let "token_translation: (string * string * (string -> string * int)) list"
wenzelm@5830
   217
    "Theory.add_tokentrfuns token_translation";
wenzelm@5830
   218
wenzelm@5830
   219
wenzelm@5830
   220
(* add_oracle *)
wenzelm@5830
   221
wenzelm@5830
   222
fun add_oracle (name, txt) =
wenzelm@5830
   223
  use_let
wenzelm@5830
   224
    "oracle: bstring * (Sign.sg * Object.T -> term)"
wenzelm@5830
   225
    "Theory.add_oracle oracle"
wenzelm@5830
   226
    ("(" ^ quote name ^ ", " ^ txt ^ ")");
wenzelm@5830
   227
wenzelm@5830
   228
wenzelm@6331
   229
(* theory init and exit *)	(* FIXME move? rearrange? *)
wenzelm@5830
   230
wenzelm@6331
   231
fun begin_theory name parents files =
wenzelm@6266
   232
  let
wenzelm@6331
   233
    val paths = map (apfst Path.unpack) files;
wenzelm@6331
   234
    val thy = ThyInfo.begin_theory name parents paths;
wenzelm@6331
   235
  in Present.begin_theory name parents paths; thy end;
wenzelm@5830
   236
wenzelm@6331
   237
wenzelm@6331
   238
(* FIXME
wenzelm@5830
   239
fun end_theory thy =
wenzelm@5830
   240
  let val thy' = PureThy.end_theory thy in
wenzelm@6331
   241
    Present.end_theory (PureThy.get_name thy');
wenzelm@6198
   242
    transform_error ThyInfo.put_theory thy'
wenzelm@5830
   243
      handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
wenzelm@5830
   244
  end;
wenzelm@6331
   245
*)
wenzelm@5830
   246
wenzelm@6331
   247
fun end_theory thy =
wenzelm@6331
   248
  (Present.end_theory (PureThy.get_name thy); ThyInfo.end_theory thy);
wenzelm@6331
   249
wenzelm@6331
   250
fun bg_theory (name, parents, files) () = begin_theory name parents files;
wenzelm@6331
   251
fun en_theory thy = (end_theory thy; ());
wenzelm@6331
   252
wenzelm@6331
   253
fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory;
wenzelm@6246
   254
wenzelm@6246
   255
wenzelm@6246
   256
(* context switch *)
wenzelm@6246
   257
wenzelm@6253
   258
fun switch_theory load name =
wenzelm@6246
   259
  Toplevel.init_theory
wenzelm@6253
   260
    (fn () => (Context.save load name; ThyInfo.get_theory name)) (K ());
wenzelm@6246
   261
wenzelm@6246
   262
val context = switch_theory ThyInfo.use_thy;
wenzelm@6246
   263
val update_context = switch_theory ThyInfo.update_thy;
wenzelm@6246
   264
wenzelm@5830
   265
wenzelm@5830
   266
end;