src/Pure/Isar/isar_thy.ML
author wenzelm
Mon Apr 17 14:07:00 2000 +0200 (2000-04-17 ago)
changeset 8724 ef7efded8fdc
parent 8681 957a5fe9b212
child 8731 085f0e32b9d6
permissions -rw-r--r--
global/local_path: comment;
added name space hide;
     1 (*  Title:      Pure/Isar/isar_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Pure/Isar derived theory operations.
     6 *)
     7 
     8 signature ISAR_THY =
     9 sig
    10   val add_header: Comment.text -> Toplevel.transition -> Toplevel.transition
    11   val add_chapter: Comment.text -> theory -> theory
    12   val add_section: Comment.text -> theory -> theory
    13   val add_subsection: Comment.text -> theory -> theory
    14   val add_subsubsection: Comment.text -> theory -> theory
    15   val add_text: Comment.text -> theory -> theory
    16   val add_text_raw: Comment.text -> theory -> theory
    17   val add_sect: Comment.text -> ProofHistory.T -> ProofHistory.T
    18   val add_subsect: Comment.text -> ProofHistory.T -> ProofHistory.T
    19   val add_subsubsect: Comment.text -> ProofHistory.T -> ProofHistory.T
    20   val add_txt: Comment.text -> ProofHistory.T -> ProofHistory.T
    21   val add_txt_raw: Comment.text -> ProofHistory.T -> ProofHistory.T
    22   val global_path: Comment.text -> theory -> theory
    23   val local_path: Comment.text -> theory -> theory
    24   val hide_space: (string * xstring list) * Comment.text -> theory -> theory
    25   val hide_space_i: (string * string list) * Comment.text -> theory -> theory
    26   val add_classes: (bclass * xclass list) list * Comment.text -> theory -> theory
    27   val add_classes_i: (bclass * class list) list * Comment.text  -> theory -> theory
    28   val add_classrel: (xclass * xclass) * Comment.text -> theory -> theory
    29   val add_classrel_i: (class * class) * Comment.text -> theory -> theory
    30   val add_defsort: xsort * Comment.text -> theory -> theory
    31   val add_defsort_i: sort * Comment.text -> theory -> theory
    32   val add_nonterminals: (bstring * Comment.text) list -> theory -> theory
    33   val add_tyabbrs: ((bstring * string list * string * mixfix) * Comment.text) list
    34     -> theory -> theory
    35   val add_tyabbrs_i: ((bstring * string list * typ * mixfix) * Comment.text) list
    36     -> theory -> theory
    37   val add_arities: ((xstring * xsort list * xsort) * Comment.text) list -> theory -> theory
    38   val add_arities_i: ((string * sort list * sort) * Comment.text) list -> theory -> theory
    39   val add_typedecl: (bstring * string list * mixfix) * Comment.text -> theory -> theory
    40   val add_consts: ((bstring * string * mixfix) * Comment.text) list -> theory -> theory
    41   val add_consts_i: ((bstring * typ * mixfix) * Comment.text) list -> theory -> theory
    42   val add_modesyntax: string * bool -> ((bstring * string * mixfix) * Comment.text) list
    43     -> theory -> theory
    44   val add_modesyntax_i: string * bool -> ((bstring * typ * mixfix) * Comment.text) list
    45     -> theory -> theory
    46   val add_trrules: ((xstring * string) Syntax.trrule * Comment.text) list -> theory -> theory
    47   val add_trrules_i: (Syntax.ast Syntax.trrule * Comment.text) list -> theory -> theory
    48   val add_judgment: (bstring * string * mixfix) * Comment.text -> theory -> theory
    49   val add_judgment_i: (bstring * typ * mixfix) * Comment.text -> theory -> theory
    50   val add_axioms: (((bstring * string) * Args.src list) * Comment.text) list -> theory -> theory
    51   val add_axioms_i: (((bstring * term) * theory attribute list) * Comment.text) list
    52     -> theory -> theory
    53   val add_defs: (((bstring * string) * Args.src list) * Comment.text) list -> theory -> theory
    54   val add_defs_i: (((bstring * term) * theory attribute list) * Comment.text) list
    55     -> theory -> theory
    56   val add_constdefs: (((bstring * string * mixfix) * Comment.text) * (string * Comment.text)) list
    57     -> theory -> theory
    58   val add_constdefs_i: (((bstring * typ * mixfix) * Comment.text) * (term * Comment.text)) list
    59     -> theory -> theory
    60   val apply_theorems: (xstring * Args.src list) list -> theory -> theory * thm list
    61   val apply_theorems_i: (thm * theory attribute list) list -> theory -> theory * thm list
    62   val have_theorems: ((bstring * Args.src list) * (xstring * Args.src list) list) * Comment.text
    63     -> theory -> theory
    64   val have_theorems_i: ((bstring * theory attribute list) * (thm * theory attribute list) list)
    65     * Comment.text -> theory -> theory
    66   val have_lemmas: ((bstring * Args.src list) * (xstring * Args.src list) list) * Comment.text
    67     -> theory -> theory
    68   val have_lemmas_i: ((bstring * theory attribute list) * (thm * theory attribute list) list)
    69     * Comment.text -> theory -> theory
    70   val have_facts: ((string * Args.src list) * (string * Args.src list) list) * Comment.text
    71     -> ProofHistory.T -> ProofHistory.T
    72   val have_facts_i: ((string * Proof.context attribute list) *
    73     (thm * Proof.context attribute list) list) * Comment.text -> ProofHistory.T -> ProofHistory.T
    74   val from_facts: (string * Args.src list) list * Comment.text -> ProofHistory.T -> ProofHistory.T
    75   val from_facts_i: (thm * Proof.context attribute list) list * Comment.text
    76     -> ProofHistory.T -> ProofHistory.T
    77   val with_facts: (string * Args.src list) list * Comment.text -> ProofHistory.T -> ProofHistory.T
    78   val with_facts_i: (thm * Proof.context attribute list) list * Comment.text
    79     -> ProofHistory.T -> ProofHistory.T
    80   val chain: Comment.text -> ProofHistory.T -> ProofHistory.T
    81   val fix: ((string list * string option) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    82   val fix_i: ((string list * typ option) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    83   val let_bind: ((string list * string) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    84   val let_bind_i: ((term list * term) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    85   val invoke_case: (string * Args.src list) * Comment.text -> ProofHistory.T -> ProofHistory.T
    86   val invoke_case_i: (string * Proof.context attribute list) * Comment.text
    87     -> ProofHistory.T -> ProofHistory.T
    88   val theorem: (bstring * Args.src list * (string * (string list * string list)))
    89     * Comment.text -> bool -> theory -> ProofHistory.T
    90   val theorem_i: (bstring * theory attribute list * (term * (term list * term list)))
    91     * Comment.text -> bool -> theory -> ProofHistory.T
    92   val lemma: (bstring * Args.src list * (string * (string list * string list)))
    93     * Comment.text -> bool -> theory -> ProofHistory.T
    94   val lemma_i: (bstring * theory attribute list * (term * (term list * term list)))
    95     * Comment.text -> bool -> theory -> ProofHistory.T
    96   val assume: ((string * Args.src list * (string * (string list * string list)) list)
    97     * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    98   val assume_i: ((string * Proof.context attribute list * (term * (term list * term list)) list)
    99     * Comment.text) list -> ProofHistory.T -> ProofHistory.T
   100   val presume: ((string * Args.src list * (string * (string list * string list)) list)
   101     * Comment.text) list -> ProofHistory.T -> ProofHistory.T
   102   val presume_i: ((string * Proof.context attribute list * (term * (term list * term list)) list)
   103     * Comment.text) list -> ProofHistory.T -> ProofHistory.T
   104   val local_def: (string * Args.src list * ((string * string option) * (string * string list)))
   105     * Comment.text -> ProofHistory.T -> ProofHistory.T
   106   val local_def_i: (string * Args.src list * ((string * typ option) * (term * term list)))
   107     * Comment.text -> ProofHistory.T -> ProofHistory.T
   108   val show: (string * Args.src list * (string * (string list * string list)))
   109     * Comment.text -> ProofHistory.T -> ProofHistory.T
   110   val show_i: (string * Proof.context attribute list * (term * (term list * term list)))
   111     * Comment.text -> ProofHistory.T -> ProofHistory.T
   112   val have: (string * Args.src list * (string * (string list * string list)))
   113     * Comment.text -> ProofHistory.T -> ProofHistory.T
   114   val have_i: (string * Proof.context attribute list * (term * (term list * term list)))
   115     * Comment.text -> ProofHistory.T -> ProofHistory.T
   116   val thus: (string * Args.src list * (string * (string list * string list)))
   117     * Comment.text -> ProofHistory.T -> ProofHistory.T
   118   val thus_i: (string * Proof.context attribute list * (term * (term list * term list)))
   119     * Comment.text -> ProofHistory.T -> ProofHistory.T
   120   val hence: (string * Args.src list * (string * (string list * string list)))
   121     * Comment.text -> ProofHistory.T -> ProofHistory.T
   122   val hence_i: (string * Proof.context attribute list * (term * (term list * term list)))
   123     * Comment.text -> ProofHistory.T -> ProofHistory.T
   124   val begin_block: ProofHistory.T -> ProofHistory.T
   125   val next_block: ProofHistory.T -> ProofHistory.T
   126   val end_block: ProofHistory.T -> ProofHistory.T
   127   val defer: int option * Comment.text -> ProofHistory.T -> ProofHistory.T
   128   val prefer: int * Comment.text -> ProofHistory.T -> ProofHistory.T
   129   val apply: Method.text * Comment.text -> ProofHistory.T -> ProofHistory.T
   130   val apply_end: Method.text * Comment.text -> ProofHistory.T -> ProofHistory.T
   131   val proof: (Comment.interest * (Method.text * Comment.interest) option) * Comment.text
   132     -> ProofHistory.T -> ProofHistory.T
   133   val qed: (Method.text * Comment.interest) option * Comment.text
   134     -> Toplevel.transition -> Toplevel.transition
   135   val terminal_proof: ((Method.text * Comment.interest) * (Method.text * Comment.interest) option)
   136     * Comment.text -> Toplevel.transition -> Toplevel.transition
   137   val immediate_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
   138   val default_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
   139   val skip_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
   140   val forget_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
   141   val get_thmss: (string * Args.src list) list -> Proof.state -> thm list
   142   val also: ((string * Args.src list) list * Comment.interest) option * Comment.text
   143     -> Toplevel.transition -> Toplevel.transition
   144   val also_i: (thm list * Comment.interest) option * Comment.text
   145     -> Toplevel.transition -> Toplevel.transition
   146   val finally: ((string * Args.src list) list * Comment.interest) option * Comment.text
   147     -> Toplevel.transition -> Toplevel.transition
   148   val finally_i: (thm list * Comment.interest) option * Comment.text
   149     -> Toplevel.transition -> Toplevel.transition
   150   val moreover: Comment.text -> Toplevel.transition -> Toplevel.transition
   151   val ultimately: Comment.text -> Toplevel.transition -> Toplevel.transition
   152   val parse_ast_translation: string -> theory -> theory
   153   val parse_translation: string -> theory -> theory
   154   val print_translation: string -> theory -> theory
   155   val typed_print_translation: string -> theory -> theory
   156   val print_ast_translation: string -> theory -> theory
   157   val token_translation: string -> theory -> theory
   158   val add_oracle: (bstring * string) * Comment.text -> theory -> theory
   159   val begin_theory: string -> string list -> (string * bool) list -> theory
   160   val end_theory: theory -> theory
   161   val kill_theory: string -> unit
   162   val theory: string * string list * (string * bool) list
   163     -> Toplevel.transition -> Toplevel.transition
   164   val init_context: ('a -> unit) -> 'a -> Toplevel.transition -> Toplevel.transition
   165   val context: string -> Toplevel.transition -> Toplevel.transition
   166 end;
   167 
   168 structure IsarThy: ISAR_THY =
   169 struct
   170 
   171 
   172 (** derived theory and proof operations **)
   173 
   174 (* markup commands *)
   175 
   176 fun add_header comment =
   177   Toplevel.keep (fn state => if Toplevel.is_toplevel state then () else raise Toplevel.UNDEF);
   178 
   179 fun add_text _ x = x;
   180 fun add_text_raw _ x = x;
   181 
   182 fun add_heading add present txt thy =
   183   (Context.setmp (Some thy) present (Comment.string_of txt); add txt thy);
   184 
   185 val add_chapter = add_heading add_text Present.section;
   186 val add_section = add_heading add_text Present.section;
   187 val add_subsection = add_heading add_text Present.subsection;
   188 val add_subsubsection = add_heading add_text Present.subsubsection;
   189 
   190 fun add_txt (_: Comment.text) = ProofHistory.apply I;
   191 val add_txt_raw = add_txt;
   192 val add_sect = add_txt;
   193 val add_subsect = add_txt;
   194 val add_subsubsect = add_txt;
   195 
   196 
   197 (* name spaces *)
   198 
   199 fun global_path comment_ignore = PureThy.global_path;
   200 fun local_path comment_ignore = PureThy.local_path;
   201 
   202 fun gen_hide f ((kind, names), comment_ignore) thy =
   203   if kind mem_string [Sign.classK, Sign.typeK, Sign.constK] then f (kind, names) thy
   204   else error ("Bad name space specification: " ^ quote kind);
   205 
   206 val hide_space = gen_hide Theory.hide_space;
   207 val hide_space_i = gen_hide Theory.hide_space_i;
   208 
   209 
   210 (* signature and syntax *)
   211 
   212 val add_classes = Theory.add_classes o Comment.ignore;
   213 val add_classes_i = Theory.add_classes_i o Comment.ignore;
   214 val add_classrel = Theory.add_classrel o single o Comment.ignore;
   215 val add_classrel_i = Theory.add_classrel_i o single o Comment.ignore;
   216 val add_defsort = Theory.add_defsort o Comment.ignore;
   217 val add_defsort_i = Theory.add_defsort_i o Comment.ignore;
   218 val add_nonterminals = Theory.add_nonterminals o map Comment.ignore;
   219 val add_tyabbrs = Theory.add_tyabbrs o map Comment.ignore;
   220 val add_tyabbrs_i = Theory.add_tyabbrs_i o map Comment.ignore;
   221 val add_arities = Theory.add_arities o map Comment.ignore;
   222 val add_arities_i = Theory.add_arities_i o map Comment.ignore;
   223 val add_typedecl = PureThy.add_typedecls o single o Comment.ignore;
   224 val add_consts = Theory.add_consts o map Comment.ignore;
   225 val add_consts_i = Theory.add_consts_i o map Comment.ignore;
   226 fun add_modesyntax mode = Theory.add_modesyntax mode o map Comment.ignore;
   227 fun add_modesyntax_i mode = Theory.add_modesyntax_i mode o map Comment.ignore;
   228 val add_trrules = Theory.add_trrules o map Comment.ignore;
   229 val add_trrules_i = Theory.add_trrules_i o map Comment.ignore;
   230 val add_judgment = AutoBind.add_judgment o Comment.ignore;
   231 val add_judgment_i = AutoBind.add_judgment_i o Comment.ignore;
   232 
   233 
   234 (* axioms and defs *)
   235 
   236 fun add_axms f args thy =
   237   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
   238 
   239 val add_axioms = add_axms (#1 oo PureThy.add_axioms) o map Comment.ignore;
   240 val add_axioms_i = (#1 oo PureThy.add_axioms_i) o map Comment.ignore;
   241 val add_defs = add_axms (#1 oo PureThy.add_defs) o map Comment.ignore;
   242 val add_defs_i = (#1 oo PureThy.add_defs_i) o map Comment.ignore;
   243 
   244 
   245 (* constdefs *)
   246 
   247 fun gen_add_constdefs consts defs args thy =
   248   thy
   249   |> consts (map (Comment.ignore o fst) args)
   250   |> defs (map (fn (((c, _, mx), _), (s, _)) =>
   251     (((Thm.def_name (Syntax.const_name c mx), s), []), Comment.none)) args);
   252 
   253 fun add_constdefs args = gen_add_constdefs Theory.add_consts add_defs args;
   254 fun add_constdefs_i args = gen_add_constdefs Theory.add_consts_i add_defs_i args;
   255 
   256 
   257 (* theorems *)
   258 
   259 fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
   260   f name (map (attrib x) more_srcs)
   261     (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
   262 
   263 fun global_have_thmss kind f (x as ((name, _), _)) thy =
   264   let val (thy', thms) = gen_have_thmss PureThy.get_thms Attrib.global_attribute f x thy in
   265     if kind <> "" then Context.setmp (Some thy') (Present.results kind name) thms else ();
   266     (thy', thms)
   267   end;
   268 
   269 fun local_have_thmss x =
   270   gen_have_thmss (ProofContext.get_thms o Proof.context_of)
   271     (Attrib.local_attribute o Proof.theory_of) x;
   272 
   273 fun gen_have_thmss_i f ((name, more_atts), th_atts) =
   274   f name more_atts (map (apfst single) th_atts);
   275 
   276 fun have_lemss name atts = PureThy.have_thmss name (atts @ [Drule.tag_lemma]);
   277 
   278 
   279 fun apply_theorems th_srcs = global_have_thmss "" PureThy.have_thmss (("", []), th_srcs);
   280 fun apply_theorems_i th_srcs = gen_have_thmss_i PureThy.have_thmss (("", []), th_srcs);
   281 val have_theorems = #1 oo (global_have_thmss "theorems" PureThy.have_thmss o Comment.ignore);
   282 val have_theorems_i = #1 oo (gen_have_thmss_i PureThy.have_thmss o Comment.ignore);
   283 val have_lemmas = #1 oo (global_have_thmss "lemmas" have_lemss o Comment.ignore);
   284 val have_lemmas_i = #1 oo (gen_have_thmss_i have_lemss o Comment.ignore);
   285 val have_facts = ProofHistory.apply o local_have_thmss (Proof.have_thmss []) o Comment.ignore;
   286 val have_facts_i = ProofHistory.apply o gen_have_thmss_i (Proof.have_thmss []) o Comment.ignore;
   287 
   288 
   289 (* forward chaining *)
   290 
   291 fun gen_from_facts f = ProofHistory.apply o (Proof.chain oo curry f ("", []));
   292 
   293 fun add_thmss name atts ths_atts state =
   294   Proof.have_thmss (Proof.the_facts state) name atts ths_atts state;
   295 
   296 val from_facts = gen_from_facts (local_have_thmss (Proof.have_thmss [])) o Comment.ignore;
   297 val from_facts_i = gen_from_facts (gen_have_thmss_i (Proof.have_thmss [])) o Comment.ignore;
   298 val with_facts = gen_from_facts (local_have_thmss add_thmss) o Comment.ignore;
   299 val with_facts_i = gen_from_facts (gen_have_thmss_i add_thmss) o Comment.ignore;
   300 
   301 fun chain comment_ignore = ProofHistory.apply Proof.chain;
   302 
   303 
   304 (* context *)
   305 
   306 val fix = ProofHistory.apply o Proof.fix o map Comment.ignore;
   307 val fix_i = ProofHistory.apply o Proof.fix_i o map Comment.ignore;
   308 val let_bind = ProofHistory.apply o Proof.let_bind o map Comment.ignore;
   309 val let_bind_i = ProofHistory.apply o Proof.let_bind_i o map Comment.ignore;
   310 
   311 fun invoke_case ((name, src), comment_ignore) = ProofHistory.apply (fn state =>
   312   Proof.invoke_case (name, map (Attrib.local_attribute (Proof.theory_of state)) src) state);
   313 val invoke_case_i = ProofHistory.apply o Proof.invoke_case o Comment.ignore;
   314 
   315 
   316 (* statements *)
   317 
   318 local
   319 
   320 fun global_statement f (name, src, s) int thy =
   321   ProofHistory.init (Toplevel.undo_limit int)
   322     (f name (map (Attrib.global_attribute thy) src) s thy);
   323 
   324 fun global_statement_i f (name, atts, t) int thy =
   325   ProofHistory.init (Toplevel.undo_limit int) (f name atts t thy);
   326 
   327 fun local_statement f g (name, src, s) = ProofHistory.apply (fn state =>
   328   f name (map (Attrib.local_attribute (Proof.theory_of state)) src) s (g state));
   329 
   330 fun local_statement_i f g (name, atts, t) = ProofHistory.apply (f name atts t o g);
   331 
   332 fun multi_local_attribute state (name, src, s) =
   333   (name, map (Attrib.local_attribute (Proof.theory_of state)) src, s);
   334 
   335 fun multi_statement f args = ProofHistory.apply (fn state =>
   336   f (map (multi_local_attribute state) args) state);
   337 
   338 fun multi_statement_i f args = ProofHistory.apply (f args);
   339 
   340 in
   341 
   342 val theorem     = global_statement Proof.theorem o Comment.ignore;
   343 val theorem_i   = global_statement_i Proof.theorem_i o Comment.ignore;
   344 val lemma       = global_statement Proof.lemma o Comment.ignore;
   345 val lemma_i     = global_statement_i Proof.lemma_i o Comment.ignore;
   346 val assume      = multi_statement Proof.assume o map Comment.ignore;
   347 val assume_i    = multi_statement_i Proof.assume_i o map Comment.ignore;
   348 val presume     = multi_statement Proof.presume o map Comment.ignore;
   349 val presume_i   = multi_statement_i Proof.presume_i o map Comment.ignore;
   350 val local_def   = local_statement LocalDefs.def I o Comment.ignore;
   351 val local_def_i = local_statement LocalDefs.def_i I o Comment.ignore;
   352 val show        = local_statement (Proof.show Seq.single) I o Comment.ignore;
   353 val show_i      = local_statement_i (Proof.show_i Seq.single) I o Comment.ignore;
   354 val have        = local_statement (Proof.have Seq.single) I o Comment.ignore;
   355 val have_i      = local_statement_i (Proof.have_i Seq.single) I o Comment.ignore;
   356 val thus        = local_statement (Proof.show Seq.single) Proof.chain o Comment.ignore;
   357 val thus_i      = local_statement_i (Proof.show_i Seq.single) Proof.chain o Comment.ignore;
   358 val hence       = local_statement (Proof.have Seq.single) Proof.chain o Comment.ignore;
   359 val hence_i     = local_statement_i (Proof.have_i Seq.single) Proof.chain o Comment.ignore;
   360 
   361 end;
   362 
   363 
   364 (* blocks *)
   365 
   366 val begin_block = ProofHistory.apply Proof.begin_block;
   367 val next_block = ProofHistory.apply Proof.next_block;
   368 val end_block = ProofHistory.applys Proof.end_block;
   369 
   370 
   371 (* shuffle state *)
   372 
   373 fun shuffle meth i = Method.refine (Method.Basic (K (meth i))) o Proof.assert_no_chain;
   374 
   375 fun defer (i, comment_ignore) = ProofHistory.applys (shuffle Method.defer i);
   376 fun prefer (i, comment_ignore) = ProofHistory.applys (shuffle Method.prefer i);
   377 
   378 
   379 (* backward steps *)
   380 
   381 fun apply (m, comment_ignore) = ProofHistory.applys (Method.refine m o Proof.assert_backward);
   382 fun apply_end (m, comment_ignore) =
   383   ProofHistory.applys (Method.refine_end m o Proof.assert_forward);
   384 
   385 val proof = ProofHistory.applys o Method.proof o
   386   apsome Comment.ignore_interest o Comment.ignore_interest' o Comment.ignore;
   387 
   388 
   389 (* print result *)
   390 
   391 local
   392 
   393 fun pretty_result {kind, name, thm} =
   394   Pretty.block [Pretty.str (kind ^ (if name = "" then "" else " " ^ name) ^ ":"),
   395     Pretty.fbrk, Proof.pretty_thm thm];
   396 
   397 fun pretty_rule thm =
   398   Pretty.block [Pretty.str "trying to solve goal by rule:", Pretty.fbrk, Proof.pretty_thm thm];
   399 
   400 in
   401 
   402 val print_result = Pretty.writeln o pretty_result;
   403 
   404 fun cond_print_result_rule int =
   405   if int then (print_result, Pretty.writeln o pretty_rule)
   406   else (K (), K ());
   407 
   408 fun proof'' f = Toplevel.proof' (f o cond_print_result_rule);
   409 
   410 end;
   411 
   412 
   413 (* local endings *)
   414 
   415 val local_qed =
   416   proof'' o (ProofHistory.applys oo Method.local_qed) o apsome Comment.ignore_interest;
   417 
   418 fun ignore_interests (x, y) = (Comment.ignore_interest x, apsome Comment.ignore_interest y);
   419 
   420 val local_terminal_proof =
   421   proof'' o (ProofHistory.applys oo Method.local_terminal_proof) o ignore_interests;
   422 
   423 val local_immediate_proof = proof'' (ProofHistory.applys o Method.local_immediate_proof);
   424 val local_default_proof = proof'' (ProofHistory.applys o Method.local_default_proof);
   425 val local_skip_proof = proof'' (ProofHistory.applys o SkipProof.local_skip_proof);
   426 
   427 
   428 (* global endings *)
   429 
   430 fun global_result finish = Toplevel.proof_to_theory (fn prf =>
   431   let
   432     val state = ProofHistory.current prf;
   433     val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
   434     val (thy, res as {kind, name, thm}) = finish state;
   435   in print_result res; Context.setmp (Some thy) (Present.result kind name) thm; thy end);
   436 
   437 val global_qed = global_result o Method.global_qed o apsome Comment.ignore_interest;
   438 val global_terminal_proof = global_result o Method.global_terminal_proof o ignore_interests;
   439 val global_immediate_proof = global_result Method.global_immediate_proof;
   440 val global_default_proof = global_result Method.global_default_proof;
   441 val global_skip_proof = global_result SkipProof.global_skip_proof;
   442 
   443 
   444 (* common endings *)
   445 
   446 fun qed (meth, comment) = local_qed meth o global_qed meth;
   447 fun terminal_proof (meths, comment) = local_terminal_proof meths o global_terminal_proof meths;
   448 fun immediate_proof comment = local_immediate_proof o global_immediate_proof;
   449 fun default_proof comment = local_default_proof o global_default_proof;
   450 fun skip_proof comment = local_skip_proof o global_skip_proof;
   451 
   452 fun forget_proof comment = Toplevel.proof_to_theory (Proof.theory_of o ProofHistory.current);
   453 
   454 
   455 (* calculational proof commands *)
   456 
   457 local
   458 
   459 fun cond_print_calc int thms =
   460   if int then Pretty.writeln (Pretty.big_list "calculation:" (map Proof.pretty_thm thms))
   461   else ();
   462 
   463 fun proof''' f = Toplevel.proof' (f o cond_print_calc);
   464 
   465 fun gen_calc get f (args, _) prt state =
   466   f (apsome (fn (srcs, _) => get srcs state) args) prt state;
   467 
   468 in
   469 
   470 fun get_thmss srcs = Proof.the_facts o local_have_thmss (Proof.have_thmss []) (("", []), srcs); 
   471 fun get_thmss_i thms _ = thms;
   472 
   473 fun also x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.also x);
   474 fun also_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.also x);
   475 fun finally x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.finally x);
   476 fun finally_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.finally x);
   477 fun moreover comment = proof''' (ProofHistory.apply o Calculation.moreover);
   478 fun ultimately comment = proof''' (ProofHistory.apply o Calculation.ultimately);
   479 
   480 end;
   481 
   482 
   483 (* translation functions *)
   484 
   485 val parse_ast_translation =
   486   Context.use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   487     "Theory.add_trfuns (parse_ast_translation, [], [], [])";
   488 
   489 val parse_translation =
   490   Context.use_let "parse_translation: (string * (term list -> term)) list"
   491     "Theory.add_trfuns ([], parse_translation, [], [])";
   492 
   493 val print_translation =
   494   Context.use_let "print_translation: (string * (term list -> term)) list"
   495     "Theory.add_trfuns ([], [], print_translation, [])";
   496 
   497 val print_ast_translation =
   498   Context.use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
   499     "Theory.add_trfuns ([], [], [], print_ast_translation)";
   500 
   501 val typed_print_translation =
   502   Context.use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
   503     "Theory.add_trfunsT typed_print_translation";
   504 
   505 val token_translation =
   506   Context.use_let "token_translation: (string * string * (string -> string * int)) list"
   507     "Theory.add_tokentrfuns token_translation";
   508 
   509 
   510 (* add_oracle *)
   511 
   512 fun add_oracle ((name, txt), comment_ignore) =
   513   Context.use_let
   514     "oracle: bstring * (Sign.sg * Object.T -> term)"
   515     "Theory.add_oracle oracle"
   516     ("(" ^ quote name ^ ", " ^ txt ^ ")");
   517 
   518 
   519 (* theory init and exit *)
   520 
   521 fun gen_begin_theory bg name parents files =
   522   let
   523     val paths = map (apfst Path.unpack) files;
   524     val thy = bg name parents paths;
   525   in Present.begin_theory name parents paths thy end;
   526 
   527 val begin_theory = gen_begin_theory ThyInfo.begin_theory;
   528 val begin_update_theory = gen_begin_theory ThyInfo.begin_update_theory;
   529 
   530 fun end_theory thy =
   531   if ThyInfo.check_known_thy (PureThy.get_name thy) then ThyInfo.end_theory thy
   532   else thy;
   533 
   534 val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy;
   535 
   536 
   537 fun bg_theory (name, parents, files) int =
   538   (if int then begin_update_theory else begin_theory) name parents files;
   539 
   540 fun en_theory thy = (end_theory thy; ());
   541 
   542 fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory (kill_theory o PureThy.get_name);
   543 
   544 
   545 (* context switch *)
   546 
   547 fun fetch_context f x =
   548   (case Context.pass None f x of
   549     ((), None) => raise Toplevel.UNDEF
   550   | ((), Some thy) => thy);
   551 
   552 fun init_context f x = Toplevel.init_theory (fn _ => fetch_context f x) (K ()) (K ());
   553 
   554 val context = init_context (ThyInfo.quiet_update_thy true);
   555 
   556 
   557 end;