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