src/Pure/Isar/isar_cmd.ML
author wenzelm
Thu Nov 08 14:48:20 2018 +0100 (11 months ago ago)
changeset 69268 c546e37f6cb9
parent 69221 1a52baa70aed
child 70064 f8293bf510a0
permissions -rw-r--r--
clarified ML positions (see also 1a52baa70aed);
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@59064
    19
  val declaration: {syntax: bool, pervasive: bool} -> Input.source -> local_theory -> local_theory
wenzelm@59064
    20
  val simproc_setup: string * Position.T -> string list -> Input.source ->
wenzelm@62913
    21
    local_theory -> local_theory
wenzelm@49889
    22
  val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
wenzelm@49889
    23
  val terminal_proof: Method.text_range * Method.text_range option ->
wenzelm@21350
    24
    Toplevel.transition -> Toplevel.transition
wenzelm@21350
    25
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    26
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    27
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    28
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@59064
    29
  val ml_diag: bool -> Input.source -> Toplevel.transition -> Toplevel.transition
wenzelm@48776
    30
  val diag_state: Proof.context -> Toplevel.state
wenzelm@48776
    31
  val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
wenzelm@57605
    32
  val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
wenzelm@58011
    33
  val print_stmts: string list * (Facts.ref * Token.src list) list
wenzelm@19268
    34
    -> Toplevel.transition -> Toplevel.transition
wenzelm@58011
    35
  val print_thms: string list * (Facts.ref * Token.src list) list
wenzelm@10581
    36
    -> Toplevel.transition -> Toplevel.transition
wenzelm@58011
    37
  val print_prfs: bool -> string list * (Facts.ref * Token.src list) list option
berghofe@11524
    38
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    39
  val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    40
  val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@48792
    41
  val print_type: (string list * (string * string option)) ->
wenzelm@48792
    42
    Toplevel.transition -> Toplevel.transition
wenzelm@5831
    43
end;
wenzelm@5831
    44
wenzelm@37216
    45
structure Isar_Cmd: ISAR_CMD =
wenzelm@5831
    46
struct
wenzelm@5831
    47
wenzelm@5831
    48
wenzelm@22116
    49
(** theory declarations **)
wenzelm@22116
    50
wenzelm@30461
    51
(* generic setup *)
wenzelm@22116
    52
wenzelm@59930
    53
fun setup source =
wenzelm@69221
    54
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    55
    (ML_Lex.read "Theory.setup (" @ ML_Lex.read_source source @ ML_Lex.read ")")
wenzelm@26435
    56
  |> Context.theory_map;
wenzelm@22116
    57
wenzelm@55828
    58
fun local_setup source =
wenzelm@69221
    59
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    60
    (ML_Lex.read "Theory.local_setup (" @ ML_Lex.read_source source @ ML_Lex.read ")")
wenzelm@30461
    61
  |> Context.proof_map;
wenzelm@30461
    62
wenzelm@22116
    63
wenzelm@22116
    64
(* translation functions *)
wenzelm@22116
    65
wenzelm@55828
    66
fun parse_ast_translation source =
wenzelm@69221
    67
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    68
    (ML_Lex.read "Theory.setup (Sign.parse_ast_translation (" @
wenzelm@69221
    69
      ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@22116
    70
  |> Context.theory_map;
wenzelm@22116
    71
wenzelm@55828
    72
fun parse_translation source =
wenzelm@69221
    73
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    74
    (ML_Lex.read "Theory.setup (Sign.parse_translation (" @
wenzelm@69221
    75
      ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@52143
    76
  |> Context.theory_map;
wenzelm@52143
    77
wenzelm@55828
    78
fun print_translation source =
wenzelm@69221
    79
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    80
    (ML_Lex.read "Theory.setup (Sign.print_translation (" @
wenzelm@69221
    81
      ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@22116
    82
  |> Context.theory_map;
wenzelm@22116
    83
wenzelm@55828
    84
fun typed_print_translation source =
wenzelm@69221
    85
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    86
    (ML_Lex.read "Theory.setup (Sign.typed_print_translation (" @
wenzelm@69221
    87
      ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@22116
    88
  |> Context.theory_map;
wenzelm@22116
    89
wenzelm@55828
    90
fun print_ast_translation source =
wenzelm@69221
    91
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
    92
    (ML_Lex.read "Theory.setup (Sign.print_ast_translation (" @
wenzelm@69221
    93
      ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@22116
    94
  |> Context.theory_map;
wenzelm@22116
    95
wenzelm@22116
    96
wenzelm@42204
    97
(* translation rules *)
wenzelm@42204
    98
wenzelm@42204
    99
fun read_trrules thy raw_rules =
wenzelm@42204
   100
  let
wenzelm@42360
   101
    val ctxt = Proof_Context.init_global thy;
wenzelm@56006
   102
    val read_root =
wenzelm@56006
   103
      #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} ctxt;
wenzelm@42204
   104
  in
wenzelm@56006
   105
    raw_rules
wenzelm@56006
   106
    |> map (Syntax.map_trrule (fn (r, s) => Syntax_Phases.parse_ast_pattern ctxt (read_root r, s)))
wenzelm@42204
   107
  end;
wenzelm@42204
   108
wenzelm@42204
   109
fun translations args thy = Sign.add_trrules (read_trrules thy args) thy;
wenzelm@42204
   110
fun no_translations args thy = Sign.del_trrules (read_trrules thy args) thy;
wenzelm@42204
   111
wenzelm@42204
   112
wenzelm@22116
   113
(* oracles *)
wenzelm@22116
   114
wenzelm@59029
   115
fun oracle (name, range) source =
wenzelm@69268
   116
  ML_Context.expression (Input.pos_of source)
wenzelm@69268
   117
    (ML_Lex.read "val " @
wenzelm@69268
   118
     ML_Lex.read_set_range range name @
wenzelm@69268
   119
     ML_Lex.read
wenzelm@69268
   120
      (" = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (" ^
wenzelm@69268
   121
        ML_Syntax.make_binding (name, #1 range) ^ ", ") @
wenzelm@69268
   122
     ML_Lex.read_source source @
wenzelm@69268
   123
     ML_Lex.read "))))")
wenzelm@69268
   124
  |> Context.theory_map;
wenzelm@22116
   125
wenzelm@22116
   126
wenzelm@22087
   127
(* declarations *)
wenzelm@22087
   128
wenzelm@55828
   129
fun declaration {syntax, pervasive} source =
wenzelm@69221
   130
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
   131
    (ML_Lex.read
wenzelm@69221
   132
      ("Theory.local_setup (Local_Theory.declaration {syntax = " ^
wenzelm@69221
   133
        Bool.toString syntax ^ ", pervasive = " ^ Bool.toString pervasive ^ "} (") @
wenzelm@69221
   134
     ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@26385
   135
  |> Context.proof_map;
wenzelm@22087
   136
wenzelm@22087
   137
wenzelm@22202
   138
(* simprocs *)
wenzelm@22202
   139
wenzelm@62913
   140
fun simproc_setup name lhss source =
wenzelm@69221
   141
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
   142
    (ML_Lex.read
wenzelm@69221
   143
      ("Theory.local_setup (Simplifier.define_simproc_cmd (" ^
wenzelm@69221
   144
        ML_Syntax.make_binding name ^ ") {lhss = " ^ ML_Syntax.print_strings lhss ^
wenzelm@69221
   145
        ", proc = (") @ ML_Lex.read_source source @ ML_Lex.read ")})")
wenzelm@22202
   146
  |> Context.proof_map;
wenzelm@22202
   147
wenzelm@22202
   148
wenzelm@21350
   149
(* local endings *)
wenzelm@21350
   150
wenzelm@29383
   151
fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
wenzelm@60403
   152
val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
wenzelm@29383
   153
val local_default_proof = Toplevel.proof Proof.local_default_proof;
wenzelm@29383
   154
val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
wenzelm@29383
   155
val local_done_proof = Toplevel.proof Proof.local_done_proof;
wenzelm@29383
   156
val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
wenzelm@21350
   157
wenzelm@21350
   158
wenzelm@21350
   159
(* global endings *)
wenzelm@21350
   160
wenzelm@21350
   161
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@60403
   162
val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
wenzelm@21350
   163
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   164
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   165
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   166
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   167
wenzelm@21350
   168
wenzelm@21350
   169
(* common endings *)
wenzelm@21350
   170
wenzelm@60695
   171
fun qed m = local_qed m o global_qed m;
wenzelm@21350
   172
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   173
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   174
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   175
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   176
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   177
wenzelm@21350
   178
wenzelm@26489
   179
(* diagnostic ML evaluation *)
wenzelm@5831
   180
wenzelm@37305
   181
structure Diag_State = Proof_Data
wenzelm@37305
   182
(
wenzelm@37305
   183
  type T = Toplevel.state;
wenzelm@37305
   184
  fun init _ = Toplevel.toplevel;
wenzelm@37305
   185
);
wenzelm@37305
   186
wenzelm@55828
   187
fun ml_diag verbose source = Toplevel.keep (fn state =>
wenzelm@56304
   188
  let
wenzelm@56304
   189
    val opt_ctxt =
wenzelm@56304
   190
      try Toplevel.generic_theory_of state
wenzelm@56304
   191
      |> Option.map (Context.proof_of #> Diag_State.put state);
wenzelm@56304
   192
    val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
wenzelm@56304
   193
  in ML_Context.eval_source_in opt_ctxt flags source end);
wenzelm@37305
   194
wenzelm@48776
   195
val diag_state = Diag_State.get;
wenzelm@60096
   196
val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
wenzelm@37305
   197
wenzelm@53171
   198
val _ = Theory.setup
wenzelm@67147
   199
  (ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>state\<close>)
wenzelm@53171
   200
    (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
wenzelm@67147
   201
   ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>goal\<close>)
wenzelm@53171
   202
    (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
wenzelm@5831
   203
wenzelm@5831
   204
wenzelm@56868
   205
(* theorems of theory or proof context *)
wenzelm@17066
   206
wenzelm@56868
   207
fun pretty_theorems verbose st =
wenzelm@56868
   208
  if Toplevel.is_proof st then
wenzelm@59917
   209
    Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
wenzelm@56868
   210
  else
wenzelm@56868
   211
    let
wenzelm@61266
   212
      val ctxt = Toplevel.context_of st;
wenzelm@56868
   213
      val prev_thys =
wenzelm@67391
   214
        (case Toplevel.previous_theory_of st of
wenzelm@67391
   215
          SOME thy => [thy]
wenzelm@61266
   216
        | NONE => Theory.parents_of (Proof_Context.theory_of ctxt));
wenzelm@61266
   217
    in Proof_Display.pretty_theorems_diff verbose prev_thys ctxt end;
wenzelm@9513
   218
wenzelm@12060
   219
wenzelm@19268
   220
(* print theorems, terms, types etc. *)
wenzelm@19268
   221
wenzelm@19385
   222
local
wenzelm@19385
   223
wenzelm@38331
   224
fun string_of_stmts ctxt args =
wenzelm@38331
   225
  Attrib.eval_thms ctxt args
wenzelm@38331
   226
  |> map (Element.pretty_statement ctxt Thm.theoremK)
wenzelm@19385
   227
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   228
wenzelm@38331
   229
fun string_of_thms ctxt args =
wenzelm@51583
   230
  Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
wenzelm@5895
   231
wenzelm@19385
   232
fun string_of_prfs full state arg =
wenzelm@32859
   233
  Pretty.string_of
wenzelm@32859
   234
    (case arg of
skalberg@15531
   235
      NONE =>
berghofe@12125
   236
        let
wenzelm@38331
   237
          val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
wenzelm@28814
   238
          val prf = Thm.proof_of thm;
wenzelm@17066
   239
          val prop = Thm.full_prop_of thm;
wenzelm@32859
   240
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
berghofe@12125
   241
        in
wenzelm@33388
   242
          Proof_Syntax.pretty_proof ctxt
wenzelm@62922
   243
            (if full then Reconstruct.reconstruct_proof ctxt prop prf' else prf')
berghofe@12125
   244
        end
wenzelm@38331
   245
    | SOME srcs =>
wenzelm@63624
   246
        let
wenzelm@63624
   247
          val ctxt = Toplevel.context_of state;
wenzelm@64986
   248
          val pretty_proof = Proof_Syntax.pretty_clean_proof_of ctxt full;
wenzelm@63624
   249
        in Pretty.chunks (map pretty_proof (Attrib.eval_thms ctxt srcs)) end);
berghofe@11524
   250
wenzelm@38331
   251
fun string_of_prop ctxt s =
wenzelm@5831
   252
  let
wenzelm@24508
   253
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   254
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   255
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   256
wenzelm@38331
   257
fun string_of_term ctxt s =
wenzelm@5831
   258
  let
wenzelm@24508
   259
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   260
    val T = Term.type_of t;
wenzelm@26704
   261
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   262
  in
wenzelm@19385
   263
    Pretty.string_of
wenzelm@26704
   264
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   265
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   266
  end;
wenzelm@5831
   267
wenzelm@48792
   268
fun string_of_type ctxt (s, NONE) =
wenzelm@48792
   269
      let val T = Syntax.read_typ ctxt s
wenzelm@48792
   270
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
wenzelm@48792
   271
  | string_of_type ctxt (s1, SOME s2) =
wenzelm@48792
   272
      let
wenzelm@48792
   273
        val ctxt' = Config.put show_sorts true ctxt;
wenzelm@48792
   274
        val raw_T = Syntax.parse_typ ctxt' s1;
wenzelm@48792
   275
        val S = Syntax.read_sort ctxt' s2;
wenzelm@48792
   276
        val T =
wenzelm@48792
   277
          Syntax.check_term ctxt'
wenzelm@48792
   278
            (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
wenzelm@48792
   279
          |> Logic.dest_type;
wenzelm@48792
   280
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
wenzelm@9128
   281
wenzelm@23935
   282
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@38331
   283
  Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
wenzelm@19385
   284
wenzelm@19385
   285
in
wenzelm@9128
   286
wenzelm@38331
   287
val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
wenzelm@38331
   288
val print_thms = print_item (string_of_thms o Toplevel.context_of);
wenzelm@19385
   289
val print_prfs = print_item o string_of_prfs;
wenzelm@38331
   290
val print_prop = print_item (string_of_prop o Toplevel.context_of);
wenzelm@38331
   291
val print_term = print_item (string_of_term o Toplevel.context_of);
wenzelm@38331
   292
val print_type = print_item (string_of_type o Toplevel.context_of);
wenzelm@5831
   293
wenzelm@19385
   294
end;
wenzelm@19385
   295
wenzelm@5831
   296
end;