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