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