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