src/Pure/display.ML
author wenzelm
Mon May 07 00:49:59 2007 +0200 (2007-05-07)
changeset 22846 fb79144af9a3
parent 21721 908a93216f00
child 22878 ca2eb5eb615b
permissions -rw-r--r--
simplified DataFun interfaces;
     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 -> string -> bool * bool -> int -> thm -> Pretty.T list
    44   val pretty_goals: int -> thm -> Pretty.T list
    45   val print_goals: int -> thm -> unit
    46   val current_goals_markers: (string * string * string) ref
    47   val pretty_current_goals: int -> int -> thm -> Pretty.T list
    48   val print_current_goals_default: int -> int -> thm -> unit
    49   val print_current_goals_fn: (int -> int -> thm -> unit) ref
    50 end;
    51 
    52 structure Display: DISPLAY =
    53 struct
    54 
    55 (** raw output **)
    56 
    57 val raw_string_of_sort = Sign.string_of_sort ProtoPure.thy;
    58 val raw_string_of_typ = Sign.string_of_typ ProtoPure.thy;
    59 val raw_string_of_term = Sign.string_of_term ProtoPure.thy;
    60 
    61 
    62 
    63 (** print thm **)
    64 
    65 val goals_limit = ref 10;       (*max number of goals to print*)
    66 val show_hyps = ref false;      (*false: print meta-hypotheses as dots*)
    67 val show_tags = ref false;      (*false: suppress tags*)
    68 
    69 fun pretty_tag (name, args) = Pretty.strs (name :: map Library.quote args);
    70 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    71 
    72 fun pretty_flexpair pp (t, u) = Pretty.block
    73   [Pretty.term pp t, Pretty.str " =?=", Pretty.brk 1, Pretty.term pp u];
    74 
    75 fun pretty_thm_aux pp quote show_hyps' asms raw_th =
    76   let
    77     val th = Thm.strip_shyps raw_th;
    78     val {hyps, tpairs, prop, der = (ora, _), ...} = Thm.rep_thm th;
    79     val xshyps = Thm.extra_shyps th;
    80     val tags = Thm.get_tags th;
    81 
    82     val q = if quote then Pretty.quote else I;
    83     val prt_term = q o (Pretty.term pp);
    84 
    85     val hyps' = if ! show_hyps then hyps else subtract (op aconv) asms hyps;
    86     val ora' = ora andalso (! show_hyps orelse not (! quick_and_dirty));
    87     val hlen = length xshyps + length hyps' + length tpairs;
    88     val hsymbs =
    89       if hlen = 0 andalso not ora' then []
    90       else if ! show_hyps orelse show_hyps' then
    91         [Pretty.brk 2, Pretty.list "[" "]"
    92           (map (q o pretty_flexpair pp) tpairs @ map prt_term hyps' @
    93            map (Pretty.sort pp) xshyps @
    94             (if ora' then [Pretty.str "!"] else []))]
    95       else [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^
    96         (if ora' then "!" else "") ^ "]")];
    97     val tsymbs =
    98       if null tags orelse not (! show_tags) then []
    99       else [Pretty.brk 1, pretty_tags tags];
   100   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
   101 
   102 fun pretty_thm th =
   103   pretty_thm_aux (Sign.pp (Thm.theory_of_thm th)) true false [] th;
   104 
   105 val string_of_thm = Pretty.string_of o pretty_thm;
   106 
   107 fun pretty_thms [th] = pretty_thm th
   108   | pretty_thms ths = Pretty.block (Pretty.fbreaks (map pretty_thm ths));
   109 
   110 val pretty_thm_sg = pretty_thm oo Thm.transfer;
   111 val pretty_thms_sg = pretty_thms oo (map o Thm.transfer);
   112 
   113 
   114 (* top-level commands for printing theorems *)
   115 
   116 val print_thm = Pretty.writeln o pretty_thm;
   117 val print_thms = Pretty.writeln o pretty_thms;
   118 
   119 fun prth th = (print_thm th; th);
   120 fun prthq thq = (Seq.print (K print_thm) 100000 thq; thq);
   121 fun prths ths = (prthq (Seq.of_list ths); ths);
   122 
   123 
   124 (* other printing commands *)
   125 
   126 fun pretty_ctyp cT =
   127   let val {thy, T, ...} = rep_ctyp cT in Sign.pretty_typ thy T end;
   128 
   129 fun string_of_ctyp cT =
   130   let val {thy, T, ...} = rep_ctyp cT in Sign.string_of_typ thy T end;
   131 
   132 val print_ctyp = writeln o string_of_ctyp;
   133 
   134 fun pretty_cterm ct =
   135   let val {thy, t, ...} = rep_cterm ct in Sign.pretty_term thy t end;
   136 
   137 fun string_of_cterm ct =
   138   let val {thy, t, ...} = rep_cterm ct in Sign.string_of_term thy t end;
   139 
   140 val print_cterm = writeln o string_of_cterm;
   141 
   142 
   143 
   144 (** print theory **)
   145 
   146 val print_syntax = Syntax.print_syntax o Sign.syn_of;
   147 
   148 
   149 (* pretty_full_theory *)
   150 
   151 fun pretty_full_theory verbose thy =
   152   let
   153     fun prt_cls c = Sign.pretty_sort thy [c];
   154     fun prt_sort S = Sign.pretty_sort thy S;
   155     fun prt_arity t (c, (_, Ss)) = Sign.pretty_arity thy (t, Ss, [c]);
   156     fun prt_typ ty = Pretty.quote (Sign.pretty_typ thy ty);
   157     val prt_typ_no_tvars = prt_typ o Logic.unvarifyT;
   158     fun prt_term t = Pretty.quote (Sign.pretty_term thy t);
   159     val prt_term_no_vars = prt_term o Logic.unvarify;
   160     fun prt_const (c, ty) = [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   161     val prt_const' = Defs.pretty_const (Sign.pp thy);
   162 
   163     fun pretty_classrel (c, []) = prt_cls c
   164       | pretty_classrel (c, cs) = Pretty.block
   165           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   166             Pretty.commas (map prt_cls cs));
   167 
   168     fun pretty_default S = Pretty.block
   169       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   170 
   171     fun pretty_witness NONE = Pretty.str "universal non-emptiness witness: -"
   172       | pretty_witness (SOME (T, S)) = Pretty.block
   173           [Pretty.str "universal non-emptiness witness:", Pretty.brk 1,
   174             prt_typ_no_tvars T, Pretty.str " ::", Pretty.brk 1, prt_sort S];
   175 
   176     val tfrees = map (fn v => TFree (v, []));
   177     fun pretty_type syn (t, (Type.LogicalType n, _)) =
   178           if syn then NONE
   179           else SOME (prt_typ (Type (t, tfrees (Name.invent_list [] "'a" n))))
   180       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'), _)) =
   181           if syn <> syn' then NONE
   182           else SOME (Pretty.block
   183             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   184       | pretty_type syn (t, (Type.Nonterminal, _)) =
   185           if not syn then NONE
   186           else SOME (prt_typ (Type (t, [])));
   187 
   188     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   189 
   190     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   191       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   192 
   193     fun pretty_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term_no_vars t];
   194 
   195     fun pretty_finals reds = Pretty.block
   196       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
   197 
   198     fun pretty_reduct (lhs, rhs) = Pretty.block
   199       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   200         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   201 
   202     fun pretty_restrict (const, name) =
   203       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   204 
   205     val {axioms, defs, oracles} = Theory.rep_theory thy;
   206     val {restricts, reducts} = Defs.dest defs;
   207     val {naming, syn = _, tsig, consts} = Sign.rep_sg thy;
   208     val {constants, constraints} = Consts.dest consts;
   209     val extern_const = NameSpace.extern (#1 constants);
   210     val {classes, default, types, witness, ...} = Type.rep_tsig tsig;
   211     val (class_space, class_algebra) = classes;
   212     val {classes, arities} = Sorts.rep_algebra class_algebra;
   213 
   214     fun prune xs = if verbose then xs else filter_out (can Name.dest_internal o fst) xs;
   215     fun prune' xs = if verbose then xs else filter_out (can Name.dest_internal o fst o fst) xs;
   216     fun dest_table tab = prune (NameSpace.dest_table tab);
   217     fun extern_table tab = prune (NameSpace.extern_table tab);
   218 
   219     val clsses = dest_table (class_space, Symtab.make (Graph.dest classes));
   220     val tdecls = dest_table types;
   221     val arties = dest_table (Sign.type_space thy, arities);
   222     val cnsts = extern_table constants;
   223     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   224     val abbrevs = map_filter (fn (c, (ty, SOME (t, _))) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   225     val cnstrs = extern_table constraints;
   226     val axms = extern_table axioms;
   227     val oras = extern_table oracles;
   228 
   229     val (reds0, (reds1, reds2)) = prune' reducts |> map (fn (lhs, rhs) =>
   230         (apfst extern_const lhs, map (apfst extern_const) (prune 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 begin_goal (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.blk (0,
   317      [Pretty.str (begin_goal ^ " " ^ 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_marker bg n th =
   351   pretty_goals_aux (Sign.pp (Thm.theory_of_thm th)) bg (true, true) n th;
   352 
   353 val pretty_goals = pretty_goals_marker "";
   354 val print_goals = (Pretty.writeln o Pretty.chunks) oo pretty_goals_marker "";
   355 
   356 end;
   357 
   358 
   359 (* print_current_goals *)
   360 
   361 val current_goals_markers = ref ("", "", "");
   362 
   363 fun pretty_current_goals n m th =
   364   let
   365     val ref (begin_state, end_state, begin_goal) = current_goals_markers;
   366     val ngoals = nprems_of th;
   367   in
   368     (if begin_state = "" then [] else [Pretty.str begin_state]) @
   369     [Pretty.str ("Level " ^ string_of_int n ^
   370       (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
   371         (if ngoals <> 1 then "s" else "") ^ ")"
   372       else ""))] @
   373     pretty_goals_marker begin_goal m th @
   374     (if end_state = "" then [] else [Pretty.str end_state])
   375   end;
   376 
   377 fun print_current_goals_default n m th =
   378   Pretty.writeln (Pretty.chunks (pretty_current_goals n m th));
   379 
   380 val print_current_goals_fn = ref print_current_goals_default;
   381 
   382 end;
   383 
   384 structure BasicDisplay: BASIC_DISPLAY = Display;
   385 open BasicDisplay;