src/Pure/display.ML
author wenzelm
Wed Oct 15 15:13:25 1997 +0200 (1997-10-15)
changeset 3873 64f496e0885d
parent 3866 97f66ba17458
child 3879 de18c0c1141c
permissions -rw-r--r--
improved print_data;
paulson@1591
     1
(*  Title:      Pure/display.ML
paulson@1591
     2
    ID:         $Id$
paulson@1591
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@1591
     4
    Copyright   1993  University of Cambridge
paulson@1591
     5
paulson@1591
     6
Printing of theories, theorems, etc.
paulson@1591
     7
*)
paulson@1591
     8
paulson@1591
     9
signature DISPLAY =
paulson@1591
    10
  sig
paulson@1591
    11
  val pprint_cterm	: cterm -> pprint_args -> unit
paulson@1591
    12
  val pprint_ctyp	: ctyp -> pprint_args -> unit
paulson@1591
    13
  val pprint_theory	: theory -> pprint_args -> unit
paulson@1591
    14
  val pprint_thm	: thm -> pprint_args -> unit
wenzelm@3547
    15
  val pretty_cterm	: cterm -> Pretty.T
paulson@1591
    16
  val pretty_thm	: thm -> Pretty.T
paulson@1591
    17
  val print_cterm	: cterm -> unit
paulson@1591
    18
  val print_ctyp	: ctyp -> unit
wenzelm@3851
    19
  val show_consts	: bool ref
paulson@1591
    20
  val print_goals	: int -> thm -> unit
paulson@1591
    21
  val print_syntax	: theory -> unit
paulson@1591
    22
  val print_theory	: theory -> unit
wenzelm@3873
    23
  val print_data	: theory -> string -> unit
paulson@1591
    24
  val print_thm		: thm -> unit
paulson@1591
    25
  val prth		: thm -> thm
paulson@1591
    26
  val prthq		: thm Sequence.seq -> thm Sequence.seq
paulson@1591
    27
  val prths		: thm list -> thm list
paulson@1591
    28
  val show_hyps		: bool ref
paulson@1591
    29
  val string_of_cterm	: cterm -> string
paulson@1591
    30
  val string_of_ctyp	: ctyp -> string
paulson@1591
    31
  val string_of_thm	: thm -> string
paulson@1591
    32
  end;
paulson@1591
    33
paulson@1591
    34
paulson@1591
    35
structure Display : DISPLAY =
paulson@1591
    36
struct
paulson@1591
    37
paulson@1591
    38
(*If false, hypotheses are printed as dots*)
paulson@1591
    39
val show_hyps = ref true;
paulson@1591
    40
paulson@1591
    41
fun pretty_thm th =
paulson@1591
    42
  let
paulson@1591
    43
    val {sign, hyps, prop, ...} = rep_thm th;
paulson@1591
    44
    val xshyps = extra_shyps th;
paulson@1591
    45
    val hlen = length xshyps + length hyps;
paulson@1591
    46
    val hsymbs =
paulson@1591
    47
      if hlen = 0 then []
paulson@1591
    48
      else if ! show_hyps then
paulson@1591
    49
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@3785
    50
          (map (Sign.pretty_term sign) hyps @
wenzelm@3785
    51
           map (Sign.pretty_sort sign) xshyps)]
paulson@1591
    52
      else
paulson@1591
    53
        [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
paulson@1591
    54
  in
paulson@1591
    55
    Pretty.block (Sign.pretty_term sign prop :: hsymbs)
paulson@1591
    56
  end;
paulson@1591
    57
paulson@1591
    58
val string_of_thm = Pretty.string_of o pretty_thm;
paulson@1591
    59
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
paulson@1591
    60
paulson@1591
    61
paulson@1591
    62
(** Top-level commands for printing theorems **)
paulson@1591
    63
val print_thm = writeln o string_of_thm;
paulson@1591
    64
paulson@1591
    65
fun prth th = (print_thm th; th);
paulson@1591
    66
paulson@1591
    67
(*Print and return a sequence of theorems, separated by blank lines. *)
paulson@1591
    68
fun prthq thseq =
paulson@1591
    69
  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
paulson@1591
    70
paulson@1591
    71
(*Print and return a list of theorems, separated by blank lines. *)
paulson@1591
    72
fun prths ths = (print_list_ln print_thm ths; ths);
paulson@1591
    73
paulson@1591
    74
paulson@1591
    75
(* other printing commands *)
paulson@1591
    76
paulson@1591
    77
fun pprint_ctyp cT =
paulson@1591
    78
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
paulson@1591
    79
paulson@1591
    80
fun string_of_ctyp cT =
paulson@1591
    81
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
paulson@1591
    82
paulson@1591
    83
val print_ctyp = writeln o string_of_ctyp;
paulson@1591
    84
wenzelm@3547
    85
fun pretty_cterm ct =
wenzelm@3547
    86
  let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
wenzelm@3547
    87
paulson@1591
    88
fun pprint_cterm ct =
paulson@1591
    89
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
paulson@1591
    90
paulson@1591
    91
fun string_of_cterm ct =
paulson@1591
    92
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
paulson@1591
    93
paulson@1591
    94
val print_cterm = writeln o string_of_cterm;
paulson@1591
    95
paulson@1591
    96
paulson@1591
    97
(* print theory *)
paulson@1591
    98
paulson@1591
    99
val pprint_theory = Sign.pprint_sg o sign_of;
paulson@1591
   100
paulson@1591
   101
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@3873
   102
val print_data = Sign.print_data o sign_of;
paulson@1591
   103
wenzelm@3811
   104
fun print_thy thy =
paulson@1591
   105
  let
wenzelm@3811
   106
    val {sign, new_axioms, oracles, ...} = rep_theory thy;
paulson@1591
   107
    val axioms = Symtab.dest new_axioms;
wenzelm@3811
   108
    val oras = map fst (Symtab.dest oracles);
paulson@1591
   109
paulson@1591
   110
    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
paulson@1591
   111
      Pretty.quote (Sign.pretty_term sign t)];
paulson@1591
   112
  in
wenzelm@3811
   113
    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms));
wenzelm@3873
   114
    Pretty.writeln (Pretty.strs ("oracles:" :: oras))
paulson@1591
   115
  end;
paulson@1591
   116
wenzelm@3811
   117
fun print_theory thy = (Sign.print_sg (sign_of thy); print_thy thy);
paulson@1591
   118
paulson@1591
   119
paulson@1591
   120
paulson@1591
   121
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
paulson@1591
   122
wenzelm@3851
   123
(*show consts in case of showing types?*)
wenzelm@3851
   124
val show_consts = ref false;
wenzelm@3851
   125
paulson@1591
   126
paulson@1591
   127
local
wenzelm@3851
   128
wenzelm@3851
   129
  (* utils *)
paulson@1591
   130
paulson@1591
   131
  fun ins_entry (x, y) [] = [(x, [y])]
paulson@1591
   132
    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
wenzelm@3851
   133
        if x = x' then (x', y ins ys') :: pairs
paulson@1591
   134
        else pair :: ins_entry (x, y) pairs;
paulson@1591
   135
wenzelm@3851
   136
  fun add_consts (Const (c, T), env) = ins_entry (T, c) env
wenzelm@3851
   137
    | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
wenzelm@3851
   138
    | add_consts (Abs (_, _, t), env) = add_consts (t, env)
wenzelm@3851
   139
    | add_consts (_, env) = env;
paulson@1591
   140
wenzelm@3851
   141
  fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
wenzelm@3851
   142
    | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
wenzelm@3851
   143
    | add_vars (Abs (_, _, t), env) = add_vars (t, env)
wenzelm@3851
   144
    | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
wenzelm@3851
   145
    | add_vars (_, env) = env;
paulson@1591
   146
wenzelm@3851
   147
  fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
wenzelm@3851
   148
    | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
wenzelm@3851
   149
    | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
paulson@1591
   150
wenzelm@3851
   151
  fun less_idx ((x, i):indexname, (y, j):indexname) =
wenzelm@3851
   152
    x < y orelse x = y andalso i < j;
wenzelm@3851
   153
  val sort_idxs = map (apsnd (sort less_idx));
wenzelm@3851
   154
  val sort_strs = map (apsnd sort_strings);
paulson@1591
   155
paulson@1591
   156
wenzelm@3851
   157
  (* prepare atoms *)
wenzelm@3851
   158
wenzelm@3851
   159
  fun consts_of t = sort_strs (add_consts (t, []));
wenzelm@3851
   160
  fun vars_of t = sort_idxs (add_vars (t, []));
wenzelm@3851
   161
  fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
wenzelm@3851
   162
wenzelm@3851
   163
in
wenzelm@3851
   164
wenzelm@3851
   165
  (* print_goals *)
wenzelm@3851
   166
wenzelm@3851
   167
  fun print_goals maxgoals state =
wenzelm@3851
   168
    let
wenzelm@3851
   169
      val {sign, ...} = rep_thm state;
paulson@1591
   170
wenzelm@3851
   171
      val prt_term = Sign.pretty_term sign;
wenzelm@3851
   172
      val prt_typ = Sign.pretty_typ sign;
wenzelm@3851
   173
      val prt_sort = Sign.pretty_sort sign;
wenzelm@3851
   174
wenzelm@3851
   175
      fun prt_atoms prt prtT (X, xs) = Pretty.block
wenzelm@3851
   176
        [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
wenzelm@3851
   177
          Pretty.brk 1, prtT X];
paulson@1591
   178
wenzelm@3851
   179
      fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@3851
   180
        | prt_var xi = prt_term (Syntax.var xi);
wenzelm@3851
   181
wenzelm@3851
   182
      fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@3851
   183
        | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@3851
   184
wenzelm@3851
   185
      val prt_consts = prt_atoms (prt_term o Syntax.const) prt_typ;
wenzelm@3851
   186
      val prt_vars = prt_atoms prt_var prt_typ;
wenzelm@3851
   187
      val prt_varsT = prt_atoms prt_varT prt_sort;
paulson@1591
   188
paulson@1591
   189
wenzelm@3851
   190
      fun print_list _ _ [] = ()
wenzelm@3851
   191
        | print_list name prt lst = (writeln "";
wenzelm@3851
   192
            Pretty.writeln (Pretty.big_list name (map prt lst)));
wenzelm@3851
   193
wenzelm@3851
   194
      fun print_subgoals (_, []) = ()
wenzelm@3851
   195
        | print_subgoals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
wenzelm@3851
   196
            [Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A]));
wenzelm@3851
   197
              print_subgoals (n + 1, As));
paulson@1591
   198
wenzelm@3851
   199
      val print_ffpairs =
wenzelm@3851
   200
        print_list "Flex-flex pairs:" (prt_term o Logic.mk_flexpair);
wenzelm@3851
   201
wenzelm@3851
   202
      val print_consts = print_list "Constants:" prt_consts o consts_of;
wenzelm@3851
   203
      val print_vars = print_list "Variables:" prt_vars o vars_of;
wenzelm@3851
   204
      val print_varsT = print_list "Type variables:" prt_varsT o varsT_of;
wenzelm@3851
   205
wenzelm@3851
   206
wenzelm@3851
   207
      val {prop, ...} = rep_thm state;
wenzelm@3851
   208
      val (tpairs, As, B) = Logic.strip_horn prop;
wenzelm@3851
   209
      val ngoals = length As;
paulson@1591
   210
wenzelm@3851
   211
      fun print_gs (types, sorts) =
wenzelm@3851
   212
       (Pretty.writeln (prt_term B);
wenzelm@3851
   213
        if ngoals = 0 then writeln "No subgoals!"
wenzelm@3851
   214
        else if ngoals > maxgoals then
wenzelm@3851
   215
          (print_subgoals (1, take (maxgoals, As));
wenzelm@3851
   216
            writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
wenzelm@3851
   217
        else print_subgoals (1, As);
wenzelm@3851
   218
wenzelm@3851
   219
        print_ffpairs tpairs;
paulson@1591
   220
wenzelm@3851
   221
        if types andalso ! show_consts then print_consts prop else ();
wenzelm@3851
   222
        if types then print_vars prop else ();
wenzelm@3851
   223
        if sorts then print_varsT prop else ());
wenzelm@3851
   224
    in
wenzelm@3851
   225
      setmp show_no_free_types true
wenzelm@3851
   226
        (setmp show_types (! show_types orelse ! show_sorts)
wenzelm@3851
   227
          (setmp show_sorts false print_gs))
wenzelm@3851
   228
     (! show_types orelse ! show_sorts, ! show_sorts)
wenzelm@3851
   229
  end;
paulson@1591
   230
wenzelm@3851
   231
end;
paulson@1591
   232
paulson@1591
   233
paulson@1591
   234
end;
paulson@1591
   235
paulson@1591
   236
open Display;