New module for display/printing operations, taken from drule.ML
authorpaulson
Wed Mar 20 18:39:59 1996 +0100 (1996-03-20)
changeset 15917fa0265dcd13
parent 1590 1547174673e1
child 1592 d89d5ff2397f
New module for display/printing operations, taken from drule.ML
src/Pure/display.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/display.ML	Wed Mar 20 18:39:59 1996 +0100
     1.3 @@ -0,0 +1,208 @@
     1.4 +(*  Title:      Pure/display.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1993  University of Cambridge
     1.8 +
     1.9 +Printing of theories, theorems, etc.
    1.10 +*)
    1.11 +
    1.12 +signature DISPLAY =
    1.13 +  sig
    1.14 +  val pprint_cterm	: cterm -> pprint_args -> unit
    1.15 +  val pprint_ctyp	: ctyp -> pprint_args -> unit
    1.16 +  val pprint_theory	: theory -> pprint_args -> unit
    1.17 +  val pprint_thm	: thm -> pprint_args -> unit
    1.18 +  val pretty_thm	: thm -> Pretty.T
    1.19 +  val print_cterm	: cterm -> unit
    1.20 +  val print_ctyp	: ctyp -> unit
    1.21 +  val print_goals	: int -> thm -> unit
    1.22 +  val print_goals_ref	: (int -> thm -> unit) ref
    1.23 +  val print_syntax	: theory -> unit
    1.24 +  val print_theory	: theory -> unit
    1.25 +  val print_thm		: thm -> unit
    1.26 +  val prth		: thm -> thm
    1.27 +  val prthq		: thm Sequence.seq -> thm Sequence.seq
    1.28 +  val prths		: thm list -> thm list
    1.29 +  val show_hyps		: bool ref
    1.30 +  val string_of_cterm	: cterm -> string
    1.31 +  val string_of_ctyp	: ctyp -> string
    1.32 +  val string_of_thm	: thm -> string
    1.33 +  end;
    1.34 +
    1.35 +
    1.36 +structure Display : DISPLAY =
    1.37 +struct
    1.38 +
    1.39 +(*If false, hypotheses are printed as dots*)
    1.40 +val show_hyps = ref true;
    1.41 +
    1.42 +fun pretty_thm th =
    1.43 +  let
    1.44 +    val {sign, hyps, prop, ...} = rep_thm th;
    1.45 +    val xshyps = extra_shyps th;
    1.46 +    val hlen = length xshyps + length hyps;
    1.47 +    val hsymbs =
    1.48 +      if hlen = 0 then []
    1.49 +      else if ! show_hyps then
    1.50 +        [Pretty.brk 2, Pretty.list "[" "]"
    1.51 +          (map (Sign.pretty_term sign) hyps @ map Sign.pretty_sort xshyps)]
    1.52 +      else
    1.53 +        [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
    1.54 +  in
    1.55 +    Pretty.block (Sign.pretty_term sign prop :: hsymbs)
    1.56 +  end;
    1.57 +
    1.58 +val string_of_thm = Pretty.string_of o pretty_thm;
    1.59 +val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
    1.60 +
    1.61 +
    1.62 +(** Top-level commands for printing theorems **)
    1.63 +val print_thm = writeln o string_of_thm;
    1.64 +
    1.65 +fun prth th = (print_thm th; th);
    1.66 +
    1.67 +(*Print and return a sequence of theorems, separated by blank lines. *)
    1.68 +fun prthq thseq =
    1.69 +  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
    1.70 +
    1.71 +(*Print and return a list of theorems, separated by blank lines. *)
    1.72 +fun prths ths = (print_list_ln print_thm ths; ths);
    1.73 +
    1.74 +
    1.75 +(* other printing commands *)
    1.76 +
    1.77 +fun pprint_ctyp cT =
    1.78 +  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
    1.79 +
    1.80 +fun string_of_ctyp cT =
    1.81 +  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
    1.82 +
    1.83 +val print_ctyp = writeln o string_of_ctyp;
    1.84 +
    1.85 +fun pprint_cterm ct =
    1.86 +  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
    1.87 +
    1.88 +fun string_of_cterm ct =
    1.89 +  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
    1.90 +
    1.91 +val print_cterm = writeln o string_of_cterm;
    1.92 +
    1.93 +
    1.94 +(* print theory *)
    1.95 +
    1.96 +val pprint_theory = Sign.pprint_sg o sign_of;
    1.97 +
    1.98 +val print_syntax = Syntax.print_syntax o syn_of;
    1.99 +
   1.100 +fun print_axioms thy =
   1.101 +  let
   1.102 +    val {sign, new_axioms, ...} = rep_theory thy;
   1.103 +    val axioms = Symtab.dest new_axioms;
   1.104 +
   1.105 +    fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
   1.106 +      Pretty.quote (Sign.pretty_term sign t)];
   1.107 +  in
   1.108 +    Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
   1.109 +  end;
   1.110 +
   1.111 +fun print_theory thy = (Sign.print_sg (sign_of thy); print_axioms thy);
   1.112 +
   1.113 +
   1.114 +
   1.115 +(** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
   1.116 +
   1.117 +(* get type_env, sort_env of term *)
   1.118 +
   1.119 +local
   1.120 +  open Syntax;
   1.121 +
   1.122 +  fun ins_entry (x, y) [] = [(x, [y])]
   1.123 +    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   1.124 +        if x = x' then (x', y ins ys') :: pairs
   1.125 +        else pair :: ins_entry (x, y) pairs;
   1.126 +
   1.127 +  fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
   1.128 +    | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
   1.129 +    | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
   1.130 +    | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
   1.131 +    | add_type_env (_, env) = env;
   1.132 +
   1.133 +  fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
   1.134 +    | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
   1.135 +    | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
   1.136 +
   1.137 +  val sort = map (apsnd sort_strings);
   1.138 +in
   1.139 +  fun type_env t = sort (add_type_env (t, []));
   1.140 +  fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
   1.141 +end;
   1.142 +
   1.143 +
   1.144 +(* print_goals *)
   1.145 +
   1.146 +fun print_goals maxgoals state =
   1.147 +  let
   1.148 +    open Syntax;
   1.149 +
   1.150 +    val {sign, prop, ...} = rep_thm state;
   1.151 +
   1.152 +    val pretty_term = Sign.pretty_term sign;
   1.153 +    val pretty_typ = Sign.pretty_typ sign;
   1.154 +    val pretty_sort = Sign.pretty_sort;
   1.155 +
   1.156 +    fun pretty_vars prtf (X, vs) = Pretty.block
   1.157 +      [Pretty.block (Pretty.commas (map Pretty.str vs)),
   1.158 +        Pretty.str " ::", Pretty.brk 1, prtf X];
   1.159 +
   1.160 +    fun print_list _ _ [] = ()
   1.161 +      | print_list name prtf lst =
   1.162 +          (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
   1.163 +
   1.164 +
   1.165 +    fun print_goals (_, []) = ()
   1.166 +      | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
   1.167 +          [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
   1.168 +            print_goals (n + 1, As));
   1.169 +
   1.170 +    val print_ffpairs =
   1.171 +      print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
   1.172 +
   1.173 +    val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
   1.174 +    val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
   1.175 +
   1.176 +
   1.177 +    val (tpairs, As, B) = Logic.strip_horn prop;
   1.178 +    val ngoals = length As;
   1.179 +
   1.180 +    val orig_no_freeTs = ! show_no_free_types;
   1.181 +    val orig_sorts = ! show_sorts;
   1.182 +
   1.183 +    fun restore () =
   1.184 +      (show_no_free_types := orig_no_freeTs; show_sorts := orig_sorts);
   1.185 +  in
   1.186 +    (show_no_free_types := true; show_sorts := false;
   1.187 +      Pretty.writeln (pretty_term B);
   1.188 +      if ngoals = 0 then writeln "No subgoals!"
   1.189 +      else if ngoals > maxgoals then
   1.190 +        (print_goals (1, take (maxgoals, As));
   1.191 +          writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   1.192 +      else print_goals (1, As);
   1.193 +
   1.194 +      print_ffpairs tpairs;
   1.195 +
   1.196 +      if orig_sorts then
   1.197 +        (print_types prop; print_sorts prop)
   1.198 +      else if ! show_types then
   1.199 +        print_types prop
   1.200 +      else ())
   1.201 +    handle exn => (restore (); raise exn);
   1.202 +    restore ()
   1.203 +  end;
   1.204 +
   1.205 +
   1.206 +(*"hook" for user interfaces: allows print_goals to be replaced*)
   1.207 +val print_goals_ref = ref print_goals;
   1.208 +
   1.209 +end;
   1.210 +
   1.211 +open Display;