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