src/Pure/Isar/isar_syn.ML
author wenzelm
Wed Oct 11 00:27:31 2006 +0200 (2006-10-11 ago)
changeset 20958 802705101b2a
parent 20907 9673c67dde9b
child 20979 7e5ba4a1f72f
permissions -rw-r--r--
'end': handle local theory;
'locale': begin local theory;
     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.print o Toplevel.exit o Toplevel.exit_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 >> (Toplevel.print oo IsarThy.context));
    28 
    29 
    30 
    31 (** markup commands **)
    32 
    33 val headerP = OuterSyntax.markup_command IsarOutput.Markup "header" "theory header" K.diag
    34   (P.position P.text >> IsarCmd.add_header);
    35 
    36 val chapterP = OuterSyntax.markup_command IsarOutput.Markup "chapter" "chapter heading"
    37   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_chapter);
    38 
    39 val sectionP = OuterSyntax.markup_command IsarOutput.Markup "section" "section heading"
    40   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_section);
    41 
    42 val subsectionP = OuterSyntax.markup_command IsarOutput.Markup "subsection" "subsection heading"
    43   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_subsection);
    44 
    45 val subsubsectionP =
    46   OuterSyntax.markup_command IsarOutput.Markup "subsubsection" "subsubsection heading"
    47   K.thy_heading (P.opt_locale_target -- P.position P.text >> IsarCmd.add_subsubsection);
    48 
    49 val textP = OuterSyntax.markup_command IsarOutput.MarkupEnv "text" "formal comment (theory)"
    50   K.thy_decl (P.opt_locale_target -- P.position P.text >> IsarCmd.add_text);
    51 
    52 val text_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "text_raw"
    53   "raw document preparation text"
    54   K.thy_decl (P.position P.text >> IsarCmd.add_text_raw);
    55 
    56 val sectP = OuterSyntax.markup_command IsarOutput.Markup "sect" "formal comment (proof)"
    57   (K.tag_proof K.prf_heading) (P.position P.text >> IsarCmd.add_sect);
    58 
    59 val subsectP = OuterSyntax.markup_command IsarOutput.Markup "subsect" "formal comment (proof)"
    60   (K.tag_proof K.prf_heading) (P.position P.text >> IsarCmd.add_subsect);
    61 
    62 val subsubsectP = OuterSyntax.markup_command IsarOutput.Markup "subsubsect"
    63   "formal comment (proof)" (K.tag_proof K.prf_heading)
    64   (P.position P.text >> IsarCmd.add_subsubsect);
    65 
    66 val txtP = OuterSyntax.markup_command IsarOutput.MarkupEnv "txt" "formal comment (proof)"
    67   (K.tag_proof K.prf_decl) (P.position P.text >> IsarCmd.add_txt);
    68 
    69 val txt_rawP = OuterSyntax.markup_command IsarOutput.Verbatim "txt_raw"
    70   "raw document preparation text (proof)" (K.tag_proof K.prf_decl)
    71   (P.position P.text >> IsarCmd.add_txt_raw);
    72 
    73 
    74 
    75 (** theory sections **)
    76 
    77 (* classes and sorts *)
    78 
    79 val classesP =
    80   OuterSyntax.command "classes" "declare type classes" K.thy_decl
    81     (Scan.repeat1 (P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    82         P.!!! (P.list1 P.xname)) []) >> (Toplevel.theory o fold AxClass.axiomatize_class));
    83 
    84 val classrelP =
    85   OuterSyntax.command "classrel" "state inclusion of type classes (axiomatic!)" K.thy_decl
    86     (P.and_list1 (P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname))
    87     >> (Toplevel.theory o AxClass.axiomatize_classrel));
    88 
    89 val defaultsortP =
    90   OuterSyntax.command "defaultsort" "declare default sort" K.thy_decl
    91     (P.sort >> (Toplevel.theory o Theory.add_defsort));
    92 
    93 val axclassP =
    94   OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
    95     ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    96         P.!!! (P.list1 P.xname)) []) --
    97           Scan.repeat (P.thm_name ":" -- (P.prop >> single))
    98       >> (fn (x, y) => Toplevel.theory (snd o AxClass.define_class x [] y)));
    99 
   100 
   101 (* types *)
   102 
   103 val typedeclP =
   104   OuterSyntax.command "typedecl" "type declaration" K.thy_decl
   105     (P.type_args -- P.name -- P.opt_infix >> (fn ((args, a), mx) =>
   106       Toplevel.theory (Theory.add_typedecls [(a, args, mx)])));
   107 
   108 val typeabbrP =
   109   OuterSyntax.command "types" "declare type abbreviations" K.thy_decl
   110     (Scan.repeat1
   111       (P.type_args -- P.name -- (P.$$$ "=" |-- P.!!! (P.typ -- P.opt_infix')))
   112       >> (Toplevel.theory o Theory.add_tyabbrs o
   113         map (fn ((args, a), (T, mx)) => (a, args, T, mx))));
   114 
   115 val nontermP =
   116   OuterSyntax.command "nonterminals" "declare types treated as grammar nonterminal symbols"
   117     K.thy_decl (Scan.repeat1 P.name >> (Toplevel.theory o Theory.add_nonterminals));
   118 
   119 val aritiesP =
   120   OuterSyntax.command "arities" "state type arities (axiomatic!)" K.thy_decl
   121     (Scan.repeat1 (P.xname -- (P.$$$ "::" |-- P.!!! P.arity) >> P.triple2)
   122       >> (Toplevel.theory o fold AxClass.axiomatize_arity));
   123 
   124 
   125 (* consts and syntax *)
   126 
   127 val judgmentP =
   128   OuterSyntax.command "judgment" "declare object-logic judgment" K.thy_decl
   129     (P.const >> (Toplevel.theory o ObjectLogic.add_judgment));
   130 
   131 val constsP =
   132   OuterSyntax.command "consts" "declare constants" K.thy_decl
   133     (Scan.repeat1 P.const >> (Toplevel.theory o Theory.add_consts));
   134 
   135 val opt_overloaded = P.opt_keyword "overloaded";
   136 
   137 val finalconstsP =
   138   OuterSyntax.command "finalconsts" "declare constants as final" K.thy_decl
   139     (opt_overloaded -- Scan.repeat1 P.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   140 
   141 val mode_spec =
   142   (P.$$$ "output" >> K ("", false)) || P.name -- Scan.optional (P.$$$ "output" >> K false) true;
   143 
   144 val opt_mode =
   145   Scan.optional (P.$$$ "(" |-- P.!!! (mode_spec --| P.$$$ ")")) Syntax.default_mode;
   146 
   147 val syntaxP =
   148   OuterSyntax.command "syntax" "declare syntactic constants" K.thy_decl
   149     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Theory.add_modesyntax));
   150 
   151 val no_syntaxP =
   152   OuterSyntax.command "no_syntax" "delete syntax declarations" K.thy_decl
   153     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Theory.del_modesyntax));
   154 
   155 
   156 (* translations *)
   157 
   158 val trans_pat =
   159   Scan.optional (P.$$$ "(" |-- P.!!! (P.xname --| P.$$$ ")")) "logic" -- P.string;
   160 
   161 fun trans_arrow toks =
   162   ((P.$$$ "\\<rightharpoonup>" || P.$$$ "=>") >> K Syntax.ParseRule ||
   163     (P.$$$ "\\<leftharpoondown>" || P.$$$ "<=") >> K Syntax.PrintRule ||
   164     (P.$$$ "\\<rightleftharpoons>" || P.$$$ "==") >> K Syntax.ParsePrintRule) toks;
   165 
   166 val trans_line =
   167   trans_pat -- P.!!! (trans_arrow -- trans_pat)
   168     >> (fn (left, (arr, right)) => arr (left, right));
   169 
   170 val translationsP =
   171   OuterSyntax.command "translations" "declare syntax translation rules" K.thy_decl
   172     (Scan.repeat1 trans_line >> (Toplevel.theory o Theory.add_trrules));
   173 
   174 val no_translationsP =
   175   OuterSyntax.command "no_translations" "remove syntax translation rules" K.thy_decl
   176     (Scan.repeat1 trans_line >> (Toplevel.theory o Theory.del_trrules));
   177 
   178 
   179 (* axioms and definitions *)
   180 
   181 val axiomsP =
   182   OuterSyntax.command "axioms" "state arbitrary propositions (axiomatic!)" K.thy_decl
   183     (Scan.repeat1 P.spec_name >> (Toplevel.theory o IsarThy.add_axioms));
   184 
   185 val opt_unchecked_overloaded =
   186   Scan.optional (P.$$$ "(" |-- P.!!!
   187     (((P.$$$ "unchecked" >> K true) -- Scan.optional (P.$$$ "overloaded" >> K true) false ||
   188       P.$$$ "overloaded" >> K (false, true)) --| P.$$$ ")")) (false, false);
   189 
   190 val defsP =
   191   OuterSyntax.command "defs" "define constants" K.thy_decl
   192     (opt_unchecked_overloaded -- Scan.repeat1 P.spec_name
   193       >> (Toplevel.theory o IsarThy.add_defs));
   194 
   195 
   196 (* constant definitions and abbreviations *)
   197 
   198 val constdecl =
   199   (P.name --| P.$$$ "where") >> (fn x => (x, NONE, NoSyn)) ||
   200     P.name -- (P.$$$ "::" |-- P.!!! P.typ >> SOME) -- P.opt_mixfix'
   201       --| Scan.option (P.$$$ "where") >> P.triple1 ||
   202     P.name -- (P.mixfix >> pair NONE) --| Scan.option (P.$$$ "where") >> P.triple2;
   203 
   204 val constdef = Scan.option constdecl -- (P.opt_thm_name ":" -- P.prop);
   205 
   206 val structs =
   207   Scan.optional ((P.$$$ "(" -- P.$$$ "structure") |-- P.!!! (P.simple_fixes --| P.$$$ ")")) [];
   208 
   209 val constdefsP =
   210   OuterSyntax.command "constdefs" "old-style constant definition" K.thy_decl
   211     (structs -- Scan.repeat1 constdef >> (Toplevel.theory o Constdefs.add_constdefs));
   212 
   213 val definitionP =
   214   OuterSyntax.command "definition" "constant definition" K.thy_decl
   215     (P.opt_locale_target -- Scan.repeat1 constdef
   216     >> (fn (loc, args) => Toplevel.local_theory loc (#2 o Specification.definition args)));
   217 
   218 val abbreviationP =
   219   OuterSyntax.command "abbreviation" "constant abbreviation" K.thy_decl
   220     (P.opt_locale_target -- opt_mode --
   221       Scan.repeat1 (Scan.option constdecl -- P.prop)
   222     >> (fn ((loc, mode), args) =>
   223         Toplevel.local_theory loc (Specification.abbreviation mode args)));
   224 
   225 val const_syntaxP =
   226   OuterSyntax.command "const_syntax" "constant syntax" K.thy_decl
   227     (P.opt_locale_target -- opt_mode -- Scan.repeat1 (P.xname -- P.mixfix)
   228     >> (fn ((loc, mode), args) =>
   229         Toplevel.local_theory loc (Specification.const_syntax mode args)));
   230 
   231 
   232 (* constant specifications *)
   233 
   234 val axiomatizationP =
   235   OuterSyntax.command "axiomatization" "axiomatic constant specification" K.thy_decl
   236     (P.opt_locale_target --
   237      (Scan.optional P.fixes [] --
   238       Scan.optional (P.$$$ "where" |-- P.!!! (P.and_list1 P.spec)) [])
   239     >> (fn (loc, (x, y)) => Toplevel.local_theory loc (#2 o Specification.axiomatization x y)));
   240 
   241 
   242 (* theorems *)
   243 
   244 fun theorems kind = P.opt_locale_target -- P.name_facts
   245   >> (fn (loc, args) => Toplevel.local_theory loc (#2 o Specification.theorems kind args));
   246 
   247 val theoremsP =
   248   OuterSyntax.command "theorems" "define theorems" K.thy_decl (theorems PureThy.theoremK);
   249 
   250 val lemmasP =
   251   OuterSyntax.command "lemmas" "define lemmas" K.thy_decl (theorems PureThy.lemmaK);
   252 
   253 val declareP =
   254   OuterSyntax.command "declare" "declare theorems (improper)" K.thy_script
   255     (P.opt_locale_target -- (P.and_list1 P.xthms1 >> flat)
   256       >> (fn (loc, args) => Toplevel.local_theory loc
   257           (#2 o Specification.theorems "" [(("", []), args)])));
   258 
   259 
   260 (* name space entry path *)
   261 
   262 val globalP =
   263   OuterSyntax.command "global" "disable prefixing of theory name" K.thy_decl
   264     (Scan.succeed (Toplevel.theory Sign.root_path));
   265 
   266 val localP =
   267   OuterSyntax.command "local" "enable prefixing of theory name" K.thy_decl
   268     (Scan.succeed (Toplevel.theory Sign.local_path));
   269 
   270 val hideP =
   271   OuterSyntax.command "hide" "hide names from given name space" K.thy_decl
   272     ((P.opt_keyword "open" >> not) -- (P.name -- Scan.repeat1 P.xname) >>
   273       (Toplevel.theory o uncurry Sign.hide_names));
   274 
   275 
   276 (* use ML text *)
   277 
   278 val useP =
   279   OuterSyntax.command "use" "eval ML text from file" (K.tag_ml K.diag)
   280     (P.path >> (Toplevel.no_timing oo IsarCmd.use));
   281 
   282 val mlP =
   283   OuterSyntax.command "ML" "eval ML text (diagnostic)" (K.tag_ml K.diag)
   284     (P.text >> IsarCmd.use_mltext true);
   285 
   286 val ml_commandP =
   287   OuterSyntax.command "ML_command" "eval ML text" (K.tag_ml K.diag)
   288     (P.text >> (Toplevel.no_timing oo IsarCmd.use_mltext false));
   289 
   290 val ml_setupP =
   291   OuterSyntax.command "ML_setup" "eval ML text (may change theory)" (K.tag_ml K.thy_decl)
   292     (P.text >> IsarCmd.use_mltext_theory);
   293 
   294 val setupP =
   295   OuterSyntax.command "setup" "apply ML theory setup" (K.tag_ml K.thy_decl)
   296     (Scan.option P.text >> (Toplevel.theory o PureThy.generic_setup));
   297 
   298 val method_setupP =
   299   OuterSyntax.command "method_setup" "define proof method in ML" (K.tag_ml K.thy_decl)
   300     (((P.name -- P.!!! (P.$$$ "=" |-- P.text -- P.text) >> P.triple2))
   301       >> (Toplevel.theory o Method.method_setup));
   302 
   303 
   304 (* translation functions *)
   305 
   306 val trfun = P.opt_keyword "advanced" -- P.text;
   307 
   308 val parse_ast_translationP =
   309   OuterSyntax.command "parse_ast_translation" "install parse ast translation functions"
   310     (K.tag_ml K.thy_decl)
   311     (trfun >> (Toplevel.theory o Sign.parse_ast_translation));
   312 
   313 val parse_translationP =
   314   OuterSyntax.command "parse_translation" "install parse translation functions"
   315     (K.tag_ml K.thy_decl)
   316     (trfun >> (Toplevel.theory o Sign.parse_translation));
   317 
   318 val print_translationP =
   319   OuterSyntax.command "print_translation" "install print translation functions"
   320     (K.tag_ml K.thy_decl)
   321     (trfun >> (Toplevel.theory o Sign.print_translation));
   322 
   323 val typed_print_translationP =
   324   OuterSyntax.command "typed_print_translation" "install typed print translation functions"
   325     (K.tag_ml K.thy_decl)
   326     (trfun >> (Toplevel.theory o Sign.typed_print_translation));
   327 
   328 val print_ast_translationP =
   329   OuterSyntax.command "print_ast_translation" "install print ast translation functions"
   330     (K.tag_ml K.thy_decl)
   331     (trfun >> (Toplevel.theory o Sign.print_ast_translation));
   332 
   333 val token_translationP =
   334   OuterSyntax.command "token_translation" "install token translation functions"
   335     (K.tag_ml K.thy_decl)
   336     (P.text >> (Toplevel.theory o Sign.token_translation));
   337 
   338 
   339 (* oracles *)
   340 
   341 val oracleP =
   342   OuterSyntax.command "oracle" "declare oracle" (K.tag_ml K.thy_decl)
   343     (P.name -- (P.$$$ "(" |-- P.text --| P.$$$ ")" --| P.$$$ "=")
   344       -- P.text >> (Toplevel.theory o PureThy.add_oracle o P.triple1));
   345 
   346 
   347 (* locales *)
   348 
   349 val locale_val =
   350   (P.locale_expr --
   351     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 P.context_element)) [] ||
   352   Scan.repeat1 P.context_element >> pair Locale.empty);
   353 
   354 val localeP =
   355   OuterSyntax.command "locale" "define named proof context" K.thy_decl
   356     ((P.opt_keyword "open" >> not) -- P.name
   357         -- Scan.optional (P.$$$ "=" |-- P.!!! locale_val) (Locale.empty, [])
   358         -- P.opt_begin
   359       >> (fn (((is_open, name), (expr, elems)), begin) =>
   360           Toplevel.begin_local_theory begin (Locale.add_locale is_open name expr elems)));
   361 
   362 val interpretationP =
   363   OuterSyntax.command "interpretation"
   364     "prove and register interpretation of locale expression in theory or locale" K.thy_goal
   365     (P.xname --| (P.$$$ "\\<subseteq>" || P.$$$ "<") -- P.!!! P.locale_expr
   366       >> (Toplevel.print oo (Toplevel.theory_to_proof o Locale.interpretation_in_locale I)) ||
   367       P.opt_thm_name ":" -- P.locale_expr -- P.locale_insts >> (fn ((x, y), z) =>
   368         Toplevel.print o Toplevel.theory_to_proof (Locale.interpretation I x y z)));
   369 
   370 val interpretP =
   371   OuterSyntax.command "interpret"
   372     "prove and register interpretation of locale expression in proof context"
   373     (K.tag_proof K.prf_goal)
   374     (P.opt_thm_name ":" -- P.locale_expr -- P.locale_insts >> (fn ((x, y), z) =>
   375       Toplevel.print o Toplevel.proof' (Locale.interpret Seq.single x y z)));
   376 
   377 
   378 
   379 (** proof commands **)
   380 
   381 (* statements *)
   382 
   383 fun gen_theorem kind =
   384   OuterSyntax.command kind ("state " ^ kind) K.thy_goal
   385     (P.opt_locale_target -- Scan.optional (P.opt_thm_name ":" --|
   386       Scan.ahead (P.locale_keyword || P.statement_keyword)) ("", []) --
   387       P.general_statement >> (fn ((x, y), (z, w)) =>
   388       (Toplevel.print o Toplevel.theory_to_proof (Locale.smart_theorem kind x y z w))));
   389 
   390 val theoremP = gen_theorem PureThy.theoremK;
   391 val lemmaP = gen_theorem PureThy.lemmaK;
   392 val corollaryP = gen_theorem PureThy.corollaryK;
   393 
   394 val haveP =
   395   OuterSyntax.command "have" "state local goal"
   396     (K.tag_proof K.prf_goal)
   397     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.have));
   398 
   399 val henceP =
   400   OuterSyntax.command "hence" "abbreviates \"then have\""
   401     (K.tag_proof K.prf_goal)
   402     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.hence));
   403 
   404 val showP =
   405   OuterSyntax.command "show" "state local goal, solving current obligation"
   406     (K.tag_proof K.prf_goal)
   407     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.show));
   408 
   409 val thusP =
   410   OuterSyntax.command "thus" "abbreviates \"then show\""
   411     (K.tag_proof K.prf_goal)
   412     (P.statement >> ((Toplevel.print oo Toplevel.proof') o IsarThy.thus));
   413 
   414 
   415 (* facts *)
   416 
   417 val facts = P.and_list1 P.xthms1;
   418 
   419 val thenP =
   420   OuterSyntax.command "then" "forward chaining"
   421     (K.tag_proof K.prf_chain)
   422     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   423 
   424 val fromP =
   425   OuterSyntax.command "from" "forward chaining from given facts"
   426     (K.tag_proof K.prf_chain)
   427     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss)));
   428 
   429 val withP =
   430   OuterSyntax.command "with" "forward chaining from given and current facts"
   431     (K.tag_proof K.prf_chain)
   432     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss)));
   433 
   434 val noteP =
   435   OuterSyntax.command "note" "define facts"
   436     (K.tag_proof K.prf_decl)
   437     (P.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss)));
   438 
   439 val usingP =
   440   OuterSyntax.command "using" "augment goal facts"
   441     (K.tag_proof K.prf_decl)
   442     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using)));
   443 
   444 val unfoldingP =
   445   OuterSyntax.command "unfolding" "unfold definitions in goal and facts"
   446     (K.tag_proof K.prf_decl)
   447     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding)));
   448 
   449 
   450 (* proof context *)
   451 
   452 val fixP =
   453   OuterSyntax.command "fix" "fix local variables (Skolem constants)"
   454     (K.tag_proof K.prf_asm)
   455     (P.fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix)));
   456 
   457 val assumeP =
   458   OuterSyntax.command "assume" "assume propositions"
   459     (K.tag_proof K.prf_asm)
   460     (P.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume)));
   461 
   462 val presumeP =
   463   OuterSyntax.command "presume" "assume propositions, to be established later"
   464     (K.tag_proof K.prf_asm)
   465     (P.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume)));
   466 
   467 val defP =
   468   OuterSyntax.command "def" "local definition"
   469     (K.tag_proof K.prf_asm)
   470     (P.and_list1
   471       (P.opt_thm_name ":" --
   472         ((P.name -- P.opt_mixfix) -- ((P.$$$ "\\<equiv>" || P.$$$ "==") |-- P.!!! P.termp)))
   473     >> (Toplevel.print oo (Toplevel.proof o Proof.def)));
   474 
   475 val obtainP =
   476   OuterSyntax.command "obtain" "generalized existence"
   477     (K.tag_proof K.prf_asm_goal)
   478     (P.parname -- Scan.optional (P.fixes --| P.$$$ "where") [] -- P.statement
   479       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain x y z)));
   480 
   481 val guessP =
   482   OuterSyntax.command "guess" "wild guessing (unstructured)"
   483     (K.tag_proof K.prf_asm_goal)
   484     (Scan.optional P.fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess)));
   485 
   486 val letP =
   487   OuterSyntax.command "let" "bind text variables"
   488     (K.tag_proof K.prf_decl)
   489     (P.and_list1 (P.enum1 "and" P.term -- (P.$$$ "=" |-- P.term))
   490       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind)));
   491 
   492 val case_spec =
   493   (P.$$$ "(" |-- P.!!! (P.xname -- Scan.repeat1 (P.maybe P.name) --| P.$$$ ")") ||
   494     P.xname >> rpair []) -- P.opt_attribs >> P.triple1;
   495 
   496 val caseP =
   497   OuterSyntax.command "case" "invoke local context"
   498     (K.tag_proof K.prf_asm)
   499     (case_spec >> (Toplevel.print oo (Toplevel.proof o Proof.invoke_case)));
   500 
   501 
   502 (* proof structure *)
   503 
   504 val begin_blockP =
   505   OuterSyntax.command "{" "begin explicit proof block"
   506     (K.tag_proof K.prf_open)
   507     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   508 
   509 val end_blockP =
   510   OuterSyntax.command "}" "end explicit proof block"
   511     (K.tag_proof K.prf_close)
   512     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.end_block));
   513 
   514 val nextP =
   515   OuterSyntax.command "next" "enter next proof block"
   516     (K.tag_proof K.prf_block)
   517     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   518 
   519 
   520 (* end proof *)
   521 
   522 val qedP =
   523   OuterSyntax.command "qed" "conclude (sub-)proof"
   524     (K.tag_proof K.qed_block)
   525     (Scan.option P.method >> (Toplevel.print3 oo IsarThy.qed));
   526 
   527 val terminal_proofP =
   528   OuterSyntax.command "by" "terminal backward proof"
   529     (K.tag_proof K.qed)
   530     (P.method -- Scan.option P.method >> (Toplevel.print3 oo IsarThy.terminal_proof));
   531 
   532 val default_proofP =
   533   OuterSyntax.command ".." "default proof"
   534     (K.tag_proof K.qed)
   535     (Scan.succeed (Toplevel.print3 o IsarThy.default_proof));
   536 
   537 val immediate_proofP =
   538   OuterSyntax.command "." "immediate proof"
   539     (K.tag_proof K.qed)
   540     (Scan.succeed (Toplevel.print3 o IsarThy.immediate_proof));
   541 
   542 val done_proofP =
   543   OuterSyntax.command "done" "done proof"
   544     (K.tag_proof K.qed)
   545     (Scan.succeed (Toplevel.print3 o IsarThy.done_proof));
   546 
   547 val skip_proofP =
   548   OuterSyntax.improper_command "sorry" "skip proof (quick-and-dirty mode only!)"
   549     (K.tag_proof K.qed)
   550     (Scan.succeed (Toplevel.print3 o IsarThy.skip_proof));
   551 
   552 val forget_proofP =
   553   OuterSyntax.command "oops" "forget proof"
   554     (K.tag_proof K.qed_global)
   555     (Scan.succeed Toplevel.forget_proof);
   556 
   557 
   558 (* proof steps *)
   559 
   560 val deferP =
   561   OuterSyntax.command "defer" "shuffle internal proof state"
   562     (K.tag_proof K.prf_script)
   563     (Scan.option P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.defer)));
   564 
   565 val preferP =
   566   OuterSyntax.command "prefer" "shuffle internal proof state"
   567     (K.tag_proof K.prf_script)
   568     (P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.prefer)));
   569 
   570 val applyP =
   571   OuterSyntax.command "apply" "initial refinement step (unstructured)"
   572     (K.tag_proof K.prf_script)
   573     (P.method >> (Toplevel.print oo (Toplevel.proofs o Proof.apply)));
   574 
   575 val apply_endP =
   576   OuterSyntax.command "apply_end" "terminal refinement (unstructured)"
   577     (K.tag_proof K.prf_script)
   578     (P.method >> (Toplevel.print oo (Toplevel.proofs o Proof.apply_end)));
   579 
   580 val proofP =
   581   OuterSyntax.command "proof" "backward proof"
   582     (K.tag_proof K.prf_block)
   583     (Scan.option P.method >> (fn m => Toplevel.print o
   584       Toplevel.actual_proof (ProofHistory.applys (Proof.proof m)) o
   585       Toplevel.skip_proof (History.apply (fn i => i + 1))));
   586 
   587 
   588 (* calculational proof commands *)
   589 
   590 val calc_args =
   591   Scan.option (P.$$$ "(" |-- P.!!! ((P.xthms1 --| P.$$$ ")")));
   592 
   593 val alsoP =
   594   OuterSyntax.command "also" "combine calculation and current facts"
   595     (K.tag_proof K.prf_decl)
   596     (calc_args >> (Toplevel.proofs' o Calculation.also));
   597 
   598 val finallyP =
   599   OuterSyntax.command "finally" "combine calculation and current facts, exhibit result"
   600     (K.tag_proof K.prf_chain)
   601     (calc_args >> (Toplevel.proofs' o Calculation.finally));
   602 
   603 val moreoverP =
   604   OuterSyntax.command "moreover" "augment calculation by current facts"
   605     (K.tag_proof K.prf_decl)
   606     (Scan.succeed (Toplevel.proof' Calculation.moreover));
   607 
   608 val ultimatelyP =
   609   OuterSyntax.command "ultimately" "augment calculation by current facts, exhibit result"
   610     (K.tag_proof K.prf_chain)
   611     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
   612 
   613 
   614 (* proof navigation *)
   615 
   616 val backP =
   617   OuterSyntax.command "back" "backtracking of proof command"
   618     (K.tag_proof K.prf_script)
   619     (Scan.succeed (Toplevel.print o IsarCmd.back));
   620 
   621 
   622 (* history *)
   623 
   624 val cannot_undoP =    (* FIXME ProofGeneral compatibility *)
   625   OuterSyntax.improper_command "cannot_undo" "attempt to undo 'end'" K.control
   626     (P.name >> K (Toplevel.no_timing o IsarCmd.undo_end));
   627 
   628 val undo_endP =
   629   OuterSyntax.improper_command "undo_end" "attempt to undo 'end'" K.control
   630     (Scan.succeed (Toplevel.no_timing o IsarCmd.undo_end));
   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_lthmsP =
   754   OuterSyntax.improper_command "print_facts" "print facts of proof context" K.diag
   755     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_lthms));
   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, const_syntaxP,
   908   axiomatizationP, theoremsP, lemmasP, declareP, globalP, localP,
   909   hideP, useP, mlP, 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, undo_endP, clear_undosP, redoP, undos_proofP, undoP,
   921   killP, interpretationP, interpretP,
   922   (*diagnostic commands*)
   923   pretty_setmarginP, print_commandsP, print_contextP, print_theoryP,
   924   print_syntaxP, print_theoremsP, print_localesP, print_localeP,
   925   print_registrationsP, print_attributesP, print_simpsetP,
   926   print_rulesP, print_induct_rulesP, print_trans_rulesP,
   927   print_methodsP, print_antiquotationsP, class_depsP, thm_depsP,
   928   find_theoremsP, print_bindsP, print_lthmsP, 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;