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