src/Pure/Isar/isar_cmd.ML
author wenzelm
Wed Sep 02 18:14:10 2015 +0200 (2015-09-02 ago)
changeset 61086 fc7ab11128dc
parent 60695 044f8bb3dd30
child 61144 5e94dfead1c2
permissions -rw-r--r--
tuned message;
wenzelm@5831
     1
(*  Title:      Pure/Isar/isar_cmd.ML
wenzelm@5831
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5831
     3
wenzelm@30805
     4
Miscellaneous Isar commands.
wenzelm@5831
     5
*)
wenzelm@5831
     6
wenzelm@5831
     7
signature ISAR_CMD =
wenzelm@5831
     8
sig
wenzelm@59930
     9
  val setup: Input.source -> theory -> theory
wenzelm@59064
    10
  val local_setup: Input.source -> Proof.context -> Proof.context
wenzelm@59064
    11
  val parse_ast_translation: Input.source -> theory -> theory
wenzelm@59064
    12
  val parse_translation: Input.source -> theory -> theory
wenzelm@59064
    13
  val print_translation: Input.source -> theory -> theory
wenzelm@59064
    14
  val typed_print_translation: Input.source -> theory -> theory
wenzelm@59064
    15
  val print_ast_translation: Input.source -> theory -> theory
wenzelm@42204
    16
  val translations: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@42204
    17
  val no_translations: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@59064
    18
  val oracle: bstring * Position.range -> Input.source -> theory -> theory
wenzelm@58011
    19
  val add_defs: (bool * bool) * ((binding * string) * Token.src list) list -> theory -> theory
wenzelm@59064
    20
  val declaration: {syntax: bool, pervasive: bool} -> Input.source -> local_theory -> local_theory
wenzelm@59064
    21
  val simproc_setup: string * Position.T -> string list -> Input.source ->
wenzelm@42464
    22
    string list -> local_theory -> local_theory
wenzelm@49889
    23
  val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
wenzelm@49889
    24
  val terminal_proof: Method.text_range * Method.text_range option ->
wenzelm@21350
    25
    Toplevel.transition -> Toplevel.transition
wenzelm@21350
    26
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    27
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    28
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    29
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@59064
    30
  val ml_diag: bool -> Input.source -> Toplevel.transition -> Toplevel.transition
wenzelm@48776
    31
  val diag_state: Proof.context -> Toplevel.state
wenzelm@48776
    32
  val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
wenzelm@57605
    33
  val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
wenzelm@58011
    34
  val print_stmts: string list * (Facts.ref * Token.src list) list
wenzelm@19268
    35
    -> Toplevel.transition -> Toplevel.transition
wenzelm@58011
    36
  val print_thms: string list * (Facts.ref * Token.src list) list
wenzelm@10581
    37
    -> Toplevel.transition -> Toplevel.transition
wenzelm@58011
    38
  val print_prfs: bool -> string list * (Facts.ref * Token.src list) list option
berghofe@11524
    39
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    40
  val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    41
  val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@48792
    42
  val print_type: (string list * (string * string option)) ->
wenzelm@48792
    43
    Toplevel.transition -> Toplevel.transition
wenzelm@5831
    44
end;
wenzelm@5831
    45
wenzelm@37216
    46
structure Isar_Cmd: ISAR_CMD =
wenzelm@5831
    47
struct
wenzelm@5831
    48
wenzelm@5831
    49
wenzelm@22116
    50
(** theory declarations **)
wenzelm@22116
    51
wenzelm@30461
    52
(* generic setup *)
wenzelm@22116
    53
wenzelm@59930
    54
fun setup source =
wenzelm@56278
    55
  ML_Lex.read_source false source
wenzelm@59064
    56
  |> ML_Context.expression (Input.range_of source) "setup" "theory -> theory"
wenzelm@58991
    57
    "Context.map_theory setup"
wenzelm@26435
    58
  |> Context.theory_map;
wenzelm@22116
    59
wenzelm@55828
    60
fun local_setup source =
wenzelm@56278
    61
  ML_Lex.read_source false source
wenzelm@59064
    62
  |> ML_Context.expression (Input.range_of source) "local_setup" "local_theory -> local_theory"
wenzelm@58991
    63
    "Context.map_proof local_setup"
wenzelm@30461
    64
  |> Context.proof_map;
wenzelm@30461
    65
wenzelm@22116
    66
wenzelm@22116
    67
(* translation functions *)
wenzelm@22116
    68
wenzelm@55828
    69
fun parse_ast_translation source =
wenzelm@56278
    70
  ML_Lex.read_source false source
wenzelm@59064
    71
  |> ML_Context.expression (Input.range_of source) "parse_ast_translation"
wenzelm@58991
    72
    "(string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
wenzelm@52143
    73
    "Context.map_theory (Sign.parse_ast_translation parse_ast_translation)"
wenzelm@22116
    74
  |> Context.theory_map;
wenzelm@22116
    75
wenzelm@55828
    76
fun parse_translation source =
wenzelm@56278
    77
  ML_Lex.read_source false source
wenzelm@59064
    78
  |> ML_Context.expression (Input.range_of source) "parse_translation"
wenzelm@58991
    79
    "(string * (Proof.context -> term list -> term)) list"
wenzelm@52143
    80
    "Context.map_theory (Sign.parse_translation parse_translation)"
wenzelm@52143
    81
  |> Context.theory_map;
wenzelm@52143
    82
wenzelm@55828
    83
fun print_translation source =
wenzelm@56278
    84
  ML_Lex.read_source false source
wenzelm@59064
    85
  |> ML_Context.expression (Input.range_of source) "print_translation"
wenzelm@58991
    86
    "(string * (Proof.context -> term list -> term)) list"
wenzelm@52143
    87
    "Context.map_theory (Sign.print_translation print_translation)"
wenzelm@22116
    88
  |> Context.theory_map;
wenzelm@22116
    89
wenzelm@55828
    90
fun typed_print_translation source =
wenzelm@56278
    91
  ML_Lex.read_source false source
wenzelm@59064
    92
  |> ML_Context.expression (Input.range_of source) "typed_print_translation"
wenzelm@58991
    93
    "(string * (Proof.context -> typ -> term list -> term)) list"
wenzelm@52143
    94
    "Context.map_theory (Sign.typed_print_translation typed_print_translation)"
wenzelm@22116
    95
  |> Context.theory_map;
wenzelm@22116
    96
wenzelm@55828
    97
fun print_ast_translation source =
wenzelm@56278
    98
  ML_Lex.read_source false source
wenzelm@59064
    99
  |> ML_Context.expression (Input.range_of source) "print_ast_translation"
wenzelm@58991
   100
    "(string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
wenzelm@52143
   101
    "Context.map_theory (Sign.print_ast_translation print_ast_translation)"
wenzelm@22116
   102
  |> Context.theory_map;
wenzelm@22116
   103
wenzelm@22116
   104
wenzelm@42204
   105
(* translation rules *)
wenzelm@42204
   106
wenzelm@42204
   107
fun read_trrules thy raw_rules =
wenzelm@42204
   108
  let
wenzelm@42360
   109
    val ctxt = Proof_Context.init_global thy;
wenzelm@56006
   110
    val read_root =
wenzelm@56006
   111
      #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} ctxt;
wenzelm@42204
   112
  in
wenzelm@56006
   113
    raw_rules
wenzelm@56006
   114
    |> map (Syntax.map_trrule (fn (r, s) => Syntax_Phases.parse_ast_pattern ctxt (read_root r, s)))
wenzelm@42204
   115
  end;
wenzelm@42204
   116
wenzelm@42204
   117
fun translations args thy = Sign.add_trrules (read_trrules thy args) thy;
wenzelm@42204
   118
fun no_translations args thy = Sign.del_trrules (read_trrules thy args) thy;
wenzelm@42204
   119
wenzelm@42204
   120
wenzelm@22116
   121
(* oracles *)
wenzelm@22116
   122
wenzelm@59029
   123
fun oracle (name, range) source =
wenzelm@27871
   124
  let
wenzelm@59064
   125
    val body_range = Input.range_of source;
wenzelm@56278
   126
    val body = ML_Lex.read_source false source;
wenzelm@59029
   127
wenzelm@37198
   128
    val ants =
wenzelm@59067
   129
      ML_Lex.read
wenzelm@37198
   130
       ("local\n\
wenzelm@59029
   131
        \  val binding = " ^ ML_Syntax.make_binding (name, #1 range) ^ ";\n\
wenzelm@59067
   132
        \  val") @ ML_Lex.read_set_range body_range "oracle" @ ML_Lex.read "=" @ body @
wenzelm@59067
   133
        ML_Lex.read (";\nin\n\
wenzelm@59067
   134
        \  val") @ ML_Lex.read_set_range range name @ ML_Lex.read "=\
wenzelm@59029
   135
        \ snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, oracle))));\n\
wenzelm@59029
   136
        \end;\n";
wenzelm@56304
   137
  in
wenzelm@56304
   138
    Context.theory_map
wenzelm@59029
   139
      (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#1 body_range) ants))
wenzelm@56304
   140
  end;
wenzelm@22116
   141
wenzelm@22116
   142
wenzelm@50737
   143
(* old-style defs *)
wenzelm@21350
   144
wenzelm@35852
   145
fun add_defs ((unchecked, overloaded), args) thy =
wenzelm@61086
   146
 (legacy_feature ("Old 'defs' command -- use 'definition' (with 'overloading') instead" ^
wenzelm@61086
   147
    Position.here (Position.thread_data ()));
wenzelm@39557
   148
  thy |>
wenzelm@39557
   149
    (if unchecked then Global_Theory.add_defs_unchecked_cmd else Global_Theory.add_defs_cmd)
wenzelm@47815
   150
      overloaded
wenzelm@47815
   151
      (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute_cmd_global thy) srcs)) args)
wenzelm@57683
   152
  |> snd);
wenzelm@21350
   153
wenzelm@21350
   154
wenzelm@22087
   155
(* declarations *)
wenzelm@22087
   156
wenzelm@55828
   157
fun declaration {syntax, pervasive} source =
wenzelm@56278
   158
  ML_Lex.read_source false source
wenzelm@59064
   159
  |> ML_Context.expression (Input.range_of source) "declaration" "Morphism.declaration"
wenzelm@45291
   160
    ("Context.map_proof (Local_Theory.declaration {syntax = " ^ Bool.toString syntax ^ ", \
wenzelm@45291
   161
      \pervasive = " ^ Bool.toString pervasive ^ "} declaration)")
wenzelm@26385
   162
  |> Context.proof_map;
wenzelm@22087
   163
wenzelm@22087
   164
wenzelm@22202
   165
(* simprocs *)
wenzelm@22202
   166
wenzelm@55828
   167
fun simproc_setup name lhss source identifier =
wenzelm@56278
   168
  ML_Lex.read_source false source
wenzelm@59064
   169
  |> ML_Context.expression (Input.range_of source) "proc"
wenzelm@58991
   170
    "Morphism.morphism -> Proof.context -> cterm -> thm option"
wenzelm@42464
   171
    ("Context.map_proof (Simplifier.def_simproc_cmd {name = " ^ ML_Syntax.make_binding name ^ ", \
wenzelm@37198
   172
      \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
wenzelm@37198
   173
      \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})")
wenzelm@22202
   174
  |> Context.proof_map;
wenzelm@22202
   175
wenzelm@22202
   176
wenzelm@21350
   177
(* local endings *)
wenzelm@21350
   178
wenzelm@29383
   179
fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
wenzelm@60403
   180
val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
wenzelm@29383
   181
val local_default_proof = Toplevel.proof Proof.local_default_proof;
wenzelm@29383
   182
val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
wenzelm@29383
   183
val local_done_proof = Toplevel.proof Proof.local_done_proof;
wenzelm@29383
   184
val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
wenzelm@21350
   185
wenzelm@21350
   186
wenzelm@21350
   187
(* global endings *)
wenzelm@21350
   188
wenzelm@21350
   189
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@60403
   190
val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
wenzelm@21350
   191
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   192
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   193
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   194
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   195
wenzelm@21350
   196
wenzelm@21350
   197
(* common endings *)
wenzelm@21350
   198
wenzelm@60695
   199
fun qed m = local_qed m o global_qed m;
wenzelm@21350
   200
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   201
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   202
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   203
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   204
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   205
wenzelm@21350
   206
wenzelm@26489
   207
(* diagnostic ML evaluation *)
wenzelm@5831
   208
wenzelm@37305
   209
structure Diag_State = Proof_Data
wenzelm@37305
   210
(
wenzelm@37305
   211
  type T = Toplevel.state;
wenzelm@37305
   212
  fun init _ = Toplevel.toplevel;
wenzelm@37305
   213
);
wenzelm@37305
   214
wenzelm@55828
   215
fun ml_diag verbose source = Toplevel.keep (fn state =>
wenzelm@56304
   216
  let
wenzelm@56304
   217
    val opt_ctxt =
wenzelm@56304
   218
      try Toplevel.generic_theory_of state
wenzelm@56304
   219
      |> Option.map (Context.proof_of #> Diag_State.put state);
wenzelm@56304
   220
    val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
wenzelm@56304
   221
  in ML_Context.eval_source_in opt_ctxt flags source end);
wenzelm@37305
   222
wenzelm@48776
   223
val diag_state = Diag_State.get;
wenzelm@60096
   224
val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
wenzelm@37305
   225
wenzelm@53171
   226
val _ = Theory.setup
wenzelm@56204
   227
  (ML_Antiquotation.value (Binding.qualify true "Isar" @{binding state})
wenzelm@53171
   228
    (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
wenzelm@56204
   229
   ML_Antiquotation.value (Binding.qualify true "Isar" @{binding goal})
wenzelm@53171
   230
    (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
wenzelm@5831
   231
wenzelm@5831
   232
wenzelm@56868
   233
(* theorems of theory or proof context *)
wenzelm@17066
   234
wenzelm@56868
   235
fun pretty_theorems verbose st =
wenzelm@56868
   236
  if Toplevel.is_proof st then
wenzelm@59917
   237
    Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
wenzelm@56868
   238
  else
wenzelm@56868
   239
    let
wenzelm@56868
   240
      val thy = Toplevel.theory_of st;
wenzelm@56868
   241
      val prev_thys =
wenzelm@56868
   242
        (case Toplevel.previous_context_of st of
wenzelm@56868
   243
          SOME prev => [Proof_Context.theory_of prev]
wenzelm@56868
   244
        | NONE => Theory.parents_of thy);
wenzelm@56868
   245
    in Proof_Display.pretty_theorems_diff verbose prev_thys thy end;
wenzelm@9513
   246
wenzelm@12060
   247
wenzelm@19268
   248
(* print theorems, terms, types etc. *)
wenzelm@19268
   249
wenzelm@19385
   250
local
wenzelm@19385
   251
wenzelm@38331
   252
fun string_of_stmts ctxt args =
wenzelm@38331
   253
  Attrib.eval_thms ctxt args
wenzelm@38331
   254
  |> map (Element.pretty_statement ctxt Thm.theoremK)
wenzelm@19385
   255
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   256
wenzelm@38331
   257
fun string_of_thms ctxt args =
wenzelm@51583
   258
  Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
wenzelm@5895
   259
wenzelm@19385
   260
fun string_of_prfs full state arg =
wenzelm@32859
   261
  Pretty.string_of
wenzelm@32859
   262
    (case arg of
skalberg@15531
   263
      NONE =>
berghofe@12125
   264
        let
wenzelm@38331
   265
          val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
wenzelm@42360
   266
          val thy = Proof_Context.theory_of ctxt;
wenzelm@28814
   267
          val prf = Thm.proof_of thm;
wenzelm@17066
   268
          val prop = Thm.full_prop_of thm;
wenzelm@32859
   269
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
berghofe@12125
   270
        in
wenzelm@33388
   271
          Proof_Syntax.pretty_proof ctxt
wenzelm@17066
   272
            (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
berghofe@12125
   273
        end
wenzelm@38331
   274
    | SOME srcs =>
wenzelm@38331
   275
        let val ctxt = Toplevel.context_of state
wenzelm@38331
   276
        in map (Proof_Syntax.pretty_proof_of ctxt full) (Attrib.eval_thms ctxt srcs) end
wenzelm@38331
   277
        |> Pretty.chunks);
berghofe@11524
   278
wenzelm@38331
   279
fun string_of_prop ctxt s =
wenzelm@5831
   280
  let
wenzelm@24508
   281
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   282
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   283
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   284
wenzelm@38331
   285
fun string_of_term ctxt s =
wenzelm@5831
   286
  let
wenzelm@24508
   287
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   288
    val T = Term.type_of t;
wenzelm@26704
   289
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   290
  in
wenzelm@19385
   291
    Pretty.string_of
wenzelm@26704
   292
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   293
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   294
  end;
wenzelm@5831
   295
wenzelm@48792
   296
fun string_of_type ctxt (s, NONE) =
wenzelm@48792
   297
      let val T = Syntax.read_typ ctxt s
wenzelm@48792
   298
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
wenzelm@48792
   299
  | string_of_type ctxt (s1, SOME s2) =
wenzelm@48792
   300
      let
wenzelm@48792
   301
        val ctxt' = Config.put show_sorts true ctxt;
wenzelm@48792
   302
        val raw_T = Syntax.parse_typ ctxt' s1;
wenzelm@48792
   303
        val S = Syntax.read_sort ctxt' s2;
wenzelm@48792
   304
        val T =
wenzelm@48792
   305
          Syntax.check_term ctxt'
wenzelm@48792
   306
            (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
wenzelm@48792
   307
          |> Logic.dest_type;
wenzelm@48792
   308
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
wenzelm@9128
   309
wenzelm@23935
   310
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@38331
   311
  Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
wenzelm@19385
   312
wenzelm@19385
   313
in
wenzelm@9128
   314
wenzelm@38331
   315
val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
wenzelm@38331
   316
val print_thms = print_item (string_of_thms o Toplevel.context_of);
wenzelm@19385
   317
val print_prfs = print_item o string_of_prfs;
wenzelm@38331
   318
val print_prop = print_item (string_of_prop o Toplevel.context_of);
wenzelm@38331
   319
val print_term = print_item (string_of_term o Toplevel.context_of);
wenzelm@38331
   320
val print_type = print_item (string_of_type o Toplevel.context_of);
wenzelm@5831
   321
wenzelm@19385
   322
end;
wenzelm@19385
   323
wenzelm@5831
   324
end;