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