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