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