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