src/Pure/display.ML
author wenzelm
Tue May 16 13:01:24 2006 +0200 (2006-05-16)
changeset 19642 ea7162f84677
parent 19591 e7036e812702
child 19698 f48cfaacd92c
permissions -rw-r--r--
more abstract interface to classes/arities;
     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 
   176     fun pretty_classrel (c, []) = prt_cls c
   177       | pretty_classrel (c, cs) = Pretty.block
   178           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   179             Pretty.commas (map prt_cls cs));
   180 
   181     fun pretty_default S = Pretty.block
   182       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   183 
   184     fun pretty_witness NONE = Pretty.str "universal non-emptiness witness: -"
   185       | pretty_witness (SOME (T, S)) = Pretty.block
   186           [Pretty.str "universal non-emptiness witness:", Pretty.brk 1,
   187             prt_typ_no_tvars T, Pretty.str " ::", Pretty.brk 1, prt_sort S];
   188 
   189     val tfrees = map (fn v => TFree (v, []));
   190     fun pretty_type syn (t, (Type.LogicalType n, _)) =
   191           if syn then NONE
   192           else SOME (prt_typ (Type (t, tfrees (Term.invent_names [] "'a" n))))
   193       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'), _)) =
   194           if syn <> syn' then NONE
   195           else SOME (Pretty.block
   196             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   197       | pretty_type syn (t, (Type.Nonterminal, _)) =
   198           if not syn then NONE
   199           else SOME (prt_typ (Type (t, [])));
   200 
   201     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   202 
   203     fun pretty_const (c, ty) = Pretty.block
   204       [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   205 
   206     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   207       [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty,
   208         Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t];
   209 
   210     fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term_no_vars t];
   211 
   212     val {axioms, defs = _, oracles} = Theory.rep_theory thy;
   213     val {naming, syn = _, tsig, consts} = Sign.rep_sg thy;
   214     val {constants, constraints} = Consts.dest consts;
   215     val {classes = (class_space, class_algebra),
   216       default, types, log_types = _, witness} = Type.rep_tsig tsig;
   217     val {classes, arities} = Sorts.rep_algebra class_algebra;
   218 
   219     val clsses = NameSpace.dest_table (class_space, Symtab.make (Graph.dest classes));
   220     val tdecls = NameSpace.dest_table types;
   221     val arties = NameSpace.dest_table (Sign.type_space thy, arities);
   222     val cnsts = NameSpace.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 = NameSpace.extern_table constraints;
   226     val axms = NameSpace.extern_table axioms;
   227     val oras = NameSpace.extern_table oracles;
   228   in
   229     [Pretty.strs ("names:" :: Context.names_of thy),
   230       Pretty.strs ("theory data:" :: Context.theory_data_of thy),
   231       Pretty.strs ("proof data:" :: Context.proof_data_of thy),
   232       Pretty.strs ["name prefix:", NameSpace.path_of naming],
   233       Pretty.big_list "classes:" (map pretty_classrel clsses),
   234       pretty_default default,
   235       pretty_witness witness,
   236       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   237       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   238       Pretty.big_list "type arities:" (pretty_arities arties),
   239       Pretty.big_list "logical consts:" (map pretty_const log_cnsts),
   240       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   241       Pretty.big_list "constraints:" (map pretty_const cnstrs),
   242       Pretty.big_list "axioms:" (map prt_axm axms),
   243       Pretty.strs ("oracles:" :: (map #1 oras))]
   244   end;
   245 
   246 
   247 
   248 (** print_goals **)
   249 
   250 (* print_goals etc. *)
   251 
   252 val show_consts = ref false;  (*true: show consts with types in proof state output*)
   253 
   254 
   255 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   256 
   257 local
   258 
   259 fun ins_entry (x, y) =
   260   AList.default (op =) (x, []) #>
   261   AList.map_entry (op =) x (insert (op =) y);
   262 
   263 val add_consts = Term.fold_aterms
   264   (fn Const (c, T) => ins_entry (T, (c, T))
   265     | _ => I);
   266 
   267 val add_vars = Term.fold_aterms
   268   (fn Free (x, T) => ins_entry (T, (x, ~1))
   269     | Var (xi, T) => ins_entry (T, xi)
   270     | _ => I);
   271 
   272 val add_varsT = Term.fold_atyps
   273   (fn TFree (x, S) => ins_entry (S, (x, ~1))
   274     | TVar (xi, S) => ins_entry (S, xi)
   275     | _ => I);
   276 
   277 fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   278 fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   279 
   280 fun consts_of t = sort_cnsts (add_consts t []);
   281 fun vars_of t = sort_idxs (add_vars t []);
   282 fun varsT_of t = rev (sort_idxs (Term.fold_types add_varsT t []));
   283 
   284 in
   285 
   286 fun pretty_goals_aux pp begin_goal (msg, main) maxgoals state =
   287   let
   288     fun prt_atoms prt prtT (X, xs) = Pretty.block
   289       [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   290         Pretty.brk 1, prtT X];
   291 
   292     fun prt_var (x, ~1) = Pretty.term pp (Syntax.free x)
   293       | prt_var xi = Pretty.term pp (Syntax.var xi);
   294 
   295     fun prt_varT (x, ~1) = Pretty.typ pp (TFree (x, []))
   296       | prt_varT xi = Pretty.typ pp (TVar (xi, []));
   297 
   298     val prt_consts = prt_atoms (Pretty.term pp o Const) (Pretty.typ pp);
   299     val prt_vars = prt_atoms prt_var (Pretty.typ pp);
   300     val prt_varsT = prt_atoms prt_varT (Pretty.sort pp);
   301 
   302 
   303     fun pretty_list _ _ [] = []
   304       | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
   305 
   306     fun pretty_subgoal (n, A) = Pretty.blk (0,
   307      [Pretty.str (begin_goal ^ " " ^ string_of_int n ^ ". "), Pretty.term pp A]);
   308     fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
   309 
   310     val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair pp);
   311 
   312     val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
   313     val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
   314     val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
   315 
   316 
   317     val {prop, tpairs, ...} = Thm.rep_thm state;
   318     val (As, B) = Logic.strip_horn prop;
   319     val ngoals = length As;
   320 
   321     fun pretty_gs (types, sorts) =
   322       (if main then [Pretty.term pp B] else []) @
   323        (if ngoals = 0 then [Pretty.str "No subgoals!"]
   324         else if ngoals > maxgoals then
   325           pretty_subgoals (Library.take (maxgoals, As)) @
   326           (if msg then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
   327            else [])
   328         else pretty_subgoals As) @
   329       pretty_ffpairs tpairs @
   330       (if ! show_consts then pretty_consts prop else []) @
   331       (if types then pretty_vars prop else []) @
   332       (if sorts then pretty_varsT prop else []);
   333   in
   334     setmp show_no_free_types true
   335       (setmp show_types (! show_types orelse ! show_sorts orelse ! show_all_types)
   336         (setmp show_sorts false pretty_gs))
   337    (! show_types orelse ! show_sorts orelse ! show_all_types, ! show_sorts)
   338   end;
   339 
   340 fun pretty_goals_marker bg n th =
   341   pretty_goals_aux (Sign.pp (Thm.theory_of_thm th)) bg (true, true) n th;
   342 
   343 val pretty_goals = pretty_goals_marker "";
   344 val print_goals = (Pretty.writeln o Pretty.chunks) oo pretty_goals_marker "";
   345 
   346 end;
   347 
   348 
   349 (* print_current_goals *)
   350 
   351 val current_goals_markers = ref ("", "", "");
   352 
   353 fun pretty_current_goals n m th =
   354   let
   355     val ref (begin_state, end_state, begin_goal) = current_goals_markers;
   356     val ngoals = nprems_of th;
   357   in
   358     (if begin_state = "" then [] else [Pretty.str begin_state]) @
   359     [Pretty.str ("Level " ^ string_of_int n ^
   360       (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
   361         (if ngoals <> 1 then "s" else "") ^ ")"
   362       else ""))] @
   363     pretty_goals_marker begin_goal m th @
   364     (if end_state = "" then [] else [Pretty.str end_state])
   365   end;
   366 
   367 fun print_current_goals_default n m th =
   368   Pretty.writeln (Pretty.chunks (pretty_current_goals n m th));
   369 
   370 val print_current_goals_fn = ref print_current_goals_default;
   371 
   372 end;
   373 
   374 structure BasicDisplay: BASIC_DISPLAY = Display;
   375 open BasicDisplay;