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