src/Pure/display.ML
author wenzelm
Fri Apr 03 14:36:20 1998 +0200 (1998-04-03)
changeset 4782 9c0b31da51c6
parent 4498 a088ec3e4f5e
child 4950 226f2cde9f4d
permissions -rw-r--r--
tuned comments;
     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 pretty_name_space : string * NameSpace.T -> Pretty.T
    23   val print_syntax	: theory -> unit
    24   val print_theory	: theory -> unit
    25   val print_data	: theory -> string -> unit
    26   val print_thm		: thm -> unit
    27   val prth		: thm -> thm
    28   val prthq		: thm Seq.seq -> thm Seq.seq
    29   val prths		: thm list -> thm list
    30   val show_hyps		: bool ref
    31   val string_of_cterm	: cterm -> string
    32   val string_of_ctyp	: ctyp -> string
    33   val string_of_thm	: thm -> string
    34   end;
    35 
    36 
    37 structure Display : DISPLAY =
    38 struct
    39 
    40 (*If false, hypotheses are printed as dots*)
    41 val show_hyps = ref true;
    42 
    43 fun pretty_thm th =
    44   let
    45     val {sign, hyps, prop, ...} = rep_thm th;
    46     val xshyps = extra_shyps th;
    47     val hlen = length xshyps + length hyps;
    48     val hsymbs =
    49       if hlen = 0 then []
    50       else if ! show_hyps then
    51         [Pretty.brk 2, Pretty.list "[" "]"
    52           (map (Sign.pretty_term sign) hyps @
    53            map (Sign.pretty_sort sign) xshyps)]
    54       else
    55         [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
    56   in
    57     Pretty.block (Sign.pretty_term sign prop :: hsymbs)
    58   end;
    59 
    60 val string_of_thm = Pretty.string_of o pretty_thm;
    61 val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
    62 
    63 
    64 (** Top-level commands for printing theorems **)
    65 val print_thm = writeln o string_of_thm;
    66 
    67 fun prth th = (print_thm th; th);
    68 
    69 (*Print and return a sequence of theorems, separated by blank lines. *)
    70 fun prthq thseq =
    71   (Seq.print (fn _ => print_thm) 100000 thseq; thseq);
    72 
    73 (*Print and return a list of theorems, separated by blank lines. *)
    74 fun prths ths = (seq (fn th => (print_thm th; writeln "")) ths; ths);
    75 
    76 
    77 (* other printing commands *)
    78 
    79 fun pretty_ctyp cT =
    80   let val {sign, T} = rep_ctyp cT in Sign.pretty_typ sign T end;
    81 
    82 fun pprint_ctyp cT =
    83   let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
    84 
    85 fun string_of_ctyp cT =
    86   let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
    87 
    88 val print_ctyp = writeln o string_of_ctyp;
    89 
    90 fun pretty_cterm ct =
    91   let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
    92 
    93 fun pprint_cterm ct =
    94   let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
    95 
    96 fun string_of_cterm ct =
    97   let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
    98 
    99 val print_cterm = writeln o string_of_cterm;
   100 
   101 
   102 
   103 (** print theory **)
   104 
   105 val pprint_theory = Sign.pprint_sg o sign_of;
   106 
   107 val print_syntax = Syntax.print_syntax o syn_of;
   108 val print_data = Sign.print_data o sign_of;
   109 
   110 
   111 (* pretty_name_space  *)
   112 
   113 fun pretty_name_space (kind, space) =
   114   let
   115     fun prt_entry (name, accs) = Pretty.block
   116       (Pretty.str (quote name ^ " =") :: Pretty.brk 1 ::
   117         Pretty.commas (map (Pretty.str o quote) accs));
   118   in
   119     Pretty.fbreaks (Pretty.str (kind ^ ":") :: map prt_entry (NameSpace.dest space))
   120     |> Pretty.block
   121   end;
   122 
   123 
   124 
   125 (* print signature *)
   126 
   127 fun print_sign sg =
   128   let
   129     fun prt_cls c = Sign.pretty_sort sg [c];
   130     fun prt_sort S = Sign.pretty_sort sg S;
   131     fun prt_arity t (c, Ss) = Sign.pretty_arity sg (t, Ss, [c]);
   132     fun prt_typ ty = Pretty.quote (Sign.pretty_typ sg ty);
   133 
   134     val ext_class = Sign.cond_extern sg Sign.classK;
   135     val ext_tycon = Sign.cond_extern sg Sign.typeK;
   136     val ext_const = Sign.cond_extern sg Sign.constK;
   137 
   138 
   139     fun pretty_classes cs = Pretty.block
   140       (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
   141 
   142     fun pretty_classrel (c, cs) = Pretty.block
   143       (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   144         Pretty.commas (map prt_cls cs));
   145 
   146     fun pretty_default S = Pretty.block
   147       [Pretty.str "default:", Pretty.brk 1, prt_sort S];
   148 
   149     fun pretty_ty (t, n) = Pretty.block
   150       [Pretty.str (ext_tycon t), Pretty.str (" " ^ string_of_int n)];
   151 
   152     fun pretty_abbr (t, (vs, rhs)) = Pretty.block
   153       [prt_typ (Type (t, map (fn v => TVar ((v, 0), [])) vs)),
   154         Pretty.str " =", Pretty.brk 1, prt_typ rhs];
   155 
   156     fun pretty_arities (t, ars) = map (prt_arity t) ars;
   157 
   158     fun pretty_const (c, ty) = Pretty.block
   159       [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
   160 
   161     val {self = _, tsig, const_tab, syn = _, path, spaces, data} = Sign.rep_sg sg;
   162     val spaces' = sort_wrt fst spaces;
   163     val {classes, classrel, default, tycons, abbrs, arities} =
   164       Type.rep_tsig tsig;
   165     val consts = sort_wrt fst (map (apfst ext_const) (Symtab.dest const_tab));
   166   in
   167     Pretty.writeln (Pretty.strs ("stamps:" :: Sign.stamp_names_of sg));
   168     Pretty.writeln (Pretty.strs ("data:" :: Sign.data_kinds data));
   169     Pretty.writeln (Pretty.strs ["name prefix:", NameSpace.pack path]);
   170     Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_name_space spaces'));
   171     Pretty.writeln (pretty_classes classes);
   172     Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
   173     Pretty.writeln (pretty_default default);
   174     Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
   175     Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
   176     Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
   177     Pretty.writeln (Pretty.big_list "consts:" (map pretty_const consts))
   178   end;
   179 
   180 
   181 (* print axioms, oracles, theorems *)
   182 
   183 fun print_thy thy =
   184   let
   185     val {sign, axioms, oracles, ...} = rep_theory thy;
   186     val axioms = Symtab.dest axioms;
   187     val oras = map fst (Symtab.dest oracles);
   188 
   189     fun prt_axm (a, t) = Pretty.block
   190       [Pretty.str (Sign.cond_extern sign Theory.axiomK a ^ ":"), Pretty.brk 1,
   191         Pretty.quote (Sign.pretty_term sign t)];
   192   in
   193     Pretty.writeln (Pretty.big_list "axioms:" (map prt_axm axioms));
   194     Pretty.writeln (Pretty.strs ("oracles:" :: oras));
   195     print_data thy "Pure/theorems"	(*forward reference!*)
   196   end;
   197 
   198 fun print_theory thy = (print_sign (sign_of thy); print_thy thy);
   199 
   200 
   201 
   202 (** print_goals **)
   203 
   204 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   205 
   206 (*also show consts in case of showing types?*)
   207 val show_consts = ref false;
   208 
   209 
   210 local
   211 
   212   (* utils *)
   213 
   214   fun ins_entry (x, y) [] = [(x, [y])]
   215     | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   216         if x = x' then (x', y ins ys') :: pairs
   217         else pair :: ins_entry (x, y) pairs;
   218 
   219   fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
   220     | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
   221     | add_consts (Abs (_, _, t), env) = add_consts (t, env)
   222     | add_consts (_, env) = env;
   223 
   224   fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
   225     | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
   226     | add_vars (Abs (_, _, t), env) = add_vars (t, env)
   227     | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
   228     | add_vars (_, env) = env;
   229 
   230   fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
   231     | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
   232     | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
   233 
   234   fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   235   fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   236 
   237 
   238   (* prepare atoms *)
   239 
   240   fun consts_of t = sort_cnsts (add_consts (t, []));
   241   fun vars_of t = sort_idxs (add_vars (t, []));
   242   fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
   243 
   244 in
   245 
   246   fun print_goals maxgoals state =
   247     let
   248       val {sign, ...} = rep_thm state;
   249 
   250       val prt_term = Sign.pretty_term sign;
   251       val prt_typ = Sign.pretty_typ sign;
   252       val prt_sort = Sign.pretty_sort sign;
   253 
   254       fun prt_atoms prt prtT (X, xs) = Pretty.block
   255         [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   256           Pretty.brk 1, prtT X];
   257 
   258       fun prt_var (x, ~1) = prt_term (Syntax.free x)
   259         | prt_var xi = prt_term (Syntax.var xi);
   260 
   261       fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
   262         | prt_varT xi = prt_typ (TVar (xi, []));
   263 
   264       val prt_consts = prt_atoms (prt_term o Const) prt_typ;
   265       val prt_vars = prt_atoms prt_var prt_typ;
   266       val prt_varsT = prt_atoms prt_varT prt_sort;
   267 
   268 
   269       fun print_list _ _ [] = ()
   270         | print_list name prt lst = (writeln "";
   271             Pretty.writeln (Pretty.big_list name (map prt lst)));
   272 
   273       fun print_subgoals (_, []) = ()
   274         | print_subgoals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
   275             [Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A]));
   276               print_subgoals (n + 1, As));
   277 
   278       val print_ffpairs =
   279         print_list "Flex-flex pairs:" (prt_term o Logic.mk_flexpair);
   280 
   281       val print_consts = print_list "Constants:" prt_consts o consts_of;
   282       val print_vars = print_list "Variables:" prt_vars o vars_of;
   283       val print_varsT = print_list "Type variables:" prt_varsT o varsT_of;
   284 
   285 
   286       val {prop, ...} = rep_thm state;
   287       val (tpairs, As, B) = Logic.strip_horn prop;
   288       val ngoals = length As;
   289 
   290       fun print_gs (types, sorts) =
   291        (Pretty.writeln (prt_term B);
   292         if ngoals = 0 then writeln "No subgoals!"
   293         else if ngoals > maxgoals then
   294           (print_subgoals (1, take (maxgoals, As));
   295             writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   296         else print_subgoals (1, As);
   297 
   298         print_ffpairs tpairs;
   299 
   300         if types andalso ! show_consts then print_consts prop else ();
   301         if types then print_vars prop else ();
   302         if sorts then print_varsT prop else ());
   303     in
   304       setmp show_no_free_types true
   305         (setmp show_types (! show_types orelse ! show_sorts)
   306           (setmp show_sorts false print_gs))
   307      (! show_types orelse ! show_sorts, ! show_sorts)
   308   end;
   309 
   310 end;
   311 
   312 
   313 end;
   314 
   315 open Display;