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