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