src/Pure/display.ML
author wenzelm
Tue Aug 05 17:01:02 1997 +0200 (1997-08-05)
changeset 3593 f53de7618ef8
parent 3547 977d58464d7f
child 3669 3384c6f1f095
permissions -rw-r--r--
removed ML-Systems/smlnj-1.07.ML;
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
paulson@1591
    19
  val print_goals	: int -> thm -> unit
paulson@1591
    20
  val print_goals_ref	: (int -> thm -> unit) ref
paulson@1591
    21
  val print_syntax	: theory -> unit
paulson@1591
    22
  val print_theory	: theory -> unit
paulson@1591
    23
  val print_thm		: thm -> unit
paulson@1591
    24
  val prth		: thm -> thm
paulson@1591
    25
  val prthq		: thm Sequence.seq -> thm Sequence.seq
paulson@1591
    26
  val prths		: thm list -> thm list
paulson@1591
    27
  val show_hyps		: bool ref
paulson@1591
    28
  val string_of_cterm	: cterm -> string
paulson@1591
    29
  val string_of_ctyp	: ctyp -> string
paulson@1591
    30
  val string_of_thm	: thm -> string
paulson@1591
    31
  end;
paulson@1591
    32
paulson@1591
    33
paulson@1591
    34
structure Display : DISPLAY =
paulson@1591
    35
struct
paulson@1591
    36
paulson@1591
    37
(*If false, hypotheses are printed as dots*)
paulson@1591
    38
val show_hyps = ref true;
paulson@1591
    39
paulson@1591
    40
fun pretty_thm th =
paulson@1591
    41
  let
paulson@1591
    42
    val {sign, hyps, prop, ...} = rep_thm th;
paulson@1591
    43
    val xshyps = extra_shyps th;
paulson@1591
    44
    val hlen = length xshyps + length hyps;
paulson@1591
    45
    val hsymbs =
paulson@1591
    46
      if hlen = 0 then []
paulson@1591
    47
      else if ! show_hyps then
paulson@1591
    48
        [Pretty.brk 2, Pretty.list "[" "]"
paulson@1591
    49
          (map (Sign.pretty_term sign) hyps @ map Sign.pretty_sort xshyps)]
paulson@1591
    50
      else
paulson@1591
    51
        [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
paulson@1591
    52
  in
paulson@1591
    53
    Pretty.block (Sign.pretty_term sign prop :: hsymbs)
paulson@1591
    54
  end;
paulson@1591
    55
paulson@1591
    56
val string_of_thm = Pretty.string_of o pretty_thm;
paulson@1591
    57
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
paulson@1591
    58
paulson@1591
    59
paulson@1591
    60
(** Top-level commands for printing theorems **)
paulson@1591
    61
val print_thm = writeln o string_of_thm;
paulson@1591
    62
paulson@1591
    63
fun prth th = (print_thm th; th);
paulson@1591
    64
paulson@1591
    65
(*Print and return a sequence of theorems, separated by blank lines. *)
paulson@1591
    66
fun prthq thseq =
paulson@1591
    67
  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
paulson@1591
    68
paulson@1591
    69
(*Print and return a list of theorems, separated by blank lines. *)
paulson@1591
    70
fun prths ths = (print_list_ln print_thm ths; ths);
paulson@1591
    71
paulson@1591
    72
paulson@1591
    73
(* other printing commands *)
paulson@1591
    74
paulson@1591
    75
fun pprint_ctyp cT =
paulson@1591
    76
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
paulson@1591
    77
paulson@1591
    78
fun string_of_ctyp cT =
paulson@1591
    79
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
paulson@1591
    80
paulson@1591
    81
val print_ctyp = writeln o string_of_ctyp;
paulson@1591
    82
wenzelm@3547
    83
fun pretty_cterm ct =
wenzelm@3547
    84
  let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
wenzelm@3547
    85
paulson@1591
    86
fun pprint_cterm ct =
paulson@1591
    87
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
paulson@1591
    88
paulson@1591
    89
fun string_of_cterm ct =
paulson@1591
    90
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
paulson@1591
    91
paulson@1591
    92
val print_cterm = writeln o string_of_cterm;
paulson@1591
    93
paulson@1591
    94
paulson@1591
    95
(* print theory *)
paulson@1591
    96
paulson@1591
    97
val pprint_theory = Sign.pprint_sg o sign_of;
paulson@1591
    98
paulson@1591
    99
val print_syntax = Syntax.print_syntax o syn_of;
paulson@1591
   100
paulson@1591
   101
fun print_axioms thy =
paulson@1591
   102
  let
paulson@1591
   103
    val {sign, new_axioms, ...} = rep_theory thy;
paulson@1591
   104
    val axioms = Symtab.dest new_axioms;
paulson@1591
   105
paulson@1591
   106
    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
paulson@1591
   107
      Pretty.quote (Sign.pretty_term sign t)];
paulson@1591
   108
  in
paulson@1591
   109
    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
paulson@1591
   110
  end;
paulson@1591
   111
paulson@1591
   112
fun print_theory thy = (Sign.print_sg (sign_of thy); print_axioms thy);
paulson@1591
   113
paulson@1591
   114
paulson@1591
   115
paulson@1591
   116
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
paulson@1591
   117
paulson@1591
   118
(* get type_env, sort_env of term *)
paulson@1591
   119
paulson@1591
   120
local
paulson@1591
   121
  open Syntax;
paulson@1591
   122
paulson@1591
   123
  fun ins_entry (x, y) [] = [(x, [y])]
paulson@1591
   124
    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
paulson@2180
   125
        if x = x' then (x', y ins_string ys') :: pairs
paulson@1591
   126
        else pair :: ins_entry (x, y) pairs;
paulson@1591
   127
paulson@1591
   128
  fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
paulson@1591
   129
    | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
paulson@1591
   130
    | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
paulson@1591
   131
    | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
paulson@1591
   132
    | add_type_env (_, env) = env;
paulson@1591
   133
paulson@1591
   134
  fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
paulson@1591
   135
    | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
paulson@1591
   136
    | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
paulson@1591
   137
paulson@2226
   138
  fun sort l = map (apsnd sort_strings) l;
paulson@1591
   139
in
paulson@1591
   140
  fun type_env t = sort (add_type_env (t, []));
paulson@1591
   141
  fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
paulson@1591
   142
end;
paulson@1591
   143
paulson@1591
   144
paulson@1591
   145
(* print_goals *)
paulson@1591
   146
paulson@1591
   147
fun print_goals maxgoals state =
paulson@1591
   148
  let
paulson@1591
   149
    open Syntax;
paulson@1591
   150
paulson@1591
   151
    val {sign, prop, ...} = rep_thm state;
paulson@1591
   152
paulson@1591
   153
    val pretty_term = Sign.pretty_term sign;
paulson@1591
   154
    val pretty_typ = Sign.pretty_typ sign;
paulson@1591
   155
    val pretty_sort = Sign.pretty_sort;
paulson@1591
   156
paulson@1591
   157
    fun pretty_vars prtf (X, vs) = Pretty.block
paulson@1591
   158
      [Pretty.block (Pretty.commas (map Pretty.str vs)),
paulson@1591
   159
        Pretty.str " ::", Pretty.brk 1, prtf X];
paulson@1591
   160
paulson@1591
   161
    fun print_list _ _ [] = ()
paulson@1591
   162
      | print_list name prtf lst =
paulson@1591
   163
          (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
paulson@1591
   164
paulson@1591
   165
paulson@1591
   166
    fun print_goals (_, []) = ()
paulson@1591
   167
      | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
paulson@1591
   168
          [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
paulson@1591
   169
            print_goals (n + 1, As));
paulson@1591
   170
paulson@1591
   171
    val print_ffpairs =
paulson@1591
   172
      print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
paulson@1591
   173
paulson@1591
   174
    val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
paulson@1591
   175
    val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
paulson@1591
   176
paulson@1591
   177
paulson@1591
   178
    val (tpairs, As, B) = Logic.strip_horn prop;
paulson@1591
   179
    val ngoals = length As;
paulson@1591
   180
paulson@1591
   181
    val orig_no_freeTs = ! show_no_free_types;
wenzelm@2992
   182
    val orig_types = ! show_types;
paulson@1591
   183
    val orig_sorts = ! show_sorts;
paulson@1591
   184
paulson@1591
   185
    fun restore () =
wenzelm@2992
   186
      (show_no_free_types := orig_no_freeTs;
wenzelm@2992
   187
        show_types := orig_types;
wenzelm@2992
   188
        show_sorts := orig_sorts);
paulson@1591
   189
  in
wenzelm@2992
   190
    (show_no_free_types := true;
wenzelm@2992
   191
      show_types := (orig_types orelse orig_sorts);
wenzelm@2992
   192
      show_sorts := false;
paulson@1591
   193
      Pretty.writeln (pretty_term B);
paulson@1591
   194
      if ngoals = 0 then writeln "No subgoals!"
paulson@1591
   195
      else if ngoals > maxgoals then
paulson@1591
   196
        (print_goals (1, take (maxgoals, As));
paulson@1591
   197
          writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
paulson@1591
   198
      else print_goals (1, As);
paulson@1591
   199
paulson@1591
   200
      print_ffpairs tpairs;
paulson@1591
   201
paulson@1591
   202
      if orig_sorts then
paulson@1591
   203
        (print_types prop; print_sorts prop)
wenzelm@2992
   204
      else if orig_types then
paulson@1591
   205
        print_types prop
paulson@1591
   206
      else ())
paulson@1591
   207
    handle exn => (restore (); raise exn);
paulson@1591
   208
    restore ()
paulson@1591
   209
  end;
paulson@1591
   210
paulson@1591
   211
wenzelm@3531
   212
(*"hook" for user interfaces: allows print_goals to be replaced*)	(* FIXME remove!? *)
paulson@1591
   213
val print_goals_ref = ref print_goals;
paulson@1591
   214
paulson@1591
   215
end;
paulson@1591
   216
paulson@1591
   217
open Display;