src/Pure/Isar/isar_syn.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 30189 3633f560f4c3
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
use long names for old-style fold combinators;
     1 (*  Title:      Pure/Isar/isar_syn.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Isar/Pure outer syntax.
     5 *)
     6 
     7 structure IsarSyn: sig end =
     8 struct
     9 
    10 structure P = OuterParse and K = OuterKeyword;
    11 
    12 
    13 (** keywords **)
    14 
    15 (*keep keywords consistent with the parsers, otherwise be prepared for
    16   unexpected errors*)
    17 
    18 val _ = List.app OuterKeyword.keyword
    19  ["!!", "!", "%", "(", ")", "+", ",", "--", ":", "::", ";", "<", "<=",
    20   "=", "==", "=>", "?", "[", "\\<equiv>", "\\<leftharpoondown>",
    21   "\\<rightharpoonup>", "\\<rightleftharpoons>", "\\<subseteq>", "]",
    22   "advanced", "and", "assumes", "attach", "begin", "binder",
    23   "constrains", "defines", "fixes", "for", "identifier", "if",
    24   "imports", "in", "infix", "infixl", "infixr", "is",
    25   "notes", "obtains", "open", "output", "overloaded", "shows",
    26   "structure", "unchecked", "uses", "where", "|"];
    27 
    28 
    29 (** init and exit **)
    30 
    31 val _ =
    32   OuterSyntax.command "theory" "begin theory" (K.tag_theory K.thy_begin)
    33     (ThyHeader.args >> (Toplevel.print oo IsarCmd.init_theory));
    34 
    35 val _ =
    36   OuterSyntax.command "end" "end (local) theory" (K.tag_theory K.thy_end)
    37     (Scan.succeed (Toplevel.exit o Toplevel.end_local_theory));
    38 
    39 
    40 
    41 (** markup commands **)
    42 
    43 val _ = OuterSyntax.markup_command ThyOutput.Markup "header" "theory header" K.diag
    44   (P.doc_source >> IsarCmd.header_markup);
    45 
    46 val _ = OuterSyntax.markup_command ThyOutput.Markup "chapter" "chapter heading"
    47   K.thy_heading (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    48 
    49 val _ = OuterSyntax.markup_command ThyOutput.Markup "section" "section heading"
    50   K.thy_heading (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    51 
    52 val _ = OuterSyntax.markup_command ThyOutput.Markup "subsection" "subsection heading"
    53   K.thy_heading (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    54 
    55 val _ =
    56   OuterSyntax.markup_command ThyOutput.Markup "subsubsection" "subsubsection heading"
    57   K.thy_heading (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    58 
    59 val _ = OuterSyntax.markup_command ThyOutput.MarkupEnv "text" "formal comment (theory)"
    60   K.thy_decl (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    61 
    62 val _ = OuterSyntax.markup_command ThyOutput.Verbatim "text_raw" "raw document preparation text"
    63   K.thy_decl (P.opt_target -- P.doc_source >> IsarCmd.local_theory_markup);
    64 
    65 val _ = OuterSyntax.markup_command ThyOutput.Markup "sect" "formal comment (proof)"
    66   (K.tag_proof K.prf_heading) (P.doc_source >> IsarCmd.proof_markup);
    67 
    68 val _ = OuterSyntax.markup_command ThyOutput.Markup "subsect" "formal comment (proof)"
    69   (K.tag_proof K.prf_heading) (P.doc_source >> IsarCmd.proof_markup);
    70 
    71 val _ = OuterSyntax.markup_command ThyOutput.Markup "subsubsect" "formal comment (proof)"
    72   (K.tag_proof K.prf_heading) (P.doc_source >> IsarCmd.proof_markup);
    73 
    74 val _ = OuterSyntax.markup_command ThyOutput.MarkupEnv "txt" "formal comment (proof)"
    75   (K.tag_proof K.prf_decl) (P.doc_source >> IsarCmd.proof_markup);
    76 
    77 val _ = OuterSyntax.markup_command ThyOutput.Verbatim "txt_raw"
    78   "raw document preparation text (proof)" (K.tag_proof K.prf_decl)
    79   (P.doc_source >> IsarCmd.proof_markup);
    80 
    81 
    82 
    83 (** theory commands **)
    84 
    85 (* classes and sorts *)
    86 
    87 val _ =
    88   OuterSyntax.command "classes" "declare type classes" K.thy_decl
    89     (Scan.repeat1 (P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
    90         P.!!! (P.list1 P.xname)) []) >> (Toplevel.theory o fold AxClass.axiomatize_class_cmd));
    91 
    92 val _ =
    93   OuterSyntax.command "classrel" "state inclusion of type classes (axiomatic!)" K.thy_decl
    94     (P.and_list1 (P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname))
    95     >> (Toplevel.theory o AxClass.axiomatize_classrel_cmd));
    96 
    97 val _ =
    98   OuterSyntax.command "defaultsort" "declare default sort" K.thy_decl
    99     (P.sort >> (Toplevel.theory o Sign.add_defsort));
   100 
   101 val _ =
   102   OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
   103     (P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
   104         P.!!! (P.list1 P.xname)) []
   105         -- Scan.repeat (SpecParse.thm_name ":" -- (P.prop >> single))
   106       >> (fn (x, y) => Toplevel.theory (snd o Class.axclass_cmd x y)));
   107 
   108 
   109 (* types *)
   110 
   111 val _ =
   112   OuterSyntax.command "typedecl" "type declaration" K.thy_decl
   113     (P.type_args -- P.name -- P.opt_infix >> (fn ((args, a), mx) =>
   114       Toplevel.theory (ObjectLogic.typedecl (a, args, mx) #> snd)));
   115 
   116 val _ =
   117   OuterSyntax.command "types" "declare type abbreviations" K.thy_decl
   118     (Scan.repeat1
   119       (P.type_args -- P.name -- (P.$$$ "=" |-- P.!!! (P.typ -- P.opt_infix')))
   120       >> (Toplevel.theory o Sign.add_tyabbrs o
   121         map (fn ((args, a), (T, mx)) => (a, args, T, mx))));
   122 
   123 val _ =
   124   OuterSyntax.command "nonterminals" "declare types treated as grammar nonterminal symbols"
   125     K.thy_decl (Scan.repeat1 P.name >> (Toplevel.theory o Sign.add_nonterminals));
   126 
   127 val _ =
   128   OuterSyntax.command "arities" "state type arities (axiomatic!)" K.thy_decl
   129     (Scan.repeat1 P.arity >> (Toplevel.theory o fold AxClass.axiomatize_arity_cmd));
   130 
   131 
   132 (* consts and syntax *)
   133 
   134 val _ =
   135   OuterSyntax.command "judgment" "declare object-logic judgment" K.thy_decl
   136     (P.const >> (Toplevel.theory o ObjectLogic.add_judgment));
   137 
   138 val _ =
   139   OuterSyntax.command "consts" "declare constants" K.thy_decl
   140     (Scan.repeat1 P.const >> (Toplevel.theory o Sign.add_consts));
   141 
   142 val opt_overloaded = P.opt_keyword "overloaded";
   143 
   144 val _ =
   145   OuterSyntax.command "finalconsts" "declare constants as final" K.thy_decl
   146     (opt_overloaded -- Scan.repeat1 P.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   147 
   148 val mode_spec =
   149   (P.$$$ "output" >> K ("", false)) || P.name -- Scan.optional (P.$$$ "output" >> K false) true;
   150 
   151 val opt_mode =
   152   Scan.optional (P.$$$ "(" |-- P.!!! (mode_spec --| P.$$$ ")")) Syntax.mode_default;
   153 
   154 val _ =
   155   OuterSyntax.command "syntax" "declare syntactic constants" K.thy_decl
   156     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Sign.add_modesyntax));
   157 
   158 val _ =
   159   OuterSyntax.command "no_syntax" "delete syntax declarations" K.thy_decl
   160     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Sign.del_modesyntax));
   161 
   162 
   163 (* translations *)
   164 
   165 val trans_pat =
   166   Scan.optional (P.$$$ "(" |-- P.!!! (P.xname --| P.$$$ ")")) "logic" -- P.string;
   167 
   168 fun trans_arrow toks =
   169   ((P.$$$ "\\<rightharpoonup>" || P.$$$ "=>") >> K Syntax.ParseRule ||
   170     (P.$$$ "\\<leftharpoondown>" || P.$$$ "<=") >> K Syntax.PrintRule ||
   171     (P.$$$ "\\<rightleftharpoons>" || P.$$$ "==") >> K Syntax.ParsePrintRule) toks;
   172 
   173 val trans_line =
   174   trans_pat -- P.!!! (trans_arrow -- trans_pat)
   175     >> (fn (left, (arr, right)) => arr (left, right));
   176 
   177 val _ =
   178   OuterSyntax.command "translations" "declare syntax translation rules" K.thy_decl
   179     (Scan.repeat1 trans_line >> (Toplevel.theory o Sign.add_trrules));
   180 
   181 val _ =
   182   OuterSyntax.command "no_translations" "remove syntax translation rules" K.thy_decl
   183     (Scan.repeat1 trans_line >> (Toplevel.theory o Sign.del_trrules));
   184 
   185 
   186 (* axioms and definitions *)
   187 
   188 val _ =
   189   OuterSyntax.command "axioms" "state arbitrary propositions (axiomatic!)" K.thy_decl
   190     (Scan.repeat1 SpecParse.spec_name >> (Toplevel.theory o IsarCmd.add_axioms));
   191 
   192 val opt_unchecked_overloaded =
   193   Scan.optional (P.$$$ "(" |-- P.!!!
   194     (((P.$$$ "unchecked" >> K true) -- Scan.optional (P.$$$ "overloaded" >> K true) false ||
   195       P.$$$ "overloaded" >> K (false, true)) --| P.$$$ ")")) (false, false);
   196 
   197 val _ =
   198   OuterSyntax.command "defs" "define constants" K.thy_decl
   199     (opt_unchecked_overloaded -- Scan.repeat1 SpecParse.spec_name
   200       >> (Toplevel.theory o IsarCmd.add_defs));
   201 
   202 
   203 (* old constdefs *)
   204 
   205 val old_constdecl =
   206   P.binding --| P.where_ >> (fn x => (x, NONE, NoSyn)) ||
   207   P.binding -- (P.$$$ "::" |-- P.!!! P.typ >> SOME) -- P.opt_mixfix'
   208     --| Scan.option P.where_ >> P.triple1 ||
   209   P.binding -- (P.mixfix >> pair NONE) --| Scan.option P.where_ >> P.triple2;
   210 
   211 val old_constdef = Scan.option old_constdecl -- (SpecParse.opt_thm_name ":" -- P.prop);
   212 
   213 val structs =
   214   Scan.optional ((P.$$$ "(" -- P.$$$ "structure") |-- P.!!! (P.simple_fixes --| P.$$$ ")")) [];
   215 
   216 val _ =
   217   OuterSyntax.command "constdefs" "old-style constant definition" K.thy_decl
   218     (structs -- Scan.repeat1 old_constdef >> (Toplevel.theory o Constdefs.add_constdefs));
   219 
   220 
   221 (* constant definitions and abbreviations *)
   222 
   223 val constdecl =
   224   P.binding --
   225     (P.where_ >> K (NONE, NoSyn) ||
   226       P.$$$ "::" |-- P.!!! ((P.typ >> SOME) -- P.opt_mixfix' --| P.where_) ||
   227       Scan.ahead (P.$$$ "(") |-- P.!!! (P.mixfix' --| P.where_ >> pair NONE))
   228   >> P.triple2;
   229 
   230 val constdef = Scan.option constdecl -- (SpecParse.opt_thm_name ":" -- P.prop);
   231 
   232 val _ =
   233   OuterSyntax.local_theory "definition" "constant definition" K.thy_decl
   234     (constdef >> (fn args => #2 o Specification.definition_cmd args));
   235 
   236 val _ =
   237   OuterSyntax.local_theory "abbreviation" "constant abbreviation" K.thy_decl
   238     (opt_mode -- (Scan.option constdecl -- P.prop)
   239     >> (fn (mode, args) => Specification.abbreviation_cmd mode args));
   240 
   241 val _ =
   242   OuterSyntax.local_theory "notation" "add notation for constants / fixed variables" K.thy_decl
   243     (opt_mode -- P.and_list1 (P.xname -- SpecParse.locale_mixfix)
   244     >> (fn (mode, args) => Specification.notation_cmd true mode args));
   245 
   246 val _ =
   247   OuterSyntax.local_theory "no_notation" "delete notation for constants / fixed variables" K.thy_decl
   248     (opt_mode -- P.and_list1 (P.xname -- SpecParse.locale_mixfix)
   249     >> (fn (mode, args) => Specification.notation_cmd false mode args));
   250 
   251 
   252 (* constant specifications *)
   253 
   254 val _ =
   255   OuterSyntax.command "axiomatization" "axiomatic constant specification" K.thy_decl
   256     (Scan.optional P.fixes [] --
   257       Scan.optional (P.where_ |-- P.!!! (P.and_list1 SpecParse.spec)) []
   258     >> (fn (x, y) => Toplevel.theory (#2 o Specification.axiomatization_cmd x y)));
   259 
   260 
   261 (* theorems *)
   262 
   263 fun theorems kind =
   264   SpecParse.name_facts >> (fn args => #2 o Specification.theorems_cmd kind args);
   265 
   266 val _ =
   267   OuterSyntax.local_theory "theorems" "define theorems" K.thy_decl (theorems Thm.theoremK);
   268 
   269 val _ =
   270   OuterSyntax.local_theory "lemmas" "define lemmas" K.thy_decl (theorems Thm.lemmaK);
   271 
   272 val _ =
   273   OuterSyntax.local_theory "declare" "declare theorems (improper)" K.thy_decl
   274     (P.and_list1 SpecParse.xthms1
   275       >> (fn args => #2 o Specification.theorems_cmd "" [(Attrib.empty_binding, flat args)]));
   276 
   277 
   278 (* name space entry path *)
   279 
   280 val _ =
   281   OuterSyntax.command "global" "disable prefixing of theory name" K.thy_decl
   282     (Scan.succeed (Toplevel.theory Sign.root_path));
   283 
   284 val _ =
   285   OuterSyntax.command "local" "enable prefixing of theory name" K.thy_decl
   286     (Scan.succeed (Toplevel.theory Sign.local_path));
   287 
   288 val _ =
   289   OuterSyntax.command "hide" "hide names from given name space" K.thy_decl
   290     ((P.opt_keyword "open" >> not) -- (P.name -- Scan.repeat1 P.xname) >>
   291       (Toplevel.theory o uncurry IsarCmd.hide_names));
   292 
   293 
   294 (* use ML text *)
   295 
   296 val _ =
   297   OuterSyntax.command "use" "eval ML text from file" (K.tag_ml K.thy_decl)
   298     (P.path >> (Toplevel.generic_theory o ThyInfo.exec_file false));
   299 
   300 val _ =
   301   OuterSyntax.command "ML" "eval ML text within theory" (K.tag_ml K.thy_decl)
   302     (P.ML_source >> (fn (txt, pos) =>
   303       Toplevel.generic_theory (ML_Context.exec (fn () => ML_Context.eval true pos txt))));
   304 
   305 val _ =
   306   OuterSyntax.command "ML_prf" "eval ML text within proof" (K.tag_proof K.prf_decl)
   307     (P.ML_source >> (fn (txt, pos) =>
   308       Toplevel.proof (Proof.map_context (Context.proof_map
   309         (ML_Context.exec (fn () => ML_Context.eval true pos txt))) #>
   310           (fn prf => Proof.map_contexts (ML_Context.inherit_env (Proof.context_of prf)) prf))));
   311 
   312 val _ =
   313   OuterSyntax.command "ML_val" "eval ML text (diagnostic)" (K.tag_ml K.diag)
   314     (P.ML_source >> IsarCmd.ml_diag true);
   315 
   316 val _ =
   317   OuterSyntax.command "ML_command" "silently eval ML text (diagnostic)" (K.tag_ml K.diag)
   318     (P.ML_source >> (Toplevel.no_timing oo IsarCmd.ml_diag false));
   319 
   320 val _ =
   321   OuterSyntax.command "setup" "apply ML theory setup" (K.tag_ml K.thy_decl)
   322     (P.ML_source >> (Toplevel.theory o IsarCmd.generic_setup));
   323 
   324 val _ =
   325   OuterSyntax.command "method_setup" "define proof method in ML" (K.tag_ml K.thy_decl)
   326     (P.name -- P.!!! (P.$$$ "=" |-- P.ML_source -- P.text)
   327     >> (fn (name, (txt, cmt)) => Toplevel.theory (Method.method_setup name txt cmt)));
   328 
   329 val _ =
   330   OuterSyntax.local_theory "declaration" "generic ML declaration" (K.tag_ml K.thy_decl)
   331     (P.ML_source >> IsarCmd.declaration);
   332 
   333 val _ =
   334   OuterSyntax.local_theory "simproc_setup" "define simproc in ML" (K.tag_ml K.thy_decl)
   335     (P.name -- (P.$$$ "(" |-- P.enum1 "|" P.term --| P.$$$ ")" --| P.$$$ "=") --
   336       P.ML_source -- Scan.optional (P.$$$ "identifier" |-- Scan.repeat1 P.xname) []
   337     >> (fn (((a, b), c), d) => IsarCmd.simproc_setup a b c d));
   338 
   339 
   340 (* translation functions *)
   341 
   342 val trfun = P.opt_keyword "advanced" -- P.ML_source;
   343 
   344 val _ =
   345   OuterSyntax.command "parse_ast_translation" "install parse ast translation functions"
   346     (K.tag_ml K.thy_decl)
   347     (trfun >> (Toplevel.theory o IsarCmd.parse_ast_translation));
   348 
   349 val _ =
   350   OuterSyntax.command "parse_translation" "install parse translation functions"
   351     (K.tag_ml K.thy_decl)
   352     (trfun >> (Toplevel.theory o IsarCmd.parse_translation));
   353 
   354 val _ =
   355   OuterSyntax.command "print_translation" "install print translation functions"
   356     (K.tag_ml K.thy_decl)
   357     (trfun >> (Toplevel.theory o IsarCmd.print_translation));
   358 
   359 val _ =
   360   OuterSyntax.command "typed_print_translation" "install typed print translation functions"
   361     (K.tag_ml K.thy_decl)
   362     (trfun >> (Toplevel.theory o IsarCmd.typed_print_translation));
   363 
   364 val _ =
   365   OuterSyntax.command "print_ast_translation" "install print ast translation functions"
   366     (K.tag_ml K.thy_decl)
   367     (trfun >> (Toplevel.theory o IsarCmd.print_ast_translation));
   368 
   369 
   370 (* oracles *)
   371 
   372 val _ =
   373   OuterSyntax.command "oracle" "declare oracle" (K.tag_ml K.thy_decl)
   374     (P.name -- (P.$$$ "=" |-- P.ML_source) >> (fn (x, y) => Toplevel.theory (IsarCmd.oracle x y)));
   375 
   376 
   377 (* local theories *)
   378 
   379 val _ =
   380   OuterSyntax.command "context" "enter local theory context" K.thy_decl
   381     (P.name --| P.begin >> (fn name =>
   382       Toplevel.print o Toplevel.begin_local_theory true (TheoryTarget.context name)));
   383 
   384 
   385 (* locales *)
   386 
   387 val locale_val =
   388   SpecParse.locale_expression --
   389     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 SpecParse.context_element)) [] ||
   390   Scan.repeat1 SpecParse.context_element >> pair ([], []);
   391 
   392 val _ =
   393   OuterSyntax.command "locale" "define named proof context" K.thy_decl
   394     (P.name -- Scan.optional (P.$$$ "=" |-- P.!!! locale_val) (([], []), []) -- P.opt_begin
   395       >> (fn ((name, (expr, elems)), begin) =>
   396           (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   397             (Expression.add_locale_cmd name "" expr elems #> snd)));
   398 
   399 val _ =
   400   OuterSyntax.command "sublocale"
   401     "prove sublocale relation between a locale and a locale expression" K.thy_goal
   402     (P.xname --| (P.$$$ "\\<subseteq>" || P.$$$ "<") -- P.!!! SpecParse.locale_expression
   403       >> (fn (loc, expr) =>
   404         Toplevel.print o (Toplevel.theory_to_proof (Expression.sublocale_cmd loc expr))));
   405 
   406 val _ =
   407   OuterSyntax.command "interpretation"
   408     "prove interpretation of locale expression in theory" K.thy_goal
   409     (P.!!! SpecParse.locale_expression --
   410       Scan.optional (P.$$$ "where" |-- P.and_list1 (SpecParse.opt_thm_name ":" -- P.prop)) []
   411         >> (fn (expr, equations) => Toplevel.print o
   412             Toplevel.theory_to_proof (Expression.interpretation_cmd expr equations)));
   413 
   414 val _ =
   415   OuterSyntax.command "interpret"
   416     "prove interpretation of locale expression in proof context"
   417     (K.tag_proof K.prf_goal)
   418     (P.!!! SpecParse.locale_expression
   419         >> (fn expr => Toplevel.print o
   420             Toplevel.proof' (fn int => Expression.interpret_cmd expr int)));
   421 
   422 
   423 (* classes *)
   424 
   425 val class_val =
   426   SpecParse.class_expr --
   427     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 SpecParse.context_element)) [] ||
   428   Scan.repeat1 SpecParse.context_element >> pair [];
   429 
   430 val _ =
   431   OuterSyntax.command "class" "define type class" K.thy_decl
   432    (P.name -- Scan.optional (P.$$$ "=" |-- class_val) ([], []) -- P.opt_begin
   433     >> (fn ((name, (supclasses, elems)), begin) =>
   434         (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   435           (Class.class_cmd name supclasses elems #> snd)));
   436 
   437 val _ =
   438   OuterSyntax.local_theory_to_proof "subclass" "prove a subclass relation" K.thy_goal
   439     (P.xname >> Class.subclass_cmd);
   440 
   441 val _ =
   442   OuterSyntax.command "instantiation" "instantiate and prove type arity" K.thy_decl
   443    (P.multi_arity --| P.begin
   444      >> (fn arities => Toplevel.print o
   445          Toplevel.begin_local_theory true (TheoryTarget.instantiation_cmd arities)));
   446 
   447 val _ =
   448   OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
   449   ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> Class.classrel_cmd ||
   450     P.arity >> Class.instance_arity_cmd)
   451     >> (Toplevel.print oo Toplevel.theory_to_proof)
   452   || Scan.succeed (Toplevel.print o Toplevel.local_theory_to_proof NONE (Class.instantiation_instance I)));
   453 
   454 
   455 (* arbitrary overloading *)
   456 
   457 val _ =
   458   OuterSyntax.command "overloading" "overloaded definitions" K.thy_decl
   459    (Scan.repeat1 (P.name --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term --
   460       Scan.optional (P.$$$ "(" |-- (P.$$$ "unchecked" >> K false) --| P.$$$ ")") true
   461       >> P.triple1) --| P.begin
   462    >> (fn operations => Toplevel.print o
   463          Toplevel.begin_local_theory true (TheoryTarget.overloading_cmd operations)));
   464 
   465 
   466 (* code generation *)
   467 
   468 val _ =
   469   OuterSyntax.command "code_datatype" "define set of code datatype constructors" K.thy_decl
   470     (Scan.repeat1 P.term >> (Toplevel.theory o Code.add_datatype_cmd));
   471 
   472 
   473 
   474 (** proof commands **)
   475 
   476 (* statements *)
   477 
   478 fun gen_theorem kind =
   479   OuterSyntax.local_theory_to_proof' kind ("state " ^ kind) K.thy_goal
   480     (Scan.optional (SpecParse.opt_thm_name ":" --|
   481       Scan.ahead (SpecParse.locale_keyword || SpecParse.statement_keyword)) Attrib.empty_binding --
   482       SpecParse.general_statement >> (fn (a, (elems, concl)) =>
   483         (Specification.theorem_cmd kind NONE (K I) a elems concl)));
   484 
   485 val _ = gen_theorem Thm.theoremK;
   486 val _ = gen_theorem Thm.lemmaK;
   487 val _ = gen_theorem Thm.corollaryK;
   488 
   489 val _ =
   490   OuterSyntax.command "have" "state local goal"
   491     (K.tag_proof K.prf_goal)
   492     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.have));
   493 
   494 val _ =
   495   OuterSyntax.command "hence" "abbreviates \"then have\""
   496     (K.tag_proof K.prf_goal)
   497     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.hence));
   498 
   499 val _ =
   500   OuterSyntax.command "show" "state local goal, solving current obligation"
   501     (K.tag_proof K.prf_asm_goal)
   502     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.show));
   503 
   504 val _ =
   505   OuterSyntax.command "thus" "abbreviates \"then show\""
   506     (K.tag_proof K.prf_asm_goal)
   507     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.thus));
   508 
   509 
   510 (* facts *)
   511 
   512 val facts = P.and_list1 SpecParse.xthms1;
   513 
   514 val _ =
   515   OuterSyntax.command "then" "forward chaining"
   516     (K.tag_proof K.prf_chain)
   517     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   518 
   519 val _ =
   520   OuterSyntax.command "from" "forward chaining from given facts"
   521     (K.tag_proof K.prf_chain)
   522     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss)));
   523 
   524 val _ =
   525   OuterSyntax.command "with" "forward chaining from given and current facts"
   526     (K.tag_proof K.prf_chain)
   527     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss)));
   528 
   529 val _ =
   530   OuterSyntax.command "note" "define facts"
   531     (K.tag_proof K.prf_decl)
   532     (SpecParse.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss)));
   533 
   534 val _ =
   535   OuterSyntax.command "using" "augment goal facts"
   536     (K.tag_proof K.prf_decl)
   537     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using)));
   538 
   539 val _ =
   540   OuterSyntax.command "unfolding" "unfold definitions in goal and facts"
   541     (K.tag_proof K.prf_decl)
   542     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding)));
   543 
   544 
   545 (* proof context *)
   546 
   547 val _ =
   548   OuterSyntax.command "fix" "fix local variables (Skolem constants)"
   549     (K.tag_proof K.prf_asm)
   550     (P.fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix)));
   551 
   552 val _ =
   553   OuterSyntax.command "assume" "assume propositions"
   554     (K.tag_proof K.prf_asm)
   555     (SpecParse.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume)));
   556 
   557 val _ =
   558   OuterSyntax.command "presume" "assume propositions, to be established later"
   559     (K.tag_proof K.prf_asm)
   560     (SpecParse.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume)));
   561 
   562 val _ =
   563   OuterSyntax.command "def" "local definition"
   564     (K.tag_proof K.prf_asm)
   565     (P.and_list1
   566       (SpecParse.opt_thm_name ":" --
   567         ((P.binding -- P.opt_mixfix) -- ((P.$$$ "\\<equiv>" || P.$$$ "==") |-- P.!!! P.termp)))
   568     >> (Toplevel.print oo (Toplevel.proof o Proof.def)));
   569 
   570 val _ =
   571   OuterSyntax.command "obtain" "generalized existence"
   572     (K.tag_proof K.prf_asm_goal)
   573     (P.parname -- Scan.optional (P.fixes --| P.where_) [] -- SpecParse.statement
   574       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain x y z)));
   575 
   576 val _ =
   577   OuterSyntax.command "guess" "wild guessing (unstructured)"
   578     (K.tag_proof K.prf_asm_goal)
   579     (Scan.optional P.fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess)));
   580 
   581 val _ =
   582   OuterSyntax.command "let" "bind text variables"
   583     (K.tag_proof K.prf_decl)
   584     (P.and_list1 (P.and_list1 P.term -- (P.$$$ "=" |-- P.term))
   585       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind)));
   586 
   587 val case_spec =
   588   (P.$$$ "(" |-- P.!!! (P.xname -- Scan.repeat1 (P.maybe P.name) --| P.$$$ ")") ||
   589     P.xname >> rpair []) -- SpecParse.opt_attribs >> P.triple1;
   590 
   591 val _ =
   592   OuterSyntax.command "case" "invoke local context"
   593     (K.tag_proof K.prf_asm)
   594     (case_spec >> (Toplevel.print oo (Toplevel.proof o Proof.invoke_case)));
   595 
   596 
   597 (* proof structure *)
   598 
   599 val _ =
   600   OuterSyntax.command "{" "begin explicit proof block"
   601     (K.tag_proof K.prf_open)
   602     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   603 
   604 val _ =
   605   OuterSyntax.command "}" "end explicit proof block"
   606     (K.tag_proof K.prf_close)
   607     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.end_block));
   608 
   609 val _ =
   610   OuterSyntax.command "next" "enter next proof block"
   611     (K.tag_proof K.prf_block)
   612     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   613 
   614 
   615 (* end proof *)
   616 
   617 val _ =
   618   OuterSyntax.command "qed" "conclude (sub-)proof"
   619     (K.tag_proof K.qed_block)
   620     (Scan.option Method.parse >> IsarCmd.qed);
   621 
   622 val _ =
   623   OuterSyntax.command "by" "terminal backward proof"
   624     (K.tag_proof K.qed)
   625     (Method.parse -- Scan.option Method.parse >> IsarCmd.terminal_proof);
   626 
   627 val _ =
   628   OuterSyntax.command ".." "default proof"
   629     (K.tag_proof K.qed)
   630     (Scan.succeed IsarCmd.default_proof);
   631 
   632 val _ =
   633   OuterSyntax.command "." "immediate proof"
   634     (K.tag_proof K.qed)
   635     (Scan.succeed IsarCmd.immediate_proof);
   636 
   637 val _ =
   638   OuterSyntax.command "done" "done proof"
   639     (K.tag_proof K.qed)
   640     (Scan.succeed IsarCmd.done_proof);
   641 
   642 val _ =
   643   OuterSyntax.command "sorry" "skip proof (quick-and-dirty mode only!)"
   644     (K.tag_proof K.qed)
   645     (Scan.succeed IsarCmd.skip_proof);
   646 
   647 val _ =
   648   OuterSyntax.command "oops" "forget proof"
   649     (K.tag_proof K.qed_global)
   650     (Scan.succeed Toplevel.forget_proof);
   651 
   652 
   653 (* proof steps *)
   654 
   655 val _ =
   656   OuterSyntax.command "defer" "shuffle internal proof state"
   657     (K.tag_proof K.prf_script)
   658     (Scan.option P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.defer)));
   659 
   660 val _ =
   661   OuterSyntax.command "prefer" "shuffle internal proof state"
   662     (K.tag_proof K.prf_script)
   663     (P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.prefer)));
   664 
   665 val _ =
   666   OuterSyntax.command "apply" "initial refinement step (unstructured)"
   667     (K.tag_proof K.prf_script)
   668     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply)));
   669 
   670 val _ =
   671   OuterSyntax.command "apply_end" "terminal refinement (unstructured)"
   672     (K.tag_proof K.prf_script)
   673     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply_end)));
   674 
   675 val _ =
   676   OuterSyntax.command "proof" "backward proof"
   677     (K.tag_proof K.prf_block)
   678     (Scan.option Method.parse >> (fn m => Toplevel.print o
   679       Toplevel.actual_proof (ProofNode.applys (Proof.proof m)) o
   680       Toplevel.skip_proof (fn i => i + 1)));
   681 
   682 
   683 (* calculational proof commands *)
   684 
   685 val calc_args =
   686   Scan.option (P.$$$ "(" |-- P.!!! ((SpecParse.xthms1 --| P.$$$ ")")));
   687 
   688 val _ =
   689   OuterSyntax.command "also" "combine calculation and current facts"
   690     (K.tag_proof K.prf_decl)
   691     (calc_args >> (Toplevel.proofs' o Calculation.also));
   692 
   693 val _ =
   694   OuterSyntax.command "finally" "combine calculation and current facts, exhibit result"
   695     (K.tag_proof K.prf_chain)
   696     (calc_args >> (Toplevel.proofs' o Calculation.finally));
   697 
   698 val _ =
   699   OuterSyntax.command "moreover" "augment calculation by current facts"
   700     (K.tag_proof K.prf_decl)
   701     (Scan.succeed (Toplevel.proof' Calculation.moreover));
   702 
   703 val _ =
   704   OuterSyntax.command "ultimately" "augment calculation by current facts, exhibit result"
   705     (K.tag_proof K.prf_chain)
   706     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
   707 
   708 
   709 (* proof navigation *)
   710 
   711 val _ =
   712   OuterSyntax.command "back" "backtracking of proof command"
   713     (K.tag_proof K.prf_script)
   714     (Scan.succeed (Toplevel.print o Toplevel.actual_proof ProofNode.back o Toplevel.skip_proof I));
   715 
   716 
   717 (* nested commands *)
   718 
   719 val props_text =
   720   Scan.optional ValueParse.properties [] -- P.position P.string >> (fn (props, (str, pos)) =>
   721     (Position.of_properties (Position.default_properties pos props), str));
   722 
   723 val _ =
   724   OuterSyntax.improper_command "Isabelle.command" "nested Isabelle command" K.control
   725     (props_text :|-- (fn (pos, str) =>
   726       (case OuterSyntax.parse pos str of
   727         [tr] => Scan.succeed (K tr)
   728       | _ => Scan.fail_with (K "exactly one command expected"))
   729       handle ERROR msg => Scan.fail_with (K msg)));
   730 
   731 
   732 
   733 (** diagnostic commands (for interactive mode only) **)
   734 
   735 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   736 val opt_bang = Scan.optional (P.$$$ "!" >> K true) false;
   737 
   738 val _ =
   739   OuterSyntax.improper_command "pretty_setmargin" "change default margin for pretty printing"
   740     K.diag (P.nat >> (Toplevel.no_timing oo IsarCmd.pretty_setmargin));
   741 
   742 val _ =
   743   OuterSyntax.improper_command "help" "print outer syntax commands" K.diag
   744     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative OuterSyntax.print_outer_syntax));
   745 
   746 val _ =
   747   OuterSyntax.improper_command "print_commands" "print outer syntax commands" K.diag
   748     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative OuterSyntax.print_outer_syntax));
   749 
   750 val _ =
   751   OuterSyntax.improper_command "print_configs" "print configuration options" K.diag
   752     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_configs));
   753 
   754 val _ =
   755   OuterSyntax.improper_command "print_context" "print theory context name" K.diag
   756     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_context));
   757 
   758 val _ =
   759   OuterSyntax.improper_command "print_theory" "print logical theory contents (verbose!)" K.diag
   760     (opt_bang >> (Toplevel.no_timing oo IsarCmd.print_theory));
   761 
   762 val _ =
   763   OuterSyntax.improper_command "print_syntax" "print inner syntax of context (verbose!)" K.diag
   764     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_syntax));
   765 
   766 val _ =
   767   OuterSyntax.improper_command "print_abbrevs" "print constant abbreviation of context" K.diag
   768     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_abbrevs));
   769 
   770 val _ =
   771   OuterSyntax.improper_command "print_theorems"
   772       "print theorems of local theory or proof context" K.diag
   773     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theorems));
   774 
   775 val _ =
   776   OuterSyntax.improper_command "print_locales" "print locales of this theory" K.diag
   777     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_locales));
   778 
   779 val _ =
   780   OuterSyntax.improper_command "print_classes" "print classes of this theory" K.diag
   781     (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory
   782       o Toplevel.keep (Class.print_classes o Toplevel.theory_of)));
   783 
   784 val _ =
   785   OuterSyntax.improper_command "print_locale" "print locale expression in this theory" K.diag
   786     (opt_bang -- P.xname >> (Toplevel.no_timing oo IsarCmd.print_locale));
   787 
   788 val _ =
   789   OuterSyntax.improper_command "print_attributes" "print attributes of this theory" K.diag
   790     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_attributes));
   791 
   792 val _ =
   793   OuterSyntax.improper_command "print_simpset" "print context of Simplifier" K.diag
   794     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_simpset));
   795 
   796 val _ =
   797   OuterSyntax.improper_command "print_rules" "print intro/elim rules" K.diag
   798     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_rules));
   799 
   800 val _ =
   801   OuterSyntax.improper_command "print_trans_rules" "print transitivity rules" K.diag
   802     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_trans_rules));
   803 
   804 val _ =
   805   OuterSyntax.improper_command "print_methods" "print methods of this theory" K.diag
   806     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_methods));
   807 
   808 val _ =
   809   OuterSyntax.improper_command "print_antiquotations" "print antiquotations (global)" K.diag
   810     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_antiquotations));
   811 
   812 val _ =
   813   OuterSyntax.improper_command "thy_deps" "visualize theory dependencies"
   814     K.diag (Scan.succeed (Toplevel.no_timing o IsarCmd.thy_deps));
   815 
   816 val _ =
   817   OuterSyntax.improper_command "class_deps" "visualize class dependencies"
   818     K.diag (Scan.succeed (Toplevel.no_timing o IsarCmd.class_deps));
   819 
   820 val _ =
   821   OuterSyntax.improper_command "thm_deps" "visualize theorem dependencies"
   822     K.diag (SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.thm_deps));
   823 
   824 val _ =
   825   OuterSyntax.improper_command "print_binds" "print term bindings of proof context" K.diag
   826     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_binds));
   827 
   828 val _ =
   829   OuterSyntax.improper_command "print_facts" "print facts of proof context" K.diag
   830     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_facts));
   831 
   832 val _ =
   833   OuterSyntax.improper_command "print_cases" "print cases of proof context" K.diag
   834     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_cases));
   835 
   836 val _ =
   837   OuterSyntax.improper_command "print_statement" "print theorems as long statements" K.diag
   838     (opt_modes -- SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_stmts));
   839 
   840 val _ =
   841   OuterSyntax.improper_command "thm" "print theorems" K.diag
   842     (opt_modes -- SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_thms));
   843 
   844 val _ =
   845   OuterSyntax.improper_command "prf" "print proof terms of theorems" K.diag
   846     (opt_modes -- Scan.option SpecParse.xthms1
   847       >> (Toplevel.no_timing oo IsarCmd.print_prfs false));
   848 
   849 val _ =
   850   OuterSyntax.improper_command "full_prf" "print full proof terms of theorems" K.diag
   851     (opt_modes -- Scan.option SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs true));
   852 
   853 val _ =
   854   OuterSyntax.improper_command "prop" "read and print proposition" K.diag
   855     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_prop));
   856 
   857 val _ =
   858   OuterSyntax.improper_command "term" "read and print term" K.diag
   859     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_term));
   860 
   861 val _ =
   862   OuterSyntax.improper_command "typ" "read and print type" K.diag
   863     (opt_modes -- P.typ >> (Toplevel.no_timing oo IsarCmd.print_type));
   864 
   865 val _ =
   866   OuterSyntax.improper_command "print_codesetup" "print code generator setup of this theory" K.diag
   867     (Scan.succeed
   868       (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   869         (Code.print_codesetup o Toplevel.theory_of)));
   870 
   871 val _ =
   872   OuterSyntax.improper_command "unused_thms" "find unused theorems" K.diag
   873     (Scan.option ((Scan.repeat1 (Scan.unless P.minus P.name) --| P.minus) --
   874        Scan.option (Scan.repeat1 (Scan.unless P.minus P.name))) >>
   875          (Toplevel.no_timing oo IsarCmd.unused_thms));
   876 
   877 
   878 
   879 (** system commands (for interactive mode only) **)
   880 
   881 val _ =
   882   OuterSyntax.improper_command "cd" "change current working directory" K.diag
   883     (P.path >> (Toplevel.no_timing oo IsarCmd.cd));
   884 
   885 val _ =
   886   OuterSyntax.improper_command "pwd" "print current working directory" K.diag
   887     (Scan.succeed (Toplevel.no_timing o IsarCmd.pwd));
   888 
   889 val _ =
   890   OuterSyntax.improper_command "use_thy" "use theory file" K.diag
   891     (P.name >> (fn name =>
   892       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.use_thy name)));
   893 
   894 val _ =
   895   OuterSyntax.improper_command "touch_thy" "outdate theory, including descendants" K.diag
   896     (P.name >> (fn name =>
   897       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.touch_thy name)));
   898 
   899 val _ =
   900   OuterSyntax.improper_command "remove_thy" "remove theory from loader database" K.diag
   901     (P.name >> (fn name =>
   902       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.remove_thy name)));
   903 
   904 val _ =
   905   OuterSyntax.improper_command "kill_thy" "kill theory -- try to remove from loader database"
   906     K.diag (P.name >> (fn name =>
   907       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.kill_thy name)));
   908 
   909 val _ =
   910   OuterSyntax.improper_command "display_drafts" "display raw source files with symbols"
   911     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.display_drafts));
   912 
   913 val _ =
   914   OuterSyntax.improper_command "print_drafts" "print raw source files with symbols"
   915     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.print_drafts));
   916 
   917 val opt_limits =
   918   Scan.option P.nat -- Scan.option (P.$$$ "," |-- P.!!! P.nat);
   919 
   920 val _ =
   921   OuterSyntax.improper_command "pr" "print current proof state (if present)" K.diag
   922     (opt_modes -- opt_limits >> (Toplevel.no_timing oo IsarCmd.pr));
   923 
   924 val _ =
   925   OuterSyntax.improper_command "disable_pr" "disable printing of toplevel state" K.diag
   926     (Scan.succeed (Toplevel.no_timing o IsarCmd.disable_pr));
   927 
   928 val _ =
   929   OuterSyntax.improper_command "enable_pr" "enable printing of toplevel state" K.diag
   930     (Scan.succeed (Toplevel.no_timing o IsarCmd.enable_pr));
   931 
   932 val _ =
   933   OuterSyntax.improper_command "commit" "commit current session to ML database" K.diag
   934     (P.opt_unit >> K (Toplevel.no_timing o Toplevel.imperative Secure.commit));
   935 
   936 val _ =
   937   OuterSyntax.improper_command "quit" "quit Isabelle" K.control
   938     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.quit));
   939 
   940 val _ =
   941   OuterSyntax.improper_command "exit" "exit Isar loop" K.control
   942     (Scan.succeed (Toplevel.no_timing o IsarCmd.exit));
   943 
   944 end;
   945