src/Pure/Isar/isar_cmd.ML
author wenzelm
Thu Aug 14 20:13:41 2008 +0200 (2008-08-14 ago)
changeset 27876 4b79407825da
parent 27871 4ef76f8788ad
child 28083 103d9282a946
permissions -rw-r--r--
report doc_source;
wenzelm@5831
     1
(*  Title:      Pure/Isar/isar_cmd.ML
wenzelm@5831
     2
    ID:         $Id$
wenzelm@5831
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5831
     4
wenzelm@21350
     5
Derived Isar commands.
wenzelm@5831
     6
*)
wenzelm@5831
     7
wenzelm@5831
     8
signature ISAR_CMD =
wenzelm@5831
     9
sig
wenzelm@26435
    10
  val generic_setup: string * Position.T -> theory -> theory
wenzelm@26385
    11
  val parse_ast_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    12
  val parse_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    13
  val print_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    14
  val typed_print_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    15
  val print_ast_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@27871
    16
  val oracle: bstring -> SymbolPos.text * Position.T -> SymbolPos.text * Position.T ->
wenzelm@27871
    17
    theory -> theory
wenzelm@21350
    18
  val add_axioms: ((bstring * string) * Attrib.src list) list -> theory -> theory
wenzelm@21350
    19
  val add_defs: (bool * bool) * ((bstring * string) * Attrib.src list) list -> theory -> theory
wenzelm@26385
    20
  val declaration: string * Position.T -> local_theory -> local_theory
wenzelm@26385
    21
  val simproc_setup: string -> string list -> string * Position.T -> string list ->
wenzelm@26385
    22
    local_theory -> local_theory
wenzelm@26671
    23
  val hide_names: bool -> string * xstring list -> theory -> theory
wenzelm@21350
    24
  val have: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    25
    bool -> Proof.state -> Proof.state
wenzelm@21350
    26
  val hence: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    27
    bool -> Proof.state -> Proof.state
wenzelm@21350
    28
  val show: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    29
    bool -> Proof.state -> Proof.state
wenzelm@21350
    30
  val thus: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    31
    bool -> Proof.state -> Proof.state
wenzelm@21350
    32
  val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
wenzelm@21350
    33
  val terminal_proof: Method.text * Method.text option ->
wenzelm@21350
    34
    Toplevel.transition -> Toplevel.transition
wenzelm@21350
    35
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    36
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    37
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    38
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@27574
    39
  val init_theory: string * string list * (string * bool) list ->
wenzelm@27574
    40
    Toplevel.transition -> Toplevel.transition
wenzelm@7462
    41
  val welcome: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    42
  val exit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    43
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@9731
    44
  val pr: string list * (int option * int option) -> Toplevel.transition -> Toplevel.transition
wenzelm@8453
    45
  val disable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@8453
    46
  val enable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@26489
    47
  val ml_diag: bool -> string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@14950
    48
  val cd: Path.T -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    49
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@14950
    50
  val display_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
wenzelm@14950
    51
  val print_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
wenzelm@7124
    52
  val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
wenzelm@7308
    53
  val print_context: Toplevel.transition -> Toplevel.transition
wenzelm@20621
    54
  val print_theory: bool -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    55
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@21725
    56
  val print_abbrevs: Toplevel.transition -> Toplevel.transition
wenzelm@21003
    57
  val print_facts: Toplevel.transition -> Toplevel.transition
wenzelm@24115
    58
  val print_configs: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    59
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@12060
    60
  val print_locales: Toplevel.transition -> Toplevel.transition
wenzelm@18135
    61
  val print_locale: bool * (Locale.expr * Element.context list)
wenzelm@12758
    62
    -> Toplevel.transition -> Toplevel.transition
ballarin@17139
    63
  val print_registrations: bool -> string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    64
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@16026
    65
  val print_simpset: Toplevel.transition -> Toplevel.transition
wenzelm@12382
    66
  val print_rules: Toplevel.transition -> Toplevel.transition
wenzelm@9219
    67
  val print_trans_rules: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    68
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@9219
    69
  val print_antiquotations: Toplevel.transition -> Toplevel.transition
wenzelm@20574
    70
  val class_deps: Toplevel.transition -> Toplevel.transition
haftmann@22485
    71
  val thy_deps: Toplevel.transition -> Toplevel.transition
wenzelm@26336
    72
  val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
kleing@22340
    73
  val find_theorems: (int option * bool) * (bool * string FindTheorems.criterion) list
wenzelm@13284
    74
    -> Toplevel.transition -> Toplevel.transition
berghofe@26184
    75
  val unused_thms: (string list * string list option) option ->
berghofe@26184
    76
    Toplevel.transition -> Toplevel.transition
wenzelm@5831
    77
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@8369
    78
  val print_cases: Toplevel.transition -> Toplevel.transition
wenzelm@26336
    79
  val print_stmts: string list * (Facts.ref * Attrib.src list) list
wenzelm@19268
    80
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    81
  val print_thms: string list * (Facts.ref * Attrib.src list) list
wenzelm@10581
    82
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    83
  val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
berghofe@11524
    84
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    85
  val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    86
  val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    87
  val print_type: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@27871
    88
  val header_markup: SymbolPos.text * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@27871
    89
  val local_theory_markup: xstring option * (SymbolPos.text * Position.T) ->
wenzelm@17262
    90
    Toplevel.transition -> Toplevel.transition
wenzelm@27871
    91
  val proof_markup: SymbolPos.text * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    92
end;
wenzelm@5831
    93
wenzelm@5831
    94
structure IsarCmd: ISAR_CMD =
wenzelm@5831
    95
struct
wenzelm@5831
    96
wenzelm@5831
    97
wenzelm@22116
    98
(** theory declarations **)
wenzelm@22116
    99
wenzelm@22116
   100
(* generic_setup *)
wenzelm@22116
   101
wenzelm@26435
   102
fun generic_setup (txt, pos) =
wenzelm@26455
   103
  ML_Context.expression pos "val setup: theory -> theory" "Context.map_theory setup" txt
wenzelm@26435
   104
  |> Context.theory_map;
wenzelm@22116
   105
wenzelm@22116
   106
wenzelm@22116
   107
(* translation functions *)
wenzelm@22116
   108
wenzelm@22116
   109
local
wenzelm@22116
   110
wenzelm@22116
   111
fun advancedT false = ""
wenzelm@22116
   112
  | advancedT true = "Proof.context -> ";
wenzelm@22116
   113
wenzelm@22116
   114
fun advancedN false = ""
wenzelm@22116
   115
  | advancedN true = "advanced_";
wenzelm@22116
   116
wenzelm@22116
   117
in
wenzelm@22116
   118
wenzelm@26385
   119
fun parse_ast_translation (a, (txt, pos)) =
wenzelm@26455
   120
  txt |> ML_Context.expression pos
wenzelm@26455
   121
    ("val parse_ast_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   122
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@22116
   123
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], []))")
wenzelm@22116
   124
  |> Context.theory_map;
wenzelm@22116
   125
wenzelm@26385
   126
fun parse_translation (a, (txt, pos)) =
wenzelm@26455
   127
  txt |> ML_Context.expression pos
wenzelm@26455
   128
    ("val parse_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   129
      "term list -> term)) list")
wenzelm@22116
   130
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], []))")
wenzelm@22116
   131
  |> Context.theory_map;
wenzelm@22116
   132
wenzelm@26385
   133
fun print_translation (a, (txt, pos)) =
wenzelm@26455
   134
  txt |> ML_Context.expression pos
wenzelm@26455
   135
    ("val print_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   136
      "term list -> term)) list")
wenzelm@22116
   137
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, []))")
wenzelm@22116
   138
  |> Context.theory_map;
wenzelm@22116
   139
wenzelm@26385
   140
fun print_ast_translation (a, (txt, pos)) =
wenzelm@26455
   141
  txt |> ML_Context.expression pos
wenzelm@26455
   142
    ("val print_ast_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   143
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@22116
   144
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation))")
wenzelm@22116
   145
  |> Context.theory_map;
wenzelm@22116
   146
wenzelm@26385
   147
fun typed_print_translation (a, (txt, pos)) =
wenzelm@26455
   148
  txt |> ML_Context.expression pos
wenzelm@26455
   149
    ("val typed_print_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   150
      "bool -> typ -> term list -> term)) list")
wenzelm@22116
   151
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation)")
wenzelm@22116
   152
  |> Context.theory_map;
wenzelm@22116
   153
wenzelm@22116
   154
end;
wenzelm@22116
   155
wenzelm@22116
   156
wenzelm@22116
   157
(* oracles *)
wenzelm@22116
   158
wenzelm@27871
   159
fun oracle name typ_pos (oracle_txt, pos) =
wenzelm@27871
   160
  let
wenzelm@27871
   161
    val typ = SymbolPos.content (SymbolPos.explode typ_pos);
wenzelm@27871
   162
    val oracle = SymbolPos.content (SymbolPos.explode (oracle_txt, pos));
wenzelm@27871
   163
    val txt =
wenzelm@27871
   164
      "local\
wenzelm@27871
   165
      \  type T = " ^ typ ^ ";\
wenzelm@27871
   166
      \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
wenzelm@27871
   167
      \  val name = " ^ quote name ^ ";\n\
wenzelm@27871
   168
      \  exception Arg of T;\n\
wenzelm@27871
   169
      \  val _ = Context.>> (Context.map_theory\n\
wenzelm@27871
   170
      \    (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x)));\n\
wenzelm@27871
   171
      \  val thy = ML_Context.the_global_context ();\n\
wenzelm@27871
   172
      \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
wenzelm@27871
   173
      \in\n\
wenzelm@27871
   174
      \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
wenzelm@27871
   175
      \end;\n";
wenzelm@26455
   176
  in Context.theory_map (ML_Context.exec (fn () => ML_Context.eval false pos txt)) end;
wenzelm@22116
   177
wenzelm@22116
   178
wenzelm@21350
   179
(* axioms *)
wenzelm@21350
   180
wenzelm@21350
   181
fun add_axms f args thy =
wenzelm@21350
   182
  f (map (fn (x, srcs) => (x, map (Attrib.attribute thy) srcs)) args) thy;
wenzelm@21350
   183
haftmann@27691
   184
val add_axioms = add_axms (snd oo PureThy.add_axioms_cmd);
wenzelm@21350
   185
wenzelm@21350
   186
fun add_defs ((unchecked, overloaded), args) =
wenzelm@21350
   187
  add_axms
haftmann@27691
   188
    (snd oo (if unchecked then PureThy.add_defs_unchecked_cmd else PureThy.add_defs_cmd) overloaded) args;
wenzelm@21350
   189
wenzelm@21350
   190
wenzelm@22087
   191
(* declarations *)
wenzelm@22087
   192
wenzelm@26385
   193
fun declaration (txt, pos) =
wenzelm@26455
   194
  txt |> ML_Context.expression pos
wenzelm@26455
   195
    "val declaration: Morphism.declaration"
wenzelm@24020
   196
    "Context.map_proof (LocalTheory.declaration declaration)"
wenzelm@26385
   197
  |> Context.proof_map;
wenzelm@22087
   198
wenzelm@22087
   199
wenzelm@22202
   200
(* simprocs *)
wenzelm@22202
   201
wenzelm@26385
   202
fun simproc_setup name lhss (proc, pos) identifier =
wenzelm@26455
   203
  ML_Context.expression pos
wenzelm@22239
   204
    "val proc: Morphism.morphism -> Simplifier.simpset -> cterm -> thm option"
wenzelm@22239
   205
  ("Context.map_proof (Simplifier.def_simproc {name = " ^ ML_Syntax.print_string name ^ ", \
wenzelm@22239
   206
    \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
wenzelm@22239
   207
    \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})") proc
wenzelm@22202
   208
  |> Context.proof_map;
wenzelm@22202
   209
wenzelm@22202
   210
wenzelm@26671
   211
(* hide names *)
wenzelm@26671
   212
wenzelm@26671
   213
val hide_kinds =
wenzelm@26671
   214
 [("class", (Sign.intern_class, can o Sign.certify_class, Sign.hide_class)),
wenzelm@26671
   215
  ("type", (Sign.intern_type, Sign.declared_tyname, Sign.hide_type)),
wenzelm@26671
   216
  ("const", (Sign.intern_const, Sign.declared_const, Sign.hide_const)),
wenzelm@26694
   217
  ("fact", (PureThy.intern_fact, PureThy.defined_fact, PureThy.hide_fact))];
wenzelm@26671
   218
wenzelm@26671
   219
fun hide_names b (kind, xnames) thy =
wenzelm@26671
   220
  (case AList.lookup (op =) hide_kinds kind of
wenzelm@26671
   221
    SOME (intern, check, hide) =>
wenzelm@26671
   222
      let
wenzelm@26671
   223
        val names = map (intern thy) xnames;
wenzelm@26671
   224
        val bads = filter_out (check thy) names;
wenzelm@26671
   225
      in
wenzelm@26671
   226
        if null bads then fold (hide b) names thy
wenzelm@26671
   227
        else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
wenzelm@26671
   228
      end
wenzelm@26671
   229
  | NONE => error ("Bad name space specification: " ^ quote kind));
wenzelm@26671
   230
wenzelm@26671
   231
wenzelm@21350
   232
(* goals *)
wenzelm@21350
   233
wenzelm@21350
   234
fun goal opt_chain goal stmt int =
wenzelm@21350
   235
  opt_chain #> goal NONE (K Seq.single) stmt int;
wenzelm@21350
   236
wenzelm@21350
   237
val have = goal I Proof.have;
wenzelm@21350
   238
val hence = goal Proof.chain Proof.have;
wenzelm@21350
   239
val show = goal I Proof.show;
wenzelm@21350
   240
val thus = goal Proof.chain Proof.show;
wenzelm@21350
   241
wenzelm@21350
   242
wenzelm@21350
   243
(* local endings *)
wenzelm@21350
   244
wenzelm@21350
   245
fun local_qed m = Toplevel.proofs (Proof.local_qed (m, true));
wenzelm@21350
   246
val local_terminal_proof = Toplevel.proofs o Proof.local_terminal_proof;
wenzelm@21350
   247
val local_default_proof = Toplevel.proofs Proof.local_default_proof;
wenzelm@21350
   248
val local_immediate_proof = Toplevel.proofs Proof.local_immediate_proof;
wenzelm@21350
   249
val local_done_proof = Toplevel.proofs Proof.local_done_proof;
wenzelm@21350
   250
val local_skip_proof = Toplevel.proofs' Proof.local_skip_proof;
wenzelm@21350
   251
wenzelm@27562
   252
val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
wenzelm@21350
   253
wenzelm@21350
   254
wenzelm@21350
   255
(* global endings *)
wenzelm@21350
   256
wenzelm@21350
   257
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@21350
   258
val global_terminal_proof = Toplevel.end_proof o K o Proof.global_terminal_proof;
wenzelm@21350
   259
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   260
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   261
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   262
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   263
wenzelm@21350
   264
val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1);
wenzelm@21350
   265
wenzelm@21350
   266
wenzelm@21350
   267
(* common endings *)
wenzelm@21350
   268
wenzelm@21350
   269
fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
wenzelm@21350
   270
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   271
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   272
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   273
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   274
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   275
wenzelm@21350
   276
wenzelm@21350
   277
(* init and exit *)
wenzelm@21350
   278
wenzelm@27574
   279
fun init_theory (name, imports, uses) =
wenzelm@27574
   280
  Toplevel.init_theory name (ThyInfo.begin_theory name imports (map (apfst Path.explode) uses))
wenzelm@27574
   281
    (fn thy =>
wenzelm@27574
   282
      if ThyInfo.check_known_thy (Context.theory_name thy)
wenzelm@27574
   283
      then ThyInfo.end_theory thy else ());
wenzelm@21350
   284
wenzelm@7462
   285
val welcome = Toplevel.imperative (writeln o Session.welcome);
wenzelm@5831
   286
wenzelm@5831
   287
val exit = Toplevel.keep (fn state =>
wenzelm@26599
   288
 (Context.set_thread_data (try Toplevel.generic_theory_of state);
wenzelm@24071
   289
  raise Toplevel.TERMINATE));
wenzelm@5831
   290
wenzelm@5831
   291
val quit = Toplevel.imperative quit;
wenzelm@5831
   292
wenzelm@7101
   293
wenzelm@8453
   294
(* print state *)
wenzelm@8453
   295
skalberg@15531
   296
fun set_limit _ NONE = ()
skalberg@15531
   297
  | set_limit r (SOME n) = r := n;
wenzelm@9731
   298
wenzelm@19385
   299
fun pr (modes, (lim1, lim2)) = Toplevel.keep (fn state =>
wenzelm@9731
   300
  (set_limit goals_limit lim1; set_limit ProofContext.prems_limit lim2; Toplevel.quiet := false;
wenzelm@23935
   301
    PrintMode.with_modes modes (Toplevel.print_state true) state));
wenzelm@8453
   302
wenzelm@8453
   303
val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
wenzelm@8453
   304
val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
wenzelm@8453
   305
wenzelm@8453
   306
wenzelm@26489
   307
(* diagnostic ML evaluation *)
wenzelm@5831
   308
wenzelm@26489
   309
fun ml_diag verbose (txt, pos) = Toplevel.keep (fn state =>
wenzelm@26489
   310
  (ML_Context.eval_in (try Toplevel.generic_theory_of state) verbose pos txt));
wenzelm@5831
   311
wenzelm@5831
   312
wenzelm@5831
   313
(* current working directory *)
wenzelm@5831
   314
wenzelm@14950
   315
fun cd path = Toplevel.imperative (fn () => (File.cd path));
wenzelm@21858
   316
val pwd = Toplevel.imperative (fn () => writeln (Path.implode (File.pwd ())));
wenzelm@5831
   317
wenzelm@5831
   318
wenzelm@14934
   319
(* present draft files *)
wenzelm@14934
   320
wenzelm@14934
   321
fun display_drafts files = Toplevel.imperative (fn () =>
wenzelm@16258
   322
  let val outfile = File.shell_path (Present.drafts (getenv "ISABELLE_DOC_FORMAT") files)
wenzelm@16258
   323
  in File.isatool ("display -c " ^ outfile ^ " &"); () end);
wenzelm@14934
   324
wenzelm@14934
   325
fun print_drafts files = Toplevel.imperative (fn () =>
wenzelm@16258
   326
  let val outfile = File.shell_path (Present.drafts "ps" files)
wenzelm@16258
   327
  in File.isatool ("print -c " ^ outfile); () end);
wenzelm@14934
   328
wenzelm@14934
   329
wenzelm@7124
   330
(* pretty_setmargin *)
wenzelm@7124
   331
wenzelm@7124
   332
fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
wenzelm@5831
   333
wenzelm@5831
   334
wenzelm@9513
   335
(* print parts of theory and proof context *)
wenzelm@5831
   336
wenzelm@7308
   337
val print_context = Toplevel.keep Toplevel.print_state_context;
wenzelm@9513
   338
wenzelm@20621
   339
fun print_theory verbose = Toplevel.unknown_theory o
wenzelm@22872
   340
  Toplevel.keep (Pretty.writeln o ProofDisplay.pretty_full_theory verbose o Toplevel.theory_of);
wenzelm@9513
   341
wenzelm@21663
   342
val print_syntax = Toplevel.unknown_context o
wenzelm@21663
   343
  Toplevel.keep (ProofContext.print_syntax o Toplevel.context_of);
wenzelm@9513
   344
wenzelm@21725
   345
val print_abbrevs = Toplevel.unknown_context o
wenzelm@21725
   346
  Toplevel.keep (ProofContext.print_abbrevs o Toplevel.context_of);
wenzelm@21725
   347
wenzelm@21003
   348
val print_facts = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21003
   349
  ProofContext.setmp_verbose
wenzelm@21506
   350
    ProofContext.print_lthms (Toplevel.context_of state));
wenzelm@21003
   351
wenzelm@24115
   352
val print_configs = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@24115
   353
  Attrib.print_configs (Toplevel.context_of state));
wenzelm@23989
   354
wenzelm@17066
   355
val print_theorems_proof = Toplevel.keep (fn state =>
wenzelm@17066
   356
  ProofContext.setmp_verbose
wenzelm@17066
   357
    ProofContext.print_lthms (Proof.context_of (Toplevel.proof_of state)));
wenzelm@17066
   358
wenzelm@18588
   359
val print_theorems_theory = Toplevel.keep (fn state =>
wenzelm@18588
   360
  Toplevel.theory_of state |>
wenzelm@27574
   361
  (case Option.map Toplevel.theory_node (Toplevel.previous_node_of state) of
wenzelm@20957
   362
    SOME (SOME prev_thy) => ProofDisplay.print_theorems_diff (Context.theory_of prev_thy)
wenzelm@19430
   363
  | _ => ProofDisplay.print_theorems));
wenzelm@18588
   364
wenzelm@21663
   365
val print_theorems = Toplevel.unknown_context o print_theorems_theory o print_theorems_proof;
wenzelm@9513
   366
wenzelm@12060
   367
val print_locales = Toplevel.unknown_theory o
wenzelm@12060
   368
  Toplevel.keep (Locale.print_locales o Toplevel.theory_of);
wenzelm@12060
   369
wenzelm@22573
   370
fun print_locale (show_facts, (imports, body)) = Toplevel.unknown_theory o
wenzelm@19385
   371
  Toplevel.keep (fn state =>
wenzelm@22573
   372
    Locale.print_locale (Toplevel.theory_of state) show_facts imports body);
ballarin@15596
   373
ballarin@17139
   374
fun print_registrations show_wits name = Toplevel.unknown_context o
wenzelm@20957
   375
  Toplevel.keep (Toplevel.node_case
ballarin@24789
   376
      (Context.cases (Locale.print_registrations show_wits name o ProofContext.init)
ballarin@24789
   377
        (Locale.print_registrations show_wits name))
ballarin@24789
   378
    (Locale.print_registrations show_wits name o Proof.context_of));
wenzelm@12060
   379
wenzelm@9513
   380
val print_attributes = Toplevel.unknown_theory o
wenzelm@9513
   381
  Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@9513
   382
wenzelm@16026
   383
val print_simpset = Toplevel.unknown_context o
haftmann@27582
   384
  Toplevel.keep (Pretty.writeln o Simplifier.pretty_ss o Simplifier.local_simpset_of o Toplevel.context_of);
wenzelm@16026
   385
wenzelm@12382
   386
val print_rules = Toplevel.unknown_context o
wenzelm@18639
   387
  Toplevel.keep (ContextRules.print_rules o Toplevel.context_of);
wenzelm@12382
   388
wenzelm@9513
   389
val print_trans_rules = Toplevel.unknown_context o
wenzelm@18639
   390
  Toplevel.keep (Calculation.print_rules o Toplevel.context_of);
wenzelm@9513
   391
wenzelm@9513
   392
val print_methods = Toplevel.unknown_theory o
wenzelm@9513
   393
  Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@9513
   394
wenzelm@22116
   395
val print_antiquotations = Toplevel.imperative ThyOutput.print_antiquotations;
wenzelm@5831
   396
haftmann@22485
   397
val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
haftmann@22485
   398
  let
haftmann@22485
   399
    val thy = Toplevel.theory_of state;
wenzelm@24560
   400
    val all_thys = sort ThyInfo.thy_ord (thy :: Theory.ancestors_of thy);
wenzelm@22604
   401
    val gr = all_thys |> map (fn node =>
wenzelm@22602
   402
      let
wenzelm@22602
   403
        val name = Context.theory_name node;
wenzelm@22602
   404
        val parents = map Context.theory_name (Theory.parents_of node);
wenzelm@24560
   405
        val dir = Present.session_name node;
wenzelm@24560
   406
        val unfold = not (ThyInfo.known_thy name andalso ThyInfo.is_finished name);
wenzelm@24560
   407
      in {name = name, ID = name, parents = parents, dir = dir, unfold = unfold, path = ""} end);
wenzelm@22602
   408
  in Present.display_graph gr end);
haftmann@22485
   409
wenzelm@20574
   410
val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@20574
   411
  let
wenzelm@20574
   412
    val thy = Toplevel.theory_of state;
wenzelm@20574
   413
    val {classes = (space, algebra), ...} = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@20574
   414
    val {classes, ...} = Sorts.rep_algebra algebra;
wenzelm@20574
   415
    fun entry (c, (i, (_, cs))) =
wenzelm@20574
   416
      (i, {name = NameSpace.extern space c, ID = c, parents = cs,
wenzelm@20574
   417
            dir = "", unfold = true, path = ""});
wenzelm@20574
   418
    val gr =
wenzelm@20574
   419
      Graph.fold (cons o entry) classes []
wenzelm@20574
   420
      |> sort (int_ord o pairself #1) |> map #2;
wenzelm@20574
   421
  in Present.display_graph gr end);
wenzelm@20574
   422
kleing@15964
   423
wenzelm@16026
   424
(* retrieve theorems *)
wenzelm@7615
   425
wenzelm@9513
   426
fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@21003
   427
  ThmDeps.thm_deps (Proof.get_thmss (Toplevel.enter_proof_body state) args));
wenzelm@9454
   428
wenzelm@24560
   429
fun find_theorems ((opt_lim, rem_dups), spec) =
kleing@22340
   430
  Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@16026
   431
  let
wenzelm@21003
   432
    val proof_state = Toplevel.enter_proof_body state;
wenzelm@16026
   433
    val ctxt = Proof.context_of proof_state;
wenzelm@16026
   434
    val opt_goal = try Proof.get_goal proof_state |> Option.map (Thm.prop_of o #2 o #2);
kleing@22340
   435
  in FindTheorems.print_theorems ctxt opt_goal opt_lim rem_dups spec end);
wenzelm@16026
   436
wenzelm@5831
   437
berghofe@26184
   438
(* find unused theorems *)
berghofe@26184
   439
wenzelm@26186
   440
fun unused_thms opt_range = Toplevel.keep (fn state =>
wenzelm@26186
   441
  let
wenzelm@26186
   442
    val thy = Toplevel.theory_of state;
wenzelm@26186
   443
    val ctxt = Toplevel.context_of state;
wenzelm@26186
   444
    fun pretty_thm (a, th) = ProofContext.pretty_fact ctxt (a, [th]);
wenzelm@26186
   445
  in
wenzelm@26186
   446
    ThmDeps.unused_thms
wenzelm@26186
   447
      (case opt_range of
wenzelm@26694
   448
        NONE => (Theory.parents_of thy, [thy])
wenzelm@26694
   449
      | SOME (xs, NONE) => (map ThyInfo.get_theory xs, [thy])
wenzelm@26694
   450
      | SOME (xs, SOME ys) => (map ThyInfo.get_theory xs, map ThyInfo.get_theory ys))
wenzelm@26186
   451
    |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
wenzelm@26186
   452
  end);
berghofe@26184
   453
berghofe@26184
   454
wenzelm@5831
   455
(* print proof context contents *)
wenzelm@5831
   456
wenzelm@21663
   457
val print_binds = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21663
   458
  ProofContext.setmp_verbose ProofContext.print_binds (Toplevel.context_of state));
wenzelm@9513
   459
wenzelm@21663
   460
val print_cases = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21663
   461
  ProofContext.setmp_verbose ProofContext.print_cases (Toplevel.context_of state));
wenzelm@5831
   462
wenzelm@5831
   463
wenzelm@19268
   464
(* print theorems, terms, types etc. *)
wenzelm@19268
   465
wenzelm@19385
   466
local
wenzelm@19385
   467
wenzelm@19385
   468
fun string_of_stmts state args =
wenzelm@19268
   469
  Proof.get_thmss state args
wenzelm@21437
   470
  |> map (Element.pretty_statement (Proof.context_of state) Thm.theoremK)
wenzelm@19385
   471
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   472
wenzelm@19385
   473
fun string_of_thms state args =
wenzelm@12055
   474
  Pretty.string_of (ProofContext.pretty_thms (Proof.context_of state)
wenzelm@19385
   475
    (Proof.get_thmss state args));
wenzelm@5895
   476
wenzelm@19385
   477
fun string_of_prfs full state arg =
wenzelm@17066
   478
  Pretty.string_of (case arg of
skalberg@15531
   479
      NONE =>
berghofe@12125
   480
        let
wenzelm@17066
   481
          val (ctxt, (_, thm)) = Proof.get_goal state;
wenzelm@26626
   482
          val thy = ProofContext.theory_of ctxt;
wenzelm@26626
   483
          val prf = Thm.proof_of thm;
wenzelm@17066
   484
          val prop = Thm.full_prop_of thm;
berghofe@12125
   485
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf
berghofe@12125
   486
        in
wenzelm@27258
   487
          ProofSyntax.pretty_proof ctxt
wenzelm@17066
   488
            (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
berghofe@12125
   489
        end
skalberg@15531
   490
    | SOME args => Pretty.chunks
wenzelm@27258
   491
        (map (ProofSyntax.pretty_proof_of (Proof.context_of state) full)
wenzelm@19385
   492
          (Proof.get_thmss state args)));
berghofe@11524
   493
wenzelm@19385
   494
fun string_of_prop state s =
wenzelm@5831
   495
  let
wenzelm@12055
   496
    val ctxt = Proof.context_of state;
wenzelm@24508
   497
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   498
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   499
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   500
wenzelm@19385
   501
fun string_of_term state s =
wenzelm@5831
   502
  let
wenzelm@12055
   503
    val ctxt = Proof.context_of state;
wenzelm@24508
   504
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   505
    val T = Term.type_of t;
wenzelm@26704
   506
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   507
  in
wenzelm@19385
   508
    Pretty.string_of
wenzelm@26704
   509
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   510
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   511
  end;
wenzelm@5831
   512
wenzelm@19385
   513
fun string_of_type state s =
wenzelm@5831
   514
  let
wenzelm@12055
   515
    val ctxt = Proof.context_of state;
wenzelm@25331
   516
    val T = Syntax.read_typ ctxt s;
wenzelm@24920
   517
  in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end;
wenzelm@9128
   518
wenzelm@23935
   519
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@23935
   520
  PrintMode.with_modes modes (fn () =>
wenzelm@23935
   521
    writeln (string_of (Toplevel.enter_proof_body state) arg)) ());
wenzelm@19385
   522
wenzelm@19385
   523
in
wenzelm@9128
   524
wenzelm@19268
   525
val print_stmts = print_item string_of_stmts;
wenzelm@12876
   526
val print_thms = print_item string_of_thms;
wenzelm@19385
   527
val print_prfs = print_item o string_of_prfs;
wenzelm@12876
   528
val print_prop = print_item string_of_prop;
wenzelm@12876
   529
val print_term = print_item string_of_term;
wenzelm@12876
   530
val print_type = print_item string_of_type;
wenzelm@5831
   531
wenzelm@19385
   532
end;
wenzelm@19385
   533
wenzelm@12938
   534
wenzelm@12938
   535
(* markup commands *)
wenzelm@12938
   536
wenzelm@27876
   537
fun check_text (txt, pos) opt_node =
wenzelm@27876
   538
 (Position.report Markup.doc_source pos;
wenzelm@27876
   539
  ignore (ThyOutput.eval_antiquote (#1 (OuterKeyword.get_lexicons ())) opt_node (txt, pos)));
wenzelm@12953
   540
wenzelm@27853
   541
fun header_markup txt = Toplevel.keep (fn state =>
wenzelm@27853
   542
  if Toplevel.is_toplevel state then check_text txt NONE
wenzelm@27853
   543
  else raise Toplevel.UNDEF);
wenzelm@12953
   544
wenzelm@27853
   545
fun local_theory_markup (loc, txt) = Toplevel.present_local_theory loc (check_text txt o SOME);
wenzelm@27853
   546
fun proof_markup txt = Toplevel.present_proof (check_text txt o SOME);
wenzelm@12938
   547
wenzelm@5831
   548
end;