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