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