src/Pure/display.ML
author wenzelm
Fri Aug 28 18:19:07 2009 +0200 (2009-08-28 ago)
changeset 32430 de3727f1cf12
parent 32187 cca43ca13f4f
child 32433 11661f4327bb
permissions -rw-r--r--
removed obsolete print_ctyp, print_cterm;
     1 (*  Title:      Pure/display.ML
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Makarius
     4 
     5 Printing of theorems, results etc.
     6 *)
     7 
     8 signature BASIC_DISPLAY =
     9 sig
    10   val goals_limit: int ref
    11   val show_consts: bool ref
    12   val show_hyps: bool ref
    13   val show_tags: bool ref
    14 end;
    15 
    16 signature DISPLAY =
    17 sig
    18   include BASIC_DISPLAY
    19   val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool, show_status: bool} ->
    20     thm -> Pretty.T
    21   val pretty_thm_aux: Proof.context -> bool -> thm -> Pretty.T
    22   val pretty_thm: Proof.context -> thm -> Pretty.T
    23   val pretty_thm_global: theory -> thm -> Pretty.T
    24   val pretty_thm_without_context: thm -> Pretty.T
    25   val string_of_thm: Proof.context -> thm -> string
    26   val string_of_thm_global: theory -> thm -> string
    27   val string_of_thm_without_context: thm -> string
    28   val pretty_thms_aux: Proof.context -> bool -> thm list -> Pretty.T
    29   val pretty_thms: Proof.context -> thm list -> Pretty.T
    30   val pretty_ctyp: ctyp -> Pretty.T
    31   val string_of_ctyp: ctyp -> string
    32   val pretty_cterm: cterm -> Pretty.T
    33   val string_of_cterm: cterm -> string
    34   val print_syntax: theory -> unit
    35   val pretty_full_theory: bool -> theory -> Pretty.T list
    36 end;
    37 
    38 structure Display: DISPLAY =
    39 struct
    40 
    41 (** options **)
    42 
    43 val goals_limit = Goal_Display.goals_limit;
    44 val show_consts = Goal_Display.show_consts;
    45 
    46 val show_hyps = ref false;      (*false: print meta-hypotheses as dots*)
    47 val show_tags = ref false;      (*false: suppress tags*)
    48 
    49 
    50 
    51 (** print thm **)
    52 
    53 fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
    54 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    55 
    56 fun display_status false _ = ""
    57   | display_status true th =
    58       let
    59         val {oracle = oracle0, unfinished, failed} = Thm.status_of th;
    60         val oracle = oracle0 andalso (not (! quick_and_dirty) orelse ! show_hyps);
    61       in
    62         if failed then "!!"
    63         else if oracle andalso unfinished then "!?"
    64         else if oracle then "!"
    65         else if unfinished then "?"
    66         else ""
    67       end;
    68 
    69 fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps', show_status} raw_th =
    70   let
    71     val th = Thm.strip_shyps raw_th;
    72     val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
    73     val xshyps = Thm.extra_shyps th;
    74     val tags = Thm.get_tags th;
    75 
    76     val q = if quote then Pretty.quote else I;
    77     val prt_term = q o Syntax.pretty_term ctxt;
    78 
    79     val asms = map Thm.term_of (Assumption.all_assms_of ctxt);
    80     val hyps' = if ! show_hyps then hyps else subtract (op aconv) asms hyps;
    81     val status = display_status show_status th;
    82 
    83     val hlen = length xshyps + length hyps' + length tpairs;
    84     val hsymbs =
    85       if hlen = 0 andalso status = "" then []
    86       else if ! show_hyps orelse show_hyps' then
    87         [Pretty.brk 2, Pretty.list "[" "]"
    88           (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
    89            map (Syntax.pretty_sort ctxt) xshyps @
    90             (if status = "" then [] else [Pretty.str status]))]
    91       else [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ status ^ "]")];
    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 pretty_thm_aux ctxt show_status =
    98   pretty_thm_raw ctxt {quote = false, show_hyps = true, show_status = show_status};
    99 
   100 fun pretty_thm ctxt = pretty_thm_aux ctxt true;
   101 
   102 fun pretty_thm_global thy =
   103   pretty_thm_raw (Syntax.init_pretty_global thy)
   104     {quote = false, show_hyps = false, show_status = true};
   105 
   106 fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
   107 
   108 val string_of_thm = Pretty.string_of oo pretty_thm;
   109 val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
   110 val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
   111 
   112 
   113 (* multiple theorems *)
   114 
   115 fun pretty_thms_aux ctxt flag [th] = pretty_thm_aux ctxt flag th
   116   | pretty_thms_aux ctxt flag ths =
   117       Pretty.blk (0, Pretty.fbreaks (map (pretty_thm_aux ctxt flag) ths));
   118 
   119 fun pretty_thms ctxt = pretty_thms_aux ctxt true;
   120 
   121 
   122 (* other printing commands *)
   123 
   124 fun pretty_ctyp cT = Syntax.pretty_typ_global (Thm.theory_of_ctyp cT) (Thm.typ_of cT);
   125 fun string_of_ctyp cT = Syntax.string_of_typ_global (Thm.theory_of_ctyp cT) (Thm.typ_of cT);
   126 
   127 fun pretty_cterm ct = Syntax.pretty_term_global (Thm.theory_of_cterm ct) (Thm.term_of ct);
   128 fun string_of_cterm ct = Syntax.string_of_term_global (Thm.theory_of_cterm ct) (Thm.term_of ct);
   129 
   130 
   131 
   132 (** print theory **)
   133 
   134 val print_syntax = Syntax.print_syntax o Sign.syn_of;
   135 
   136 
   137 (* pretty_full_theory *)
   138 
   139 fun pretty_full_theory verbose thy =
   140   let
   141     val ctxt = ProofContext.init thy;
   142 
   143     fun prt_cls c = Syntax.pretty_sort ctxt [c];
   144     fun prt_sort S = Syntax.pretty_sort ctxt S;
   145     fun prt_arity t (c, (_, Ss)) = Syntax.pretty_arity ctxt (t, Ss, [c]);
   146     fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
   147     val prt_typ_no_tvars = prt_typ o Logic.unvarifyT;
   148     fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
   149     val prt_term_no_vars = prt_term o Logic.unvarify;
   150     fun prt_const (c, ty) = [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   151     val prt_const' = Defs.pretty_const (Syntax.pp ctxt);
   152 
   153     fun pretty_classrel (c, []) = prt_cls c
   154       | pretty_classrel (c, cs) = Pretty.block
   155           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   156             Pretty.commas (map prt_cls cs));
   157 
   158     fun pretty_default S = Pretty.block
   159       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   160 
   161     val tfrees = map (fn v => TFree (v, []));
   162     fun pretty_type syn (t, ((Type.LogicalType n, _), _)) =
   163           if syn then NONE
   164           else SOME (prt_typ (Type (t, tfrees (Name.invents Name.context Name.aT n))))
   165       | pretty_type syn (t, ((Type.Abbreviation (vs, U, syn'), _), _)) =
   166           if syn <> syn' then NONE
   167           else SOME (Pretty.block
   168             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   169       | pretty_type syn (t, ((Type.Nonterminal, _), _)) =
   170           if not syn then NONE
   171           else SOME (prt_typ (Type (t, [])));
   172 
   173     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   174 
   175     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   176       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   177 
   178     fun pretty_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term_no_vars t];
   179 
   180     fun pretty_finals reds = Pretty.block
   181       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
   182 
   183     fun pretty_reduct (lhs, rhs) = Pretty.block
   184       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   185         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   186 
   187     fun pretty_restrict (const, name) =
   188       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   189 
   190     val axioms = (Theory.axiom_space thy, Theory.axiom_table thy);
   191     val defs = Theory.defs_of thy;
   192     val {restricts, reducts} = Defs.dest defs;
   193     val {naming, syn = _, tsig, consts} = Sign.rep_sg thy;
   194     val {constants, constraints} = Consts.dest consts;
   195     val extern_const = NameSpace.extern (#1 constants);
   196     val {classes, default, types, ...} = Type.rep_tsig tsig;
   197     val (class_space, class_algebra) = classes;
   198     val {classes, arities} = Sorts.rep_algebra class_algebra;
   199 
   200     val clsses = NameSpace.dest_table (class_space, Symtab.make (Graph.dest classes));
   201     val tdecls = NameSpace.dest_table types;
   202     val arties = NameSpace.dest_table (Sign.type_space thy, arities);
   203 
   204     fun prune_const c = not verbose andalso
   205       member (op =) (Consts.the_tags consts c) Markup.property_internal;
   206     val cnsts = NameSpace.extern_table (#1 constants,
   207       Symtab.make (filter_out (prune_const o fst) (Symtab.dest (#2 constants))));
   208 
   209     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   210     val abbrevs = map_filter (fn (c, (ty, SOME t)) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   211     val cnstrs = NameSpace.extern_table constraints;
   212 
   213     val axms = NameSpace.extern_table axioms;
   214 
   215     val (reds0, (reds1, reds2)) = filter_out (prune_const o fst o fst) reducts
   216       |> map (fn (lhs, rhs) =>
   217         (apfst extern_const lhs, map (apfst extern_const) (filter_out (prune_const o fst) rhs)))
   218       |> sort_wrt (#1 o #1)
   219       |> List.partition (null o #2)
   220       ||> List.partition (Defs.plain_args o #2 o #1);
   221     val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
   222   in
   223     [Pretty.strs ("names:" :: Context.display_names thy)] @
   224     [Pretty.big_list "classes:" (map pretty_classrel clsses),
   225       pretty_default default,
   226       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   227       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   228       Pretty.big_list "type arities:" (pretty_arities arties),
   229       Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
   230       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   231       Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
   232       Pretty.big_list "axioms:" (map pretty_axm axms),
   233       Pretty.strs ("oracles:" :: Thm.extern_oracles thy),
   234       Pretty.big_list "definitions:"
   235         [pretty_finals reds0,
   236          Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
   237          Pretty.big_list "overloaded:" (map pretty_reduct reds2),
   238          Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
   239   end;
   240 
   241 end;
   242 
   243 structure Basic_Display: BASIC_DISPLAY = Display;
   244 open Basic_Display;