src/Pure/Isar/isar_thy.ML
author wenzelm
Tue Apr 27 15:14:22 1999 +0200 (1999-04-27)
changeset 6531 8064ed198068
parent 6501 392333eb31cb
child 6552 28553eba1913
permissions -rw-r--r--
tuned;
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@6371
     5
Pure/Isar derived theory operations.
wenzelm@5830
     6
wenzelm@5830
     7
TODO:
wenzelm@5830
     8
  - 'methods' section (proof macros, ML method defs) (!?);
wenzelm@5882
     9
  - next_block: ProofHistory open / close (!?);
wenzelm@5830
    10
*)
wenzelm@5830
    11
wenzelm@5830
    12
signature ISAR_THY =
wenzelm@5830
    13
sig
wenzelm@5959
    14
  val add_text: string -> theory -> theory
wenzelm@6354
    15
  val add_title: string -> string -> string -> theory -> theory
wenzelm@5959
    16
  val add_chapter: string -> theory -> theory
wenzelm@5959
    17
  val add_section: string -> theory -> theory
wenzelm@5959
    18
  val add_subsection: string -> theory -> theory
wenzelm@5959
    19
  val add_subsubsection: string -> theory -> theory
wenzelm@5830
    20
  val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@6371
    21
  val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory
wenzelm@5830
    22
  val add_defs: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@6371
    23
  val add_defs_i: ((bstring * term) * theory attribute list) list -> theory -> theory
wenzelm@6371
    24
  val add_constdefs: ((bstring * string * mixfix) * string) list -> theory -> theory
wenzelm@6371
    25
  val add_constdefs_i: ((bstring * typ * mixfix) * term) list -> theory -> theory
wenzelm@6371
    26
  val apply_theorems: (xstring * Args.src list) list -> theory -> theory * thm list
wenzelm@6371
    27
  val apply_theorems_i: (thm * theory attribute list) list -> theory -> theory * thm list
wenzelm@5915
    28
  val have_theorems: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    29
    -> theory -> theory
wenzelm@6371
    30
  val have_theorems_i: (bstring * theory attribute list) * (thm * theory attribute list) list
wenzelm@6371
    31
    -> theory -> theory
wenzelm@5915
    32
  val have_lemmas: (bstring * Args.src list) * (xstring * Args.src list) list
wenzelm@5915
    33
    -> theory -> theory
wenzelm@6371
    34
  val have_lemmas_i: (bstring * theory attribute list) * (thm * theory attribute list) list
wenzelm@6371
    35
    -> theory -> theory
wenzelm@5915
    36
  val have_facts: (string * Args.src list) * (string * Args.src list) list
wenzelm@5915
    37
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    38
  val have_facts_i: (string * Proof.context attribute list) *
wenzelm@6371
    39
    (thm * Proof.context attribute list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    40
  val from_facts: (string * Args.src list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    41
  val from_facts_i: (thm * Proof.context attribute list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    42
  val chain: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    43
  val fix: (string * string option) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    44
  val fix_i: (string * typ) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    45
  val match_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    46
  val match_bind_i: (term list * term) list -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    47
  val theorem: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@6371
    48
  val theorem_i: bstring -> theory attribute list -> term * term list -> theory -> ProofHistory.T
wenzelm@5938
    49
  val lemma: string -> Args.src list -> string * string list -> theory -> ProofHistory.T
wenzelm@6371
    50
  val lemma_i: bstring -> theory attribute list -> term * term list -> theory -> ProofHistory.T
wenzelm@5938
    51
  val assume: string -> Args.src list -> (string * string list) list
wenzelm@5938
    52
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    53
  val assume_i: string -> Proof.context attribute list -> (term * term list) list
wenzelm@6371
    54
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    55
  val show: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    56
  val show_i: string -> Proof.context attribute list -> term * term list
wenzelm@6371
    57
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5938
    58
  val have: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@6371
    59
  val have_i: string -> Proof.context attribute list -> term * term list
wenzelm@6371
    60
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6501
    61
  val thus: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@6501
    62
  val thus_i: string -> Proof.context attribute list -> term * term list
wenzelm@6501
    63
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6501
    64
  val hence: string -> Args.src list -> string * string list -> ProofHistory.T -> ProofHistory.T
wenzelm@6501
    65
  val hence_i: string -> Proof.context attribute list -> term * term list
wenzelm@6501
    66
    -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    67
  val begin_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    68
  val next_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    69
  val end_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
    70
  val tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5882
    71
  val then_tac: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
    72
  val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
wenzelm@6404
    73
  val kill_proof: ProofHistory.T -> theory
wenzelm@6404
    74
  val global_qed_with: bstring option * Args.src list option -> Method.text option
wenzelm@6404
    75
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6404
    76
  val global_qed_with_i: bstring option * theory attribute list option -> Method.text option
wenzelm@6404
    77
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6404
    78
  val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
wenzelm@6404
    79
  val terminal_proof: Method.text -> Toplevel.transition -> Toplevel.transition
wenzelm@6404
    80
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@6404
    81
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@5830
    82
  val use_mltext: string -> theory option -> theory option
wenzelm@5830
    83
  val use_mltext_theory: string -> theory -> theory
wenzelm@5830
    84
  val use_setup: string -> theory -> theory
wenzelm@5830
    85
  val parse_ast_translation: string -> theory -> theory
wenzelm@5830
    86
  val parse_translation: string -> theory -> theory
wenzelm@5830
    87
  val print_translation: string -> theory -> theory
wenzelm@5830
    88
  val typed_print_translation: string -> theory -> theory
wenzelm@5830
    89
  val print_ast_translation: string -> theory -> theory
wenzelm@5830
    90
  val token_translation: string -> theory -> theory
wenzelm@5830
    91
  val add_oracle: bstring * string -> theory -> theory
wenzelm@6331
    92
  val begin_theory: string -> string list -> (string * bool) list -> theory
wenzelm@6331
    93
  val end_theory: theory -> theory
wenzelm@6246
    94
  val theory: string * string list * (string * bool) list
wenzelm@6246
    95
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
    96
  val context: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
    97
  val update_context: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5830
    98
end;
wenzelm@5830
    99
wenzelm@5830
   100
structure IsarThy: ISAR_THY =
wenzelm@5830
   101
struct
wenzelm@5830
   102
wenzelm@5830
   103
wenzelm@5830
   104
(** derived theory and proof operations **)
wenzelm@5830
   105
wenzelm@6371
   106
(* formal comments *)   (* FIXME dummy *)
wenzelm@5959
   107
wenzelm@5959
   108
fun add_text (txt:string) (thy:theory) = thy;
wenzelm@6354
   109
fun add_title title author date thy = thy;
wenzelm@5959
   110
val add_chapter = add_text;
wenzelm@5959
   111
val add_section = add_text;
wenzelm@5959
   112
val add_subsection = add_text;
wenzelm@5959
   113
val add_subsubsection = add_text;
wenzelm@5959
   114
wenzelm@5959
   115
wenzelm@5830
   116
(* axioms and defs *)
wenzelm@5830
   117
wenzelm@5915
   118
fun add_axms f args thy =
wenzelm@5915
   119
  f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
wenzelm@5915
   120
wenzelm@5915
   121
val add_axioms = add_axms PureThy.add_axioms;
wenzelm@6371
   122
val add_axioms_i = PureThy.add_axioms_i;
wenzelm@5915
   123
val add_defs = add_axms PureThy.add_defs;
wenzelm@6371
   124
val add_defs_i = PureThy.add_defs_i;
wenzelm@6371
   125
wenzelm@6371
   126
wenzelm@6371
   127
(* constdefs *)
wenzelm@6371
   128
wenzelm@6371
   129
fun gen_add_constdefs consts defs args thy =
wenzelm@6371
   130
  thy
wenzelm@6371
   131
  |> consts (map fst args)
wenzelm@6371
   132
  |> defs (map (fn ((c, _, mx), s) => ((Thm.def_name (Syntax.const_name c mx), s), [])) args);
wenzelm@6371
   133
wenzelm@6371
   134
val add_constdefs = gen_add_constdefs Theory.add_consts add_defs;
wenzelm@6371
   135
val add_constdefs_i = gen_add_constdefs Theory.add_consts_i add_defs_i;
wenzelm@5915
   136
wenzelm@5915
   137
wenzelm@5915
   138
(* theorems *)
wenzelm@5915
   139
wenzelm@5915
   140
fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
wenzelm@5915
   141
  f name (map (attrib x) more_srcs)
wenzelm@5915
   142
    (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
wenzelm@5915
   143
wenzelm@6371
   144
fun global_have_thmss x = gen_have_thmss PureThy.get_thms Attrib.global_attribute x;
wenzelm@5915
   145
wenzelm@6371
   146
fun local_have_thmss x =
wenzelm@6371
   147
  gen_have_thmss (ProofContext.get_thms o Proof.context_of)
wenzelm@6371
   148
    (Attrib.local_attribute o Proof.theory_of) x;
wenzelm@6371
   149
wenzelm@6371
   150
fun have_thmss_i f ((name, more_atts), th_atts) =
wenzelm@6371
   151
  f name more_atts (map (apfst single) th_atts);
wenzelm@6371
   152
wenzelm@6371
   153
fun have_lemss name atts = PureThy.have_thmss name (atts @ [Drule.tag_lemma]);
wenzelm@5915
   154
wenzelm@5915
   155
wenzelm@6371
   156
fun apply_theorems th_srcs = global_have_thmss PureThy.have_thmss ((None, []), th_srcs);
wenzelm@6371
   157
fun apply_theorems_i th_srcs = have_thmss_i PureThy.have_thmss ((None, []), th_srcs);
wenzelm@6371
   158
val have_theorems = #1 oo global_have_thmss (PureThy.have_thmss o Some);
wenzelm@6371
   159
val have_theorems_i = #1 oo have_thmss_i (PureThy.have_thmss o Some);
wenzelm@6371
   160
val have_lemmas = #1 oo global_have_thmss (have_lemss o Some);
wenzelm@6371
   161
val have_lemmas_i = #1 oo have_thmss_i (have_lemss o Some);
wenzelm@6371
   162
val have_facts = ProofHistory.apply o local_have_thmss Proof.have_thmss;
wenzelm@6371
   163
val have_facts_i = ProofHistory.apply o have_thmss_i Proof.have_thmss;
wenzelm@6371
   164
wenzelm@5915
   165
wenzelm@6371
   166
(* forward chaining *)
wenzelm@6371
   167
wenzelm@6371
   168
val from_facts =
wenzelm@6371
   169
  ProofHistory.apply o (Proof.chain oo curry (local_have_thmss Proof.have_thmss) ("", []));
wenzelm@6371
   170
wenzelm@6371
   171
val from_facts_i =
wenzelm@6371
   172
  ProofHistory.apply o (Proof.chain oo curry (have_thmss_i Proof.have_thmss) ("", []));
wenzelm@6371
   173
wenzelm@6371
   174
val chain = ProofHistory.apply Proof.chain;
wenzelm@5830
   175
wenzelm@5830
   176
wenzelm@5830
   177
(* context *)
wenzelm@5830
   178
wenzelm@5830
   179
val fix = ProofHistory.apply o Proof.fix;
wenzelm@6371
   180
val fix_i = ProofHistory.apply o Proof.fix_i;
wenzelm@5830
   181
val match_bind = ProofHistory.apply o Proof.match_bind;
wenzelm@6371
   182
val match_bind_i = ProofHistory.apply o Proof.match_bind_i;
wenzelm@5830
   183
wenzelm@5830
   184
wenzelm@5830
   185
(* statements *)
wenzelm@5830
   186
wenzelm@6371
   187
fun global_statement f name src s thy =
wenzelm@6371
   188
  ProofHistory.init (f name (map (Attrib.global_attribute thy) src) s thy);
wenzelm@5830
   189
wenzelm@6501
   190
fun global_statement_i f name atts t thy = ProofHistory.init (f name atts t thy);
wenzelm@6501
   191
wenzelm@6501
   192
fun local_statement do_open f g name src s = ProofHistory.apply_cond_open do_open (fn state =>
wenzelm@6501
   193
  f name (map (Attrib.local_attribute (Proof.theory_of state)) src) s (g state));
wenzelm@6371
   194
wenzelm@6501
   195
fun local_statement_i do_open f g name atts t =
wenzelm@6501
   196
  ProofHistory.apply_cond_open do_open (f name atts t o g);
wenzelm@5830
   197
wenzelm@6501
   198
val theorem   = global_statement Proof.theorem;
wenzelm@6371
   199
val theorem_i = global_statement_i Proof.theorem_i;
wenzelm@6501
   200
val lemma     = global_statement Proof.lemma;
wenzelm@6501
   201
val lemma_i   = global_statement_i Proof.lemma_i;
wenzelm@6501
   202
val assume    = local_statement false Proof.assume I;
wenzelm@6501
   203
val assume_i  = local_statement_i false Proof.assume_i I;
wenzelm@6501
   204
val show      = local_statement true Proof.show I;
wenzelm@6501
   205
val show_i    = local_statement_i true Proof.show_i I;
wenzelm@6501
   206
val have      = local_statement true Proof.have I;
wenzelm@6501
   207
val have_i    = local_statement_i true Proof.have_i I;
wenzelm@6501
   208
val thus      = local_statement true Proof.show Proof.chain;
wenzelm@6501
   209
val thus_i    = local_statement_i true Proof.show_i Proof.chain;
wenzelm@6501
   210
val hence     = local_statement true Proof.have Proof.chain;
wenzelm@6501
   211
val hence_i   = local_statement_i true Proof.have_i Proof.chain;
wenzelm@5830
   212
wenzelm@5830
   213
wenzelm@5830
   214
(* blocks *)
wenzelm@5830
   215
wenzelm@5830
   216
val begin_block = ProofHistory.apply_open Proof.begin_block;
wenzelm@5830
   217
val next_block = ProofHistory.apply Proof.next_block;
wenzelm@6404
   218
val end_block = ProofHistory.apply_close Proof.end_block;
wenzelm@5830
   219
wenzelm@5830
   220
wenzelm@5830
   221
(* backward steps *)
wenzelm@5830
   222
wenzelm@5830
   223
val tac = ProofHistory.applys o Method.tac;
wenzelm@5882
   224
val then_tac = ProofHistory.applys o Method.then_tac;
wenzelm@5830
   225
val proof = ProofHistory.applys o Method.proof;
wenzelm@6404
   226
wenzelm@6404
   227
wenzelm@6404
   228
(* local endings *)
wenzelm@6404
   229
wenzelm@6531
   230
(* FIXME
wenzelm@6531
   231
val print_proof = Toplevel.print oo Toplevel.proof;
wenzelm@6531
   232
*)
wenzelm@6531
   233
val print_proof = Toplevel.proof;
wenzelm@6531
   234
wenzelm@6531
   235
val local_qed = print_proof o ProofHistory.applys_close o Method.local_qed;
wenzelm@6531
   236
val local_terminal_proof = print_proof o ProofHistory.applys_close o Method.local_terminal_proof;
wenzelm@6531
   237
val local_immediate_proof = print_proof (ProofHistory.applys_close Method.local_immediate_proof);
wenzelm@6531
   238
val local_default_proof = print_proof (ProofHistory.applys_close Method.local_default_proof);
wenzelm@6404
   239
wenzelm@6404
   240
wenzelm@6404
   241
(* global endings *)
wenzelm@6404
   242
wenzelm@6404
   243
val kill_proof = Proof.theory_of o ProofHistory.current;
wenzelm@6404
   244
wenzelm@6404
   245
fun global_result finish = Toplevel.proof_to_theory (fn prf =>
wenzelm@6404
   246
  let
wenzelm@6404
   247
    val state = ProofHistory.current prf;
wenzelm@6404
   248
    val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
wenzelm@6531
   249
    val (thy, {kind, name, thm}) = finish state;
wenzelm@6404
   250
wenzelm@6404
   251
    val prt_result = Pretty.block
wenzelm@6404
   252
      [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
wenzelm@6404
   253
  in Pretty.writeln prt_result; thy end);
wenzelm@6404
   254
wenzelm@6404
   255
fun gen_global_qed_with prep_att (alt_name, raw_atts) opt_text state =
wenzelm@6404
   256
  let
wenzelm@6404
   257
    val thy = Proof.theory_of state;
wenzelm@6404
   258
    val alt_atts = apsome (map (prep_att thy)) raw_atts;
wenzelm@6404
   259
  in Method.global_qed alt_name alt_atts opt_text state end;
wenzelm@6404
   260
wenzelm@6404
   261
val global_qed_with = global_result oo gen_global_qed_with Attrib.global_attribute;
wenzelm@6404
   262
val global_qed_with_i = global_result oo gen_global_qed_with (K I);
wenzelm@6404
   263
val global_qed = global_qed_with (None, None);
wenzelm@6404
   264
wenzelm@6404
   265
val global_terminal_proof = global_result o Method.global_terminal_proof;
wenzelm@6404
   266
val global_immediate_proof = global_result Method.global_immediate_proof;
wenzelm@6404
   267
val global_default_proof = global_result Method.global_default_proof;
wenzelm@6404
   268
wenzelm@6404
   269
wenzelm@6404
   270
(* common endings *)
wenzelm@6404
   271
wenzelm@6404
   272
fun qed opt_text = local_qed opt_text o global_qed opt_text;
wenzelm@6404
   273
fun terminal_proof opt_text = local_terminal_proof opt_text o global_terminal_proof opt_text;
wenzelm@6404
   274
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@6404
   275
val default_proof = local_default_proof o global_default_proof;
wenzelm@5830
   276
wenzelm@5830
   277
wenzelm@5830
   278
(* use ML text *)
wenzelm@5830
   279
wenzelm@6331
   280
fun use_mltext txt opt_thy = #2 (Context.pass opt_thy (use_text false) txt);
wenzelm@6331
   281
fun use_mltext_theory txt thy = #2 (Context.pass_theory thy (use_text false) txt);
wenzelm@5830
   282
wenzelm@5830
   283
fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
wenzelm@5830
   284
wenzelm@5830
   285
fun use_let name body txt =
wenzelm@5830
   286
  use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
wenzelm@5830
   287
wenzelm@5830
   288
val use_setup =
wenzelm@5915
   289
  use_let "setup: (theory -> theory) list" "Library.apply setup";
wenzelm@5830
   290
wenzelm@5830
   291
wenzelm@5830
   292
(* translation functions *)
wenzelm@5830
   293
wenzelm@5830
   294
val parse_ast_translation =
wenzelm@5830
   295
  use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   296
    "Theory.add_trfuns (parse_ast_translation, [], [], [])";
wenzelm@5830
   297
wenzelm@5830
   298
val parse_translation =
wenzelm@5830
   299
  use_let "parse_translation: (string * (term list -> term)) list"
wenzelm@5830
   300
    "Theory.add_trfuns ([], parse_translation, [], [])";
wenzelm@5830
   301
wenzelm@5830
   302
val print_translation =
wenzelm@5830
   303
  use_let "print_translation: (string * (term list -> term)) list"
wenzelm@5830
   304
    "Theory.add_trfuns ([], [], print_translation, [])";
wenzelm@5830
   305
wenzelm@5830
   306
val print_ast_translation =
wenzelm@5830
   307
  use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   308
    "Theory.add_trfuns ([], [], [], print_ast_translation)";
wenzelm@5830
   309
wenzelm@5830
   310
val typed_print_translation =
wenzelm@5830
   311
  use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
wenzelm@5830
   312
    "Theory.add_trfunsT typed_print_translation";
wenzelm@5830
   313
wenzelm@5830
   314
val token_translation =
wenzelm@5830
   315
  use_let "token_translation: (string * string * (string -> string * int)) list"
wenzelm@5830
   316
    "Theory.add_tokentrfuns token_translation";
wenzelm@5830
   317
wenzelm@5830
   318
wenzelm@5830
   319
(* add_oracle *)
wenzelm@5830
   320
wenzelm@5830
   321
fun add_oracle (name, txt) =
wenzelm@5830
   322
  use_let
wenzelm@5830
   323
    "oracle: bstring * (Sign.sg * Object.T -> term)"
wenzelm@5830
   324
    "Theory.add_oracle oracle"
wenzelm@5830
   325
    ("(" ^ quote name ^ ", " ^ txt ^ ")");
wenzelm@5830
   326
wenzelm@5830
   327
wenzelm@6371
   328
(* theory init and exit *)      (* FIXME move? rearrange? *)
wenzelm@5830
   329
wenzelm@6331
   330
fun begin_theory name parents files =
wenzelm@6266
   331
  let
wenzelm@6331
   332
    val paths = map (apfst Path.unpack) files;
wenzelm@6331
   333
    val thy = ThyInfo.begin_theory name parents paths;
wenzelm@6331
   334
  in Present.begin_theory name parents paths; thy end;
wenzelm@5830
   335
wenzelm@6331
   336
wenzelm@6331
   337
(* FIXME
wenzelm@5830
   338
fun end_theory thy =
wenzelm@5830
   339
  let val thy' = PureThy.end_theory thy in
wenzelm@6331
   340
    Present.end_theory (PureThy.get_name thy');
wenzelm@6198
   341
    transform_error ThyInfo.put_theory thy'
wenzelm@5830
   342
      handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
wenzelm@5830
   343
  end;
wenzelm@6331
   344
*)
wenzelm@5830
   345
wenzelm@6331
   346
fun end_theory thy =
wenzelm@6331
   347
  (Present.end_theory (PureThy.get_name thy); ThyInfo.end_theory thy);
wenzelm@6331
   348
wenzelm@6331
   349
fun bg_theory (name, parents, files) () = begin_theory name parents files;
wenzelm@6331
   350
fun en_theory thy = (end_theory thy; ());
wenzelm@6331
   351
wenzelm@6331
   352
fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory;
wenzelm@6246
   353
wenzelm@6246
   354
wenzelm@6246
   355
(* context switch *)
wenzelm@6246
   356
wenzelm@6483
   357
fun switch_theory load s =
wenzelm@6483
   358
  Toplevel.init_theory (fn () => (the (#2 (Context.pass None load s)))) (K ());
wenzelm@6246
   359
wenzelm@6246
   360
val context = switch_theory ThyInfo.use_thy;
wenzelm@6246
   361
val update_context = switch_theory ThyInfo.update_thy;
wenzelm@6246
   362
wenzelm@5830
   363
wenzelm@5830
   364
end;