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