src/Pure/display.ML
author blanchet
Mon May 19 23:43:53 2014 +0200 (2014-05-19)
changeset 57008 10f68b83b474
parent 56438 7f6b2634d853
child 59917 9830c944670f
permissions -rw-r--r--
use E 1.8's auto scheduler option
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 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@20629
    28
  val pretty_full_theory: bool -> theory -> Pretty.T list
wenzelm@4950
    29
end;
paulson@1591
    30
wenzelm@4950
    31
structure Display: DISPLAY =
paulson@1591
    32
struct
paulson@1591
    33
wenzelm@32089
    34
(** options **)
wenzelm@32089
    35
wenzelm@32187
    36
val show_consts = Goal_Display.show_consts;
wenzelm@32089
    37
wenzelm@56438
    38
val show_hyps_raw = Config.declare ("show_hyps", @{here}) (fn _ => Config.Bool false);
wenzelm@39166
    39
val show_hyps = Config.bool show_hyps_raw;
wenzelm@39166
    40
wenzelm@56438
    41
val show_tags_raw = Config.declare ("show_tags", @{here}) (fn _ => Config.Bool false);
wenzelm@39166
    42
val show_tags = Config.bool show_tags_raw;
wenzelm@32089
    43
wenzelm@32089
    44
wenzelm@11883
    45
wenzelm@6087
    46
(** print thm **)
wenzelm@6087
    47
wenzelm@28840
    48
fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
wenzelm@6087
    49
val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
paulson@1591
    50
wenzelm@50126
    51
fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
paulson@1591
    52
  let
wenzelm@39166
    53
    val show_tags = Config.get ctxt show_tags;
wenzelm@39166
    54
    val show_hyps = Config.get ctxt show_hyps;
wenzelm@39166
    55
wenzelm@17447
    56
    val th = Thm.strip_shyps raw_th;
wenzelm@28316
    57
    val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
wenzelm@55633
    58
    val hyps' = if show_hyps then hyps else Thm.undeclared_hyps (Context.Proof ctxt) th;
wenzelm@55633
    59
    val extra_shyps = Thm.extra_shyps th;
wenzelm@21646
    60
    val tags = Thm.get_tags th;
wenzelm@6087
    61
berghofe@13658
    62
    val q = if quote then Pretty.quote else I;
wenzelm@32145
    63
    val prt_term = q o Syntax.pretty_term ctxt;
wenzelm@6279
    64
wenzelm@28802
    65
wenzelm@55633
    66
    val hlen = length extra_shyps + length hyps' + length tpairs;
paulson@1591
    67
    val hsymbs =
wenzelm@50126
    68
      if hlen = 0 then []
wenzelm@39166
    69
      else if show_hyps orelse show_hyps' then
paulson@1591
    70
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@32187
    71
          (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
wenzelm@55633
    72
           map (Syntax.pretty_sort ctxt) extra_shyps)]
wenzelm@50126
    73
      else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
wenzelm@6087
    74
    val tsymbs =
wenzelm@39166
    75
      if null tags orelse not show_tags then []
wenzelm@6087
    76
      else [Pretty.brk 1, pretty_tags tags];
wenzelm@6279
    77
  in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
paulson@1591
    78
wenzelm@50126
    79
fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
wenzelm@51584
    80
fun pretty_thm_item ctxt th = Pretty.item [pretty_thm ctxt th];
wenzelm@32090
    81
wenzelm@32145
    82
fun pretty_thm_global thy =
wenzelm@50126
    83
  pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
wenzelm@32090
    84
wenzelm@32090
    85
fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
wenzelm@6087
    86
wenzelm@32090
    87
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@32090
    88
val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
wenzelm@32090
    89
val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
wenzelm@32090
    90
paulson@1591
    91
wenzelm@4250
    92
wenzelm@4250
    93
(** print theory **)
paulson@1591
    94
wenzelm@11883
    95
(* pretty_full_theory *)
wenzelm@4250
    96
wenzelm@20629
    97
fun pretty_full_theory verbose thy =
wenzelm@4250
    98
  let
wenzelm@35199
    99
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@24920
   100
wenzelm@24920
   101
    fun prt_cls c = Syntax.pretty_sort ctxt [c];
wenzelm@24920
   102
    fun prt_sort S = Syntax.pretty_sort ctxt S;
wenzelm@37248
   103
    fun prt_arity t (c, Ss) = Syntax.pretty_arity ctxt (t, Ss, [c]);
wenzelm@24920
   104
    fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
wenzelm@35845
   105
    val prt_typ_no_tvars = prt_typ o Logic.unvarifyT_global;
wenzelm@24920
   106
    fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
wenzelm@35845
   107
    val prt_term_no_vars = prt_term o Logic.unvarify_global;
wenzelm@50301
   108
    fun prt_const (c, ty) = [Pretty.mark_str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
wenzelm@42384
   109
    val prt_const' = Defs.pretty_const ctxt;
wenzelm@4250
   110
wenzelm@14794
   111
    fun pretty_classrel (c, []) = prt_cls c
wenzelm@14794
   112
      | pretty_classrel (c, cs) = Pretty.block
wenzelm@51510
   113
          (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 :: Pretty.commas (map prt_cls cs));
wenzelm@4250
   114
wenzelm@4250
   115
    fun pretty_default S = Pretty.block
wenzelm@14794
   116
      [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
wenzelm@4250
   117
wenzelm@14996
   118
    val tfrees = map (fn v => TFree (v, []));
wenzelm@33173
   119
    fun pretty_type syn (t, (Type.LogicalType n)) =
skalberg@15531
   120
          if syn then NONE
wenzelm@43329
   121
          else SOME (prt_typ (Type (t, tfrees (Name.invent Name.context Name.aT n))))
wenzelm@33173
   122
      | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'))) =
skalberg@15531
   123
          if syn <> syn' then NONE
skalberg@15531
   124
          else SOME (Pretty.block
wenzelm@14996
   125
            [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
wenzelm@33173
   126
      | pretty_type syn (t, Type.Nonterminal) =
skalberg@15531
   127
          if not syn then NONE
skalberg@15531
   128
          else SOME (prt_typ (Type (t, [])));
wenzelm@4250
   129
wenzelm@19482
   130
    val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
skalberg@14223
   131
wenzelm@19698
   132
    fun pretty_abbrev (c, (ty, t)) = Pretty.block
wenzelm@19698
   133
      (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
wenzelm@19698
   134
wenzelm@50301
   135
    fun pretty_axm (a, t) =
wenzelm@50301
   136
      Pretty.block [Pretty.mark_str a, Pretty.str ":", Pretty.brk 1, prt_term_no_vars t];
wenzelm@19365
   137
wenzelm@19702
   138
    fun pretty_finals reds = Pretty.block
wenzelm@19703
   139
      (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
wenzelm@19702
   140
wenzelm@19698
   141
    fun pretty_reduct (lhs, rhs) = Pretty.block
wenzelm@19702
   142
      ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
wenzelm@19702
   143
        Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
wenzelm@4250
   144
wenzelm@19698
   145
    fun pretty_restrict (const, name) =
wenzelm@19698
   146
      Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
wenzelm@8720
   147
wenzelm@24665
   148
    val defs = Theory.defs_of thy;
wenzelm@19698
   149
    val {restricts, reducts} = Defs.dest defs;
wenzelm@47005
   150
    val tsig = Sign.tsig_of thy;
wenzelm@47005
   151
    val consts = Sign.consts_of thy;
wenzelm@56025
   152
    val {const_space, constants, constraints} = Consts.dest consts;
wenzelm@56025
   153
    val extern_const = Name_Space.extern ctxt const_space;
wenzelm@26637
   154
    val {classes, default, types, ...} = Type.rep_tsig tsig;
wenzelm@19698
   155
    val (class_space, class_algebra) = classes;
wenzelm@36328
   156
    val classes = Sorts.classes_of class_algebra;
wenzelm@36328
   157
    val arities = Sorts.arities_of class_algebra;
wenzelm@14996
   158
wenzelm@49560
   159
    val clsses =
wenzelm@56052
   160
      Name_Space.extern_entries ctxt class_space
wenzelm@56052
   161
        (map (fn ((c, _), cs) => (c, Sign.minimize_sort thy cs)) (Graph.dest classes))
wenzelm@56025
   162
      |> map (apfst #1);
wenzelm@56052
   163
    val tdecls = Name_Space.extern_table ctxt types |> map (apfst #1);
wenzelm@56052
   164
    val arties =
wenzelm@56052
   165
      Name_Space.extern_entries ctxt (Type.type_space tsig) (Symtab.dest arities)
wenzelm@56052
   166
      |> map (apfst #1);
wenzelm@20629
   167
wenzelm@33158
   168
    fun prune_const c = not verbose andalso Consts.is_concealed consts c;
wenzelm@56025
   169
    val cnsts =
wenzelm@56052
   170
      Name_Space.markup_entries ctxt const_space
wenzelm@56062
   171
        (filter_out (prune_const o fst) 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@56062
   175
    val cnstrs = Name_Space.markup_entries ctxt const_space constraints;
wenzelm@19698
   176
wenzelm@56052
   177
    val axms = Name_Space.markup_table ctxt (Theory.axiom_table thy);
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;