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