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