src/Pure/display.ML
author wenzelm
Thu Oct 09 14:51:10 1997 +0200 (1997-10-09)
changeset 3811 ec27ddb5104c
parent 3785 0830d11b8916
child 3851 fe9932a7cd46
permissions -rw-r--r--
print_theory: added oracles;
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 "[" "]"
wenzelm@3785
    48
          (map (Sign.pretty_term sign) hyps @
wenzelm@3785
    49
           map (Sign.pretty_sort sign) 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
wenzelm@3811
   101
fun print_thy thy =
paulson@1591
   102
  let
wenzelm@3811
   103
    val {sign, new_axioms, oracles, ...} = rep_theory thy;
paulson@1591
   104
    val axioms = Symtab.dest new_axioms;
wenzelm@3811
   105
    val oras = map fst (Symtab.dest oracles);
paulson@1591
   106
paulson@1591
   107
    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
paulson@1591
   108
      Pretty.quote (Sign.pretty_term sign t)];
paulson@1591
   109
  in
wenzelm@3811
   110
    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms));
wenzelm@3811
   111
    Pretty.writeln (Pretty.strs ("oracles:" :: oras))
paulson@1591
   112
  end;
paulson@1591
   113
wenzelm@3811
   114
fun print_theory thy = (Sign.print_sg (sign_of thy); print_thy thy);
paulson@1591
   115
paulson@1591
   116
paulson@1591
   117
paulson@1591
   118
(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
paulson@1591
   119
paulson@1591
   120
(* get type_env, sort_env of term *)
paulson@1591
   121
paulson@1591
   122
local
paulson@1591
   123
  open Syntax;
paulson@1591
   124
paulson@1591
   125
  fun ins_entry (x, y) [] = [(x, [y])]
paulson@1591
   126
    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
paulson@2180
   127
        if x = x' then (x', y ins_string ys') :: pairs
paulson@1591
   128
        else pair :: ins_entry (x, y) pairs;
paulson@1591
   129
paulson@1591
   130
  fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
paulson@1591
   131
    | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
paulson@1591
   132
    | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
paulson@1591
   133
    | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
paulson@1591
   134
    | add_type_env (_, env) = env;
paulson@1591
   135
paulson@1591
   136
  fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
paulson@1591
   137
    | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
paulson@1591
   138
    | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
paulson@1591
   139
paulson@2226
   140
  fun sort l = map (apsnd sort_strings) l;
paulson@1591
   141
in
paulson@1591
   142
  fun type_env t = sort (add_type_env (t, []));
paulson@1591
   143
  fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
paulson@1591
   144
end;
paulson@1591
   145
paulson@1591
   146
paulson@1591
   147
(* print_goals *)
paulson@1591
   148
paulson@1591
   149
fun print_goals maxgoals state =
paulson@1591
   150
  let
paulson@1591
   151
    open Syntax;
paulson@1591
   152
paulson@1591
   153
    val {sign, prop, ...} = rep_thm state;
paulson@1591
   154
paulson@1591
   155
    val pretty_term = Sign.pretty_term sign;
paulson@1591
   156
    val pretty_typ = Sign.pretty_typ sign;
wenzelm@3785
   157
    val pretty_sort = Sign.pretty_sort sign;
paulson@1591
   158
paulson@1591
   159
    fun pretty_vars prtf (X, vs) = Pretty.block
paulson@1591
   160
      [Pretty.block (Pretty.commas (map Pretty.str vs)),
paulson@1591
   161
        Pretty.str " ::", Pretty.brk 1, prtf X];
paulson@1591
   162
paulson@1591
   163
    fun print_list _ _ [] = ()
paulson@1591
   164
      | print_list name prtf lst =
paulson@1591
   165
          (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
paulson@1591
   166
paulson@1591
   167
paulson@1591
   168
    fun print_goals (_, []) = ()
paulson@1591
   169
      | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
paulson@1591
   170
          [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
paulson@1591
   171
            print_goals (n + 1, As));
paulson@1591
   172
paulson@1591
   173
    val print_ffpairs =
paulson@1591
   174
      print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
paulson@1591
   175
paulson@1591
   176
    val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
paulson@1591
   177
    val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
paulson@1591
   178
paulson@1591
   179
paulson@1591
   180
    val (tpairs, As, B) = Logic.strip_horn prop;
paulson@1591
   181
    val ngoals = length As;
paulson@1591
   182
paulson@1591
   183
    val orig_no_freeTs = ! show_no_free_types;
wenzelm@2992
   184
    val orig_types = ! show_types;
paulson@1591
   185
    val orig_sorts = ! show_sorts;
paulson@1591
   186
paulson@1591
   187
    fun restore () =
wenzelm@2992
   188
      (show_no_free_types := orig_no_freeTs;
wenzelm@2992
   189
        show_types := orig_types;
wenzelm@2992
   190
        show_sorts := orig_sorts);
paulson@1591
   191
  in
wenzelm@2992
   192
    (show_no_free_types := true;
wenzelm@2992
   193
      show_types := (orig_types orelse orig_sorts);
wenzelm@2992
   194
      show_sorts := false;
paulson@1591
   195
      Pretty.writeln (pretty_term B);
paulson@1591
   196
      if ngoals = 0 then writeln "No subgoals!"
paulson@1591
   197
      else if ngoals > maxgoals then
paulson@1591
   198
        (print_goals (1, take (maxgoals, As));
paulson@1591
   199
          writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
paulson@1591
   200
      else print_goals (1, As);
paulson@1591
   201
paulson@1591
   202
      print_ffpairs tpairs;
paulson@1591
   203
paulson@1591
   204
      if orig_sorts then
paulson@1591
   205
        (print_types prop; print_sorts prop)
wenzelm@2992
   206
      else if orig_types then
paulson@1591
   207
        print_types prop
paulson@1591
   208
      else ())
paulson@1591
   209
    handle exn => (restore (); raise exn);
paulson@1591
   210
    restore ()
paulson@1591
   211
  end;
paulson@1591
   212
paulson@1591
   213
paulson@1591
   214
end;
paulson@1591
   215
paulson@1591
   216
open Display;