src/Pure/Isar/isar_syn.ML
author ballarin
Fri Apr 02 14:08:30 2004 +0200 (2004-04-02)
changeset 14508 859b11514537
parent 14223 0ee05eef881b
child 14528 1457584110ac
permissions -rw-r--r--
Experimental command for instantiation of locales in proof contexts:
instantiate <label>: <loc>
     1 (*  Title:      Pure/Isar/isar_syn.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Isar/Pure outer syntax.
     7 *)
     8 
     9 signature ISAR_SYN =
    10 sig
    11   val keywords: string list
    12   val parsers: OuterSyntax.parser list
    13 end;
    14 
    15 structure IsarSyn: ISAR_SYN =
    16 struct
    17 
    18 structure P = OuterParse and K = OuterSyntax.Keyword;
    19 
    20 
    21 (** init and exit **)
    22 
    23 val theoryP =
    24   OuterSyntax.command "theory" "begin theory" K.thy_begin
    25     (ThyHeader.args >> (Toplevel.print oo IsarThy.theory));
    26 
    27 val end_excursionP =
    28   OuterSyntax.command "end" "end current excursion" K.thy_end
    29     (Scan.succeed (Toplevel.print o Toplevel.exit));
    30 
    31 val contextP =
    32   OuterSyntax.improper_command "context" "switch theory context" K.thy_switch
    33     (P.name >> (Toplevel.print oo IsarThy.context));
    34 
    35 
    36 
    37 (** markup commands **)
    38 
    39 val headerP = OuterSyntax.markup_command IsarOutput.Markup "header" "theory header" K.diag
    40   (P.position P.text >> IsarCmd.add_header);
    41 
    42 val chapterP = OuterSyntax.markup_command IsarOutput.Markup "chapter" "chapter heading"
    43   K.thy_heading (P.position P.text >> IsarCmd.add_chapter);
    44 
    45 val sectionP = OuterSyntax.markup_command IsarOutput.Markup "section" "section heading"
    46   K.thy_heading (P.position P.text >> IsarCmd.add_section);
    47 
    48 val subsectionP = OuterSyntax.markup_command IsarOutput.Markup "subsection" "subsection heading"
    49   K.thy_heading (P.position P.text >> IsarCmd.add_subsection);
    50 
    51 val subsubsectionP =
    52   OuterSyntax.markup_command IsarOutput.Markup "subsubsection" "subsubsection heading"
    53   K.thy_heading (P.position P.text >> IsarCmd.add_subsubsection);
    54 
    55 val textP = OuterSyntax.markup_command IsarOutput.MarkupEnv "text" "formal comment (theory)"
    56   K.thy_decl (P.position P.text >> IsarCmd.add_text);
    57 
    58 val text_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "text_raw"
    59   "raw document preparation text" K.thy_decl
    60   (P.position P.text >> IsarCmd.add_text_raw);
    61 
    62 val sectP = OuterSyntax.markup_command IsarOutput.Markup "sect" "formal comment (proof)"
    63   K.prf_heading (P.position P.text >> IsarCmd.add_sect);
    64 
    65 val subsectP = OuterSyntax.markup_command IsarOutput.Markup "subsect" "formal comment (proof)"
    66   K.prf_heading (P.position P.text >> IsarCmd.add_subsect);
    67 
    68 val subsubsectP = OuterSyntax.markup_command IsarOutput.Markup "subsubsect"
    69   "formal comment (proof)" K.prf_heading
    70   (P.position P.text >> IsarCmd.add_subsubsect);
    71 
    72 val txtP = OuterSyntax.markup_command IsarOutput.MarkupEnv "txt" "formal comment (proof)"
    73   K.prf_decl (P.position P.text >> IsarCmd.add_txt);
    74 
    75 val txt_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "txt_raw"
    76   "raw document preparation text (proof)" K.prf_decl
    77   (P.position P.text >> IsarCmd.add_txt_raw);
    78 
    79 
    80 
    81 (** theory sections **)
    82 
    83 (* classes and sorts *)
    84 
    85 val classesP =
    86   OuterSyntax.command "classes" "declare type classes" K.thy_decl
    87     (Scan.repeat1 (P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    88         P.!!! (P.list1 P.xname)) []) >> (Toplevel.theory o Theory.add_classes));
    89 
    90 val classrelP =
    91   OuterSyntax.command "classrel" "state inclusion of type classes (axiomatic!)" K.thy_decl
    92     (P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname)
    93     >> (Toplevel.theory o Theory.add_classrel o single));
    94 
    95 val defaultsortP =
    96   OuterSyntax.command "defaultsort" "declare default sort" K.thy_decl
    97     (P.sort >> (Toplevel.theory o Theory.add_defsort));
    98 
    99 
   100 (* types *)
   101 
   102 val typedeclP =
   103   OuterSyntax.command "typedecl" "type declaration" K.thy_decl
   104     (P.type_args -- P.name -- P.opt_infix >> (fn ((args, a), mx) =>
   105       Toplevel.theory (PureThy.add_typedecls [(a, args, mx)])));
   106 
   107 val typeabbrP =
   108   OuterSyntax.command "types" "declare type abbreviations" K.thy_decl
   109     (Scan.repeat1
   110       (P.type_args -- P.name -- (P.$$$ "=" |-- P.!!! (P.typ -- P.opt_infix')))
   111       >> (Toplevel.theory o Theory.add_tyabbrs o
   112         map (fn ((args, a), (T, mx)) => (a, args, T, mx))));
   113 
   114 val nontermP =
   115   OuterSyntax.command "nonterminals" "declare types treated as grammar nonterminal symbols"
   116     K.thy_decl (Scan.repeat1 P.name >> (Toplevel.theory o Theory.add_nonterminals));
   117 
   118 val aritiesP =
   119   OuterSyntax.command "arities" "state type arities (axiomatic!)" K.thy_decl
   120     (Scan.repeat1 (P.xname -- (P.$$$ "::" |-- P.!!! P.arity) >> P.triple2)
   121       >> (Toplevel.theory o Theory.add_arities));
   122 
   123 
   124 (* consts and syntax *)
   125 
   126 val judgmentP =
   127   OuterSyntax.command "judgment" "declare object-logic judgment" K.thy_decl
   128     (P.const >> (Toplevel.theory o ObjectLogic.add_judgment));
   129 
   130 val constsP =
   131   OuterSyntax.command "consts" "declare constants" K.thy_decl
   132     (Scan.repeat1 P.const >> (Toplevel.theory o Theory.add_consts));
   133 
   134 val opt_overloaded =
   135   Scan.optional (P.$$$ "(" |-- P.!!! ((P.$$$ "overloaded" >> K true) --| P.$$$ ")")) false;
   136 
   137 val finalconstsP =
   138   OuterSyntax.command "finalconsts" "declare constants as final" K.thy_decl
   139     (opt_overloaded -- Scan.repeat1 P.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   140 
   141 val mode_spec =
   142   (P.$$$ "output" >> K ("", false)) || P.name -- Scan.optional (P.$$$ "output" >> K false) true;
   143 
   144 val opt_mode = Scan.optional (P.$$$ "(" |-- P.!!! (mode_spec --| P.$$$ ")")) ("", true);
   145 
   146 val syntaxP =
   147   OuterSyntax.command "syntax" "declare syntactic constants" K.thy_decl
   148     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Theory.add_modesyntax));
   149 
   150 
   151 (* translations *)
   152 
   153 val trans_pat =
   154   Scan.optional (P.$$$ "(" |-- P.!!! (P.xname --| P.$$$ ")")) "logic" -- P.string;
   155 
   156 fun trans_arrow toks =
   157   ((P.$$$ "\\<rightharpoonup>" || P.$$$ "=>") >> K Syntax.ParseRule ||
   158     (P.$$$ "\\<leftharpoondown>" || P.$$$ "<=") >> K Syntax.PrintRule ||
   159     (P.$$$ "\\<rightleftharpoons>" || P.$$$ "==") >> K Syntax.ParsePrintRule) toks;
   160 
   161 val trans_line =
   162   trans_pat -- P.!!! (trans_arrow -- trans_pat)
   163     >> (fn (left, (arr, right)) => arr (left, right));
   164 
   165 val translationsP =
   166   OuterSyntax.command "translations" "declare syntax translation rules" K.thy_decl
   167     (Scan.repeat1 trans_line >> (Toplevel.theory o Theory.add_trrules));
   168 
   169 
   170 (* axioms and definitions *)
   171 
   172 val axiomsP =
   173   OuterSyntax.command "axioms" "state arbitrary propositions (axiomatic!)" K.thy_decl
   174     (Scan.repeat1 P.spec_name >> (Toplevel.theory o IsarThy.add_axioms));
   175 
   176 val defsP =
   177   OuterSyntax.command "defs" "define constants" K.thy_decl
   178     (opt_overloaded -- Scan.repeat1 P.spec_name >> (Toplevel.theory o IsarThy.add_defs));
   179 
   180 val constdefsP =
   181   OuterSyntax.command "constdefs" "declare and define constants" K.thy_decl
   182     (Scan.repeat1 (P.const -- P.term) >> (Toplevel.theory o IsarThy.add_constdefs));
   183 
   184 
   185 (* theorems *)
   186 
   187 val name_facts = P.and_list1 (P.opt_thm_name "=" -- P.xthms1);
   188 
   189 fun theorems kind = P.locale_target -- name_facts
   190   >> uncurry (#1 ooo IsarThy.smart_theorems kind);
   191 
   192 val theoremsP =
   193   OuterSyntax.command "theorems" "define theorems" K.thy_decl
   194     (theorems Drule.theoremK >> Toplevel.theory);
   195 
   196 val lemmasP =
   197   OuterSyntax.command "lemmas" "define lemmas" K.thy_decl
   198     (theorems Drule.lemmaK >> Toplevel.theory);
   199 
   200 val declareP =
   201   OuterSyntax.command "declare" "declare theorems (improper)" K.thy_script
   202     (P.locale_target -- (P.and_list1 P.xthms1 >> flat)
   203       >> (Toplevel.theory o uncurry IsarThy.declare_theorems));
   204 
   205 
   206 (* name space entry path *)
   207 
   208 val globalP =
   209   OuterSyntax.command "global" "disable prefixing of theory name" K.thy_decl
   210     (Scan.succeed (Toplevel.theory PureThy.global_path));
   211 
   212 val localP =
   213   OuterSyntax.command "local" "enable prefixing of theory name" K.thy_decl
   214     (Scan.succeed (Toplevel.theory PureThy.local_path));
   215 
   216 val hideP =
   217   OuterSyntax.command "hide" "hide names from given name space" K.thy_decl
   218     (P.name -- Scan.repeat1 P.xname >> (Toplevel.theory o IsarThy.hide_space));
   219 
   220 
   221 (* use ML text *)
   222 
   223 val useP =
   224   OuterSyntax.command "use" "eval ML text from file" K.diag
   225     (P.name >> (Toplevel.no_timing oo IsarCmd.use));
   226 
   227 val mlP =
   228   OuterSyntax.command "ML" "eval ML text (diagnostic)" K.diag
   229     (P.text >> IsarCmd.use_mltext true);
   230 
   231 val ml_commandP =
   232   OuterSyntax.command "ML_command" "eval ML text" K.diag
   233     (P.text >> (Toplevel.no_timing oo IsarCmd.use_mltext false));
   234 
   235 val ml_setupP =
   236   OuterSyntax.command "ML_setup" "eval ML text (may change theory)" K.thy_decl
   237     (P.text >> IsarCmd.use_mltext_theory);
   238 
   239 val setupP =
   240   OuterSyntax.command "setup" "apply ML theory setup" K.thy_decl
   241     (P.text >> (Toplevel.theory o Context.use_setup));
   242 
   243 val method_setupP =
   244   OuterSyntax.command "method_setup" "define proof method in ML" K.thy_decl
   245     (((P.name -- P.!!! (P.$$$ "=" |-- P.text -- P.text) >> P.triple2))
   246       >> (Toplevel.theory o IsarThy.method_setup));
   247 
   248 
   249 (* translation functions *)
   250 
   251 val parse_ast_translationP =
   252   OuterSyntax.command "parse_ast_translation" "install parse ast translation functions" K.thy_decl
   253     (P.text >> (Toplevel.theory o IsarThy.parse_ast_translation));
   254 
   255 val parse_translationP =
   256   OuterSyntax.command "parse_translation" "install parse translation functions" K.thy_decl
   257     (P.text >> (Toplevel.theory o IsarThy.parse_translation));
   258 
   259 val print_translationP =
   260   OuterSyntax.command "print_translation" "install print translation functions" K.thy_decl
   261     (P.text >> (Toplevel.theory o IsarThy.print_translation));
   262 
   263 val typed_print_translationP =
   264   OuterSyntax.command "typed_print_translation" "install typed print translation functions"
   265     K.thy_decl (P.text >> (Toplevel.theory o IsarThy.typed_print_translation));
   266 
   267 val print_ast_translationP =
   268   OuterSyntax.command "print_ast_translation" "install print ast translation functions" K.thy_decl
   269     (P.text >> (Toplevel.theory o IsarThy.print_ast_translation));
   270 
   271 val token_translationP =
   272   OuterSyntax.command "token_translation" "install token translation functions" K.thy_decl
   273     (P.text >> (Toplevel.theory o IsarThy.token_translation));
   274 
   275 
   276 (* oracles *)
   277 
   278 val oracleP =
   279   OuterSyntax.command "oracle" "install oracle" K.thy_decl
   280     ((P.name --| P.$$$ "=") -- P.text >> (Toplevel.theory o IsarThy.add_oracle));
   281 
   282 
   283 (* locales *)
   284 
   285 val locale_val =
   286   (P.locale_expr --
   287     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 P.locale_element)) [] ||
   288   Scan.repeat1 P.locale_element >> pair Locale.empty);
   289 
   290 val locale_pred =
   291   Scan.optional (P.$$$ "(" |-- P.!!! ((P.$$$ "open" >> K false) --| P.$$$ ")")) true;
   292 
   293 val localeP =
   294   OuterSyntax.command "locale" "define named proof context" K.thy_decl
   295     (locale_pred -- P.name -- Scan.optional (P.$$$ "=" |-- P.!!! locale_val) (Locale.empty, [])
   296       >> (Toplevel.theory o IsarThy.add_locale o (fn ((x, y), (z, w)) => (x, y, z, w))));
   297 
   298 
   299 
   300 (** proof commands **)
   301 
   302 (* statements *)
   303 
   304 val statement = P.and_list1 (P.opt_thm_name ":" -- Scan.repeat1 P.propp);
   305 val general_statement =
   306   statement >> pair [] || Scan.repeat P.locale_element -- (P.$$$ "shows" |-- statement);
   307 
   308 fun gen_theorem k =
   309   OuterSyntax.command k ("state " ^ k) K.thy_goal
   310     (P.locale_target -- Scan.optional (P.opt_thm_name ":" --|
   311       Scan.ahead (P.locale_keyword || P.$$$ "shows")) ("", []) --
   312       general_statement >> (fn ((x, y), (z, w)) =>
   313       (Toplevel.print o Toplevel.theory_to_proof (IsarThy.smart_multi_theorem k x y z w))));
   314 
   315 val theoremP = gen_theorem Drule.theoremK;
   316 val lemmaP = gen_theorem Drule.lemmaK;
   317 val corollaryP = gen_theorem Drule.corollaryK;
   318 
   319 val showP =
   320   OuterSyntax.command "show" "state local goal, solving current obligation" K.prf_goal
   321     (statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.show));
   322 
   323 val haveP =
   324   OuterSyntax.command "have" "state local goal" K.prf_goal
   325     (statement >> ((Toplevel.print oo Toplevel.proof) o  IsarThy.have));
   326 
   327 val thusP =
   328   OuterSyntax.command "thus" "abbreviates \"then show\"" K.prf_goal
   329     (statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.thus));
   330 
   331 val henceP =
   332   OuterSyntax.command "hence" "abbreviates \"then have\"" K.prf_goal
   333     (statement >> ((Toplevel.print oo Toplevel.proof) o IsarThy.hence));
   334 
   335 
   336 (* facts *)
   337 
   338 val facts = P.and_list1 P.xthms1;
   339 
   340 val thenP =
   341   OuterSyntax.command "then" "forward chaining" K.prf_chain
   342     (Scan.succeed (Toplevel.print o (Toplevel.proof IsarThy.chain)));
   343 
   344 val fromP =
   345   OuterSyntax.command "from" "forward chaining from given facts" K.prf_chain
   346     (facts >> (Toplevel.print oo (Toplevel.proof o IsarThy.from_facts)));
   347 
   348 val withP =
   349   OuterSyntax.command "with" "forward chaining from given and current facts" K.prf_chain
   350     (facts >> (Toplevel.print oo (Toplevel.proof o IsarThy.with_facts)));
   351 
   352 val noteP =
   353   OuterSyntax.command "note" "define facts" K.prf_decl
   354     (name_facts >> (Toplevel.print oo (Toplevel.proof o IsarThy.have_facts)));
   355 
   356 val usingP =
   357   OuterSyntax.command "using" "augment goal facts" K.prf_decl
   358     (facts >> (Toplevel.print oo (Toplevel.proof o IsarThy.using_facts)));
   359 
   360 val instantiateP =
   361   OuterSyntax.command "instantiate" "instantiate locale" K.prf_decl
   362     (P.name --| P.$$$ ":" -- P.xname
   363 (*    (P.xname -- (P.$$$ "[" |-- P.name --| P.$$$ "]") *)
   364       >> (Toplevel.print oo (Toplevel.proof o IsarThy.instantiate_locale)));
   365 
   366 
   367 (* proof context *)
   368 
   369 val fixP =
   370   OuterSyntax.command "fix" "fix variables (Skolem constants)" K.prf_asm
   371     (P.and_list1 (Scan.repeat1 P.name -- Scan.option (P.$$$ "::" |-- P.typ))
   372       >> (Toplevel.print oo (Toplevel.proof o IsarThy.fix)));
   373 
   374 val assumeP =
   375   OuterSyntax.command "assume" "assume propositions" K.prf_asm
   376     (statement >> (Toplevel.print oo (Toplevel.proof o IsarThy.assume)));
   377 
   378 val presumeP =
   379   OuterSyntax.command "presume" "assume propositions, to be established later" K.prf_asm
   380     (statement >> (Toplevel.print oo (Toplevel.proof o IsarThy.presume)));
   381 
   382 val defP =
   383   OuterSyntax.command "def" "local definition" K.prf_asm
   384     (P.opt_thm_name ":" -- (P.name -- ((P.$$$ "\\<equiv>" || P.$$$ "==") |-- P.!!! P.termp))
   385       >> (Toplevel.print oo (Toplevel.proof o IsarThy.local_def)));
   386 
   387 val obtainP =
   388   OuterSyntax.command "obtain" "generalized existence"
   389     K.prf_asm_goal
   390     (Scan.optional
   391       (P.and_list1 (Scan.repeat1 P.name -- Scan.option (P.$$$ "::" |-- P.typ))
   392         --| P.$$$ "where") [] -- statement >> (Toplevel.print oo IsarThy.obtain));
   393 
   394 val letP =
   395   OuterSyntax.command "let" "bind text variables" K.prf_decl
   396     (P.and_list1 (P.enum1 "and" P.term -- (P.$$$ "=" |-- P.term))
   397       >> (Toplevel.print oo (Toplevel.proof o IsarThy.let_bind)));
   398 
   399 val case_spec =
   400   (P.$$$ "(" |-- P.!!! (P.xname -- Scan.repeat1 P.uname --| P.$$$ ")") ||
   401     P.xname >> rpair []) -- P.opt_attribs >> P.triple1;
   402 
   403 val caseP =
   404   OuterSyntax.command "case" "invoke local context" K.prf_asm
   405     (case_spec >> (Toplevel.print oo (Toplevel.proof o IsarThy.invoke_case)));
   406 
   407 
   408 (* proof structure *)
   409 
   410 val beginP =
   411   OuterSyntax.command "{" "begin explicit proof block" K.prf_open
   412     (Scan.succeed (Toplevel.print o (Toplevel.proof IsarThy.begin_block)));
   413 
   414 val endP =
   415   OuterSyntax.command "}" "end explicit proof block" K.prf_close
   416     (Scan.succeed (Toplevel.print o (Toplevel.proof IsarThy.end_block)));
   417 
   418 val nextP =
   419   OuterSyntax.command "next" "enter next proof block" K.prf_block
   420     (Scan.succeed (Toplevel.print o (Toplevel.proof IsarThy.next_block)));
   421 
   422 
   423 (* end proof *)
   424 
   425 val qedP =
   426   OuterSyntax.command "qed" "conclude (sub-)proof" K.qed_block
   427     (Scan.option P.method >> IsarThy.qed);
   428 
   429 val terminal_proofP =
   430   OuterSyntax.command "by" "terminal backward proof" K.qed
   431     (P.method -- Scan.option P.method >> IsarThy.terminal_proof);
   432 
   433 val default_proofP =
   434   OuterSyntax.command ".." "default proof" K.qed
   435     (Scan.succeed IsarThy.default_proof);
   436 
   437 val immediate_proofP =
   438   OuterSyntax.command "." "immediate proof" K.qed
   439     (Scan.succeed IsarThy.immediate_proof);
   440 
   441 val done_proofP =
   442   OuterSyntax.command "done" "done proof" K.qed
   443     (Scan.succeed IsarThy.done_proof);
   444 
   445 val skip_proofP =
   446   OuterSyntax.improper_command "sorry" "skip proof (quick-and-dirty mode only!)" K.qed
   447     (Scan.succeed IsarThy.skip_proof);
   448 
   449 val forget_proofP =
   450   OuterSyntax.command "oops" "forget proof" K.qed_global
   451     (Scan.succeed IsarThy.forget_proof);
   452 
   453 
   454 (* proof steps *)
   455 
   456 val deferP =
   457   OuterSyntax.command "defer" "shuffle internal proof state" K.prf_script
   458     (Scan.option P.nat >> (Toplevel.print oo (Toplevel.proof o IsarThy.defer)));
   459 
   460 val preferP =
   461   OuterSyntax.command "prefer" "shuffle internal proof state" K.prf_script
   462     (P.nat >> (Toplevel.print oo (Toplevel.proof o IsarThy.prefer)));
   463 
   464 val applyP =
   465   OuterSyntax.command "apply" "initial refinement step (unstructured)" K.prf_script
   466     (P.method >> (Toplevel.print oo (Toplevel.proof o IsarThy.apply)));
   467 
   468 val apply_endP =
   469   OuterSyntax.command "apply_end" "terminal refinement (unstructured)" K.prf_script
   470     (P.method >> (Toplevel.print oo (Toplevel.proof o IsarThy.apply_end)));
   471 
   472 val proofP =
   473   OuterSyntax.command "proof" "backward proof" K.prf_block
   474     (Scan.option P.method >> (Toplevel.print oo (Toplevel.proof o IsarThy.proof)));
   475 
   476 
   477 (* calculational proof commands *)
   478 
   479 val calc_args =
   480   Scan.option (P.$$$ "(" |-- P.!!! ((P.xthms1 --| P.$$$ ")")));
   481 
   482 val alsoP =
   483   OuterSyntax.command "also" "combine calculation and current facts" K.prf_decl
   484     (calc_args >> IsarThy.also);
   485 
   486 val finallyP =
   487   OuterSyntax.command "finally" "combine calculation and current facts, exhibit result" K.prf_chain
   488     (calc_args >> IsarThy.finally);
   489 
   490 val moreoverP =
   491   OuterSyntax.command "moreover" "augment calculation by current facts" K.prf_decl
   492     (Scan.succeed IsarThy.moreover);
   493 
   494 val ultimatelyP =
   495   OuterSyntax.command "ultimately" "augment calculation by current facts, exhibit result"
   496     K.prf_chain (Scan.succeed IsarThy.ultimately);
   497 
   498 
   499 (* proof navigation *)
   500 
   501 val backP =
   502   OuterSyntax.command "back" "backtracking of proof command" K.prf_script
   503     (Scan.optional (P.$$$ "!" >> K true) false >> (Toplevel.print oo IsarCmd.back));
   504 
   505 
   506 (* history *)
   507 
   508 val cannot_undoP =
   509   OuterSyntax.improper_command "cannot_undo" "report 'cannot undo' error message" K.control
   510     (P.name >> (Toplevel.no_timing oo IsarCmd.cannot_undo));
   511 
   512 val clear_undosP =
   513   OuterSyntax.improper_command "clear_undos" "clear theory-level undo information" K.control
   514     (P.nat >> (Toplevel.no_timing oo IsarCmd.clear_undos_theory));
   515 
   516 val redoP =
   517   OuterSyntax.improper_command "redo" "redo last command" K.control
   518     (Scan.succeed (Toplevel.no_timing o Toplevel.print o IsarCmd.redo));
   519 
   520 val undos_proofP =
   521   OuterSyntax.improper_command "undos_proof" "undo last proof commands" K.control
   522     (P.nat >> ((Toplevel.no_timing o Toplevel.print) oo IsarCmd.undos_proof));
   523 
   524 val undoP =
   525   OuterSyntax.improper_command "undo" "undo last command" K.control
   526     (Scan.succeed ((Toplevel.no_timing o Toplevel.print) o IsarCmd.undo));
   527 
   528 val killP =
   529   OuterSyntax.improper_command "kill" "kill current history node" K.control
   530     (Scan.succeed ((Toplevel.no_timing o Toplevel.print) o IsarCmd.kill));
   531 
   532 
   533 
   534 (** diagnostic commands (for interactive mode only) **)
   535 
   536 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   537 
   538 
   539 val pretty_setmarginP =
   540   OuterSyntax.improper_command "pretty_setmargin" "change default margin for pretty printing"
   541     K.diag (P.nat >> (Toplevel.no_timing oo IsarCmd.pretty_setmargin));
   542 
   543 val print_commandsP =
   544   OuterSyntax.improper_command "print_commands" "print outer syntax (global)" K.diag
   545     (Scan.succeed (Toplevel.no_timing o OuterSyntax.print_commands));
   546 
   547 val print_contextP =
   548   OuterSyntax.improper_command "print_context" "print theory context name" K.diag
   549     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_context));
   550 
   551 val print_theoryP =
   552   OuterSyntax.improper_command "print_theory" "print logical theory contents (verbose!)" K.diag
   553     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theory));
   554 
   555 val print_syntaxP =
   556   OuterSyntax.improper_command "print_syntax" "print inner syntax of theory (verbose!)" K.diag
   557     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_syntax));
   558 
   559 val print_theoremsP =
   560   OuterSyntax.improper_command "print_theorems" "print theorems of this theory" K.diag
   561     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theorems));
   562 
   563 val print_localesP =
   564   OuterSyntax.improper_command "print_locales" "print locales of this theory" K.diag
   565     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_locales));
   566 
   567 val print_localeP =
   568   OuterSyntax.improper_command "print_locale" "print named locale of this theory" K.diag
   569     (locale_val >> (Toplevel.no_timing oo IsarCmd.print_locale));
   570 
   571 val print_attributesP =
   572   OuterSyntax.improper_command "print_attributes" "print attributes of this theory" K.diag
   573     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_attributes));
   574 
   575 val print_rulesP =
   576   OuterSyntax.improper_command "print_rules" "print intro/elim rules" K.diag
   577     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_rules));
   578 
   579 val print_induct_rulesP =
   580   OuterSyntax.improper_command "print_induct_rules" "print induction and cases rules" K.diag
   581     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_induct_rules));
   582 
   583 val print_trans_rulesP =
   584   OuterSyntax.improper_command "print_trans_rules" "print transitivity rules" K.diag
   585     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_trans_rules));
   586 
   587 val print_methodsP =
   588   OuterSyntax.improper_command "print_methods" "print methods of this theory" K.diag
   589     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_methods));
   590 
   591 val print_antiquotationsP =
   592   OuterSyntax.improper_command "print_antiquotations" "print antiquotations (global)" K.diag
   593     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_antiquotations));
   594 
   595 val thms_containingP =
   596   OuterSyntax.improper_command "thms_containing"
   597     "print facts containing certain constants or variables"
   598     K.diag (Scan.option (P.$$$ "(" |-- P.!!! (P.nat --| P.$$$ ")")) --
   599       Scan.repeat P.term >> (Toplevel.no_timing oo IsarCmd.print_thms_containing));
   600 
   601 val thm_depsP =
   602   OuterSyntax.improper_command "thm_deps" "visualize theorem dependencies"
   603     K.diag (P.xthms1 >> (Toplevel.no_timing oo IsarCmd.thm_deps));
   604 
   605 val print_bindsP =
   606   OuterSyntax.improper_command "print_binds" "print term bindings of proof context" K.diag
   607     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_binds));
   608 
   609 val print_lthmsP =
   610   OuterSyntax.improper_command "print_facts" "print facts of proof context" K.diag
   611     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_lthms));
   612 
   613 val print_casesP =
   614   OuterSyntax.improper_command "print_cases" "print cases of proof context" K.diag
   615     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_cases));
   616 
   617 val print_introsP =
   618   OuterSyntax.improper_command "print_intros" "print matching introduction rules" K.diag
   619     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_intros));
   620 
   621 val print_thmsP =
   622   OuterSyntax.improper_command "thm" "print theorems" K.diag
   623     (opt_modes -- P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_thms));
   624 
   625 val print_prfsP =
   626   OuterSyntax.improper_command "prf" "print proof terms of theorems" K.diag
   627     (opt_modes -- Scan.option P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs false));
   628 
   629 val print_full_prfsP =
   630   OuterSyntax.improper_command "full_prf" "print full proof terms of theorems" K.diag
   631     (opt_modes -- Scan.option P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs true));
   632 
   633 val print_propP =
   634   OuterSyntax.improper_command "prop" "read and print proposition" K.diag
   635     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_prop));
   636 
   637 val print_termP =
   638   OuterSyntax.improper_command "term" "read and print term" K.diag
   639     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_term));
   640 
   641 val print_typeP =
   642   OuterSyntax.improper_command "typ" "read and print type" K.diag
   643     (opt_modes -- P.typ >> (Toplevel.no_timing oo IsarCmd.print_type));
   644 
   645 
   646 
   647 (** system commands (for interactive mode only) **)
   648 
   649 val cdP =
   650   OuterSyntax.improper_command "cd" "change current working directory" K.diag
   651     (P.name >> (Toplevel.no_timing oo IsarCmd.cd));
   652 
   653 val pwdP =
   654   OuterSyntax.improper_command "pwd" "print current working directory" K.diag
   655     (Scan.succeed (Toplevel.no_timing o IsarCmd.pwd));
   656 
   657 val use_thyP =
   658   OuterSyntax.improper_command "use_thy" "use theory file" K.diag
   659     (P.name >> (Toplevel.no_timing oo IsarCmd.use_thy));
   660 
   661 val use_thy_onlyP =
   662   OuterSyntax.improper_command "use_thy_only" "use theory file only, ignoring associated ML"
   663     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.use_thy_only));
   664 
   665 val update_thyP =
   666   OuterSyntax.improper_command "update_thy" "update theory file" K.diag
   667     (P.name >> (Toplevel.no_timing oo IsarCmd.update_thy));
   668 
   669 val update_thy_onlyP =
   670   OuterSyntax.improper_command "update_thy_only" "update theory file, ignoring associated ML"
   671     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.update_thy_only));
   672 
   673 val touch_thyP =
   674   OuterSyntax.improper_command "touch_thy" "outdate theory, including descendants" K.diag
   675     (P.name >> (Toplevel.no_timing oo IsarCmd.touch_thy));
   676 
   677 val touch_all_thysP =
   678   OuterSyntax.improper_command "touch_all_thys" "outdate all non-finished theories" K.diag
   679     (Scan.succeed (Toplevel.no_timing o IsarCmd.touch_all_thys));
   680 
   681 val touch_child_thysP =
   682   OuterSyntax.improper_command "touch_child_thys" "outdate child theories" K.diag
   683     (P.name >> (Toplevel.no_timing oo IsarCmd.touch_child_thys));
   684 
   685 val remove_thyP =
   686   OuterSyntax.improper_command "remove_thy" "remove theory from loader database" K.diag
   687     (P.name >> (Toplevel.no_timing oo IsarCmd.remove_thy));
   688 
   689 val kill_thyP =
   690   OuterSyntax.improper_command "kill_thy" "kill theory -- try to remove from loader database"
   691     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.kill_thy));
   692 
   693 val opt_limits =
   694   Scan.option P.nat -- Scan.option (P.$$$ "," |-- P.!!! P.nat);
   695 
   696 val prP =
   697   OuterSyntax.improper_command "pr" "print current proof state (if present)" K.diag
   698     (opt_modes -- opt_limits >> (Toplevel.no_timing oo IsarCmd.pr));
   699 
   700 val disable_prP =
   701   OuterSyntax.improper_command "disable_pr" "disable printing of toplevel state" K.diag
   702     (Scan.succeed (Toplevel.no_timing o IsarCmd.disable_pr));
   703 
   704 val enable_prP =
   705   OuterSyntax.improper_command "enable_pr" "enable printing of toplevel state" K.diag
   706     (Scan.succeed (Toplevel.no_timing o IsarCmd.enable_pr));
   707 
   708 val commitP =
   709   OuterSyntax.improper_command "commit" "commit current session to ML database" K.diag
   710     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.use_commit));
   711 
   712 val quitP =
   713   OuterSyntax.improper_command "quit" "quit Isabelle" K.control
   714     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.quit));
   715 
   716 val exitP =
   717   OuterSyntax.improper_command "exit" "exit Isar loop" K.control
   718     (Scan.succeed (Toplevel.no_timing o IsarCmd.exit));
   719 
   720 val init_toplevelP =
   721   OuterSyntax.improper_command "init_toplevel" "restart Isar toplevel loop" K.control
   722     (Scan.succeed (Toplevel.no_timing o IsarCmd.init_toplevel));
   723 
   724 val welcomeP =
   725   OuterSyntax.improper_command "welcome" "print welcome message" K.diag
   726     (Scan.succeed (Toplevel.no_timing o IsarCmd.welcome));
   727 
   728 
   729 
   730 (** the Pure outer syntax **)
   731 
   732 (*keep keywords consistent with the parsers, including those in
   733   outer_parse.ML, otherwise be prepared for unexpected errors*)
   734 
   735 val keywords =
   736  ["!", "!!", "%", "%%", "(", ")", "+", ",", "--", ":", "::", ";", "<",
   737   "<=", "=", "==", "=>", "?", "[", "]", "and", "assumes", "binder",
   738   "concl", "defines", "files", "fixes", "in", "includes", "infix",
   739   "infixl", "infixr", "is", "notes", "open", "output", "overloaded",
   740   "shows", "structure", "where", "|", "\\<equiv>",
   741   "\\<leftharpoondown>", "\\<rightharpoonup>",
   742   "\\<rightleftharpoons>", "\\<subseteq>"];
   743 
   744 val parsers = [
   745   (*theory structure*)
   746   theoryP, end_excursionP, contextP,
   747   (*markup commands*)
   748   headerP, chapterP, sectionP, subsectionP, subsubsectionP, textP,
   749   text_rawP, sectP, subsectP, subsubsectP, txtP, txt_rawP,
   750   (*theory sections*)
   751   classesP, classrelP, defaultsortP, typedeclP, typeabbrP, nontermP,
   752   aritiesP, judgmentP, constsP, finalconstsP, syntaxP, translationsP,
   753   axiomsP, defsP, constdefsP, theoremsP, lemmasP, declareP, globalP,
   754   localP, hideP, useP, mlP, ml_commandP, ml_setupP, setupP,
   755   method_setupP, parse_ast_translationP, parse_translationP,
   756   print_translationP, typed_print_translationP,
   757   print_ast_translationP, token_translationP, oracleP, localeP,
   758   (*proof commands*)
   759   theoremP, lemmaP, corollaryP, showP, haveP, thusP, henceP, fixP,
   760   assumeP, presumeP, defP, obtainP, letP, caseP, thenP, fromP, withP,
   761   noteP, usingP, beginP, endP, nextP, qedP, terminal_proofP,
   762   default_proofP, immediate_proofP, done_proofP, skip_proofP,
   763   forget_proofP, deferP, preferP, applyP, apply_endP, proofP, alsoP,
   764   finallyP, moreoverP, ultimatelyP, backP, cannot_undoP, clear_undosP,
   765   redoP, undos_proofP, undoP, killP, instantiateP,
   766   (*diagnostic commands*)
   767   pretty_setmarginP, print_commandsP, print_contextP, print_theoryP,
   768   print_syntaxP, print_theoremsP, print_localesP, print_localeP,
   769   print_attributesP, print_rulesP, print_induct_rulesP,
   770   print_trans_rulesP, print_methodsP, print_antiquotationsP,
   771   thms_containingP, thm_depsP, print_bindsP, print_lthmsP,
   772   print_casesP, print_introsP, print_thmsP, print_prfsP, print_full_prfsP,
   773   print_propP, print_termP, print_typeP,
   774   (*system commands*)
   775   cdP, pwdP, use_thyP, use_thy_onlyP, update_thyP, update_thy_onlyP,
   776   touch_thyP, touch_all_thysP, touch_child_thysP, remove_thyP,
   777   kill_thyP, prP, disable_prP, enable_prP, commitP, quitP, exitP,
   778   init_toplevelP, welcomeP];
   779 
   780 end;
   781 
   782 
   783 (*install the Pure outer syntax*)
   784 OuterSyntax.add_keywords IsarSyn.keywords;
   785 OuterSyntax.add_parsers IsarSyn.parsers;