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