src/Pure/display.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 56438 7f6b2634d853
child 59917 9830c944670f
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
     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: bool Config.T
    11   val show_hyps_raw: Config.raw
    12   val show_hyps: bool Config.T
    13   val show_tags_raw: Config.raw
    14   val show_tags: bool Config.T
    15 end;
    16 
    17 signature DISPLAY =
    18 sig
    19   include BASIC_DISPLAY
    20   val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool} -> thm -> Pretty.T
    21   val pretty_thm: Proof.context -> thm -> Pretty.T
    22   val pretty_thm_item: 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_full_theory: bool -> theory -> Pretty.T list
    29 end;
    30 
    31 structure Display: DISPLAY =
    32 struct
    33 
    34 (** options **)
    35 
    36 val show_consts = Goal_Display.show_consts;
    37 
    38 val show_hyps_raw = Config.declare ("show_hyps", @{here}) (fn _ => Config.Bool false);
    39 val show_hyps = Config.bool show_hyps_raw;
    40 
    41 val show_tags_raw = Config.declare ("show_tags", @{here}) (fn _ => Config.Bool false);
    42 val show_tags = Config.bool show_tags_raw;
    43 
    44 
    45 
    46 (** print thm **)
    47 
    48 fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
    49 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    50 
    51 fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
    52   let
    53     val show_tags = Config.get ctxt show_tags;
    54     val show_hyps = Config.get ctxt show_hyps;
    55 
    56     val th = Thm.strip_shyps raw_th;
    57     val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
    58     val hyps' = if show_hyps then hyps else Thm.undeclared_hyps (Context.Proof ctxt) th;
    59     val extra_shyps = Thm.extra_shyps th;
    60     val tags = Thm.get_tags th;
    61 
    62     val q = if quote then Pretty.quote else I;
    63     val prt_term = q o Syntax.pretty_term ctxt;
    64 
    65 
    66     val hlen = length extra_shyps + length hyps' + length tpairs;
    67     val hsymbs =
    68       if hlen = 0 then []
    69       else if show_hyps orelse show_hyps' then
    70         [Pretty.brk 2, Pretty.list "[" "]"
    71           (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
    72            map (Syntax.pretty_sort ctxt) extra_shyps)]
    73       else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
    74     val tsymbs =
    75       if null tags orelse not show_tags then []
    76       else [Pretty.brk 1, pretty_tags tags];
    77   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    78 
    79 fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
    80 fun pretty_thm_item ctxt th = Pretty.item [pretty_thm ctxt th];
    81 
    82 fun pretty_thm_global thy =
    83   pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
    84 
    85 fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
    86 
    87 val string_of_thm = Pretty.string_of oo pretty_thm;
    88 val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
    89 val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
    90 
    91 
    92 
    93 (** print theory **)
    94 
    95 (* pretty_full_theory *)
    96 
    97 fun pretty_full_theory verbose thy =
    98   let
    99     val ctxt = Syntax.init_pretty_global thy;
   100 
   101     fun prt_cls c = Syntax.pretty_sort ctxt [c];
   102     fun prt_sort S = Syntax.pretty_sort ctxt S;
   103     fun prt_arity t (c, Ss) = Syntax.pretty_arity ctxt (t, Ss, [c]);
   104     fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
   105     val prt_typ_no_tvars = prt_typ o Logic.unvarifyT_global;
   106     fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
   107     val prt_term_no_vars = prt_term o Logic.unvarify_global;
   108     fun prt_const (c, ty) = [Pretty.mark_str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   109     val prt_const' = Defs.pretty_const ctxt;
   110 
   111     fun pretty_classrel (c, []) = prt_cls c
   112       | pretty_classrel (c, cs) = Pretty.block
   113           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 :: Pretty.commas (map prt_cls cs));
   114 
   115     fun pretty_default S = Pretty.block
   116       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   117 
   118     val tfrees = map (fn v => TFree (v, []));
   119     fun pretty_type syn (t, (Type.LogicalType n)) =
   120           if syn then NONE
   121           else SOME (prt_typ (Type (t, tfrees (Name.invent Name.context Name.aT n))))
   122       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'))) =
   123           if syn <> syn' then NONE
   124           else SOME (Pretty.block
   125             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   126       | pretty_type syn (t, Type.Nonterminal) =
   127           if not syn then NONE
   128           else SOME (prt_typ (Type (t, [])));
   129 
   130     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   131 
   132     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   133       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   134 
   135     fun pretty_axm (a, t) =
   136       Pretty.block [Pretty.mark_str a, Pretty.str ":", Pretty.brk 1, prt_term_no_vars t];
   137 
   138     fun pretty_finals reds = Pretty.block
   139       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
   140 
   141     fun pretty_reduct (lhs, rhs) = Pretty.block
   142       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   143         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   144 
   145     fun pretty_restrict (const, name) =
   146       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   147 
   148     val defs = Theory.defs_of thy;
   149     val {restricts, reducts} = Defs.dest defs;
   150     val tsig = Sign.tsig_of thy;
   151     val consts = Sign.consts_of thy;
   152     val {const_space, constants, constraints} = Consts.dest consts;
   153     val extern_const = Name_Space.extern ctxt const_space;
   154     val {classes, default, types, ...} = Type.rep_tsig tsig;
   155     val (class_space, class_algebra) = classes;
   156     val classes = Sorts.classes_of class_algebra;
   157     val arities = Sorts.arities_of class_algebra;
   158 
   159     val clsses =
   160       Name_Space.extern_entries ctxt class_space
   161         (map (fn ((c, _), cs) => (c, Sign.minimize_sort thy cs)) (Graph.dest classes))
   162       |> map (apfst #1);
   163     val tdecls = Name_Space.extern_table ctxt types |> map (apfst #1);
   164     val arties =
   165       Name_Space.extern_entries ctxt (Type.type_space tsig) (Symtab.dest arities)
   166       |> map (apfst #1);
   167 
   168     fun prune_const c = not verbose andalso Consts.is_concealed consts c;
   169     val cnsts =
   170       Name_Space.markup_entries ctxt const_space
   171         (filter_out (prune_const o fst) 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.markup_entries ctxt const_space constraints;
   176 
   177     val axms = Name_Space.markup_table ctxt (Theory.axiom_table thy);
   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;