src/Pure/Isar/isar_syn.ML
author wenzelm
Wed Jun 07 02:01:32 2006 +0200 (2006-06-07)
changeset 19809 b8f35de1c664
parent 19659 88d246e5f4bd
child 19844 2c1fdc397ded
permissions -rw-r--r--
added 'if' and 'for' keywords;
tuned;
     1 (*  Title:      Pure/Isar/isar_syn.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Isar/Pure outer syntax.
     6 *)
     7 
     8 structure IsarSyn: sig end =
     9 struct
    10 
    11 structure P = OuterParse and K = OuterKeyword;
    12 
    13 
    14 (** init and exit **)
    15 
    16 val theoryP =
    17   OuterSyntax.command "theory" "begin theory" (K.tag_theory K.thy_begin)
    18     (ThyHeader.args >> (Toplevel.print oo IsarThy.theory));
    19 
    20 val end_excursionP =
    21   OuterSyntax.command "end" "end current excursion" (K.tag_theory K.thy_end)
    22     (Scan.succeed (Toplevel.print o Toplevel.exit));
    23 
    24 val contextP =
    25   OuterSyntax.improper_command "context" "switch theory context" (K.tag_theory K.thy_switch)
    26     (P.name >> (Toplevel.print oo IsarThy.context));
    27 
    28 
    29 
    30 (** markup commands **)
    31 
    32 val headerP = OuterSyntax.markup_command IsarOutput.Markup "header" "theory header" K.diag
    33   (P.position P.text >> IsarCmd.add_header);
    34 
    35 val chapterP = OuterSyntax.markup_command IsarOutput.Markup "chapter" "chapter heading"
    36   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_chapter);
    37 
    38 val sectionP = OuterSyntax.markup_command IsarOutput.Markup "section" "section heading"
    39   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_section);
    40 
    41 val subsectionP = OuterSyntax.markup_command IsarOutput.Markup "subsection" "subsection heading"
    42   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_subsection);
    43 
    44 val subsubsectionP =
    45   OuterSyntax.markup_command IsarOutput.Markup "subsubsection" "subsubsection heading"
    46   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_subsubsection);
    47 
    48 val textP = OuterSyntax.markup_command IsarOutput.MarkupEnv "text" "formal comment (theory)"
    49   K.thy_decl (P.opt_locale_target -- P.position P.text >> IsarCmd.add_text);
    50 
    51 val text_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "text_raw"
    52   "raw document preparation text"
    53   K.thy_decl (P.position P.text >> IsarCmd.add_text_raw);
    54 
    55 val sectP = OuterSyntax.markup_command IsarOutput.Markup "sect" "formal comment (proof)"
    56   (K.tag_proof K.prf_heading) (P.position P.text >> IsarCmd.add_sect);
    57 
    58 val subsectP = OuterSyntax.markup_command IsarOutput.Markup "subsect" "formal comment (proof)"
    59   (K.tag_proof K.prf_heading) (P.position P.text >> IsarCmd.add_subsect);
    60 
    61 val subsubsectP = OuterSyntax.markup_command IsarOutput.Markup "subsubsect"
    62   "formal comment (proof)" (K.tag_proof K.prf_heading)
    63   (P.position P.text >> IsarCmd.add_subsubsect);
    64 
    65 val txtP = OuterSyntax.markup_command IsarOutput.MarkupEnv "txt" "formal comment (proof)"
    66   (K.tag_proof K.prf_decl) (P.position P.text >> IsarCmd.add_txt);
    67 
    68 val txt_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "txt_raw"
    69   "raw document preparation text (proof)" (K.tag_proof K.prf_decl)
    70   (P.position P.text >> IsarCmd.add_txt_raw);
    71 
    72 
    73 
    74 (** theory sections **)
    75 
    76 (* classes and sorts *)
    77 
    78 val classesP =
    79   OuterSyntax.command "classes" "declare type classes" K.thy_decl
    80     (Scan.repeat1 (P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    81         P.!!! (P.list1 P.xname)) []) >> (Toplevel.theory o fold AxClass.axiomatize_class));
    82 
    83 val classrelP =
    84   OuterSyntax.command "classrel" "state inclusion of type classes (axiomatic!)" K.thy_decl
    85     (P.and_list1 (P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname))
    86     >> (Toplevel.theory o AxClass.axiomatize_classrel));
    87 
    88 val defaultsortP =
    89   OuterSyntax.command "defaultsort" "declare default sort" K.thy_decl
    90     (P.sort >> (Toplevel.theory o Theory.add_defsort));
    91 
    92 val axclassP =
    93   OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
    94     ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    95         P.!!! (P.list1 P.xname)) []) --
    96           Scan.repeat (P.thm_name ":" -- (P.prop >> single))
    97       >> (fn (x, y) => Toplevel.theory (snd o AxClass.define_class x [] y)));
    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 (Theory.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 fold AxClass.axiomatize_arity));
   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 = P.opt_keyword "overloaded";
   135 
   136 val finalconstsP =
   137   OuterSyntax.command "finalconsts" "declare constants as final" K.thy_decl
   138     (opt_overloaded -- Scan.repeat1 P.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   139 
   140 val mode_spec =
   141   (P.$$$ "output" >> K ("", false)) || P.name -- Scan.optional (P.$$$ "output" >> K false) true;
   142 
   143 val opt_mode =
   144   Scan.optional (P.$$$ "(" |-- P.!!! (mode_spec --| P.$$$ ")")) Syntax.default_mode;
   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 val no_syntaxP =
   151   OuterSyntax.command "no_syntax" "delete syntax declarations" K.thy_decl
   152     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Theory.del_modesyntax));
   153 
   154 
   155 (* translations *)
   156 
   157 val trans_pat =
   158   Scan.optional (P.$$$ "(" |-- P.!!! (P.xname --| P.$$$ ")")) "logic" -- P.string;
   159 
   160 fun trans_arrow toks =
   161   ((P.$$$ "\\<rightharpoonup>" || P.$$$ "=>") >> K Syntax.ParseRule ||
   162     (P.$$$ "\\<leftharpoondown>" || P.$$$ "<=") >> K Syntax.PrintRule ||
   163     (P.$$$ "\\<rightleftharpoons>" || P.$$$ "==") >> K Syntax.ParsePrintRule) toks;
   164 
   165 val trans_line =
   166   trans_pat -- P.!!! (trans_arrow -- trans_pat)
   167     >> (fn (left, (arr, right)) => arr (left, right));
   168 
   169 val translationsP =
   170   OuterSyntax.command "translations" "declare syntax translation rules" K.thy_decl
   171     (Scan.repeat1 trans_line >> (Toplevel.theory o Theory.add_trrules));
   172 
   173 val no_translationsP =
   174   OuterSyntax.command "no_translations" "remove syntax translation rules" K.thy_decl
   175     (Scan.repeat1 trans_line >> (Toplevel.theory o Theory.del_trrules));
   176 
   177 
   178 (* axioms and definitions *)
   179 
   180 val axiomsP =
   181   OuterSyntax.command "axioms" "state arbitrary propositions (axiomatic!)" K.thy_decl
   182     (Scan.repeat1 P.spec_name >> (Toplevel.theory o IsarThy.add_axioms));
   183 
   184 val opt_unchecked_overloaded =
   185   Scan.optional (P.$$$ "(" |-- P.!!!
   186     (((P.$$$ "unchecked" >> K true) -- Scan.optional (P.$$$ "overloaded" >> K true) false ||
   187       P.$$$ "overloaded" >> K (false, true)) --| P.$$$ ")")) (false, false);
   188 
   189 val defsP =
   190   OuterSyntax.command "defs" "define constants" K.thy_decl
   191     (opt_unchecked_overloaded -- Scan.repeat1 P.spec_name
   192       >> (Toplevel.theory o IsarThy.add_defs));
   193 
   194 
   195 (* constant definitions and abbreviations *)
   196 
   197 val constdecl =
   198   (P.name --| P.$$$ "where") >> (fn x => (x, NONE, NoSyn)) ||
   199     P.name -- (P.$$$ "::" |-- P.!!! P.typ >> SOME) -- P.opt_mixfix'
   200       --| Scan.option (P.$$$ "where") >> P.triple1 ||
   201     P.name -- (P.mixfix >> pair NONE) --| Scan.option (P.$$$ "where") >> P.triple2;
   202 
   203 val constdef = Scan.option constdecl -- (P.opt_thm_name ":" -- P.prop);
   204 
   205 val structs =
   206   Scan.optional ((P.$$$ "(" -- P.$$$ "structure") |-- P.!!! (P.simple_fixes --| P.$$$ ")")) [];
   207 
   208 val constdefsP =
   209   OuterSyntax.command "constdefs" "old-style constant definition" K.thy_decl
   210     (structs -- Scan.repeat1 constdef >> (Toplevel.theory o Constdefs.add_constdefs));
   211 
   212 val definitionP =
   213   OuterSyntax.command "definition" "constant definition" K.thy_decl
   214     (P.opt_locale_target -- Scan.repeat1 constdef
   215     >> (fn (loc, args) => Toplevel.local_theory loc (#2 o Specification.definition args)));
   216 
   217 val abbreviationP =
   218   OuterSyntax.command "abbreviation" "constant abbreviation" K.thy_decl
   219     (P.opt_locale_target -- opt_mode --
   220       Scan.repeat1 (Scan.option constdecl -- P.prop)
   221     >> (fn ((loc, mode), args) =>
   222         Toplevel.local_theory loc (Specification.abbreviation mode args)));
   223 
   224 val const_syntaxP =
   225   OuterSyntax.command "const_syntax" "constant syntax" K.thy_decl
   226     (P.opt_locale_target -- opt_mode -- Scan.repeat1 (P.xname -- P.mixfix)
   227     >> (fn ((loc, mode), args) =>
   228         Toplevel.local_theory loc (Specification.const_syntax mode args)));
   229 
   230 
   231 (* constant specifications *)
   232 
   233 val axiomatizationP =
   234   OuterSyntax.command "axiomatization" "axiomatic constant specification" K.thy_decl
   235     (P.opt_locale_target --
   236      (Scan.optional P.fixes [] --
   237       Scan.optional (P.$$$ "where" |-- P.!!! (P.and_list1 P.spec)) [])
   238     >> (fn (loc, (x, y)) => Toplevel.local_theory loc (#2 o Specification.axiomatization x y)));
   239 
   240 
   241 (* theorems *)
   242 
   243 fun theorems kind = P.opt_locale_target -- P.name_facts
   244   >> (Toplevel.theory_context o uncurry (IsarThy.smart_theorems kind));
   245 
   246 val theoremsP =
   247   OuterSyntax.command "theorems" "define theorems" K.thy_decl (theorems PureThy.theoremK);
   248 
   249 val lemmasP =
   250   OuterSyntax.command "lemmas" "define lemmas" K.thy_decl (theorems PureThy.lemmaK);
   251 
   252 val declareP =
   253   OuterSyntax.command "declare" "declare theorems (improper)" K.thy_script
   254     (P.opt_locale_target -- (P.and_list1 P.xthms1 >> flat)
   255       >> (Toplevel.theory_context o uncurry IsarThy.declare_theorems));
   256 
   257 
   258 (* name space entry path *)
   259 
   260 val globalP =
   261   OuterSyntax.command "global" "disable prefixing of theory name" K.thy_decl
   262     (Scan.succeed (Toplevel.theory Sign.root_path));
   263 
   264 val localP =
   265   OuterSyntax.command "local" "enable prefixing of theory name" K.thy_decl
   266     (Scan.succeed (Toplevel.theory Sign.local_path));
   267 
   268 val hideP =
   269   OuterSyntax.command "hide" "hide names from given name space" K.thy_decl
   270     ((P.opt_keyword "open" >> not) -- (P.name -- Scan.repeat1 P.xname) >>
   271       (Toplevel.theory o uncurry Sign.hide_names));
   272 
   273 
   274 (* use ML text *)
   275 
   276 val useP =
   277   OuterSyntax.command "use" "eval ML text from file" (K.tag_ml K.diag)
   278     (P.path >> (Toplevel.no_timing oo IsarCmd.use));
   279 
   280 val mlP =
   281   OuterSyntax.command "ML" "eval ML text (diagnostic)" (K.tag_ml K.diag)
   282     (P.text >> IsarCmd.use_mltext true);
   283 
   284 val ml_commandP =
   285   OuterSyntax.command "ML_command" "eval ML text" (K.tag_ml K.diag)
   286     (P.text >> (Toplevel.no_timing oo IsarCmd.use_mltext false));
   287 
   288 val ml_setupP =
   289   OuterSyntax.command "ML_setup" "eval ML text (may change theory)" (K.tag_ml K.thy_decl)
   290     (P.text >> IsarCmd.use_mltext_theory);
   291 
   292 val setupP =
   293   OuterSyntax.command "setup" "apply ML theory setup" (K.tag_ml K.thy_decl)
   294     (Scan.option P.text >> (Toplevel.theory o PureThy.generic_setup));
   295 
   296 val method_setupP =
   297   OuterSyntax.command "method_setup" "define proof method in ML" (K.tag_ml K.thy_decl)
   298     (((P.name -- P.!!! (P.$$$ "=" |-- P.text -- P.text) >> P.triple2))
   299       >> (Toplevel.theory o Method.method_setup));
   300 
   301 
   302 (* translation functions *)
   303 
   304 val trfun = P.opt_keyword "advanced" -- P.text;
   305 
   306 val parse_ast_translationP =
   307   OuterSyntax.command "parse_ast_translation" "install parse ast translation functions"
   308     (K.tag_ml K.thy_decl)
   309     (trfun >> (Toplevel.theory o Sign.parse_ast_translation));
   310 
   311 val parse_translationP =
   312   OuterSyntax.command "parse_translation" "install parse translation functions"
   313     (K.tag_ml K.thy_decl)
   314     (trfun >> (Toplevel.theory o Sign.parse_translation));
   315 
   316 val print_translationP =
   317   OuterSyntax.command "print_translation" "install print translation functions"
   318     (K.tag_ml K.thy_decl)
   319     (trfun >> (Toplevel.theory o Sign.print_translation));
   320 
   321 val typed_print_translationP =
   322   OuterSyntax.command "typed_print_translation" "install typed print translation functions"
   323     (K.tag_ml K.thy_decl)
   324     (trfun >> (Toplevel.theory o Sign.typed_print_translation));
   325 
   326 val print_ast_translationP =
   327   OuterSyntax.command "print_ast_translation" "install print ast translation functions"
   328     (K.tag_ml K.thy_decl)
   329     (trfun >> (Toplevel.theory o Sign.print_ast_translation));
   330 
   331 val token_translationP =
   332   OuterSyntax.command "token_translation" "install token translation functions"
   333     (K.tag_ml K.thy_decl)
   334     (P.text >> (Toplevel.theory o Sign.token_translation));
   335 
   336 
   337 (* oracles *)
   338 
   339 val oracleP =
   340   OuterSyntax.command "oracle" "declare oracle" (K.tag_ml K.thy_decl)
   341     (P.name -- (P.$$$ "(" |-- P.text --| P.$$$ ")" --| P.$$$ "=")
   342       -- P.text >> (Toplevel.theory o PureThy.add_oracle o P.triple1));
   343 
   344 
   345 (* locales *)
   346 
   347 val locale_val =
   348   (P.locale_expr --
   349     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 P.context_element)) [] ||
   350   Scan.repeat1 P.context_element >> pair Locale.empty);
   351 
   352 val localeP =
   353   OuterSyntax.command "locale" "define named proof context" K.thy_decl
   354     ((P.opt_keyword "open" >> not) -- P.name
   355         -- Scan.optional (P.$$$ "=" |-- P.!!! locale_val) (Locale.empty, [])
   356       >> (Toplevel.theory_context o (fn ((is_open, name), (expr, elems)) =>
   357              Locale.add_locale is_open name expr elems #> (fn ((_, ctxt), thy) => (ctxt, thy)))));
   358 
   359 val interpretationP =
   360   OuterSyntax.command "interpretation"
   361     "prove and register interpretation of locale expression in theory or locale" K.thy_goal
   362     (P.xname --| (P.$$$ "\\<subseteq>" || P.$$$ "<") -- P.!!! P.locale_expr
   363       >> (Toplevel.print oo (Toplevel.theory_to_proof o Locale.interpretation_in_locale)) ||
   364      P.opt_thm_name ":" -- P.locale_expr -- P.locale_insts >> (fn ((x, y), z) =>
   365       Toplevel.print o Toplevel.theory_to_proof (Locale.interpretation x y z)));
   366 
   367 val interpretP =
   368   OuterSyntax.command "interpret"
   369     "prove and register interpretation of locale expression in proof context"
   370     (K.tag_proof K.prf_goal)
   371     (P.opt_thm_name ":" -- P.locale_expr -- P.locale_insts >> (fn ((x, y), z) =>
   372       Toplevel.print o Toplevel.proof' (Locale.interpret x y z)));
   373 
   374 
   375 
   376 (** proof commands **)
   377 
   378 (* statements *)
   379 
   380 fun gen_theorem kind =
   381   OuterSyntax.command kind ("state " ^ kind) K.thy_goal
   382     (P.opt_locale_target -- Scan.optional (P.opt_thm_name ":" --|
   383       Scan.ahead (P.locale_keyword || P.statement_keyword)) ("", []) --
   384       P.general_statement >> (fn ((x, y), (z, w)) =>
   385       (Toplevel.print o Toplevel.theory_to_proof (Locale.smart_theorem kind x y z w))));
   386 
   387 val theoremP = gen_theorem PureThy.theoremK;
   388 val lemmaP = gen_theorem PureThy.lemmaK;
   389 val corollaryP = gen_theorem PureThy.corollaryK;
   390 
   391 val haveP =
   392   OuterSyntax.command "have" "state local goal"
   393     (K.tag_proof K.prf_goal)
   394     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.have));
   395 
   396 val henceP =
   397   OuterSyntax.command "hence" "abbreviates \"then have\""
   398     (K.tag_proof K.prf_goal)
   399     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.hence));
   400 
   401 val showP =
   402   OuterSyntax.command "show" "state local goal, solving current obligation"
   403     (K.tag_proof K.prf_goal)
   404     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.show));
   405 
   406 val thusP =
   407   OuterSyntax.command "thus" "abbreviates \"then show\""
   408     (K.tag_proof K.prf_goal)
   409     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.thus));
   410 
   411 
   412 (* facts *)
   413 
   414 val facts = P.and_list1 P.xthms1;
   415 
   416 val thenP =
   417   OuterSyntax.command "then" "forward chaining"
   418     (K.tag_proof K.prf_chain)
   419     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   420 
   421 val fromP =
   422   OuterSyntax.command "from" "forward chaining from given facts"
   423     (K.tag_proof K.prf_chain)
   424     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss)));
   425 
   426 val withP =
   427   OuterSyntax.command "with" "forward chaining from given and current facts"
   428     (K.tag_proof K.prf_chain)
   429     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss)));
   430 
   431 val noteP =
   432   OuterSyntax.command "note" "define facts"
   433     (K.tag_proof K.prf_decl)
   434     (P.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss)));
   435 
   436 val usingP =
   437   OuterSyntax.command "using" "augment goal facts"
   438     (K.tag_proof K.prf_decl)
   439     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using)));
   440 
   441 val unfoldingP =
   442   OuterSyntax.command "unfolding" "unfold definitions in goal and facts"
   443     (K.tag_proof K.prf_decl)
   444     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding)));
   445 
   446 
   447 (* proof context *)
   448 
   449 val fixP =
   450   OuterSyntax.command "fix" "fix local variables (Skolem constants)"
   451     (K.tag_proof K.prf_asm)
   452     (P.simple_fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix)));
   453 
   454 val assumeP =
   455   OuterSyntax.command "assume" "assume propositions"
   456     (K.tag_proof K.prf_asm)
   457     (P.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume)));
   458 
   459 val presumeP =
   460   OuterSyntax.command "presume" "assume propositions, to be established later"
   461     (K.tag_proof K.prf_asm)
   462     (P.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume)));
   463 
   464 val defP =
   465   OuterSyntax.command "def" "local definition"
   466     (K.tag_proof K.prf_asm)
   467     (P.and_list1
   468       (P.opt_thm_name ":" -- (P.name -- ((P.$$$ "\\<equiv>" || P.$$$ "==") |-- P.!!! P.termp)))
   469     >> (Toplevel.print oo (Toplevel.proof o Proof.def)));
   470 
   471 val obtainP =
   472   OuterSyntax.command "obtain" "generalized existence"
   473     (K.tag_proof K.prf_asm_goal)
   474     (P.parname -- Scan.optional (P.simple_fixes --| P.$$$ "where") [] -- P.statement
   475       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain x y z)));
   476 
   477 val guessP =
   478   OuterSyntax.command "guess" "wild guessing (unstructured)"
   479     (K.tag_proof K.prf_asm_goal)
   480     (Scan.optional P.simple_fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess)));
   481 
   482 val letP =
   483   OuterSyntax.command "let" "bind text variables"
   484     (K.tag_proof K.prf_decl)
   485     (P.and_list1 (P.enum1 "and" P.term -- (P.$$$ "=" |-- P.term))
   486       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind)));
   487 
   488 val case_spec =
   489   (P.$$$ "(" |-- P.!!! (P.xname -- Scan.repeat1 (P.maybe P.name) --| P.$$$ ")") ||
   490     P.xname >> rpair []) -- P.opt_attribs >> P.triple1;
   491 
   492 val caseP =
   493   OuterSyntax.command "case" "invoke local context"
   494     (K.tag_proof K.prf_asm)
   495     (case_spec >> (Toplevel.print oo (Toplevel.proof o Proof.invoke_case)));
   496 
   497 
   498 (* proof structure *)
   499 
   500 val beginP =
   501   OuterSyntax.command "{" "begin explicit proof block"
   502     (K.tag_proof K.prf_open)
   503     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   504 
   505 val endP =
   506   OuterSyntax.command "}" "end explicit proof block"
   507     (K.tag_proof K.prf_close)
   508     (Scan.succeed (Toplevel.print o Toplevel.proofs Proof.end_block));
   509 
   510 val nextP =
   511   OuterSyntax.command "next" "enter next proof block"
   512     (K.tag_proof K.prf_block)
   513     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   514 
   515 
   516 (* end proof *)
   517 
   518 val qedP =
   519   OuterSyntax.command "qed" "conclude (sub-)proof"
   520     (K.tag_proof K.qed_block)
   521     (Scan.option P.method >> (Toplevel.print3 oo IsarThy.qed));
   522 
   523 val terminal_proofP =
   524   OuterSyntax.command "by" "terminal backward proof"
   525     (K.tag_proof K.qed)
   526     (P.method -- Scan.option P.method >> (Toplevel.print3 oo IsarThy.terminal_proof));
   527 
   528 val default_proofP =
   529   OuterSyntax.command ".." "default proof"
   530     (K.tag_proof K.qed)
   531     (Scan.succeed (Toplevel.print3 o IsarThy.default_proof));
   532 
   533 val immediate_proofP =
   534   OuterSyntax.command "." "immediate proof"
   535     (K.tag_proof K.qed)
   536     (Scan.succeed (Toplevel.print3 o IsarThy.immediate_proof));
   537 
   538 val done_proofP =
   539   OuterSyntax.command "done" "done proof"
   540     (K.tag_proof K.qed)
   541     (Scan.succeed (Toplevel.print3 o IsarThy.done_proof));
   542 
   543 val skip_proofP =
   544   OuterSyntax.improper_command "sorry" "skip proof (quick-and-dirty mode only!)"
   545     (K.tag_proof K.qed)
   546     (Scan.succeed (Toplevel.print3 o IsarThy.skip_proof));
   547 
   548 val forget_proofP =
   549   OuterSyntax.command "oops" "forget proof"
   550     (K.tag_proof K.qed_global)
   551     (Scan.succeed Toplevel.forget_proof);
   552 
   553 
   554 (* proof steps *)
   555 
   556 val deferP =
   557   OuterSyntax.command "defer" "shuffle internal proof state"
   558     (K.tag_proof K.prf_script)
   559     (Scan.option P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.defer)));
   560 
   561 val preferP =
   562   OuterSyntax.command "prefer" "shuffle internal proof state"
   563     (K.tag_proof K.prf_script)
   564     (P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.prefer)));
   565 
   566 val applyP =
   567   OuterSyntax.command "apply" "initial refinement step (unstructured)"
   568     (K.tag_proof K.prf_script)
   569     (P.method >> (Toplevel.print oo (Toplevel.proofs o Proof.apply)));
   570 
   571 val apply_endP =
   572   OuterSyntax.command "apply_end" "terminal refinement (unstructured)"
   573     (K.tag_proof K.prf_script)
   574     (P.method >> (Toplevel.print oo (Toplevel.proofs o Proof.apply_end)));
   575 
   576 val proofP =
   577   OuterSyntax.command "proof" "backward proof"
   578     (K.tag_proof K.prf_block)
   579     (Scan.option P.method >> (fn m => Toplevel.print o
   580       Toplevel.actual_proof (ProofHistory.applys (Proof.proof m)) o
   581       Toplevel.skip_proof (History.apply (fn i => i + 1))));
   582 
   583 
   584 (* calculational proof commands *)
   585 
   586 val calc_args =
   587   Scan.option (P.$$$ "(" |-- P.!!! ((P.xthms1 --| P.$$$ ")")));
   588 
   589 val alsoP =
   590   OuterSyntax.command "also" "combine calculation and current facts"
   591     (K.tag_proof K.prf_decl)
   592     (calc_args >> (Toplevel.proofs' o Calculation.also));
   593 
   594 val finallyP =
   595   OuterSyntax.command "finally" "combine calculation and current facts, exhibit result"
   596     (K.tag_proof K.prf_chain)
   597     (calc_args >> (Toplevel.proofs' o Calculation.finally));
   598 
   599 val moreoverP =
   600   OuterSyntax.command "moreover" "augment calculation by current facts"
   601     (K.tag_proof K.prf_decl)
   602     (Scan.succeed (Toplevel.proof' Calculation.moreover));
   603 
   604 val ultimatelyP =
   605   OuterSyntax.command "ultimately" "augment calculation by current facts, exhibit result"
   606     (K.tag_proof K.prf_chain)
   607     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
   608 
   609 
   610 (* proof navigation *)
   611 
   612 val backP =
   613   OuterSyntax.command "back" "backtracking of proof command"
   614     (K.tag_proof K.prf_script)
   615     (Scan.succeed (Toplevel.print o IsarCmd.back));
   616 
   617 
   618 (* history *)
   619 
   620 val cannot_undoP =
   621   OuterSyntax.improper_command "cannot_undo" "report 'cannot undo' error message" K.control
   622     (P.name >> (Toplevel.no_timing oo IsarCmd.cannot_undo));
   623 
   624 val clear_undosP =
   625   OuterSyntax.improper_command "clear_undos" "clear theory-level undo information" K.control
   626     (P.nat >> (Toplevel.no_timing oo IsarCmd.clear_undos_theory));
   627 
   628 val redoP =
   629   OuterSyntax.improper_command "redo" "redo last command" K.control
   630     (Scan.succeed (Toplevel.no_timing o Toplevel.print o IsarCmd.redo));
   631 
   632 val undos_proofP =
   633   OuterSyntax.improper_command "undos_proof" "undo last proof commands" K.control
   634     (P.nat >> ((Toplevel.no_timing o Toplevel.print) oo IsarCmd.undos_proof));
   635 
   636 val undoP =
   637   OuterSyntax.improper_command "undo" "undo last command" K.control
   638     (Scan.succeed ((Toplevel.no_timing o Toplevel.print) o IsarCmd.undo));
   639 
   640 val killP =
   641   OuterSyntax.improper_command "kill" "kill current history node" K.control
   642     (Scan.succeed ((Toplevel.no_timing o Toplevel.print) o IsarCmd.kill));
   643 
   644 
   645 
   646 (** diagnostic commands (for interactive mode only) **)
   647 
   648 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   649 
   650 
   651 val pretty_setmarginP =
   652   OuterSyntax.improper_command "pretty_setmargin" "change default margin for pretty printing"
   653     K.diag (P.nat >> (Toplevel.no_timing oo IsarCmd.pretty_setmargin));
   654 
   655 val print_commandsP =
   656   OuterSyntax.improper_command "print_commands" "print outer syntax (global)" K.diag
   657     (Scan.succeed (Toplevel.no_timing o OuterSyntax.print_commands));
   658 
   659 val print_contextP =
   660   OuterSyntax.improper_command "print_context" "print theory context name" K.diag
   661     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_context));
   662 
   663 val print_theoryP =
   664   OuterSyntax.improper_command "print_theory" "print logical theory contents (verbose!)" K.diag
   665     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theory));
   666 
   667 val print_syntaxP =
   668   OuterSyntax.improper_command "print_syntax" "print inner syntax of theory (verbose!)" K.diag
   669     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_syntax));
   670 
   671 val print_theoremsP =
   672   OuterSyntax.improper_command "print_theorems"
   673       "print theorems of local theory or proof context" K.diag
   674     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theorems));
   675 
   676 val print_localesP =
   677   OuterSyntax.improper_command "print_locales" "print locales of this theory" K.diag
   678     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_locales));
   679 
   680 val print_localeP =
   681   OuterSyntax.improper_command "print_locale" "print locale expression in this theory" K.diag
   682     (Scan.optional (P.$$$ "!" >> K true) false -- locale_val
   683       >> (Toplevel.no_timing oo IsarCmd.print_locale));
   684 
   685 val print_registrationsP =
   686   OuterSyntax.improper_command "print_interps"
   687     "print interpretations of named locale" K.diag
   688     (Scan.optional (P.$$$ "!" >> K true) false -- P.xname
   689       >> (Toplevel.no_timing oo uncurry IsarCmd.print_registrations));
   690 
   691 val print_attributesP =
   692   OuterSyntax.improper_command "print_attributes" "print attributes of this theory" K.diag
   693     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_attributes));
   694 
   695 val print_simpsetP =
   696   OuterSyntax.improper_command "print_simpset" "print context of Simplifier" K.diag
   697     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_simpset));
   698 
   699 val print_rulesP =
   700   OuterSyntax.improper_command "print_rules" "print intro/elim rules" K.diag
   701     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_rules));
   702 
   703 val print_induct_rulesP =
   704   OuterSyntax.improper_command "print_induct_rules" "print induction and cases rules" K.diag
   705     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_induct_rules));
   706 
   707 val print_trans_rulesP =
   708   OuterSyntax.improper_command "print_trans_rules" "print transitivity rules" K.diag
   709     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_trans_rules));
   710 
   711 val print_methodsP =
   712   OuterSyntax.improper_command "print_methods" "print methods of this theory" K.diag
   713     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_methods));
   714 
   715 val print_antiquotationsP =
   716   OuterSyntax.improper_command "print_antiquotations" "print antiquotations (global)" K.diag
   717     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_antiquotations));
   718 
   719 val thm_depsP =
   720   OuterSyntax.improper_command "thm_deps" "visualize theorem dependencies"
   721     K.diag (P.xthms1 >> (Toplevel.no_timing oo IsarCmd.thm_deps));
   722 
   723 val criterion =
   724   P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> FindTheorems.Name ||
   725   P.reserved "intro" >> K FindTheorems.Intro ||
   726   P.reserved "elim" >> K FindTheorems.Elim ||
   727   P.reserved "dest" >> K FindTheorems.Dest ||
   728   P.reserved "simp" |-- P.!!! (P.$$$ ":" |-- P.term) >> FindTheorems.Simp ||
   729   P.term >> FindTheorems.Pattern;
   730 
   731 val find_theoremsP =
   732   OuterSyntax.improper_command "find_theorems"
   733     "print theorems meeting specified criteria" K.diag
   734     (Scan.option (P.$$$ "(" |-- P.!!! (P.nat --| P.$$$ ")")) --
   735      Scan.repeat (((Scan.option P.minus >> is_none) -- criterion))
   736       >> (Toplevel.no_timing oo IsarCmd.find_theorems));
   737 
   738 val print_bindsP =
   739   OuterSyntax.improper_command "print_binds" "print term bindings of proof context" K.diag
   740     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_binds));
   741 
   742 val print_lthmsP =
   743   OuterSyntax.improper_command "print_facts" "print facts of proof context" K.diag
   744     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_lthms));
   745 
   746 val print_casesP =
   747   OuterSyntax.improper_command "print_cases" "print cases of proof context" K.diag
   748     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_cases));
   749 
   750 val print_stmtsP =
   751   OuterSyntax.improper_command "print_statement" "print theorems as long statements" K.diag
   752     (opt_modes -- P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_stmts));
   753 
   754 val print_thmsP =
   755   OuterSyntax.improper_command "thm" "print theorems" K.diag
   756     (opt_modes -- P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_thms));
   757 
   758 val print_prfsP =
   759   OuterSyntax.improper_command "prf" "print proof terms of theorems" K.diag
   760     (opt_modes -- Scan.option P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs false));
   761 
   762 val print_full_prfsP =
   763   OuterSyntax.improper_command "full_prf" "print full proof terms of theorems" K.diag
   764     (opt_modes -- Scan.option P.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs true));
   765 
   766 val print_propP =
   767   OuterSyntax.improper_command "prop" "read and print proposition" K.diag
   768     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_prop));
   769 
   770 val print_termP =
   771   OuterSyntax.improper_command "term" "read and print term" K.diag
   772     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_term));
   773 
   774 val print_typeP =
   775   OuterSyntax.improper_command "typ" "read and print type" K.diag
   776     (opt_modes -- P.typ >> (Toplevel.no_timing oo IsarCmd.print_type));
   777 
   778 
   779 
   780 (** system commands (for interactive mode only) **)
   781 
   782 val cdP =
   783   OuterSyntax.improper_command "cd" "change current working directory" K.diag
   784     (P.path >> (Toplevel.no_timing oo IsarCmd.cd));
   785 
   786 val pwdP =
   787   OuterSyntax.improper_command "pwd" "print current working directory" K.diag
   788     (Scan.succeed (Toplevel.no_timing o IsarCmd.pwd));
   789 
   790 val use_thyP =
   791   OuterSyntax.improper_command "use_thy" "use theory file" K.diag
   792     (P.name >> (Toplevel.no_timing oo IsarCmd.use_thy));
   793 
   794 val use_thy_onlyP =
   795   OuterSyntax.improper_command "use_thy_only" "use theory file only, ignoring associated ML"
   796     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.use_thy_only));
   797 
   798 val update_thyP =
   799   OuterSyntax.improper_command "update_thy" "update theory file" K.diag
   800     (P.name >> (Toplevel.no_timing oo IsarCmd.update_thy));
   801 
   802 val update_thy_onlyP =
   803   OuterSyntax.improper_command "update_thy_only" "update theory file, ignoring associated ML"
   804     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.update_thy_only));
   805 
   806 val touch_thyP =
   807   OuterSyntax.improper_command "touch_thy" "outdate theory, including descendants" K.diag
   808     (P.name >> (Toplevel.no_timing oo IsarCmd.touch_thy));
   809 
   810 val touch_all_thysP =
   811   OuterSyntax.improper_command "touch_all_thys" "outdate all non-finished theories" K.diag
   812     (Scan.succeed (Toplevel.no_timing o IsarCmd.touch_all_thys));
   813 
   814 val touch_child_thysP =
   815   OuterSyntax.improper_command "touch_child_thys" "outdate child theories" K.diag
   816     (P.name >> (Toplevel.no_timing oo IsarCmd.touch_child_thys));
   817 
   818 val remove_thyP =
   819   OuterSyntax.improper_command "remove_thy" "remove theory from loader database" K.diag
   820     (P.name >> (Toplevel.no_timing oo IsarCmd.remove_thy));
   821 
   822 val kill_thyP =
   823   OuterSyntax.improper_command "kill_thy" "kill theory -- try to remove from loader database"
   824     K.diag (P.name >> (Toplevel.no_timing oo IsarCmd.kill_thy));
   825 
   826 val display_draftsP =
   827   OuterSyntax.improper_command "display_drafts" "display raw source files with symbols"
   828     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.display_drafts));
   829 
   830 val print_draftsP =
   831   OuterSyntax.improper_command "print_drafts" "print raw source files with symbols"
   832     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.print_drafts));
   833 
   834 val opt_limits =
   835   Scan.option P.nat -- Scan.option (P.$$$ "," |-- P.!!! P.nat);
   836 
   837 val prP =
   838   OuterSyntax.improper_command "pr" "print current proof state (if present)" K.diag
   839     (opt_modes -- opt_limits >> (Toplevel.no_timing oo IsarCmd.pr));
   840 
   841 val disable_prP =
   842   OuterSyntax.improper_command "disable_pr" "disable printing of toplevel state" K.diag
   843     (Scan.succeed (Toplevel.no_timing o IsarCmd.disable_pr));
   844 
   845 val enable_prP =
   846   OuterSyntax.improper_command "enable_pr" "enable printing of toplevel state" K.diag
   847     (Scan.succeed (Toplevel.no_timing o IsarCmd.enable_pr));
   848 
   849 val commitP =
   850   OuterSyntax.improper_command "commit" "commit current session to ML database" K.diag
   851     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.use_commit));
   852 
   853 val quitP =
   854   OuterSyntax.improper_command "quit" "quit Isabelle" K.control
   855     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.quit));
   856 
   857 val exitP =
   858   OuterSyntax.improper_command "exit" "exit Isar loop" K.control
   859     (Scan.succeed (Toplevel.no_timing o IsarCmd.exit));
   860 
   861 val init_toplevelP =
   862   OuterSyntax.improper_command "init_toplevel" "restart Isar toplevel loop" K.control
   863     (Scan.succeed (Toplevel.no_timing o IsarCmd.init_toplevel));
   864 
   865 val welcomeP =
   866   OuterSyntax.improper_command "welcome" "print welcome message" K.diag
   867     (Scan.succeed (Toplevel.no_timing o IsarCmd.welcome));
   868 
   869 
   870 
   871 (** the Pure outer syntax **)
   872 
   873 (*keep keywords consistent with the parsers, including those in
   874   outer_parse.ML, otherwise be prepared for unexpected errors*)
   875 
   876 val _ = OuterSyntax.add_keywords
   877  ["!", "!!", "%", "(", ")", "+", ",", "--", ":", "::", ";", "<", "<=",
   878   "=", "==", "=>", "?", "[", "]", "advanced", "and", "assumes",
   879   "begin", "binder", "concl", "constrains", "defines", "fixes", "for",
   880   "imports", "if", "in", "includes", "infix", "infixl", "infixr",
   881   "is", "notes", "obtains", "open", "output", "overloaded", "shows",
   882   "structure", "unchecked", "uses", "where", "|", "\\<equiv>",
   883   "\\<leftharpoondown>", "\\<rightharpoonup>",
   884   "\\<rightleftharpoons>", "\\<subseteq>"];
   885 
   886 val _ = OuterSyntax.add_parsers [
   887   (*theory structure*)
   888   theoryP, end_excursionP, contextP,
   889   (*markup commands*)
   890   headerP, chapterP, sectionP, subsectionP, subsubsectionP, textP,
   891   text_rawP, sectP, subsectP, subsubsectP, txtP, txt_rawP,
   892   (*theory sections*)
   893   classesP, classrelP, defaultsortP, axclassP, typedeclP, typeabbrP,
   894   nontermP, aritiesP, judgmentP, constsP, finalconstsP, syntaxP,
   895   no_syntaxP, translationsP, no_translationsP, axiomsP, defsP,
   896   constdefsP, definitionP, abbreviationP, const_syntaxP,
   897   axiomatizationP, theoremsP, lemmasP, declareP, globalP, localP,
   898   hideP, useP, mlP, ml_commandP, ml_setupP, setupP, method_setupP,
   899   parse_ast_translationP, parse_translationP, print_translationP,
   900   typed_print_translationP, print_ast_translationP,
   901   token_translationP, oracleP, localeP,
   902   (*proof commands*)
   903   theoremP, lemmaP, corollaryP, haveP, henceP, showP, thusP, fixP,
   904   assumeP, presumeP, defP, obtainP, guessP, letP, caseP, thenP, fromP,
   905   withP, noteP, usingP, unfoldingP, beginP, endP, nextP, qedP,
   906   terminal_proofP, default_proofP, immediate_proofP, done_proofP,
   907   skip_proofP, forget_proofP, deferP, preferP, applyP, apply_endP,
   908   proofP, alsoP, finallyP, moreoverP, ultimatelyP, backP,
   909   cannot_undoP, clear_undosP, redoP, undos_proofP, undoP, killP,
   910   interpretationP, interpretP,
   911   (*diagnostic commands*)
   912   pretty_setmarginP, print_commandsP, print_contextP, print_theoryP,
   913   print_syntaxP, print_theoremsP, print_localesP, print_localeP,
   914   print_registrationsP, print_attributesP, print_simpsetP,
   915   print_rulesP, print_induct_rulesP, print_trans_rulesP,
   916   print_methodsP, print_antiquotationsP, thm_depsP, find_theoremsP,
   917   print_bindsP, print_lthmsP, print_casesP, print_stmtsP, print_thmsP,
   918   print_prfsP, print_full_prfsP, print_propP, print_termP,
   919   print_typeP,
   920   (*system commands*)
   921   cdP, pwdP, use_thyP, use_thy_onlyP, update_thyP, update_thy_onlyP,
   922   touch_thyP, touch_all_thysP, touch_child_thysP, remove_thyP,
   923   kill_thyP, display_draftsP, print_draftsP, prP, disable_prP,
   924   enable_prP, commitP, quitP, exitP, init_toplevelP, welcomeP];
   925 
   926 end;