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