src/Pure/Isar/isar_syn.ML
author wenzelm
Fri Apr 16 19:43:06 2010 +0200 (2010-04-16 ago)
changeset 36172 fc407d02af4a
parent 36112 7fa17a225852
child 36174 feb6f24de47e
permissions -rw-r--r--
local type abbreviations;
     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.local_theory "type_abbrev" "type abbreviation (input only)" K.thy_decl
   112     (P.type_args -- P.binding -- P.opt_mixfix -- (P.$$$ "=" |-- P.!!! P.typ)
   113       >> (fn (((args, a), mx), rhs) => Typedecl.abbrev_cmd (a, args, mx) rhs #> snd));
   114 
   115 val _ =
   116   OuterSyntax.command "types" "declare type abbreviations" K.thy_decl
   117     (Scan.repeat1
   118       (P.type_args -- P.binding -- (P.$$$ "=" |-- P.!!! (P.typ -- P.opt_mixfix')))
   119       >> (Toplevel.theory o Sign.add_tyabbrs o map (fn ((args, a), (T, mx)) => (a, args, T, mx))));
   120 
   121 val _ =
   122   OuterSyntax.command "nonterminals" "declare types treated as grammar nonterminal symbols"
   123     K.thy_decl (Scan.repeat1 P.binding >> (Toplevel.theory o Sign.add_nonterminals));
   124 
   125 val _ =
   126   OuterSyntax.command "arities" "state type arities (axiomatic!)" K.thy_decl
   127     (Scan.repeat1 P.arity >> (Toplevel.theory o fold AxClass.axiomatize_arity_cmd));
   128 
   129 
   130 (* consts and syntax *)
   131 
   132 val _ =
   133   OuterSyntax.command "judgment" "declare object-logic judgment" K.thy_decl
   134     (P.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
   135 
   136 val _ =
   137   OuterSyntax.command "consts" "declare constants" K.thy_decl
   138     (Scan.repeat1 P.const_binding >> (Toplevel.theory o Sign.add_consts));
   139 
   140 val opt_overloaded = P.opt_keyword "overloaded";
   141 
   142 val _ =
   143   OuterSyntax.command "finalconsts" "declare constants as final" K.thy_decl
   144     (opt_overloaded -- Scan.repeat1 P.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   145 
   146 val mode_spec =
   147   (P.$$$ "output" >> K ("", false)) || P.name -- Scan.optional (P.$$$ "output" >> K false) true;
   148 
   149 val opt_mode =
   150   Scan.optional (P.$$$ "(" |-- P.!!! (mode_spec --| P.$$$ ")")) Syntax.mode_default;
   151 
   152 val _ =
   153   OuterSyntax.command "syntax" "declare syntactic constants" K.thy_decl
   154     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Sign.add_modesyntax));
   155 
   156 val _ =
   157   OuterSyntax.command "no_syntax" "delete syntax declarations" K.thy_decl
   158     (opt_mode -- Scan.repeat1 P.const >> (Toplevel.theory o uncurry Sign.del_modesyntax));
   159 
   160 
   161 (* translations *)
   162 
   163 val trans_pat =
   164   Scan.optional (P.$$$ "(" |-- P.!!! (P.xname --| P.$$$ ")")) "logic" -- P.string;
   165 
   166 fun trans_arrow toks =
   167   ((P.$$$ "\\<rightharpoonup>" || P.$$$ "=>") >> K Syntax.ParseRule ||
   168     (P.$$$ "\\<leftharpoondown>" || P.$$$ "<=") >> K Syntax.PrintRule ||
   169     (P.$$$ "\\<rightleftharpoons>" || P.$$$ "==") >> K Syntax.ParsePrintRule) toks;
   170 
   171 val trans_line =
   172   trans_pat -- P.!!! (trans_arrow -- trans_pat)
   173     >> (fn (left, (arr, right)) => arr (left, right));
   174 
   175 val _ =
   176   OuterSyntax.command "translations" "declare syntax translation rules" K.thy_decl
   177     (Scan.repeat1 trans_line >> (Toplevel.theory o Sign.add_trrules));
   178 
   179 val _ =
   180   OuterSyntax.command "no_translations" "remove syntax translation rules" K.thy_decl
   181     (Scan.repeat1 trans_line >> (Toplevel.theory o Sign.del_trrules));
   182 
   183 
   184 (* axioms and definitions *)
   185 
   186 val _ =
   187   OuterSyntax.command "axioms" "state arbitrary propositions (axiomatic!)" K.thy_decl
   188     (Scan.repeat1 SpecParse.spec >> (Toplevel.theory o IsarCmd.add_axioms));
   189 
   190 val opt_unchecked_overloaded =
   191   Scan.optional (P.$$$ "(" |-- P.!!!
   192     (((P.$$$ "unchecked" >> K true) -- Scan.optional (P.$$$ "overloaded" >> K true) false ||
   193       P.$$$ "overloaded" >> K (false, true)) --| P.$$$ ")")) (false, false);
   194 
   195 val _ =
   196   OuterSyntax.command "defs" "define constants" K.thy_decl
   197     (opt_unchecked_overloaded --
   198       Scan.repeat1 (SpecParse.thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y)))
   199       >> (Toplevel.theory o IsarCmd.add_defs));
   200 
   201 
   202 (* old constdefs *)
   203 
   204 val old_constdecl =
   205   P.binding --| P.where_ >> (fn x => (x, NONE, NoSyn)) ||
   206   P.binding -- (P.$$$ "::" |-- P.!!! P.typ >> SOME) -- P.opt_mixfix'
   207     --| Scan.option P.where_ >> P.triple1 ||
   208   P.binding -- (P.mixfix >> pair NONE) --| Scan.option P.where_ >> P.triple2;
   209 
   210 val old_constdef = Scan.option old_constdecl -- (SpecParse.opt_thm_name ":" -- P.prop);
   211 
   212 val structs =
   213   Scan.optional ((P.$$$ "(" -- P.$$$ "structure") |-- P.!!! (P.simple_fixes --| P.$$$ ")")) [];
   214 
   215 val _ =
   216   OuterSyntax.command "constdefs" "old-style constant definition" K.thy_decl
   217     (structs -- Scan.repeat1 old_constdef >> (Toplevel.theory o Constdefs.add_constdefs));
   218 
   219 
   220 (* constant definitions and abbreviations *)
   221 
   222 val _ =
   223   OuterSyntax.local_theory "definition" "constant definition" K.thy_decl
   224     (SpecParse.constdef >> (fn args => #2 o Specification.definition_cmd args));
   225 
   226 val _ =
   227   OuterSyntax.local_theory "abbreviation" "constant abbreviation" K.thy_decl
   228     (opt_mode -- (Scan.option SpecParse.constdecl -- P.prop)
   229     >> (fn (mode, args) => Specification.abbreviation_cmd mode args));
   230 
   231 val _ =
   232   OuterSyntax.local_theory "type_notation" "add 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 true mode args));
   235 
   236 val _ =
   237   OuterSyntax.local_theory "no_type_notation" "delete notation for type constructors" K.thy_decl
   238     (opt_mode -- P.and_list1 (P.xname -- P.mixfix)
   239     >> (fn (mode, args) => Specification.type_notation_cmd false 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.specs)) []
   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" 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 fun propagate_env (context as Context.Proof lthy) =
   297       Context.Proof (Local_Theory.map_contexts (ML_Env.inherit context) lthy)
   298   | propagate_env context = context;
   299 
   300 fun propagate_env_prf prf = Proof.map_contexts
   301   (Context.proof_map (ML_Env.inherit (Context.Proof (Proof.context_of prf)))) prf;
   302 
   303 val _ =
   304   OuterSyntax.command "use" "ML text from file" (K.tag_ml K.thy_decl)
   305     (P.path >> (fn path => Toplevel.generic_theory (ThyInfo.exec_file false path #> propagate_env)));
   306 
   307 val _ =
   308   OuterSyntax.command "ML" "ML text within theory or local theory" (K.tag_ml K.thy_decl)
   309     (P.ML_source >> (fn (txt, pos) =>
   310       Toplevel.generic_theory
   311         (ML_Context.exec (fn () => ML_Context.eval true pos txt) #> propagate_env)));
   312 
   313 val _ =
   314   OuterSyntax.command "ML_prf" "ML text within proof" (K.tag_proof K.prf_decl)
   315     (P.ML_source >> (fn (txt, pos) =>
   316       Toplevel.proof (Proof.map_context (Context.proof_map
   317         (ML_Context.exec (fn () => ML_Context.eval true pos txt))) #> propagate_env_prf)));
   318 
   319 val _ =
   320   OuterSyntax.command "ML_val" "diagnostic ML text" (K.tag_ml K.diag)
   321     (P.ML_source >> IsarCmd.ml_diag true);
   322 
   323 val _ =
   324   OuterSyntax.command "ML_command" "diagnostic ML text (silent)" (K.tag_ml K.diag)
   325     (P.ML_source >> (Toplevel.no_timing oo IsarCmd.ml_diag false));
   326 
   327 val _ =
   328   OuterSyntax.command "setup" "ML theory setup" (K.tag_ml K.thy_decl)
   329     (P.ML_source >> (Toplevel.theory o IsarCmd.global_setup));
   330 
   331 val _ =
   332   OuterSyntax.local_theory "local_setup" "ML local theory setup" (K.tag_ml K.thy_decl)
   333     (P.ML_source >> IsarCmd.local_setup);
   334 
   335 val _ =
   336   OuterSyntax.command "attribute_setup" "define attribute 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 (Attrib.attribute_setup name txt cmt)));
   339 
   340 val _ =
   341   OuterSyntax.command "method_setup" "define proof method in ML" (K.tag_ml K.thy_decl)
   342     (P.position P.name -- P.!!! (P.$$$ "=" |-- P.ML_source -- P.text)
   343     >> (fn (name, (txt, cmt)) => Toplevel.theory (Method.method_setup name txt cmt)));
   344 
   345 val _ =
   346   OuterSyntax.local_theory "declaration" "generic ML declaration" (K.tag_ml K.thy_decl)
   347     (P.opt_keyword "pervasive" -- P.ML_source >> uncurry IsarCmd.declaration);
   348 
   349 val _ =
   350   OuterSyntax.local_theory "simproc_setup" "define simproc in ML" (K.tag_ml K.thy_decl)
   351     (P.name -- (P.$$$ "(" |-- P.enum1 "|" P.term --| P.$$$ ")" --| P.$$$ "=") --
   352       P.ML_source -- Scan.optional (P.$$$ "identifier" |-- Scan.repeat1 P.xname) []
   353     >> (fn (((a, b), c), d) => IsarCmd.simproc_setup a b c d));
   354 
   355 
   356 (* translation functions *)
   357 
   358 val trfun = P.opt_keyword "advanced" -- P.ML_source;
   359 
   360 val _ =
   361   OuterSyntax.command "parse_ast_translation" "install parse ast translation functions"
   362     (K.tag_ml K.thy_decl)
   363     (trfun >> (Toplevel.theory o IsarCmd.parse_ast_translation));
   364 
   365 val _ =
   366   OuterSyntax.command "parse_translation" "install parse translation functions"
   367     (K.tag_ml K.thy_decl)
   368     (trfun >> (Toplevel.theory o IsarCmd.parse_translation));
   369 
   370 val _ =
   371   OuterSyntax.command "print_translation" "install print translation functions"
   372     (K.tag_ml K.thy_decl)
   373     (trfun >> (Toplevel.theory o IsarCmd.print_translation));
   374 
   375 val _ =
   376   OuterSyntax.command "typed_print_translation" "install typed print translation functions"
   377     (K.tag_ml K.thy_decl)
   378     (trfun >> (Toplevel.theory o IsarCmd.typed_print_translation));
   379 
   380 val _ =
   381   OuterSyntax.command "print_ast_translation" "install print ast translation functions"
   382     (K.tag_ml K.thy_decl)
   383     (trfun >> (Toplevel.theory o IsarCmd.print_ast_translation));
   384 
   385 
   386 (* oracles *)
   387 
   388 val _ =
   389   OuterSyntax.command "oracle" "declare oracle" (K.tag_ml K.thy_decl)
   390     (P.position P.name -- (P.$$$ "=" |-- P.ML_source) >>
   391       (fn (x, y) => Toplevel.theory (IsarCmd.oracle x y)));
   392 
   393 
   394 (* local theories *)
   395 
   396 val _ =
   397   OuterSyntax.command "context" "enter local theory context" K.thy_decl
   398     (P.name --| P.begin >> (fn name =>
   399       Toplevel.print o Toplevel.begin_local_theory true (Theory_Target.context_cmd name)));
   400 
   401 
   402 (* locales *)
   403 
   404 val locale_val =
   405   SpecParse.locale_expression false --
   406     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 SpecParse.context_element)) [] ||
   407   Scan.repeat1 SpecParse.context_element >> pair ([], []);
   408 
   409 val _ =
   410   OuterSyntax.command "locale" "define named proof context" K.thy_decl
   411     (P.binding -- Scan.optional (P.$$$ "=" |-- P.!!! locale_val) (([], []), []) -- P.opt_begin
   412       >> (fn ((name, (expr, elems)), begin) =>
   413           (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   414             (Expression.add_locale_cmd name Binding.empty expr elems #> snd)));
   415 
   416 val _ =
   417   OuterSyntax.command "sublocale"
   418     "prove sublocale relation between a locale and a locale expression" K.thy_goal
   419     (P.xname --| (P.$$$ "\\<subseteq>" || P.$$$ "<") -- P.!!! (SpecParse.locale_expression false)
   420       >> (fn (loc, expr) =>
   421           Toplevel.print o Toplevel.theory_to_proof (Expression.sublocale_cmd loc expr)));
   422 
   423 val _ =
   424   OuterSyntax.command "interpretation"
   425     "prove interpretation of locale expression in theory" K.thy_goal
   426     (P.!!! (SpecParse.locale_expression true) --
   427       Scan.optional (P.where_ |-- P.and_list1 (SpecParse.opt_thm_name ":" -- P.prop)) []
   428       >> (fn (expr, equations) => Toplevel.print o
   429           Toplevel.theory_to_proof (Expression.interpretation_cmd expr equations)));
   430 
   431 val _ =
   432   OuterSyntax.command "interpret"
   433     "prove interpretation of locale expression in proof context"
   434     (K.tag_proof K.prf_goal)
   435     (P.!!! (SpecParse.locale_expression true)
   436       >> (fn expr => Toplevel.print o Toplevel.proof' (Expression.interpret_cmd expr)));
   437 
   438 
   439 (* classes *)
   440 
   441 val class_val =
   442   SpecParse.class_expr --
   443     Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 SpecParse.context_element)) [] ||
   444   Scan.repeat1 SpecParse.context_element >> pair [];
   445 
   446 val _ =
   447   OuterSyntax.command "class" "define type class" K.thy_decl
   448    (P.binding -- Scan.optional (P.$$$ "=" |-- class_val) ([], []) -- P.opt_begin
   449     >> (fn ((name, (supclasses, elems)), begin) =>
   450         (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   451           (Class.class_cmd name supclasses elems #> snd)));
   452 
   453 val _ =
   454   OuterSyntax.local_theory_to_proof "subclass" "prove a subclass relation" K.thy_goal
   455     (P.xname >> Class.subclass_cmd);
   456 
   457 val _ =
   458   OuterSyntax.command "instantiation" "instantiate and prove type arity" K.thy_decl
   459    (P.multi_arity --| P.begin
   460      >> (fn arities => Toplevel.print o
   461          Toplevel.begin_local_theory true (Theory_Target.instantiation_cmd arities)));
   462 
   463 val _ =
   464   OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
   465   ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> Class.classrel_cmd ||
   466     P.multi_arity >> Class.instance_arity_cmd)
   467     >> (Toplevel.print oo Toplevel.theory_to_proof)
   468   || Scan.succeed (Toplevel.print o Toplevel.local_theory_to_proof NONE (Class.instantiation_instance I)));
   469 
   470 
   471 (* arbitrary overloading *)
   472 
   473 val _ =
   474   OuterSyntax.command "overloading" "overloaded definitions" K.thy_decl
   475    (Scan.repeat1 (P.name --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term --
   476       Scan.optional (P.$$$ "(" |-- (P.$$$ "unchecked" >> K false) --| P.$$$ ")") true
   477       >> P.triple1) --| P.begin
   478    >> (fn operations => Toplevel.print o
   479          Toplevel.begin_local_theory true (Theory_Target.overloading_cmd operations)));
   480 
   481 
   482 (* code generation *)
   483 
   484 val _ =
   485   OuterSyntax.command "code_datatype" "define set of code datatype constructors" K.thy_decl
   486     (Scan.repeat1 P.term >> (Toplevel.theory o Code.add_datatype_cmd));
   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