src/Pure/display.ML
author wenzelm
Tue May 23 13:55:02 2006 +0200 (2006-05-23)
changeset 19702 2ab12e94156f
parent 19698 f48cfaacd92c
child 19703 9c84266e1d5f
permissions -rw-r--r--
pretty_full_theory: tuned output of definitions;
     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 theorems, goals, results etc.
     7 *)
     8 
     9 signature BASIC_DISPLAY =
    10 sig
    11   val goals_limit: int ref
    12   val show_hyps: bool ref
    13   val show_tags: bool ref
    14   val string_of_thm: thm -> string
    15   val print_thm: thm -> unit
    16   val print_thms: thm list -> unit
    17   val prth: thm -> thm
    18   val prthq: thm Seq.seq -> thm Seq.seq
    19   val prths: thm list -> thm list
    20   val string_of_ctyp: ctyp -> string
    21   val print_ctyp: ctyp -> unit
    22   val string_of_cterm: cterm -> string
    23   val print_cterm: cterm -> unit
    24   val print_syntax: theory -> unit
    25   val show_consts: bool ref
    26 end;
    27 
    28 signature DISPLAY =
    29 sig
    30   include BASIC_DISPLAY
    31   val raw_string_of_sort: sort -> string
    32   val raw_string_of_typ: typ -> string
    33   val raw_string_of_term: term -> string
    34   val pretty_flexpair: Pretty.pp -> term * term -> Pretty.T
    35   val pretty_thm_aux: Pretty.pp -> bool -> bool -> term list -> thm -> Pretty.T
    36   val pretty_thm_no_quote: thm -> Pretty.T
    37   val pretty_thm: thm -> Pretty.T
    38   val pretty_thms: thm list -> Pretty.T
    39   val pretty_thm_sg: theory -> thm -> Pretty.T
    40   val pretty_thms_sg: theory -> thm list -> Pretty.T
    41   val pprint_thm: thm -> pprint_args -> unit
    42   val pretty_ctyp: ctyp -> Pretty.T
    43   val pprint_ctyp: ctyp -> pprint_args -> unit
    44   val pretty_cterm: cterm -> Pretty.T
    45   val pprint_cterm: cterm -> pprint_args -> unit
    46   val pretty_full_theory: theory -> Pretty.T list
    47   val pretty_goals_aux: Pretty.pp -> string -> bool * bool -> int -> thm -> Pretty.T list
    48   val pretty_goals: int -> thm -> Pretty.T list
    49   val print_goals: int -> thm -> unit
    50   val current_goals_markers: (string * string * string) ref
    51   val pretty_current_goals: int -> int -> thm -> Pretty.T list
    52   val print_current_goals_default: int -> int -> thm -> unit
    53   val print_current_goals_fn: (int -> int -> thm -> unit) ref
    54 end;
    55 
    56 structure Display: DISPLAY =
    57 struct
    58 
    59 (** raw output **)
    60 
    61 val raw_string_of_sort = Sign.string_of_sort ProtoPure.thy;
    62 val raw_string_of_typ = Sign.string_of_typ ProtoPure.thy;
    63 val raw_string_of_term = Sign.string_of_term ProtoPure.thy;
    64 
    65 
    66 
    67 (** print thm **)
    68 
    69 val goals_limit = ref 10;       (*max number of goals to print*)
    70 val show_hyps = ref false;      (*false: print meta-hypotheses as dots*)
    71 val show_tags = ref false;      (*false: suppress tags*)
    72 
    73 fun pretty_tag (name, args) = Pretty.strs (name :: map Library.quote args);
    74 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    75 
    76 fun pretty_flexpair pp (t, u) = Pretty.block
    77   [Pretty.term pp t, Pretty.str " =?=", Pretty.brk 1, Pretty.term pp u];
    78 
    79 fun pretty_thm_aux pp quote show_hyps' asms raw_th =
    80   let
    81     val th = Thm.strip_shyps raw_th;
    82     val {hyps, tpairs, prop, der = (ora, _), ...} = Thm.rep_thm th;
    83     val xshyps = Thm.extra_shyps th;
    84     val (_, tags) = Thm.get_name_tags th;
    85 
    86     val q = if quote then Pretty.quote else I;
    87     val prt_term = q o (Pretty.term pp);
    88 
    89     val hyps' = if ! show_hyps then hyps else subtract (op aconv) asms hyps;
    90     val ora' = ora andalso (! show_hyps orelse not (! quick_and_dirty));
    91     val hlen = length xshyps + length hyps' + length tpairs;
    92     val hsymbs =
    93       if hlen = 0 andalso not ora' then []
    94       else if ! show_hyps orelse show_hyps' then
    95         [Pretty.brk 2, Pretty.list "[" "]"
    96           (map (q o pretty_flexpair pp) tpairs @ map prt_term hyps' @
    97            map (Pretty.sort pp) xshyps @
    98             (if ora' then [Pretty.str "!"] else []))]
    99       else [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^
   100         (if ora' then "!" else "") ^ "]")];
   101     val tsymbs =
   102       if null tags orelse not (! show_tags) then []
   103       else [Pretty.brk 1, pretty_tags tags];
   104   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
   105 
   106 fun gen_pretty_thm quote th =
   107   pretty_thm_aux (Sign.pp (Thm.theory_of_thm th)) quote false [] th;
   108 
   109 val pretty_thm = gen_pretty_thm true;
   110 val pretty_thm_no_quote = gen_pretty_thm false;
   111 
   112 
   113 val string_of_thm = Pretty.string_of o pretty_thm;
   114 val pprint_thm = Pretty.pprint o pretty_thm;
   115 
   116 fun pretty_thms [th] = pretty_thm th
   117   | pretty_thms ths = Pretty.block (Pretty.fbreaks (map pretty_thm ths));
   118 
   119 val pretty_thm_sg = pretty_thm oo Thm.transfer;
   120 val pretty_thms_sg = pretty_thms oo (map o Thm.transfer);
   121 
   122 
   123 (* top-level commands for printing theorems *)
   124 
   125 val print_thm = Pretty.writeln o pretty_thm;
   126 val print_thms = Pretty.writeln o pretty_thms;
   127 
   128 fun prth th = (print_thm th; th);
   129 fun prthq thq = (Seq.print (K print_thm) 100000 thq; thq);
   130 fun prths ths = (prthq (Seq.of_list ths); ths);
   131 
   132 
   133 (* other printing commands *)
   134 
   135 fun pretty_ctyp cT =
   136   let val {thy, T, ...} = rep_ctyp cT in Sign.pretty_typ thy T end;
   137 
   138 fun pprint_ctyp cT =
   139   let val {thy, T, ...} = rep_ctyp cT in Sign.pprint_typ thy T end;
   140 
   141 fun string_of_ctyp cT =
   142   let val {thy, T, ...} = rep_ctyp cT in Sign.string_of_typ thy T end;
   143 
   144 val print_ctyp = writeln o string_of_ctyp;
   145 
   146 fun pretty_cterm ct =
   147   let val {thy, t, ...} = rep_cterm ct in Sign.pretty_term thy t end;
   148 
   149 fun pprint_cterm ct =
   150   let val {thy, t, ...} = rep_cterm ct in Sign.pprint_term thy t end;
   151 
   152 fun string_of_cterm ct =
   153   let val {thy, t, ...} = rep_cterm ct in Sign.string_of_term thy t end;
   154 
   155 val print_cterm = writeln o string_of_cterm;
   156 
   157 
   158 
   159 (** print theory **)
   160 
   161 val print_syntax = Syntax.print_syntax o Sign.syn_of;
   162 
   163 
   164 (* pretty_full_theory *)
   165 
   166 fun pretty_full_theory thy =
   167   let
   168     fun prt_cls c = Sign.pretty_sort thy [c];
   169     fun prt_sort S = Sign.pretty_sort thy S;
   170     fun prt_arity t (c, (_, Ss)) = Sign.pretty_arity thy (t, Ss, [c]);
   171     fun prt_typ ty = Pretty.quote (Sign.pretty_typ thy ty);
   172     val prt_typ_no_tvars = prt_typ o Type.freeze_type;
   173     fun prt_term t = Pretty.quote (Sign.pretty_term thy t);
   174     val prt_term_no_vars = prt_term o Logic.unvarify;
   175     fun prt_const (c, ty) = [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   176     val prt_const' = Defs.pretty_const (Sign.pp thy);
   177 
   178     fun pretty_classrel (c, []) = prt_cls c
   179       | pretty_classrel (c, cs) = Pretty.block
   180           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   181             Pretty.commas (map prt_cls cs));
   182 
   183     fun pretty_default S = Pretty.block
   184       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   185 
   186     fun pretty_witness NONE = Pretty.str "universal non-emptiness witness: -"
   187       | pretty_witness (SOME (T, S)) = Pretty.block
   188           [Pretty.str "universal non-emptiness witness:", Pretty.brk 1,
   189             prt_typ_no_tvars T, Pretty.str " ::", Pretty.brk 1, prt_sort S];
   190 
   191     val tfrees = map (fn v => TFree (v, []));
   192     fun pretty_type syn (t, (Type.LogicalType n, _)) =
   193           if syn then NONE
   194           else SOME (prt_typ (Type (t, tfrees (Term.invent_names [] "'a" n))))
   195       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'), _)) =
   196           if syn <> syn' then NONE
   197           else SOME (Pretty.block
   198             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   199       | pretty_type syn (t, (Type.Nonterminal, _)) =
   200           if not syn then NONE
   201           else SOME (prt_typ (Type (t, [])));
   202 
   203     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   204 
   205     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   206       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   207 
   208     fun pretty_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term_no_vars t];
   209 
   210     fun pretty_finals reds = Pretty.block
   211       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o #1) reds));
   212 
   213     fun pretty_reduct (lhs, rhs) = Pretty.block
   214       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   215         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   216 
   217     fun pretty_restrict (const, name) =
   218       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   219 
   220     val {axioms, defs, oracles} = Theory.rep_theory thy;
   221     val {restricts, reducts} = Defs.dest defs;
   222     val {naming, syn = _, tsig, consts} = Sign.rep_sg thy;
   223     val {constants, constraints} = Consts.dest consts;
   224     val extern_const = NameSpace.extern (#1 constants);
   225     val {classes, default, types, witness, ...} = Type.rep_tsig tsig;
   226     val (class_space, class_algebra) = classes;
   227     val {classes, arities} = Sorts.rep_algebra class_algebra;
   228 
   229     val clsses = NameSpace.dest_table (class_space, Symtab.make (Graph.dest classes));
   230     val tdecls = NameSpace.dest_table types;
   231     val arties = NameSpace.dest_table (Sign.type_space thy, arities);
   232     val cnsts = NameSpace.extern_table constants;
   233     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   234     val abbrevs = map_filter (fn (c, (ty, SOME t)) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   235     val cnstrs = NameSpace.extern_table constraints;
   236     val axms = NameSpace.extern_table axioms;
   237     val oras = NameSpace.extern_table oracles;
   238 
   239     val (reds0, (reds1, reds2)) = reducts |> map (fn (lhs, rhs) =>
   240         (apfst extern_const lhs, map (apfst extern_const) rhs))
   241       |> sort_wrt (#1 o #1)
   242       |> List.partition (null o #2)
   243       ||> List.partition (Defs.plain_args o #2 o #1);
   244     val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
   245   in
   246     [Pretty.strs ("names:" :: Context.names_of thy),
   247       Pretty.strs ("theory data:" :: Context.theory_data_of thy),
   248       Pretty.strs ("proof data:" :: Context.proof_data_of thy),
   249       Pretty.strs ["name prefix:", NameSpace.path_of naming],
   250       Pretty.big_list "classes:" (map pretty_classrel clsses),
   251       pretty_default default,
   252       pretty_witness witness,
   253       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   254       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   255       Pretty.big_list "type arities:" (pretty_arities arties),
   256       Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
   257       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   258       Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
   259       Pretty.big_list "axioms:" (map pretty_axm axms),
   260       Pretty.strs ("oracles:" :: (map #1 oras)),
   261       Pretty.big_list "definitions:"
   262         [pretty_finals reds0,
   263          Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
   264          Pretty.big_list "overloaded:" (map pretty_reduct reds2),
   265          Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
   266   end;
   267 
   268 
   269 
   270 (** print_goals **)
   271 
   272 (* print_goals etc. *)
   273 
   274 val show_consts = ref false;  (*true: show consts with types in proof state output*)
   275 
   276 
   277 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   278 
   279 local
   280 
   281 fun ins_entry (x, y) =
   282   AList.default (op =) (x, []) #>
   283   AList.map_entry (op =) x (insert (op =) y);
   284 
   285 val add_consts = Term.fold_aterms
   286   (fn Const (c, T) => ins_entry (T, (c, T))
   287     | _ => I);
   288 
   289 val add_vars = Term.fold_aterms
   290   (fn Free (x, T) => ins_entry (T, (x, ~1))
   291     | Var (xi, T) => ins_entry (T, xi)
   292     | _ => I);
   293 
   294 val add_varsT = Term.fold_atyps
   295   (fn TFree (x, S) => ins_entry (S, (x, ~1))
   296     | TVar (xi, S) => ins_entry (S, xi)
   297     | _ => I);
   298 
   299 fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   300 fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   301 
   302 fun consts_of t = sort_cnsts (add_consts t []);
   303 fun vars_of t = sort_idxs (add_vars t []);
   304 fun varsT_of t = rev (sort_idxs (Term.fold_types add_varsT t []));
   305 
   306 in
   307 
   308 fun pretty_goals_aux pp begin_goal (msg, main) maxgoals state =
   309   let
   310     fun prt_atoms prt prtT (X, xs) = Pretty.block
   311       [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   312         Pretty.brk 1, prtT X];
   313 
   314     fun prt_var (x, ~1) = Pretty.term pp (Syntax.free x)
   315       | prt_var xi = Pretty.term pp (Syntax.var xi);
   316 
   317     fun prt_varT (x, ~1) = Pretty.typ pp (TFree (x, []))
   318       | prt_varT xi = Pretty.typ pp (TVar (xi, []));
   319 
   320     val prt_consts = prt_atoms (Pretty.term pp o Const) (Pretty.typ pp);
   321     val prt_vars = prt_atoms prt_var (Pretty.typ pp);
   322     val prt_varsT = prt_atoms prt_varT (Pretty.sort pp);
   323 
   324 
   325     fun pretty_list _ _ [] = []
   326       | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
   327 
   328     fun pretty_subgoal (n, A) = Pretty.blk (0,
   329      [Pretty.str (begin_goal ^ " " ^ string_of_int n ^ ". "), Pretty.term pp A]);
   330     fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
   331 
   332     val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair pp);
   333 
   334     val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
   335     val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
   336     val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
   337 
   338 
   339     val {prop, tpairs, ...} = Thm.rep_thm state;
   340     val (As, B) = Logic.strip_horn prop;
   341     val ngoals = length As;
   342 
   343     fun pretty_gs (types, sorts) =
   344       (if main then [Pretty.term pp B] else []) @
   345        (if ngoals = 0 then [Pretty.str "No subgoals!"]
   346         else if ngoals > maxgoals then
   347           pretty_subgoals (Library.take (maxgoals, As)) @
   348           (if msg then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
   349            else [])
   350         else pretty_subgoals As) @
   351       pretty_ffpairs tpairs @
   352       (if ! show_consts then pretty_consts prop else []) @
   353       (if types then pretty_vars prop else []) @
   354       (if sorts then pretty_varsT prop else []);
   355   in
   356     setmp show_no_free_types true
   357       (setmp show_types (! show_types orelse ! show_sorts orelse ! show_all_types)
   358         (setmp show_sorts false pretty_gs))
   359    (! show_types orelse ! show_sorts orelse ! show_all_types, ! show_sorts)
   360   end;
   361 
   362 fun pretty_goals_marker bg n th =
   363   pretty_goals_aux (Sign.pp (Thm.theory_of_thm th)) bg (true, true) n th;
   364 
   365 val pretty_goals = pretty_goals_marker "";
   366 val print_goals = (Pretty.writeln o Pretty.chunks) oo pretty_goals_marker "";
   367 
   368 end;
   369 
   370 
   371 (* print_current_goals *)
   372 
   373 val current_goals_markers = ref ("", "", "");
   374 
   375 fun pretty_current_goals n m th =
   376   let
   377     val ref (begin_state, end_state, begin_goal) = current_goals_markers;
   378     val ngoals = nprems_of th;
   379   in
   380     (if begin_state = "" then [] else [Pretty.str begin_state]) @
   381     [Pretty.str ("Level " ^ string_of_int n ^
   382       (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
   383         (if ngoals <> 1 then "s" else "") ^ ")"
   384       else ""))] @
   385     pretty_goals_marker begin_goal m th @
   386     (if end_state = "" then [] else [Pretty.str end_state])
   387   end;
   388 
   389 fun print_current_goals_default n m th =
   390   Pretty.writeln (Pretty.chunks (pretty_current_goals n m th));
   391 
   392 val print_current_goals_fn = ref print_current_goals_default;
   393 
   394 end;
   395 
   396 structure BasicDisplay: BASIC_DISPLAY = Display;
   397 open BasicDisplay;