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