src/Pure/Isar/isar_cmd.ML
author wenzelm
Wed Oct 31 15:53:32 2018 +0100 (7 months ago ago)
changeset 69221 1a52baa70aed
parent 69188 d8849cfad60f
child 69268 c546e37f6cb9
permissions -rw-r--r--
clarified ML_Context.expression: it is a closed expression, not a let-declaration -- thus source positions are more accurate (amending d8849cfad60f, 162a4c2e97bc);
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@27871
   116
  let
wenzelm@59064
   117
    val body_range = Input.range_of source;
wenzelm@68823
   118
    val body = ML_Lex.read_source source;
wenzelm@59029
   119
wenzelm@37198
   120
    val ants =
wenzelm@59067
   121
      ML_Lex.read
wenzelm@37198
   122
       ("local\n\
wenzelm@59029
   123
        \  val binding = " ^ ML_Syntax.make_binding (name, #1 range) ^ ";\n\
wenzelm@59067
   124
        \  val") @ ML_Lex.read_set_range body_range "oracle" @ ML_Lex.read "=" @ body @
wenzelm@59067
   125
        ML_Lex.read (";\nin\n\
wenzelm@59067
   126
        \  val") @ ML_Lex.read_set_range range name @ ML_Lex.read "=\
wenzelm@59029
   127
        \ snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, oracle))));\n\
wenzelm@59029
   128
        \end;\n";
wenzelm@56304
   129
  in
wenzelm@56304
   130
    Context.theory_map
wenzelm@59029
   131
      (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#1 body_range) ants))
wenzelm@56304
   132
  end;
wenzelm@22116
   133
wenzelm@22116
   134
wenzelm@22087
   135
(* declarations *)
wenzelm@22087
   136
wenzelm@55828
   137
fun declaration {syntax, pervasive} source =
wenzelm@69221
   138
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
   139
    (ML_Lex.read
wenzelm@69221
   140
      ("Theory.local_setup (Local_Theory.declaration {syntax = " ^
wenzelm@69221
   141
        Bool.toString syntax ^ ", pervasive = " ^ Bool.toString pervasive ^ "} (") @
wenzelm@69221
   142
     ML_Lex.read_source source @ ML_Lex.read "))")
wenzelm@26385
   143
  |> Context.proof_map;
wenzelm@22087
   144
wenzelm@22087
   145
wenzelm@22202
   146
(* simprocs *)
wenzelm@22202
   147
wenzelm@62913
   148
fun simproc_setup name lhss source =
wenzelm@69221
   149
  ML_Context.expression (Input.pos_of source)
wenzelm@69221
   150
    (ML_Lex.read
wenzelm@69221
   151
      ("Theory.local_setup (Simplifier.define_simproc_cmd (" ^
wenzelm@69221
   152
        ML_Syntax.make_binding name ^ ") {lhss = " ^ ML_Syntax.print_strings lhss ^
wenzelm@69221
   153
        ", proc = (") @ ML_Lex.read_source source @ ML_Lex.read ")})")
wenzelm@22202
   154
  |> Context.proof_map;
wenzelm@22202
   155
wenzelm@22202
   156
wenzelm@21350
   157
(* local endings *)
wenzelm@21350
   158
wenzelm@29383
   159
fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
wenzelm@60403
   160
val local_terminal_proof = Toplevel.proof o Proof.local_future_terminal_proof;
wenzelm@29383
   161
val local_default_proof = Toplevel.proof Proof.local_default_proof;
wenzelm@29383
   162
val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
wenzelm@29383
   163
val local_done_proof = Toplevel.proof Proof.local_done_proof;
wenzelm@29383
   164
val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
wenzelm@21350
   165
wenzelm@21350
   166
wenzelm@21350
   167
(* global endings *)
wenzelm@21350
   168
wenzelm@21350
   169
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@60403
   170
val global_terminal_proof = Toplevel.end_proof o K o Proof.global_future_terminal_proof;
wenzelm@21350
   171
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   172
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   173
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   174
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   175
wenzelm@21350
   176
wenzelm@21350
   177
(* common endings *)
wenzelm@21350
   178
wenzelm@60695
   179
fun qed m = local_qed m o global_qed m;
wenzelm@21350
   180
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   181
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   182
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   183
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   184
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   185
wenzelm@21350
   186
wenzelm@26489
   187
(* diagnostic ML evaluation *)
wenzelm@5831
   188
wenzelm@37305
   189
structure Diag_State = Proof_Data
wenzelm@37305
   190
(
wenzelm@37305
   191
  type T = Toplevel.state;
wenzelm@37305
   192
  fun init _ = Toplevel.toplevel;
wenzelm@37305
   193
);
wenzelm@37305
   194
wenzelm@55828
   195
fun ml_diag verbose source = Toplevel.keep (fn state =>
wenzelm@56304
   196
  let
wenzelm@56304
   197
    val opt_ctxt =
wenzelm@56304
   198
      try Toplevel.generic_theory_of state
wenzelm@56304
   199
      |> Option.map (Context.proof_of #> Diag_State.put state);
wenzelm@56304
   200
    val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
wenzelm@56304
   201
  in ML_Context.eval_source_in opt_ctxt flags source end);
wenzelm@37305
   202
wenzelm@48776
   203
val diag_state = Diag_State.get;
wenzelm@60096
   204
val diag_goal = Proof.goal o Toplevel.proof_of o diag_state;
wenzelm@37305
   205
wenzelm@53171
   206
val _ = Theory.setup
wenzelm@67147
   207
  (ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>state\<close>)
wenzelm@53171
   208
    (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
wenzelm@67147
   209
   ML_Antiquotation.value (Binding.qualify true "Isar" \<^binding>\<open>goal\<close>)
wenzelm@53171
   210
    (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
wenzelm@5831
   211
wenzelm@5831
   212
wenzelm@56868
   213
(* theorems of theory or proof context *)
wenzelm@17066
   214
wenzelm@56868
   215
fun pretty_theorems verbose st =
wenzelm@56868
   216
  if Toplevel.is_proof st then
wenzelm@59917
   217
    Proof_Context.pretty_local_facts verbose (Toplevel.context_of st)
wenzelm@56868
   218
  else
wenzelm@56868
   219
    let
wenzelm@61266
   220
      val ctxt = Toplevel.context_of st;
wenzelm@56868
   221
      val prev_thys =
wenzelm@67391
   222
        (case Toplevel.previous_theory_of st of
wenzelm@67391
   223
          SOME thy => [thy]
wenzelm@61266
   224
        | NONE => Theory.parents_of (Proof_Context.theory_of ctxt));
wenzelm@61266
   225
    in Proof_Display.pretty_theorems_diff verbose prev_thys ctxt end;
wenzelm@9513
   226
wenzelm@12060
   227
wenzelm@19268
   228
(* print theorems, terms, types etc. *)
wenzelm@19268
   229
wenzelm@19385
   230
local
wenzelm@19385
   231
wenzelm@38331
   232
fun string_of_stmts ctxt args =
wenzelm@38331
   233
  Attrib.eval_thms ctxt args
wenzelm@38331
   234
  |> map (Element.pretty_statement ctxt Thm.theoremK)
wenzelm@19385
   235
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   236
wenzelm@38331
   237
fun string_of_thms ctxt args =
wenzelm@51583
   238
  Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
wenzelm@5895
   239
wenzelm@19385
   240
fun string_of_prfs full state arg =
wenzelm@32859
   241
  Pretty.string_of
wenzelm@32859
   242
    (case arg of
skalberg@15531
   243
      NONE =>
berghofe@12125
   244
        let
wenzelm@38331
   245
          val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
wenzelm@28814
   246
          val prf = Thm.proof_of thm;
wenzelm@17066
   247
          val prop = Thm.full_prop_of thm;
wenzelm@32859
   248
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
berghofe@12125
   249
        in
wenzelm@33388
   250
          Proof_Syntax.pretty_proof ctxt
wenzelm@62922
   251
            (if full then Reconstruct.reconstruct_proof ctxt prop prf' else prf')
berghofe@12125
   252
        end
wenzelm@38331
   253
    | SOME srcs =>
wenzelm@63624
   254
        let
wenzelm@63624
   255
          val ctxt = Toplevel.context_of state;
wenzelm@64986
   256
          val pretty_proof = Proof_Syntax.pretty_clean_proof_of ctxt full;
wenzelm@63624
   257
        in Pretty.chunks (map pretty_proof (Attrib.eval_thms ctxt srcs)) end);
berghofe@11524
   258
wenzelm@38331
   259
fun string_of_prop ctxt s =
wenzelm@5831
   260
  let
wenzelm@24508
   261
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   262
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   263
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   264
wenzelm@38331
   265
fun string_of_term ctxt s =
wenzelm@5831
   266
  let
wenzelm@24508
   267
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   268
    val T = Term.type_of t;
wenzelm@26704
   269
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   270
  in
wenzelm@19385
   271
    Pretty.string_of
wenzelm@26704
   272
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   273
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   274
  end;
wenzelm@5831
   275
wenzelm@48792
   276
fun string_of_type ctxt (s, NONE) =
wenzelm@48792
   277
      let val T = Syntax.read_typ ctxt s
wenzelm@48792
   278
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
wenzelm@48792
   279
  | string_of_type ctxt (s1, SOME s2) =
wenzelm@48792
   280
      let
wenzelm@48792
   281
        val ctxt' = Config.put show_sorts true ctxt;
wenzelm@48792
   282
        val raw_T = Syntax.parse_typ ctxt' s1;
wenzelm@48792
   283
        val S = Syntax.read_sort ctxt' s2;
wenzelm@48792
   284
        val T =
wenzelm@48792
   285
          Syntax.check_term ctxt'
wenzelm@48792
   286
            (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
wenzelm@48792
   287
          |> Logic.dest_type;
wenzelm@48792
   288
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
wenzelm@9128
   289
wenzelm@23935
   290
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@38331
   291
  Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
wenzelm@19385
   292
wenzelm@19385
   293
in
wenzelm@9128
   294
wenzelm@38331
   295
val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
wenzelm@38331
   296
val print_thms = print_item (string_of_thms o Toplevel.context_of);
wenzelm@19385
   297
val print_prfs = print_item o string_of_prfs;
wenzelm@38331
   298
val print_prop = print_item (string_of_prop o Toplevel.context_of);
wenzelm@38331
   299
val print_term = print_item (string_of_term o Toplevel.context_of);
wenzelm@38331
   300
val print_type = print_item (string_of_type o Toplevel.context_of);
wenzelm@5831
   301
wenzelm@19385
   302
end;
wenzelm@19385
   303
wenzelm@5831
   304
end;