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