src/Pure/display.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 51584 98029ceda8ce
child 52043 286629271d65
permissions -rw-r--r--
tuned signature;
tuned comments;
     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 show_consts_default: bool Unsynchronized.ref
    11   val show_consts: bool Config.T
    12   val show_hyps_raw: Config.raw
    13   val show_hyps: bool Config.T
    14   val show_tags_raw: Config.raw
    15   val show_tags: bool Config.T
    16 end;
    17 
    18 signature DISPLAY =
    19 sig
    20   include BASIC_DISPLAY
    21   val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool} -> thm -> Pretty.T
    22   val pretty_thm: Proof.context -> thm -> Pretty.T
    23   val pretty_thm_item: Proof.context -> thm -> Pretty.T
    24   val pretty_thm_global: theory -> thm -> Pretty.T
    25   val pretty_thm_without_context: thm -> Pretty.T
    26   val string_of_thm: Proof.context -> thm -> string
    27   val string_of_thm_global: theory -> thm -> string
    28   val string_of_thm_without_context: thm -> string
    29   val pretty_full_theory: bool -> theory -> Pretty.T list
    30 end;
    31 
    32 structure Display: DISPLAY =
    33 struct
    34 
    35 (** options **)
    36 
    37 val show_consts_default = Goal_Display.show_consts_default;
    38 val show_consts = Goal_Display.show_consts;
    39 
    40 val show_hyps_raw = Config.declare "show_hyps" (fn _ => Config.Bool false);
    41 val show_hyps = Config.bool show_hyps_raw;
    42 
    43 val show_tags_raw = Config.declare "show_tags" (fn _ => Config.Bool false);
    44 val show_tags = Config.bool show_tags_raw;
    45 
    46 
    47 
    48 (** print thm **)
    49 
    50 fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
    51 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    52 
    53 fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
    54   let
    55     val show_tags = Config.get ctxt show_tags;
    56     val show_hyps = Config.get ctxt show_hyps;
    57 
    58     val th = Thm.strip_shyps raw_th;
    59     val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
    60     val xshyps = Thm.extra_shyps th;
    61     val tags = Thm.get_tags th;
    62 
    63     val q = if quote then Pretty.quote else I;
    64     val prt_term = q o Syntax.pretty_term ctxt;
    65 
    66     val asms = map Thm.term_of (Assumption.all_assms_of ctxt);
    67     val hyps' = if show_hyps then hyps else subtract (op aconv) asms hyps;
    68 
    69     val hlen = length xshyps + length hyps' + length tpairs;
    70     val hsymbs =
    71       if hlen = 0 then []
    72       else if show_hyps orelse show_hyps' then
    73         [Pretty.brk 2, Pretty.list "[" "]"
    74           (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
    75            map (Syntax.pretty_sort ctxt) xshyps)]
    76       else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
    77     val tsymbs =
    78       if null tags orelse not show_tags then []
    79       else [Pretty.brk 1, pretty_tags tags];
    80   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    81 
    82 fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
    83 fun pretty_thm_item ctxt th = Pretty.item [pretty_thm ctxt th];
    84 
    85 fun pretty_thm_global thy =
    86   pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
    87 
    88 fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
    89 
    90 val string_of_thm = Pretty.string_of oo pretty_thm;
    91 val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
    92 val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
    93 
    94 
    95 
    96 (** print theory **)
    97 
    98 (* pretty_full_theory *)
    99 
   100 fun pretty_full_theory verbose thy =
   101   let
   102     val ctxt = Syntax.init_pretty_global thy;
   103 
   104     fun prt_cls c = Syntax.pretty_sort ctxt [c];
   105     fun prt_sort S = Syntax.pretty_sort ctxt S;
   106     fun prt_arity t (c, Ss) = Syntax.pretty_arity ctxt (t, Ss, [c]);
   107     fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
   108     val prt_typ_no_tvars = prt_typ o Logic.unvarifyT_global;
   109     fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
   110     val prt_term_no_vars = prt_term o Logic.unvarify_global;
   111     fun prt_const (c, ty) = [Pretty.mark_str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   112     val prt_const' = Defs.pretty_const ctxt;
   113 
   114     fun pretty_classrel (c, []) = prt_cls c
   115       | pretty_classrel (c, cs) = Pretty.block
   116           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 :: Pretty.commas (map prt_cls cs));
   117 
   118     fun pretty_default S = Pretty.block
   119       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   120 
   121     val tfrees = map (fn v => TFree (v, []));
   122     fun pretty_type syn (t, (Type.LogicalType n)) =
   123           if syn then NONE
   124           else SOME (prt_typ (Type (t, tfrees (Name.invent Name.context Name.aT n))))
   125       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'))) =
   126           if syn <> syn' then NONE
   127           else SOME (Pretty.block
   128             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   129       | pretty_type syn (t, Type.Nonterminal) =
   130           if not syn then NONE
   131           else SOME (prt_typ (Type (t, [])));
   132 
   133     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   134 
   135     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   136       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   137 
   138     fun pretty_axm (a, t) =
   139       Pretty.block [Pretty.mark_str a, Pretty.str ":", Pretty.brk 1, prt_term_no_vars t];
   140 
   141     fun pretty_finals reds = Pretty.block
   142       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
   143 
   144     fun pretty_reduct (lhs, rhs) = Pretty.block
   145       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   146         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   147 
   148     fun pretty_restrict (const, name) =
   149       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   150 
   151     val axioms = (Theory.axiom_space thy, Theory.axiom_table thy);
   152     val defs = Theory.defs_of thy;
   153     val {restricts, reducts} = Defs.dest defs;
   154     val tsig = Sign.tsig_of thy;
   155     val consts = Sign.consts_of thy;
   156     val {constants, constraints} = Consts.dest consts;
   157     val extern_const = Name_Space.extern ctxt (#1 constants);
   158     val {classes, default, types, ...} = Type.rep_tsig tsig;
   159     val (class_space, class_algebra) = classes;
   160     val classes = Sorts.classes_of class_algebra;
   161     val arities = Sorts.arities_of class_algebra;
   162 
   163     val clsses =
   164       Name_Space.dest_table ctxt (class_space,
   165         Symtab.make (map (fn ((c, _), cs) => (c, Sign.minimize_sort thy cs)) (Graph.dest classes)));
   166     val tdecls = Name_Space.dest_table ctxt types;
   167     val arties = Name_Space.dest_table ctxt (Type.type_space tsig, arities);
   168 
   169     fun prune_const c = not verbose andalso Consts.is_concealed consts c;
   170     val cnsts = Name_Space.extern_table ctxt (#1 constants,
   171       Symtab.make (filter_out (prune_const o fst) (Symtab.dest (#2 constants))));
   172 
   173     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   174     val abbrevs = map_filter (fn (c, (ty, SOME t)) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   175     val cnstrs = Name_Space.extern_table ctxt constraints;
   176 
   177     val axms = Name_Space.extern_table ctxt axioms;
   178 
   179     val (reds0, (reds1, reds2)) = filter_out (prune_const o fst o fst) reducts
   180       |> map (fn (lhs, rhs) =>
   181         (apfst extern_const lhs, map (apfst extern_const) (filter_out (prune_const o fst) rhs)))
   182       |> sort_wrt (#1 o #1)
   183       |> List.partition (null o #2)
   184       ||> List.partition (Defs.plain_args o #2 o #1);
   185     val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
   186   in
   187     [Pretty.strs ("names:" :: Context.display_names thy)] @
   188     [Pretty.big_list "classes:" (map pretty_classrel clsses),
   189       pretty_default default,
   190       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   191       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   192       Pretty.big_list "type arities:" (pretty_arities arties),
   193       Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
   194       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   195       Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
   196       Pretty.big_list "axioms:" (map pretty_axm axms),
   197       Pretty.block
   198         (Pretty.breaks (Pretty.str "oracles:" :: map Pretty.mark_str (Thm.extern_oracles ctxt))),
   199       Pretty.big_list "definitions:"
   200         [pretty_finals reds0,
   201          Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
   202          Pretty.big_list "overloaded:" (map pretty_reduct reds2),
   203          Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
   204   end;
   205 
   206 end;
   207 
   208 structure Basic_Display: BASIC_DISPLAY = Display;
   209 open Basic_Display;