src/Pure/Isar/isar_syn.ML
author wenzelm
Thu Nov 05 22:06:46 2009 +0100 (2009-11-05 ago)
changeset 33456 fbd47f9b9b12
parent 33390 5b499f36dd25
child 33515 d066e8369a33
permissions -rw-r--r--
allow "pervasive" local theory declarations, which are applied the background theory;
     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 (LocalTheory.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 (TheoryTarget.context 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 (TheoryTarget.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 (TheoryTarget.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 
   483 
   484 (** proof commands **)
   485 
   486 (* statements *)
   487 
   488 fun gen_theorem kind =
   489   OuterSyntax.local_theory_to_proof' kind ("state " ^ kind) K.thy_goal
   490     (Scan.optional (SpecParse.opt_thm_name ":" --|
   491       Scan.ahead (SpecParse.locale_keyword || SpecParse.statement_keyword)) Attrib.empty_binding --
   492       SpecParse.general_statement >> (fn (a, (elems, concl)) =>
   493         (Specification.theorem_cmd kind NONE (K I) a elems concl)));
   494 
   495 val _ = gen_theorem Thm.theoremK;
   496 val _ = gen_theorem Thm.lemmaK;
   497 val _ = gen_theorem Thm.corollaryK;
   498 
   499 val _ =
   500   OuterSyntax.command "have" "state local goal"
   501     (K.tag_proof K.prf_goal)
   502     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.have));
   503 
   504 val _ =
   505   OuterSyntax.command "hence" "abbreviates \"then have\""
   506     (K.tag_proof K.prf_goal)
   507     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.hence));
   508 
   509 val _ =
   510   OuterSyntax.command "show" "state local goal, solving current obligation"
   511     (K.tag_proof K.prf_asm_goal)
   512     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.show));
   513 
   514 val _ =
   515   OuterSyntax.command "thus" "abbreviates \"then show\""
   516     (K.tag_proof K.prf_asm_goal)
   517     (SpecParse.statement >> ((Toplevel.print oo Toplevel.proof') o IsarCmd.thus));
   518 
   519 
   520 (* facts *)
   521 
   522 val facts = P.and_list1 SpecParse.xthms1;
   523 
   524 val _ =
   525   OuterSyntax.command "then" "forward chaining"
   526     (K.tag_proof K.prf_chain)
   527     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   528 
   529 val _ =
   530   OuterSyntax.command "from" "forward chaining from given facts"
   531     (K.tag_proof K.prf_chain)
   532     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss)));
   533 
   534 val _ =
   535   OuterSyntax.command "with" "forward chaining from given and current facts"
   536     (K.tag_proof K.prf_chain)
   537     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss)));
   538 
   539 val _ =
   540   OuterSyntax.command "note" "define facts"
   541     (K.tag_proof K.prf_decl)
   542     (SpecParse.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss)));
   543 
   544 val _ =
   545   OuterSyntax.command "using" "augment goal facts"
   546     (K.tag_proof K.prf_decl)
   547     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using)));
   548 
   549 val _ =
   550   OuterSyntax.command "unfolding" "unfold definitions in goal and facts"
   551     (K.tag_proof K.prf_decl)
   552     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding)));
   553 
   554 
   555 (* proof context *)
   556 
   557 val _ =
   558   OuterSyntax.command "fix" "fix local variables (Skolem constants)"
   559     (K.tag_proof K.prf_asm)
   560     (P.fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix)));
   561 
   562 val _ =
   563   OuterSyntax.command "assume" "assume propositions"
   564     (K.tag_proof K.prf_asm)
   565     (SpecParse.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume)));
   566 
   567 val _ =
   568   OuterSyntax.command "presume" "assume propositions, to be established later"
   569     (K.tag_proof K.prf_asm)
   570     (SpecParse.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume)));
   571 
   572 val _ =
   573   OuterSyntax.command "def" "local definition"
   574     (K.tag_proof K.prf_asm)
   575     (P.and_list1
   576       (SpecParse.opt_thm_name ":" --
   577         ((P.binding -- P.opt_mixfix) -- ((P.$$$ "\\<equiv>" || P.$$$ "==") |-- P.!!! P.termp)))
   578     >> (Toplevel.print oo (Toplevel.proof o Proof.def)));
   579 
   580 val _ =
   581   OuterSyntax.command "obtain" "generalized existence"
   582     (K.tag_proof K.prf_asm_goal)
   583     (P.parname -- Scan.optional (P.fixes --| P.where_) [] -- SpecParse.statement
   584       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain x y z)));
   585 
   586 val _ =
   587   OuterSyntax.command "guess" "wild guessing (unstructured)"
   588     (K.tag_proof K.prf_asm_goal)
   589     (Scan.optional P.fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess)));
   590 
   591 val _ =
   592   OuterSyntax.command "let" "bind text variables"
   593     (K.tag_proof K.prf_decl)
   594     (P.and_list1 (P.and_list1 P.term -- (P.$$$ "=" |-- P.term))
   595       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind)));
   596 
   597 val case_spec =
   598   (P.$$$ "(" |-- P.!!! (P.xname -- Scan.repeat1 (P.maybe P.name) --| P.$$$ ")") ||
   599     P.xname >> rpair []) -- SpecParse.opt_attribs >> P.triple1;
   600 
   601 val _ =
   602   OuterSyntax.command "case" "invoke local context"
   603     (K.tag_proof K.prf_asm)
   604     (case_spec >> (Toplevel.print oo (Toplevel.proof o Proof.invoke_case)));
   605 
   606 
   607 (* proof structure *)
   608 
   609 val _ =
   610   OuterSyntax.command "{" "begin explicit proof block"
   611     (K.tag_proof K.prf_open)
   612     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   613 
   614 val _ =
   615   OuterSyntax.command "}" "end explicit proof block"
   616     (K.tag_proof K.prf_close)
   617     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.end_block));
   618 
   619 val _ =
   620   OuterSyntax.command "next" "enter next proof block"
   621     (K.tag_proof K.prf_block)
   622     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   623 
   624 
   625 (* end proof *)
   626 
   627 val _ =
   628   OuterSyntax.command "qed" "conclude (sub-)proof"
   629     (K.tag_proof K.qed_block)
   630     (Scan.option Method.parse >> IsarCmd.qed);
   631 
   632 val _ =
   633   OuterSyntax.command "by" "terminal backward proof"
   634     (K.tag_proof K.qed)
   635     (Method.parse -- Scan.option Method.parse >> IsarCmd.terminal_proof);
   636 
   637 val _ =
   638   OuterSyntax.command ".." "default proof"
   639     (K.tag_proof K.qed)
   640     (Scan.succeed IsarCmd.default_proof);
   641 
   642 val _ =
   643   OuterSyntax.command "." "immediate proof"
   644     (K.tag_proof K.qed)
   645     (Scan.succeed IsarCmd.immediate_proof);
   646 
   647 val _ =
   648   OuterSyntax.command "done" "done proof"
   649     (K.tag_proof K.qed)
   650     (Scan.succeed IsarCmd.done_proof);
   651 
   652 val _ =
   653   OuterSyntax.command "sorry" "skip proof (quick-and-dirty mode only!)"
   654     (K.tag_proof K.qed)
   655     (Scan.succeed IsarCmd.skip_proof);
   656 
   657 val _ =
   658   OuterSyntax.command "oops" "forget proof"
   659     (K.tag_proof K.qed_global)
   660     (Scan.succeed Toplevel.forget_proof);
   661 
   662 
   663 (* proof steps *)
   664 
   665 val _ =
   666   OuterSyntax.command "defer" "shuffle internal proof state"
   667     (K.tag_proof K.prf_script)
   668     (Scan.option P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.defer)));
   669 
   670 val _ =
   671   OuterSyntax.command "prefer" "shuffle internal proof state"
   672     (K.tag_proof K.prf_script)
   673     (P.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.prefer)));
   674 
   675 val _ =
   676   OuterSyntax.command "apply" "initial refinement step (unstructured)"
   677     (K.tag_proof K.prf_script)
   678     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply)));
   679 
   680 val _ =
   681   OuterSyntax.command "apply_end" "terminal refinement (unstructured)"
   682     (K.tag_proof K.prf_script)
   683     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply_end)));
   684 
   685 val _ =
   686   OuterSyntax.command "proof" "backward proof"
   687     (K.tag_proof K.prf_block)
   688     (Scan.option Method.parse >> (fn m => Toplevel.print o
   689       Toplevel.actual_proof (Proof_Node.applys (Proof.proof m)) o
   690       Toplevel.skip_proof (fn i => i + 1)));
   691 
   692 
   693 (* calculational proof commands *)
   694 
   695 val calc_args =
   696   Scan.option (P.$$$ "(" |-- P.!!! ((SpecParse.xthms1 --| P.$$$ ")")));
   697 
   698 val _ =
   699   OuterSyntax.command "also" "combine calculation and current facts"
   700     (K.tag_proof K.prf_decl)
   701     (calc_args >> (Toplevel.proofs' o Calculation.also));
   702 
   703 val _ =
   704   OuterSyntax.command "finally" "combine calculation and current facts, exhibit result"
   705     (K.tag_proof K.prf_chain)
   706     (calc_args >> (Toplevel.proofs' o Calculation.finally));
   707 
   708 val _ =
   709   OuterSyntax.command "moreover" "augment calculation by current facts"
   710     (K.tag_proof K.prf_decl)
   711     (Scan.succeed (Toplevel.proof' Calculation.moreover));
   712 
   713 val _ =
   714   OuterSyntax.command "ultimately" "augment calculation by current facts, exhibit result"
   715     (K.tag_proof K.prf_chain)
   716     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
   717 
   718 
   719 (* proof navigation *)
   720 
   721 val _ =
   722   OuterSyntax.command "back" "backtracking of proof command"
   723     (K.tag_proof K.prf_script)
   724     (Scan.succeed (Toplevel.print o Toplevel.actual_proof Proof_Node.back o Toplevel.skip_proof I));
   725 
   726 
   727 (* nested commands *)
   728 
   729 val props_text =
   730   Scan.optional ValueParse.properties [] -- P.position P.string >> (fn (props, (str, pos)) =>
   731     (Position.of_properties (Position.default_properties pos props), str));
   732 
   733 val _ =
   734   OuterSyntax.improper_command "Isabelle.command" "nested Isabelle command" K.control
   735     (props_text :|-- (fn (pos, str) =>
   736       (case OuterSyntax.parse pos str of
   737         [tr] => Scan.succeed (K tr)
   738       | _ => Scan.fail_with (K "exactly one command expected"))
   739       handle ERROR msg => Scan.fail_with (K msg)));
   740 
   741 
   742 
   743 (** diagnostic commands (for interactive mode only) **)
   744 
   745 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   746 val opt_bang = Scan.optional (P.$$$ "!" >> K true) false;
   747 
   748 val _ =
   749   OuterSyntax.improper_command "pretty_setmargin" "change default margin for pretty printing"
   750     K.diag (P.nat >> (Toplevel.no_timing oo IsarCmd.pretty_setmargin));
   751 
   752 val _ =
   753   OuterSyntax.improper_command "help" "print outer syntax commands" K.diag
   754     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative OuterSyntax.print_outer_syntax));
   755 
   756 val _ =
   757   OuterSyntax.improper_command "print_commands" "print outer syntax commands" K.diag
   758     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative OuterSyntax.print_outer_syntax));
   759 
   760 val _ =
   761   OuterSyntax.improper_command "print_configs" "print configuration options" K.diag
   762     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_configs));
   763 
   764 val _ =
   765   OuterSyntax.improper_command "print_context" "print theory context name" K.diag
   766     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_context));
   767 
   768 val _ =
   769   OuterSyntax.improper_command "print_theory" "print logical theory contents (verbose!)" K.diag
   770     (opt_bang >> (Toplevel.no_timing oo IsarCmd.print_theory));
   771 
   772 val _ =
   773   OuterSyntax.improper_command "print_syntax" "print inner syntax of context (verbose!)" K.diag
   774     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_syntax));
   775 
   776 val _ =
   777   OuterSyntax.improper_command "print_abbrevs" "print constant abbreviation of context" K.diag
   778     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_abbrevs));
   779 
   780 val _ =
   781   OuterSyntax.improper_command "print_theorems"
   782       "print theorems of local theory or proof context" K.diag
   783     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_theorems));
   784 
   785 val _ =
   786   OuterSyntax.improper_command "print_locales" "print locales of this theory" K.diag
   787     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_locales));
   788 
   789 val _ =
   790   OuterSyntax.improper_command "print_classes" "print classes of this theory" K.diag
   791     (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory
   792       o Toplevel.keep (Class.print_classes o Toplevel.theory_of)));
   793 
   794 val _ =
   795   OuterSyntax.improper_command "print_locale" "print locale of this theory" K.diag
   796     (opt_bang -- P.xname >> (Toplevel.no_timing oo IsarCmd.print_locale));
   797 
   798 val _ =
   799   OuterSyntax.improper_command "print_interps"
   800     "print interpretations of locale for this theory" K.diag
   801     (P.xname >> (Toplevel.no_timing oo IsarCmd.print_registrations));
   802 
   803 val _ =
   804   OuterSyntax.improper_command "print_attributes" "print attributes of this theory" K.diag
   805     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_attributes));
   806 
   807 val _ =
   808   OuterSyntax.improper_command "print_simpset" "print context of Simplifier" K.diag
   809     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_simpset));
   810 
   811 val _ =
   812   OuterSyntax.improper_command "print_rules" "print intro/elim rules" K.diag
   813     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_rules));
   814 
   815 val _ =
   816   OuterSyntax.improper_command "print_trans_rules" "print transitivity rules" K.diag
   817     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_trans_rules));
   818 
   819 val _ =
   820   OuterSyntax.improper_command "print_methods" "print methods of this theory" K.diag
   821     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_methods));
   822 
   823 val _ =
   824   OuterSyntax.improper_command "print_antiquotations" "print antiquotations (global)" K.diag
   825     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_antiquotations));
   826 
   827 val _ =
   828   OuterSyntax.improper_command "thy_deps" "visualize theory dependencies"
   829     K.diag (Scan.succeed (Toplevel.no_timing o IsarCmd.thy_deps));
   830 
   831 val _ =
   832   OuterSyntax.improper_command "class_deps" "visualize class dependencies"
   833     K.diag (Scan.succeed (Toplevel.no_timing o IsarCmd.class_deps));
   834 
   835 val _ =
   836   OuterSyntax.improper_command "thm_deps" "visualize theorem dependencies"
   837     K.diag (SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.thm_deps));
   838 
   839 val _ =
   840   OuterSyntax.improper_command "print_binds" "print term bindings of proof context" K.diag
   841     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_binds));
   842 
   843 val _ =
   844   OuterSyntax.improper_command "print_facts" "print facts of proof context" K.diag
   845     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_facts));
   846 
   847 val _ =
   848   OuterSyntax.improper_command "print_cases" "print cases of proof context" K.diag
   849     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_cases));
   850 
   851 val _ =
   852   OuterSyntax.improper_command "print_statement" "print theorems as long statements" K.diag
   853     (opt_modes -- SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_stmts));
   854 
   855 val _ =
   856   OuterSyntax.improper_command "thm" "print theorems" K.diag
   857     (opt_modes -- SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_thms));
   858 
   859 val _ =
   860   OuterSyntax.improper_command "prf" "print proof terms of theorems" K.diag
   861     (opt_modes -- Scan.option SpecParse.xthms1
   862       >> (Toplevel.no_timing oo IsarCmd.print_prfs false));
   863 
   864 val _ =
   865   OuterSyntax.improper_command "full_prf" "print full proof terms of theorems" K.diag
   866     (opt_modes -- Scan.option SpecParse.xthms1 >> (Toplevel.no_timing oo IsarCmd.print_prfs true));
   867 
   868 val _ =
   869   OuterSyntax.improper_command "prop" "read and print proposition" K.diag
   870     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_prop));
   871 
   872 val _ =
   873   OuterSyntax.improper_command "term" "read and print term" K.diag
   874     (opt_modes -- P.term >> (Toplevel.no_timing oo IsarCmd.print_term));
   875 
   876 val _ =
   877   OuterSyntax.improper_command "typ" "read and print type" K.diag
   878     (opt_modes -- P.typ >> (Toplevel.no_timing oo IsarCmd.print_type));
   879 
   880 val _ =
   881   OuterSyntax.improper_command "print_codesetup" "print code generator setup" K.diag
   882     (Scan.succeed
   883       (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   884         (Code.print_codesetup o Toplevel.theory_of)));
   885 
   886 val _ =
   887   OuterSyntax.improper_command "unused_thms" "find unused theorems" K.diag
   888     (Scan.option ((Scan.repeat1 (Scan.unless P.minus P.name) --| P.minus) --
   889        Scan.option (Scan.repeat1 (Scan.unless P.minus P.name))) >>
   890          (Toplevel.no_timing oo IsarCmd.unused_thms));
   891 
   892 
   893 
   894 (** system commands (for interactive mode only) **)
   895 
   896 val _ =
   897   OuterSyntax.improper_command "cd" "change current working directory" K.diag
   898     (P.path >> (Toplevel.no_timing oo IsarCmd.cd));
   899 
   900 val _ =
   901   OuterSyntax.improper_command "pwd" "print current working directory" K.diag
   902     (Scan.succeed (Toplevel.no_timing o IsarCmd.pwd));
   903 
   904 val _ =
   905   OuterSyntax.improper_command "use_thy" "use theory file" K.diag
   906     (P.name >> (fn name =>
   907       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.use_thy name)));
   908 
   909 val _ =
   910   OuterSyntax.improper_command "touch_thy" "outdate theory, including descendants" K.diag
   911     (P.name >> (fn name =>
   912       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.touch_thy name)));
   913 
   914 val _ =
   915   OuterSyntax.improper_command "remove_thy" "remove theory from loader database" K.diag
   916     (P.name >> (fn name =>
   917       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.remove_thy name)));
   918 
   919 val _ =
   920   OuterSyntax.improper_command "kill_thy" "kill theory -- try to remove from loader database"
   921     K.diag (P.name >> (fn name =>
   922       Toplevel.no_timing o Toplevel.imperative (fn () => ThyInfo.kill_thy name)));
   923 
   924 val _ =
   925   OuterSyntax.improper_command "display_drafts" "display raw source files with symbols"
   926     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.display_drafts));
   927 
   928 val _ =
   929   OuterSyntax.improper_command "print_drafts" "print raw source files with symbols"
   930     K.diag (Scan.repeat1 P.path >> (Toplevel.no_timing oo IsarCmd.print_drafts));
   931 
   932 val opt_limits =
   933   Scan.option P.nat -- Scan.option (P.$$$ "," |-- P.!!! P.nat);
   934 
   935 val _ =
   936   OuterSyntax.improper_command "pr" "print current proof state (if present)" K.diag
   937     (opt_modes -- opt_limits >> (Toplevel.no_timing oo IsarCmd.pr));
   938 
   939 val _ =
   940   OuterSyntax.improper_command "disable_pr" "disable printing of toplevel state" K.diag
   941     (Scan.succeed (Toplevel.no_timing o IsarCmd.disable_pr));
   942 
   943 val _ =
   944   OuterSyntax.improper_command "enable_pr" "enable printing of toplevel state" K.diag
   945     (Scan.succeed (Toplevel.no_timing o IsarCmd.enable_pr));
   946 
   947 val _ =
   948   OuterSyntax.improper_command "commit" "commit current session to ML database" K.diag
   949     (P.opt_unit >> K (Toplevel.no_timing o Toplevel.imperative Secure.commit));
   950 
   951 val _ =
   952   OuterSyntax.improper_command "quit" "quit Isabelle" K.control
   953     (P.opt_unit >> (Toplevel.no_timing oo K IsarCmd.quit));
   954 
   955 val _ =
   956   OuterSyntax.improper_command "exit" "exit Isar loop" K.control
   957     (Scan.succeed (Toplevel.no_timing o IsarCmd.exit));
   958 
   959 end;
   960