src/Pure/display.ML
author paulson
Fri Mar 19 10:42:38 2004 +0100 (2004-03-19)
changeset 14472 cba7c0a3ffb3
parent 14223 0ee05eef881b
child 14598 7009f59711e3
permissions -rw-r--r--
Removing the datatype declaration of "order" allows the standard General.order
to be used. Thus we can use Int.compare and String.compare instead of the
slower home-grown versions.
     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 theories, theorems, 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 pretty_flexpair: (term -> Pretty.T) -> term * term -> Pretty.T
    32   val pretty_thm_aux: (sort -> Pretty.T) * (term -> Pretty.T) -> bool -> thm -> Pretty.T
    33   val pretty_thm_no_quote: thm -> Pretty.T
    34   val pretty_thm: thm -> Pretty.T
    35   val pretty_thms: thm list -> Pretty.T
    36   val pretty_thm_sg: Sign.sg -> thm -> Pretty.T
    37   val pretty_thms_sg: Sign.sg -> thm list -> Pretty.T
    38   val pprint_thm: thm -> pprint_args -> unit
    39   val pretty_ctyp: ctyp -> Pretty.T
    40   val pprint_ctyp: ctyp -> pprint_args -> unit
    41   val pretty_cterm: cterm -> Pretty.T
    42   val pprint_cterm: cterm -> pprint_args -> unit
    43   val pretty_theory: theory -> Pretty.T
    44   val pprint_theory: theory -> pprint_args -> unit
    45   val pretty_full_theory: theory -> Pretty.T list
    46   val pretty_name_space: string * NameSpace.T -> Pretty.T
    47   val pretty_goals_aux: (sort -> Pretty.T) * (typ -> Pretty.T) * (term -> Pretty.T)
    48     -> string -> bool * bool -> int -> thm -> Pretty.T list
    49   val pretty_goals: int -> thm -> Pretty.T list
    50   val print_goals: int -> thm -> unit
    51   val current_goals_markers: (string * string * string) ref
    52   val pretty_current_goals: int -> int -> thm -> Pretty.T list
    53   val print_current_goals_default: int -> int -> thm -> unit
    54   val print_current_goals_fn: (int -> int -> thm -> unit) ref
    55 end;
    56 
    57 structure Display: DISPLAY =
    58 struct
    59 
    60 
    61 (** print thm **)
    62 
    63 val goals_limit = ref 10;       (*max number of goals to print -- set by user*)
    64 val show_hyps = ref false;      (*false: print meta-hypotheses as dots*)
    65 val show_tags = ref false;      (*false: suppress tags*)
    66 
    67 fun pretty_tag (name, args) = Pretty.strs (name :: map quote args);
    68 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    69 
    70 fun pretty_flexpair pretty_term (t, u) = Pretty.block
    71   [pretty_term t, Pretty.str " =?=", Pretty.brk 1, pretty_term u];
    72 
    73 fun pretty_thm_aux (pretty_sort, pretty_term) quote th =
    74   let
    75     val {hyps, tpairs, prop, der = (ora, _), ...} = rep_thm th;
    76     val xshyps = Thm.extra_shyps th;
    77     val (_, tags) = Thm.get_name_tags th;
    78 
    79     val q = if quote then Pretty.quote else I;
    80     val prt_term = q o pretty_term;
    81 
    82     val hlen = length xshyps + length hyps + length tpairs;
    83     val hsymbs =
    84       if hlen = 0 andalso not ora then []
    85       else if ! show_hyps then
    86         [Pretty.brk 2, Pretty.list "[" "]"
    87           (map (q o pretty_flexpair pretty_term) tpairs @ map prt_term hyps @
    88            map pretty_sort xshyps @
    89             (if ora then [Pretty.str "!"] else []))]
    90       else [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^
    91         (if ora then "!" else "") ^ "]")];
    92     val tsymbs =
    93       if null tags orelse not (! show_tags) then []
    94       else [Pretty.brk 1, pretty_tags tags];
    95   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    96 
    97 fun gen_pretty_thm quote th =
    98   let val sg = Thm.sign_of_thm th
    99   in pretty_thm_aux (Sign.pretty_sort sg, Sign.pretty_term sg) quote th end;
   100 
   101 val pretty_thm = gen_pretty_thm true;
   102 val pretty_thm_no_quote = gen_pretty_thm false;
   103 
   104 
   105 val string_of_thm = Pretty.string_of o pretty_thm;
   106 val pprint_thm = Pretty.pprint o pretty_thm;
   107 
   108 fun pretty_thms [th] = pretty_thm th
   109   | pretty_thms ths = Pretty.block (Pretty.fbreaks (map pretty_thm ths));
   110 
   111 val pretty_thm_sg = pretty_thm oo Thm.transfer_sg;
   112 val pretty_thms_sg = pretty_thms oo (map o Thm.transfer_sg);
   113 
   114 
   115 (* top-level commands for printing theorems *)
   116 
   117 val print_thm = Pretty.writeln o pretty_thm;
   118 val print_thms = Pretty.writeln o pretty_thms;
   119 
   120 fun prth th = (print_thm th; th);
   121 
   122 (*Print and return a sequence of theorems, separated by blank lines. *)
   123 fun prthq thseq =
   124   (Seq.print (fn _ => print_thm) 100000 thseq; thseq);
   125 
   126 (*Print and return a list of theorems, separated by blank lines. *)
   127 fun prths ths = (seq (fn th => (print_thm th; writeln "")) ths; ths);
   128 
   129 
   130 (* other printing commands *)
   131 
   132 fun pretty_ctyp cT =
   133   let val {sign, T} = rep_ctyp cT in Sign.pretty_typ sign T end;
   134 
   135 fun pprint_ctyp cT =
   136   let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
   137 
   138 fun string_of_ctyp cT =
   139   let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
   140 
   141 val print_ctyp = writeln o string_of_ctyp;
   142 
   143 fun pretty_cterm ct =
   144   let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
   145 
   146 fun pprint_cterm ct =
   147   let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
   148 
   149 fun string_of_cterm ct =
   150   let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
   151 
   152 val print_cterm = writeln o string_of_cterm;
   153 
   154 
   155 
   156 (** print theory **)
   157 
   158 val pretty_theory = Sign.pretty_sg o Theory.sign_of;
   159 val pprint_theory = Sign.pprint_sg o Theory.sign_of;
   160 
   161 val print_syntax = Syntax.print_syntax o Theory.syn_of;
   162 
   163 
   164 (* pretty_name_space  *)
   165 
   166 fun pretty_name_space (kind, space) =
   167   let
   168     fun prt_entry (name, accs) = Pretty.block
   169       (Pretty.str (quote name ^ " =") :: Pretty.brk 1 ::
   170         Pretty.commas (map (Pretty.str o quote) accs));
   171   in
   172     Pretty.fbreaks (Pretty.str (kind ^ ":") :: map prt_entry (NameSpace.dest space))
   173     |> Pretty.block
   174   end;
   175 
   176 
   177 (* pretty_full_theory *)
   178 
   179 fun pretty_full_theory thy =
   180   let
   181     val sg = Theory.sign_of thy;
   182 
   183     fun prt_cls c = Sign.pretty_sort sg [c];
   184     fun prt_sort S = Sign.pretty_sort sg S;
   185     fun prt_arity t (c, Ss) = Sign.pretty_arity sg (t, Ss, [c]);
   186     fun prt_typ_no_tvars ty = Pretty.quote (Sign.pretty_typ sg (#1 (Type.freeze_thaw_type ty)));
   187     fun prt_term t = Pretty.quote (Sign.pretty_term sg t);
   188 
   189     fun pretty_classes cs = Pretty.block
   190       (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
   191 
   192     fun pretty_classrel (c, cs) = Pretty.block
   193       (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   194         Pretty.commas (map prt_cls cs));
   195 
   196     fun pretty_default S = Pretty.block
   197       [Pretty.str "default:", Pretty.brk 1, prt_sort S];
   198 
   199     fun pretty_ty (t, n) = Pretty.block
   200       [Pretty.str t, Pretty.brk 1, Pretty.str (string_of_int n)];
   201 
   202     fun pretty_log_types ts = Pretty.block
   203       (Pretty.breaks (Pretty.str "logical types:" :: map Pretty.str ts));
   204 
   205     fun pretty_witness None = Pretty.str "universal non-emptiness witness: --"
   206       | pretty_witness (Some (T, S)) = Pretty.block
   207           [Pretty.str "universal non-emptiness witness:", Pretty.brk 1, prt_typ_no_tvars T,
   208             Pretty.brk 1, prt_sort S];
   209 
   210     fun pretty_abbr (t, (vs, rhs)) = Pretty.block
   211       [prt_typ_no_tvars (Type (t, map (fn v => TVar ((v, 0), [])) vs)),
   212         Pretty.str " =", Pretty.brk 1, prt_typ_no_tvars rhs];
   213 
   214     fun pretty_arities (t, ars) = map (prt_arity t) ars;
   215 
   216     fun pretty_final (c:string, tys:typ list) = Pretty.block
   217       ([Pretty.str c, Pretty.str " ::", Pretty.brk 1] @
   218          (if null tys then [Pretty.str "<all instances>"]
   219 	  else Pretty.commas (map prt_typ_no_tvars tys)));
   220 
   221     fun pretty_const (c, ty) = Pretty.block
   222       [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   223 
   224     fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
   225 
   226 
   227     val {self = _, tsig, const_tab, syn = _, path, spaces, data} = Sign.rep_sg sg;
   228     val {axioms, oracles, ...} = Theory.rep_theory thy;
   229     val spaces' = Library.sort_wrt fst spaces;
   230     val {classes, classrel, default, tycons = type_tab, log_types, univ_witness, abbrs, arities} =
   231       Type.rep_tsig tsig;
   232     val finals = Symtab.dest (#final_consts (rep_theory thy));
   233     val tycons = Sign.cond_extern_table sg Sign.typeK type_tab;
   234     val consts = Sign.cond_extern_table sg Sign.constK const_tab;
   235     val axms = Sign.cond_extern_table sg Theory.axiomK axioms;
   236     val oras = Sign.cond_extern_table sg Theory.oracleK oracles;
   237   in
   238     [Pretty.strs ("stamps:" :: Sign.stamp_names_of sg),
   239       Pretty.strs ("data:" :: Sign.data_kinds data),
   240       Pretty.strs ["name prefix:", NameSpace.pack (if_none path ["-"])],
   241       Pretty.big_list "name spaces:" (map pretty_name_space spaces'),
   242       pretty_classes classes,
   243       Pretty.big_list "class relation:" (map pretty_classrel (Symtab.dest classrel)),
   244       pretty_default default,
   245       pretty_log_types log_types,
   246       pretty_witness univ_witness,
   247       Pretty.big_list "type constructors:" (map pretty_ty tycons),
   248       Pretty.big_list "type abbreviations:" (map pretty_abbr (Symtab.dest abbrs)),
   249       Pretty.big_list "type arities:" (flat (map pretty_arities (Symtab.dest arities))),
   250       Pretty.big_list "consts:" (map pretty_const consts),
   251       Pretty.big_list "finals:" (map pretty_final finals),
   252       Pretty.big_list "axioms:" (map prt_axm axms),
   253       Pretty.strs ("oracles:" :: (map #1 oras))]
   254   end;
   255 
   256 
   257 
   258 (** print_goals **)
   259 
   260 (* print_goals etc. *)
   261 
   262 (*show consts with types in proof state output?*)
   263 val show_consts = ref false;
   264 
   265 
   266 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   267 
   268 local
   269 
   270 fun ins_entry (x, y) [] = [(x, [y])]
   271   | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   272       if x = x' then (x', y ins ys') :: pairs
   273       else pair :: ins_entry (x, y) pairs;
   274 
   275 fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
   276   | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
   277   | add_consts (Abs (_, _, t), env) = add_consts (t, env)
   278   | add_consts (_, env) = env;
   279 
   280 fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
   281   | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
   282   | add_vars (Abs (_, _, t), env) = add_vars (t, env)
   283   | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
   284   | add_vars (_, env) = env;
   285 
   286 fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
   287   | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
   288   | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
   289 
   290 fun sort_idxs vs = map (apsnd (sort (prod_ord String.compare Int.compare))) vs;
   291 fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   292 
   293 fun consts_of t = sort_cnsts (add_consts (t, []));
   294 fun vars_of t = sort_idxs (add_vars (t, []));
   295 fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
   296 
   297 in
   298 
   299 fun pretty_goals_aux (prt_sort, prt_typ, prt_term) begin_goal (msg, main) maxgoals state =
   300   let
   301     fun prt_atoms prt prtT (X, xs) = Pretty.block
   302       [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
   303         Pretty.brk 1, prtT X];
   304 
   305     fun prt_var (x, ~1) = prt_term (Syntax.free x)
   306       | prt_var xi = prt_term (Syntax.var xi);
   307 
   308     fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
   309       | prt_varT xi = prt_typ (TVar (xi, []));
   310 
   311     val prt_consts = prt_atoms (prt_term o Const) prt_typ;
   312     val prt_vars = prt_atoms prt_var prt_typ;
   313     val prt_varsT = prt_atoms prt_varT prt_sort;
   314 
   315 
   316     fun pretty_list _ _ [] = []
   317       | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
   318 
   319     fun pretty_subgoal (n, A) =
   320       Pretty.blk (0, [Pretty.str (begin_goal ^ " " ^ string_of_int n ^ ". "), prt_term A]);
   321     fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
   322 
   323     val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair prt_term);
   324 
   325     val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
   326     val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
   327     val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
   328 
   329 
   330     val {prop, tpairs, ...} = rep_thm state;
   331     val (As, B) = Logic.strip_horn prop;
   332     val ngoals = length As;
   333 
   334     fun pretty_gs (types, sorts) =
   335       (if main then [prt_term B] else []) @
   336        (if ngoals = 0 then [Pretty.str "No subgoals!"]
   337         else if ngoals > maxgoals then
   338           pretty_subgoals (take (maxgoals, As)) @
   339           (if msg then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
   340            else [])
   341         else pretty_subgoals As) @
   342       pretty_ffpairs tpairs @
   343       (if ! show_consts then pretty_consts prop else []) @
   344       (if types then pretty_vars prop else []) @
   345       (if sorts then pretty_varsT prop else []);
   346   in
   347     setmp show_no_free_types true
   348       (setmp show_types (! show_types orelse ! show_sorts orelse ! show_all_types)
   349         (setmp show_sorts false pretty_gs))
   350    (! show_types orelse ! show_sorts orelse ! show_all_types, ! show_sorts)
   351   end;
   352 
   353 fun pretty_goals_marker bg n th =
   354   let val sg = Thm.sign_of_thm th in
   355     pretty_goals_aux (Sign.pretty_sort sg, Sign.pretty_typ sg, Sign.pretty_term sg)
   356       bg (true, true) n th
   357   end;
   358 
   359 val pretty_goals = pretty_goals_marker "";
   360 val print_goals = (Pretty.writeln o Pretty.chunks) oo pretty_goals_marker "";
   361 
   362 end;
   363 
   364 
   365 (* print_current_goals *)
   366 
   367 val current_goals_markers = ref ("", "", "");
   368 
   369 fun pretty_current_goals n m th =
   370   let
   371     val ref (begin_state, end_state, begin_goal) = current_goals_markers;
   372     val ngoals = nprems_of th;
   373   in
   374     (if begin_state = "" then [] else [Pretty.str begin_state]) @
   375     [Pretty.str ("Level " ^ string_of_int n ^
   376       (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
   377         (if ngoals <> 1 then "s" else "") ^ ")"
   378       else ""))] @
   379     pretty_goals_marker begin_goal m th @
   380     (if end_state = "" then [] else [Pretty.str end_state])
   381   end;
   382 
   383 fun print_current_goals_default n m th =
   384   Pretty.writeln (Pretty.chunks (pretty_current_goals n m th));
   385 
   386 val print_current_goals_fn = ref print_current_goals_default;
   387 
   388 end;
   389 
   390 structure BasicDisplay: BASIC_DISPLAY = Display;
   391 open BasicDisplay;