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