src/Pure/Isar/isar_thy.ML
author wenzelm
Fri, 05 Feb 1999 21:04:58 +0100
changeset 6246 0aa2e536bc20
parent 6198 7fa2deb92b39
child 6253 dbaf79ac2ff9
permissions -rw-r--r--
improved theory, context, update_context;
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
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6091
diff changeset
    48
  val immediate_proof: ProofHistory.T -> ProofHistory.T
5830
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
6246
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
    60
  val theory: string * string list * (string * bool) list
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
    61
    -> Toplevel.transition -> Toplevel.transition
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
    62
  val context: string -> Toplevel.transition -> Toplevel.transition
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
    63
  val update_context: string -> Toplevel.transition -> Toplevel.transition
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    64
end;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    65
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    66
structure IsarThy: ISAR_THY =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    67
struct
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    68
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    69
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    70
(** derived theory and proof operations **)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    71
5959
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    72
(* formal comments *)	(* FIXME dummy *)
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    73
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    74
fun add_text (txt:string) (thy:theory) = thy;
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    75
val add_chapter = add_text;
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    76
val add_section = add_text;
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    77
val add_subsection = add_text;
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    78
val add_subsubsection = add_text;
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    79
7af99477751b add_text, add_chapter etc.: dummy;
wenzelm
parents: 5938
diff changeset
    80
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    81
(* axioms and defs *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    82
5915
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    83
fun add_axms f args thy =
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    84
  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
    85
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    86
val add_axioms = add_axms PureThy.add_axioms;
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    87
val add_defs = add_axms PureThy.add_defs;
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
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    90
(* theorems *)
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    91
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    92
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
    93
  f name (map (attrib x) more_srcs)
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    94
    (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
    95
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
    96
5915
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    97
val have_theorems =
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
    98
  #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute PureThy.have_thmss;
5915
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
    99
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   100
val have_lemmas =
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   101
  #1 oo gen_have_thmss PureThy.get_thms Attrib.global_attribute
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   102
    (fn name => fn more_atts => PureThy.have_thmss name (more_atts @ [Drule.tag_lemma]));
5915
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
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   105
val have_thmss =
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   106
  gen_have_thmss (ProofContext.get_thms o Proof.context_of)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   107
    (Attrib.local_attribute o Proof.theory_of) Proof.have_thmss;
5915
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   108
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   109
val have_facts = ProofHistory.apply o have_thmss;
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   110
val from_facts = ProofHistory.apply o (Proof.chain oo curry have_thmss ("", []));
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   111
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   112
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   113
(* context *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   114
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   115
val fix = ProofHistory.apply o Proof.fix;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   116
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   117
val match_bind = ProofHistory.apply o Proof.match_bind;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   118
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   119
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   120
(* statements *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   121
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   122
fun global_statement f name tags s thy =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   123
  ProofHistory.init (f name (map (Attrib.global_attribute thy) tags) s thy);
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   124
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   125
fun local_statement f name tags s = ProofHistory.apply_open (fn state =>
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   126
  f name (map (Attrib.local_attribute (Proof.theory_of state)) tags) s state);
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   127
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   128
val theorem = global_statement Proof.theorem;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   129
val lemma = global_statement Proof.lemma;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   130
val assume = local_statement Proof.assume;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   131
val show = local_statement Proof.show;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   132
val have = local_statement Proof.have;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   133
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   134
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   135
(* forward chaining *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   136
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   137
val chain = ProofHistory.apply Proof.chain;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   138
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   139
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   140
(* end proof *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   141
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   142
fun qed_with (alt_name, alt_tags) prf =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   143
  let
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   144
    val state = ProofHistory.current prf;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   145
    val thy = Proof.theory_of state;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   146
    val alt_atts = apsome (map (Attrib.global_attribute thy)) alt_tags;
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   147
    val (thy', (kind, name, thm)) = Method.qed alt_name alt_atts state;
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   148
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   149
    val prt_result = Pretty.block
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5959
diff changeset
   150
      [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, Display.pretty_thm thm];
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   151
  in Pretty.writeln prt_result; thy end;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   152
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   153
val qed = qed_with (None, None);
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   154
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   155
val kill_proof = Proof.theory_of o ProofHistory.current;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   156
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   157
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   158
(* blocks *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   159
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   160
val begin_block = ProofHistory.apply_open Proof.begin_block;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   161
val next_block = ProofHistory.apply Proof.next_block;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   162
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   163
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   164
(* backward steps *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   165
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   166
val tac = ProofHistory.applys o Method.tac;
5882
c8096461f5c8 renamed tac / etac to refine / then_refine;
wenzelm
parents: 5830
diff changeset
   167
val then_tac = ProofHistory.applys o Method.then_tac;
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   168
val proof = ProofHistory.applys o Method.proof;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   169
val end_block = ProofHistory.applys_close Method.end_block;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   170
val terminal_proof = ProofHistory.applys_close o Method.terminal_proof;
6108
2c9ed58c30ba 'same' method, 'immediate' proof;
wenzelm
parents: 6091
diff changeset
   171
val immediate_proof = ProofHistory.applys_close Method.immediate_proof;
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   172
val default_proof = ProofHistory.applys_close Method.default_proof;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   173
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   174
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   175
(* use ML text *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   176
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   177
fun use_mltext txt opt_thy =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   178
  let
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   179
    val save_context = Context.get_context ();
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   180
    val _ = Context.set_context opt_thy;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   181
    val opt_exn = (transform_error (use_text false) txt; None) handle exn => Some exn;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   182
    val opt_thy' = Context.get_context ();
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   183
  in
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   184
    Context.set_context save_context;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   185
    (case opt_exn of
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   186
      None => opt_thy'
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   187
    | Some exn => raise exn)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   188
  end;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   189
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   190
fun use_mltext_theory txt thy =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   191
  (case use_mltext txt (Some thy) of
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   192
    Some thy' => thy'
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   193
  | None => error "Missing result ML theory context");
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   194
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   195
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   196
fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");");
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   197
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   198
fun use_let name body txt =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   199
  use_context ("let val " ^ name ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   200
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   201
val use_setup =
5915
66f4bde4c6e7 added have_theorems, have_lemmas, have_facts;
wenzelm
parents: 5882
diff changeset
   202
  use_let "setup: (theory -> theory) list" "Library.apply setup";
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   203
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   204
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   205
(* translation functions *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   206
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   207
val parse_ast_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   208
  use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   209
    "Theory.add_trfuns (parse_ast_translation, [], [], [])";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   210
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   211
val parse_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   212
  use_let "parse_translation: (string * (term list -> term)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   213
    "Theory.add_trfuns ([], parse_translation, [], [])";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   214
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   215
val print_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   216
  use_let "print_translation: (string * (term list -> term)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   217
    "Theory.add_trfuns ([], [], print_translation, [])";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   218
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   219
val print_ast_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   220
  use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   221
    "Theory.add_trfuns ([], [], [], print_ast_translation)";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   222
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   223
val typed_print_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   224
  use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   225
    "Theory.add_trfunsT typed_print_translation";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   226
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   227
val token_translation =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   228
  use_let "token_translation: (string * string * (string -> string * int)) list"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   229
    "Theory.add_tokentrfuns token_translation";
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   230
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   231
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   232
(* add_oracle *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   233
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   234
fun add_oracle (name, txt) =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   235
  use_let
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   236
    "oracle: bstring * (Sign.sg * Object.T -> term)"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   237
    "Theory.add_oracle oracle"
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   238
    ("(" ^ quote name ^ ", " ^ txt ^ ")");
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   239
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   240
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   241
(* theory init and exit *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   242
6246
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   243
fun begin_theory (name, parents, files) () =
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   244
  let val thy = ThyInfo.begin_theory name parents
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   245
  in seq ThyInfo.use (mapfilter (fn (x, true) => Some x | _ => None) files); thy end;
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   246
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   247
fun end_theory thy =
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   248
  let val thy' = PureThy.end_theory thy in
6198
7fa2deb92b39 ThyInfo.begin_theory;
wenzelm
parents: 6108
diff changeset
   249
    transform_error ThyInfo.put_theory thy'
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   250
      handle exn => raise PureThy.ROLLBACK (thy', Some exn)     (* FIXME !!?? *)
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   251
  end;
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   252
6246
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   253
fun theory spec = Toplevel.init_theory (begin_theory spec) end_theory;
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   254
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   255
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   256
(* context switch *)
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   257
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   258
fun switch_theory require name =
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   259
  Toplevel.init_theory
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   260
    (fn () => (Context.save require name; ThyInfo.get_theory name)) (K ());
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   261
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   262
val context = switch_theory ThyInfo.use_thy;
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   263
val update_context = switch_theory ThyInfo.update_thy;
0aa2e536bc20 improved theory, context, update_context;
wenzelm
parents: 6198
diff changeset
   264
5830
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   265
95b619c7289b Derived theory operations.
wenzelm
parents:
diff changeset
   266
end;