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