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