src/Pure/display.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50301 56b4c9afd7be
child 51509 22fd3605c7a2
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
paulson@1591
     1
(*  Title:      Pure/display.ML
paulson@1591
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@32089
     3
    Author:     Makarius
paulson@1591
     4
wenzelm@32089
     5
Printing of theorems, results etc.
paulson@1591
     6
*)
paulson@1591
     7
wenzelm@11883
     8
signature BASIC_DISPLAY =
wenzelm@11883
     9
sig
wenzelm@39050
    10
  val show_consts_default: bool Unsynchronized.ref
wenzelm@39050
    11
  val show_consts: bool Config.T
wenzelm@39166
    12
  val show_hyps_raw: Config.raw
wenzelm@39166
    13
  val show_hyps: bool Config.T
wenzelm@39166
    14
  val show_tags_raw: Config.raw
wenzelm@39166
    15
  val show_tags: bool Config.T
wenzelm@11883
    16
end;
wenzelm@11883
    17
paulson@1591
    18
signature DISPLAY =
wenzelm@4950
    19
sig
wenzelm@11883
    20
  include BASIC_DISPLAY
wenzelm@50126
    21
  val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool} -> thm -> Pretty.T
wenzelm@32090
    22
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@32090
    23
  val pretty_thm_global: theory -> thm -> Pretty.T
wenzelm@32090
    24
  val pretty_thm_without_context: thm -> Pretty.T
wenzelm@32090
    25
  val string_of_thm: Proof.context -> thm -> string
wenzelm@32090
    26
  val string_of_thm_global: theory -> thm -> string
wenzelm@32090
    27
  val string_of_thm_without_context: thm -> string
wenzelm@32090
    28
  val pretty_thms: Proof.context -> thm list -> Pretty.T
wenzelm@26928
    29
  val print_syntax: theory -> unit
wenzelm@20629
    30
  val pretty_full_theory: bool -> theory -> Pretty.T list
wenzelm@4950
    31
end;
paulson@1591
    32
wenzelm@4950
    33
structure Display: DISPLAY =
paulson@1591
    34
struct
paulson@1591
    35
wenzelm@32089
    36
(** options **)
wenzelm@32089
    37
wenzelm@39050
    38
val show_consts_default = Goal_Display.show_consts_default;
wenzelm@32187
    39
val show_consts = Goal_Display.show_consts;
wenzelm@32089
    40
wenzelm@39166
    41
val show_hyps_raw = Config.declare "show_hyps" (fn _ => Config.Bool false);
wenzelm@39166
    42
val show_hyps = Config.bool show_hyps_raw;
wenzelm@39166
    43
wenzelm@39166
    44
val show_tags_raw = Config.declare "show_tags" (fn _ => Config.Bool false);
wenzelm@39166
    45
val show_tags = Config.bool show_tags_raw;
wenzelm@32089
    46
wenzelm@32089
    47
wenzelm@11883
    48
wenzelm@6087
    49
(** print thm **)
wenzelm@6087
    50
wenzelm@28840
    51
fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
wenzelm@6087
    52
val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
paulson@1591
    53
wenzelm@50126
    54
fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
paulson@1591
    55
  let
wenzelm@39166
    56
    val show_tags = Config.get ctxt show_tags;
wenzelm@39166
    57
    val show_hyps = Config.get ctxt show_hyps;
wenzelm@39166
    58
wenzelm@17447
    59
    val th = Thm.strip_shyps raw_th;
wenzelm@28316
    60
    val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
wenzelm@6087
    61
    val xshyps = Thm.extra_shyps th;
wenzelm@21646
    62
    val tags = Thm.get_tags th;
wenzelm@6087
    63
berghofe@13658
    64
    val q = if quote then Pretty.quote else I;
wenzelm@32145
    65
    val prt_term = q o Syntax.pretty_term ctxt;
wenzelm@6279
    66
wenzelm@32145
    67
    val asms = map Thm.term_of (Assumption.all_assms_of ctxt);
wenzelm@39166
    68
    val hyps' = if show_hyps then hyps else subtract (op aconv) asms hyps;
wenzelm@28802
    69
wenzelm@17447
    70
    val hlen = length xshyps + length hyps' + length tpairs;
paulson@1591
    71
    val hsymbs =
wenzelm@50126
    72
      if hlen = 0 then []
wenzelm@39166
    73
      else if show_hyps orelse show_hyps' then
paulson@1591
    74
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@32187
    75
          (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
wenzelm@50126
    76
           map (Syntax.pretty_sort ctxt) xshyps)]
wenzelm@50126
    77
      else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
wenzelm@6087
    78
    val tsymbs =
wenzelm@39166
    79
      if null tags orelse not show_tags then []
wenzelm@6087
    80
      else [Pretty.brk 1, pretty_tags tags];
wenzelm@6279
    81
  in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
paulson@1591
    82
wenzelm@50126
    83
fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
wenzelm@32090
    84
wenzelm@32145
    85
fun pretty_thm_global thy =
wenzelm@50126
    86
  pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
wenzelm@32090
    87
wenzelm@32090
    88
fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
wenzelm@6087
    89
wenzelm@32090
    90
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@32090
    91
val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
wenzelm@32090
    92
val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
wenzelm@32090
    93
paulson@1591
    94
wenzelm@32090
    95
(* multiple theorems *)
wenzelm@32090
    96
wenzelm@50126
    97
fun pretty_thms ctxt [th] = pretty_thm ctxt th
wenzelm@50126
    98
  | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
paulson@1591
    99
paulson@1591
   100
wenzelm@4250
   101
wenzelm@4250
   102
(** print theory **)
paulson@1591
   103
wenzelm@16437
   104
val print_syntax = Syntax.print_syntax o Sign.syn_of;
wenzelm@4498
   105
wenzelm@4498
   106
wenzelm@11883
   107
(* pretty_full_theory *)
wenzelm@4250
   108
wenzelm@20629
   109
fun pretty_full_theory verbose thy =
wenzelm@4250
   110
  let
wenzelm@35199
   111
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@24920
   112
wenzelm@24920
   113
    fun prt_cls c = Syntax.pretty_sort ctxt [c];
wenzelm@24920
   114
    fun prt_sort S = Syntax.pretty_sort ctxt S;
wenzelm@37248
   115
    fun prt_arity t (c, Ss) = Syntax.pretty_arity ctxt (t, Ss, [c]);
wenzelm@24920
   116
    fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
wenzelm@35845
   117
    val prt_typ_no_tvars = prt_typ o Logic.unvarifyT_global;
wenzelm@24920
   118
    fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
wenzelm@35845
   119
    val prt_term_no_vars = prt_term o Logic.unvarify_global;
wenzelm@50301
   120
    fun prt_const (c, ty) = [Pretty.mark_str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
wenzelm@42384
   121
    val prt_const' = Defs.pretty_const ctxt;
wenzelm@4250
   122
wenzelm@14794
   123
    fun pretty_classrel (c, []) = prt_cls c
wenzelm@14794
   124
      | pretty_classrel (c, cs) = Pretty.block
wenzelm@14794
   125
          (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@14794
   126
            Pretty.commas (map prt_cls cs));
wenzelm@4250
   127
wenzelm@4250
   128
    fun pretty_default S = Pretty.block
wenzelm@14794
   129
      [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
wenzelm@4250
   130
wenzelm@14996
   131
    val tfrees = map (fn v => TFree (v, []));
wenzelm@33173
   132
    fun pretty_type syn (t, (Type.LogicalType n)) =
skalberg@15531
   133
          if syn then NONE
wenzelm@43329
   134
          else SOME (prt_typ (Type (t, tfrees (Name.invent Name.context Name.aT n))))
wenzelm@33173
   135
      | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'))) =
skalberg@15531
   136
          if syn <> syn' then NONE
skalberg@15531
   137
          else SOME (Pretty.block
wenzelm@14996
   138
            [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
wenzelm@33173
   139
      | pretty_type syn (t, Type.Nonterminal) =
skalberg@15531
   140
          if not syn then NONE
skalberg@15531
   141
          else SOME (prt_typ (Type (t, [])));
wenzelm@4250
   142
wenzelm@19482
   143
    val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
skalberg@14223
   144
wenzelm@19698
   145
    fun pretty_abbrev (c, (ty, t)) = Pretty.block
wenzelm@19698
   146
      (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
wenzelm@19698
   147
wenzelm@50301
   148
    fun pretty_axm (a, t) =
wenzelm@50301
   149
      Pretty.block [Pretty.mark_str a, Pretty.str ":", Pretty.brk 1, prt_term_no_vars t];
wenzelm@19365
   150
wenzelm@19702
   151
    fun pretty_finals reds = Pretty.block
wenzelm@19703
   152
      (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
wenzelm@19702
   153
wenzelm@19698
   154
    fun pretty_reduct (lhs, rhs) = Pretty.block
wenzelm@19702
   155
      ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
wenzelm@19702
   156
        Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
wenzelm@4250
   157
wenzelm@19698
   158
    fun pretty_restrict (const, name) =
wenzelm@19698
   159
      Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
wenzelm@8720
   160
wenzelm@24665
   161
    val axioms = (Theory.axiom_space thy, Theory.axiom_table thy);
wenzelm@24665
   162
    val defs = Theory.defs_of thy;
wenzelm@19698
   163
    val {restricts, reducts} = Defs.dest defs;
wenzelm@47005
   164
    val tsig = Sign.tsig_of thy;
wenzelm@47005
   165
    val consts = Sign.consts_of thy;
wenzelm@18936
   166
    val {constants, constraints} = Consts.dest consts;
wenzelm@42358
   167
    val extern_const = Name_Space.extern ctxt (#1 constants);
wenzelm@26637
   168
    val {classes, default, types, ...} = Type.rep_tsig tsig;
wenzelm@19698
   169
    val (class_space, class_algebra) = classes;
wenzelm@36328
   170
    val classes = Sorts.classes_of class_algebra;
wenzelm@36328
   171
    val arities = Sorts.arities_of class_algebra;
wenzelm@14996
   172
wenzelm@49560
   173
    val clsses =
wenzelm@49560
   174
      Name_Space.dest_table ctxt (class_space, Symtab.make (map (apfst fst) (Graph.dest classes)));
wenzelm@42358
   175
    val tdecls = Name_Space.dest_table ctxt types;
wenzelm@42359
   176
    val arties = Name_Space.dest_table ctxt (Type.type_space tsig, arities);
wenzelm@20629
   177
wenzelm@33158
   178
    fun prune_const c = not verbose andalso Consts.is_concealed consts c;
wenzelm@42358
   179
    val cnsts = Name_Space.extern_table ctxt (#1 constants,
wenzelm@24774
   180
      Symtab.make (filter_out (prune_const o fst) (Symtab.dest (#2 constants))));
wenzelm@24774
   181
wenzelm@19482
   182
    val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
wenzelm@25405
   183
    val abbrevs = map_filter (fn (c, (ty, SOME t)) => SOME (c, (ty, t)) | _ => NONE) cnsts;
wenzelm@42358
   184
    val cnstrs = Name_Space.extern_table ctxt constraints;
wenzelm@19698
   185
wenzelm@42358
   186
    val axms = Name_Space.extern_table ctxt axioms;
wenzelm@24774
   187
wenzelm@24774
   188
    val (reds0, (reds1, reds2)) = filter_out (prune_const o fst o fst) reducts
wenzelm@24774
   189
      |> map (fn (lhs, rhs) =>
wenzelm@24774
   190
        (apfst extern_const lhs, map (apfst extern_const) (filter_out (prune_const o fst) rhs)))
wenzelm@19702
   191
      |> sort_wrt (#1 o #1)
wenzelm@19702
   192
      |> List.partition (null o #2)
wenzelm@19702
   193
      ||> List.partition (Defs.plain_args o #2 o #1);
wenzelm@19698
   194
    val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
wenzelm@4250
   195
  in
wenzelm@29091
   196
    [Pretty.strs ("names:" :: Context.display_names thy)] @
wenzelm@30409
   197
    [Pretty.big_list "classes:" (map pretty_classrel clsses),
wenzelm@8720
   198
      pretty_default default,
wenzelm@19482
   199
      Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
wenzelm@19482
   200
      Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
wenzelm@16534
   201
      Pretty.big_list "type arities:" (pretty_arities arties),
wenzelm@19698
   202
      Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
wenzelm@19365
   203
      Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
wenzelm@19698
   204
      Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
wenzelm@19698
   205
      Pretty.big_list "axioms:" (map pretty_axm axms),
wenzelm@50301
   206
      Pretty.block
wenzelm@50301
   207
        (Pretty.breaks (Pretty.str "oracles:" :: map Pretty.mark_str (Thm.extern_oracles ctxt))),
wenzelm@19698
   208
      Pretty.big_list "definitions:"
wenzelm@19702
   209
        [pretty_finals reds0,
wenzelm@19702
   210
         Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
wenzelm@19702
   211
         Pretty.big_list "overloaded:" (map pretty_reduct reds2),
wenzelm@19702
   212
         Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
wenzelm@4250
   213
  end;
wenzelm@4250
   214
paulson@1591
   215
end;
paulson@1591
   216
wenzelm@32089
   217
structure Basic_Display: BASIC_DISPLAY = Display;
wenzelm@32089
   218
open Basic_Display;