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