src/Pure/display.ML
author wenzelm
Sun Jul 08 19:52:05 2007 +0200 (2007-07-08)
changeset 23657 2332c79f4dc8
parent 23634 55e579ef85aa
child 24665 e5bea50b9b89
permissions -rw-r--r--
thm tag: Markup.property list;
     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, defs, oracles} = Theory.rep_theory thy;
   202     val {restricts, reducts} = Defs.dest defs;
   203     val {naming, syn = _, tsig, consts} = Sign.rep_sg thy;
   204     val {constants, constraints} = Consts.dest consts;
   205     val extern_const = NameSpace.extern (#1 constants);
   206     val {classes, default, types, witness, ...} = Type.rep_tsig tsig;
   207     val (class_space, class_algebra) = classes;
   208     val {classes, arities} = Sorts.rep_algebra class_algebra;
   209 
   210     fun prune xs = if verbose then xs else filter_out (can Name.dest_internal o fst) xs;
   211     fun prune' xs = if verbose then xs else filter_out (can Name.dest_internal o fst o fst) xs;
   212     fun dest_table tab = prune (NameSpace.dest_table tab);
   213     fun extern_table tab = prune (NameSpace.extern_table tab);
   214 
   215     val clsses = dest_table (class_space, Symtab.make (Graph.dest classes));
   216     val tdecls = dest_table types;
   217     val arties = dest_table (Sign.type_space thy, arities);
   218     val cnsts = extern_table constants;
   219     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   220     val abbrevs = map_filter (fn (c, (ty, SOME (t, _))) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   221     val cnstrs = extern_table constraints;
   222     val axms = extern_table axioms;
   223     val oras = extern_table oracles;
   224 
   225     val (reds0, (reds1, reds2)) = prune' reducts |> map (fn (lhs, rhs) =>
   226         (apfst extern_const lhs, map (apfst extern_const) (prune rhs)))
   227       |> sort_wrt (#1 o #1)
   228       |> List.partition (null o #2)
   229       ||> List.partition (Defs.plain_args o #2 o #1);
   230     val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
   231   in
   232     [Pretty.strs ("names:" :: Context.names_of thy)] @
   233     [Pretty.strs ["name prefix:", NameSpace.path_of naming],
   234       Pretty.big_list "classes:" (map pretty_classrel clsses),
   235       pretty_default default,
   236       pretty_witness witness,
   237       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   238       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   239       Pretty.big_list "type arities:" (pretty_arities arties),
   240       Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
   241       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   242       Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
   243       Pretty.big_list "axioms:" (map pretty_axm axms),
   244       Pretty.strs ("oracles:" :: (map #1 oras)),
   245       Pretty.big_list "definitions:"
   246         [pretty_finals reds0,
   247          Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
   248          Pretty.big_list "overloaded:" (map pretty_reduct reds2),
   249          Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
   250   end;
   251 
   252 
   253 
   254 (** print_goals **)
   255 
   256 (* print_goals etc. *)
   257 
   258 val show_consts = ref false;  (*true: show consts with types in proof state output*)
   259 
   260 
   261 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   262 
   263 local
   264 
   265 fun ins_entry (x, y) =
   266   AList.default (op =) (x, []) #>
   267   AList.map_entry (op =) x (insert (op =) y);
   268 
   269 val add_consts = Term.fold_aterms
   270   (fn Const (c, T) => ins_entry (T, (c, T))
   271     | _ => I);
   272 
   273 val add_vars = Term.fold_aterms
   274   (fn Free (x, T) => ins_entry (T, (x, ~1))
   275     | Var (xi, T) => ins_entry (T, xi)
   276     | _ => I);
   277 
   278 val add_varsT = Term.fold_atyps
   279   (fn TFree (x, S) => ins_entry (S, (x, ~1))
   280     | TVar (xi, S) => ins_entry (S, xi)
   281     | _ => I);
   282 
   283 fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   284 fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   285 
   286 fun consts_of t = sort_cnsts (add_consts t []);
   287 fun vars_of t = sort_idxs (add_vars t []);
   288 fun varsT_of t = rev (sort_idxs (Term.fold_types add_varsT t []));
   289 
   290 in
   291 
   292 fun pretty_goals_aux pp markup (msg, main) maxgoals state =
   293   let
   294     fun prt_atoms prt prtT (X, xs) = Pretty.block
   295       [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   296         Pretty.brk 1, prtT X];
   297 
   298     fun prt_var (x, ~1) = Pretty.term pp (Syntax.free x)
   299       | prt_var xi = Pretty.term pp (Syntax.var xi);
   300 
   301     fun prt_varT (x, ~1) = Pretty.typ pp (TFree (x, []))
   302       | prt_varT xi = Pretty.typ pp (TVar (xi, []));
   303 
   304     val prt_consts = prt_atoms (Pretty.term pp o Const) (Pretty.typ pp);
   305     val prt_vars = prt_atoms prt_var (Pretty.typ pp);
   306     val prt_varsT = prt_atoms prt_varT (Pretty.sort pp);
   307 
   308 
   309     fun pretty_list _ _ [] = []
   310       | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
   311 
   312     fun pretty_subgoal (n, A) = Pretty.markup markup
   313       [Pretty.str (" " ^ string_of_int n ^ ". "), Pretty.term pp A];
   314     fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
   315 
   316     val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair pp);
   317 
   318     val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
   319     val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
   320     val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
   321 
   322 
   323     val {prop, tpairs, ...} = Thm.rep_thm state;
   324     val (As, B) = Logic.strip_horn prop;
   325     val ngoals = length As;
   326 
   327     fun pretty_gs (types, sorts) =
   328       (if main then [Pretty.term pp B] else []) @
   329        (if ngoals = 0 then [Pretty.str "No subgoals!"]
   330         else if ngoals > maxgoals then
   331           pretty_subgoals (Library.take (maxgoals, As)) @
   332           (if msg then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
   333            else [])
   334         else pretty_subgoals As) @
   335       pretty_ffpairs tpairs @
   336       (if ! show_consts then pretty_consts prop else []) @
   337       (if types then pretty_vars prop else []) @
   338       (if sorts then pretty_varsT prop else []);
   339   in
   340     setmp show_no_free_types true
   341       (setmp show_types (! show_types orelse ! show_sorts orelse ! show_all_types)
   342         (setmp show_sorts false pretty_gs))
   343    (! show_types orelse ! show_sorts orelse ! show_all_types, ! show_sorts)
   344   end;
   345 
   346 fun pretty_goals n th =
   347   pretty_goals_aux (Sign.pp (Thm.theory_of_thm th)) Markup.none (true, true) n th;
   348 
   349 val print_goals = (Pretty.writeln o Pretty.chunks) oo pretty_goals;
   350 
   351 end;
   352 
   353 
   354 end;
   355 
   356 structure BasicDisplay: BASIC_DISPLAY = Display;
   357 open BasicDisplay;