src/Pure/display.ML
author paulson
Wed Mar 20 18:39:59 1996 +0100 (1996-03-20)
changeset 1591 7fa0265dcd13
child 2180 934572a94139
permissions -rw-r--r--
New module for display/printing operations, taken from drule.ML
     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_thm	: thm -> Pretty.T
    16   val print_cterm	: cterm -> unit
    17   val print_ctyp	: ctyp -> unit
    18   val print_goals	: int -> thm -> unit
    19   val print_goals_ref	: (int -> thm -> unit) ref
    20   val print_syntax	: theory -> unit
    21   val print_theory	: theory -> unit
    22   val print_thm		: thm -> unit
    23   val prth		: thm -> thm
    24   val prthq		: thm Sequence.seq -> thm Sequence.seq
    25   val prths		: thm list -> thm list
    26   val show_hyps		: bool ref
    27   val string_of_cterm	: cterm -> string
    28   val string_of_ctyp	: ctyp -> string
    29   val string_of_thm	: thm -> string
    30   end;
    31 
    32 
    33 structure Display : DISPLAY =
    34 struct
    35 
    36 (*If false, hypotheses are printed as dots*)
    37 val show_hyps = ref true;
    38 
    39 fun pretty_thm th =
    40   let
    41     val {sign, hyps, prop, ...} = rep_thm th;
    42     val xshyps = extra_shyps th;
    43     val hlen = length xshyps + length hyps;
    44     val hsymbs =
    45       if hlen = 0 then []
    46       else if ! show_hyps then
    47         [Pretty.brk 2, Pretty.list "[" "]"
    48           (map (Sign.pretty_term sign) hyps @ map Sign.pretty_sort xshyps)]
    49       else
    50         [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
    51   in
    52     Pretty.block (Sign.pretty_term sign prop :: hsymbs)
    53   end;
    54 
    55 val string_of_thm = Pretty.string_of o pretty_thm;
    56 val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
    57 
    58 
    59 (** Top-level commands for printing theorems **)
    60 val print_thm = writeln o string_of_thm;
    61 
    62 fun prth th = (print_thm th; th);
    63 
    64 (*Print and return a sequence of theorems, separated by blank lines. *)
    65 fun prthq thseq =
    66   (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
    67 
    68 (*Print and return a list of theorems, separated by blank lines. *)
    69 fun prths ths = (print_list_ln print_thm ths; ths);
    70 
    71 
    72 (* other printing commands *)
    73 
    74 fun pprint_ctyp cT =
    75   let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
    76 
    77 fun string_of_ctyp cT =
    78   let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
    79 
    80 val print_ctyp = writeln o string_of_ctyp;
    81 
    82 fun pprint_cterm ct =
    83   let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
    84 
    85 fun string_of_cterm ct =
    86   let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
    87 
    88 val print_cterm = writeln o string_of_cterm;
    89 
    90 
    91 (* print theory *)
    92 
    93 val pprint_theory = Sign.pprint_sg o sign_of;
    94 
    95 val print_syntax = Syntax.print_syntax o syn_of;
    96 
    97 fun print_axioms thy =
    98   let
    99     val {sign, new_axioms, ...} = rep_theory thy;
   100     val axioms = Symtab.dest new_axioms;
   101 
   102     fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
   103       Pretty.quote (Sign.pretty_term sign t)];
   104   in
   105     Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
   106   end;
   107 
   108 fun print_theory thy = (Sign.print_sg (sign_of thy); print_axioms thy);
   109 
   110 
   111 
   112 (** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
   113 
   114 (* get type_env, sort_env of term *)
   115 
   116 local
   117   open Syntax;
   118 
   119   fun ins_entry (x, y) [] = [(x, [y])]
   120     | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   121         if x = x' then (x', y ins ys') :: pairs
   122         else pair :: ins_entry (x, y) pairs;
   123 
   124   fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
   125     | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
   126     | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
   127     | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
   128     | add_type_env (_, env) = env;
   129 
   130   fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
   131     | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
   132     | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
   133 
   134   val sort = map (apsnd sort_strings);
   135 in
   136   fun type_env t = sort (add_type_env (t, []));
   137   fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
   138 end;
   139 
   140 
   141 (* print_goals *)
   142 
   143 fun print_goals maxgoals state =
   144   let
   145     open Syntax;
   146 
   147     val {sign, prop, ...} = rep_thm state;
   148 
   149     val pretty_term = Sign.pretty_term sign;
   150     val pretty_typ = Sign.pretty_typ sign;
   151     val pretty_sort = Sign.pretty_sort;
   152 
   153     fun pretty_vars prtf (X, vs) = Pretty.block
   154       [Pretty.block (Pretty.commas (map Pretty.str vs)),
   155         Pretty.str " ::", Pretty.brk 1, prtf X];
   156 
   157     fun print_list _ _ [] = ()
   158       | print_list name prtf lst =
   159           (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
   160 
   161 
   162     fun print_goals (_, []) = ()
   163       | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
   164           [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
   165             print_goals (n + 1, As));
   166 
   167     val print_ffpairs =
   168       print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
   169 
   170     val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
   171     val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
   172 
   173 
   174     val (tpairs, As, B) = Logic.strip_horn prop;
   175     val ngoals = length As;
   176 
   177     val orig_no_freeTs = ! show_no_free_types;
   178     val orig_sorts = ! show_sorts;
   179 
   180     fun restore () =
   181       (show_no_free_types := orig_no_freeTs; show_sorts := orig_sorts);
   182   in
   183     (show_no_free_types := true; show_sorts := false;
   184       Pretty.writeln (pretty_term B);
   185       if ngoals = 0 then writeln "No subgoals!"
   186       else if ngoals > maxgoals then
   187         (print_goals (1, take (maxgoals, As));
   188           writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   189       else print_goals (1, As);
   190 
   191       print_ffpairs tpairs;
   192 
   193       if orig_sorts then
   194         (print_types prop; print_sorts prop)
   195       else if ! show_types then
   196         print_types prop
   197       else ())
   198     handle exn => (restore (); raise exn);
   199     restore ()
   200   end;
   201 
   202 
   203 (*"hook" for user interfaces: allows print_goals to be replaced*)
   204 val print_goals_ref = ref print_goals;
   205 
   206 end;
   207 
   208 open Display;