src/Pure/display.ML
author paulson
Thu Oct 16 10:22:37 1997 +0200 (1997-10-16)
changeset 3879 de18c0c1141c
parent 3873 64f496e0885d
child 3936 1954255c29ef
permissions -rw-r--r--
Eta-expansion of function declarations, for value polymorphism
     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 [Pretty.str (a ^ ":"), Pretty.brk 1,
   111       Pretty.quote (Sign.pretty_term sign t)];
   112   in
   113     Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms));
   114     Pretty.writeln (Pretty.strs ("oracles:" :: oras))
   115   end;
   116 
   117 fun print_theory thy = (Sign.print_sg (sign_of thy); print_thy thy);
   118 
   119 
   120 
   121 (** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
   122 
   123 (*show consts in case of showing types?*)
   124 val show_consts = ref false;
   125 
   126 
   127 local
   128 
   129   (* utils *)
   130 
   131   fun ins_entry (x, y) [] = [(x, [y])]
   132     | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   133         if x = x' then (x', y ins ys') :: pairs
   134         else pair :: ins_entry (x, y) pairs;
   135 
   136   fun add_consts (Const (c, T), env) = ins_entry (T, c) env
   137     | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
   138     | add_consts (Abs (_, _, t), env) = add_consts (t, env)
   139     | add_consts (_, env) = env;
   140 
   141   fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
   142     | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
   143     | add_vars (Abs (_, _, t), env) = add_vars (t, env)
   144     | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
   145     | add_vars (_, env) = env;
   146 
   147   fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
   148     | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
   149     | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
   150 
   151   fun less_idx ((x, i):indexname, (y, j):indexname) =
   152     x < y orelse x = y andalso i < j;
   153   fun sort_idxs l = map (apsnd (sort less_idx)) l;
   154   fun sort_strs l = map (apsnd sort_strings) l;
   155 
   156 
   157   (* prepare atoms *)
   158 
   159   fun consts_of t = sort_strs (add_consts (t, []));
   160   fun vars_of t = sort_idxs (add_vars (t, []));
   161   fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
   162 
   163 in
   164 
   165   (* print_goals *)
   166 
   167   fun print_goals maxgoals state =
   168     let
   169       val {sign, ...} = rep_thm state;
   170 
   171       val prt_term = Sign.pretty_term sign;
   172       val prt_typ = Sign.pretty_typ sign;
   173       val prt_sort = Sign.pretty_sort sign;
   174 
   175       fun prt_atoms prt prtT (X, xs) = Pretty.block
   176         [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   177           Pretty.brk 1, prtT X];
   178 
   179       fun prt_var (x, ~1) = prt_term (Syntax.free x)
   180         | prt_var xi = prt_term (Syntax.var xi);
   181 
   182       fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
   183         | prt_varT xi = prt_typ (TVar (xi, []));
   184 
   185       val prt_consts = prt_atoms (prt_term o Syntax.const) prt_typ;
   186       val prt_vars = prt_atoms prt_var prt_typ;
   187       val prt_varsT = prt_atoms prt_varT prt_sort;
   188 
   189 
   190       fun print_list _ _ [] = ()
   191         | print_list name prt lst = (writeln "";
   192             Pretty.writeln (Pretty.big_list name (map prt lst)));
   193 
   194       fun print_subgoals (_, []) = ()
   195         | print_subgoals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
   196             [Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A]));
   197               print_subgoals (n + 1, As));
   198 
   199       val print_ffpairs =
   200         print_list "Flex-flex pairs:" (prt_term o Logic.mk_flexpair);
   201 
   202       val print_consts = print_list "Constants:" prt_consts o consts_of;
   203       val print_vars = print_list "Variables:" prt_vars o vars_of;
   204       val print_varsT = print_list "Type variables:" prt_varsT o varsT_of;
   205 
   206 
   207       val {prop, ...} = rep_thm state;
   208       val (tpairs, As, B) = Logic.strip_horn prop;
   209       val ngoals = length As;
   210 
   211       fun print_gs (types, sorts) =
   212        (Pretty.writeln (prt_term B);
   213         if ngoals = 0 then writeln "No subgoals!"
   214         else if ngoals > maxgoals then
   215           (print_subgoals (1, take (maxgoals, As));
   216             writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   217         else print_subgoals (1, As);
   218 
   219         print_ffpairs tpairs;
   220 
   221         if types andalso ! show_consts then print_consts prop else ();
   222         if types then print_vars prop else ();
   223         if sorts then print_varsT prop else ());
   224     in
   225       setmp show_no_free_types true
   226         (setmp show_types (! show_types orelse ! show_sorts)
   227           (setmp show_sorts false print_gs))
   228      (! show_types orelse ! show_sorts, ! show_sorts)
   229   end;
   230 
   231 end;
   232 
   233 
   234 end;
   235 
   236 open Display;