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