src/Pure/display.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3669 3384c6f1f095
child 3785 0830d11b8916
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
     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 print_goals	: int -> thm -> unit
    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 pretty_cterm ct =
    83   let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
    84 
    85 fun pprint_cterm ct =
    86   let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
    87 
    88 fun string_of_cterm ct =
    89   let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
    90 
    91 val print_cterm = writeln o string_of_cterm;
    92 
    93 
    94 (* print theory *)
    95 
    96 val pprint_theory = Sign.pprint_sg o sign_of;
    97 
    98 val print_syntax = Syntax.print_syntax o syn_of;
    99 
   100 fun print_axioms thy =
   101   let
   102     val {sign, new_axioms, ...} = rep_theory thy;
   103     val axioms = Symtab.dest new_axioms;
   104 
   105     fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
   106       Pretty.quote (Sign.pretty_term sign t)];
   107   in
   108     Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
   109   end;
   110 
   111 fun print_theory thy = (Sign.print_sg (sign_of thy); print_axioms thy);
   112 
   113 
   114 
   115 (** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
   116 
   117 (* get type_env, sort_env of term *)
   118 
   119 local
   120   open Syntax;
   121 
   122   fun ins_entry (x, y) [] = [(x, [y])]
   123     | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   124         if x = x' then (x', y ins_string ys') :: pairs
   125         else pair :: ins_entry (x, y) pairs;
   126 
   127   fun add_type_env (Free (x, T), env) = ins_entry (T, x) env
   128     | add_type_env (Var (xi, T), env) = ins_entry (T, string_of_vname xi) env
   129     | add_type_env (Abs (_, _, t), env) = add_type_env (t, env)
   130     | add_type_env (t $ u, env) = add_type_env (u, add_type_env (t, env))
   131     | add_type_env (_, env) = env;
   132 
   133   fun add_sort_env (Type (_, Ts), env) = foldr add_sort_env (Ts, env)
   134     | add_sort_env (TFree (x, S), env) = ins_entry (S, x) env
   135     | add_sort_env (TVar (xi, S), env) = ins_entry (S, string_of_vname xi) env;
   136 
   137   fun sort l = map (apsnd sort_strings) l;
   138 in
   139   fun type_env t = sort (add_type_env (t, []));
   140   fun sort_env t = rev (sort (it_term_types add_sort_env (t, [])));
   141 end;
   142 
   143 
   144 (* print_goals *)
   145 
   146 fun print_goals maxgoals state =
   147   let
   148     open Syntax;
   149 
   150     val {sign, prop, ...} = rep_thm state;
   151 
   152     val pretty_term = Sign.pretty_term sign;
   153     val pretty_typ = Sign.pretty_typ sign;
   154     val pretty_sort = Sign.pretty_sort;
   155 
   156     fun pretty_vars prtf (X, vs) = Pretty.block
   157       [Pretty.block (Pretty.commas (map Pretty.str vs)),
   158         Pretty.str " ::", Pretty.brk 1, prtf X];
   159 
   160     fun print_list _ _ [] = ()
   161       | print_list name prtf lst =
   162           (writeln ""; Pretty.writeln (Pretty.big_list name (map prtf lst)));
   163 
   164 
   165     fun print_goals (_, []) = ()
   166       | print_goals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
   167           [Pretty.str (" " ^ string_of_int n ^ ". "), pretty_term A]));
   168             print_goals (n + 1, As));
   169 
   170     val print_ffpairs =
   171       print_list "Flex-flex pairs:" (pretty_term o Logic.mk_flexpair);
   172 
   173     val print_types = print_list "Types:" (pretty_vars pretty_typ) o type_env;
   174     val print_sorts = print_list "Sorts:" (pretty_vars pretty_sort) o sort_env;
   175 
   176 
   177     val (tpairs, As, B) = Logic.strip_horn prop;
   178     val ngoals = length As;
   179 
   180     val orig_no_freeTs = ! show_no_free_types;
   181     val orig_types = ! show_types;
   182     val orig_sorts = ! show_sorts;
   183 
   184     fun restore () =
   185       (show_no_free_types := orig_no_freeTs;
   186         show_types := orig_types;
   187         show_sorts := orig_sorts);
   188   in
   189     (show_no_free_types := true;
   190       show_types := (orig_types orelse orig_sorts);
   191       show_sorts := false;
   192       Pretty.writeln (pretty_term B);
   193       if ngoals = 0 then writeln "No subgoals!"
   194       else if ngoals > maxgoals then
   195         (print_goals (1, take (maxgoals, As));
   196           writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   197       else print_goals (1, As);
   198 
   199       print_ffpairs tpairs;
   200 
   201       if orig_sorts then
   202         (print_types prop; print_sorts prop)
   203       else if orig_types then
   204         print_types prop
   205       else ())
   206     handle exn => (restore (); raise exn);
   207     restore ()
   208   end;
   209 
   210 
   211 end;
   212 
   213 open Display;