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