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