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