src/Pure/display.ML
author bulwahn
Tue Aug 31 08:00:53 2010 +0200 (2010-08-31)
changeset 38950 62578950e748
parent 37248 8e8e5f9d1441
child 39050 600de0485859
permissions -rw-r--r--
storing options for prolog code generation in the theory
     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 Unsynchronized.ref
    11   val show_consts: bool Unsynchronized.ref
    12   val show_hyps: bool Unsynchronized.ref
    13   val show_tags: bool Unsynchronized.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 print_syntax: theory -> unit
    31   val pretty_full_theory: bool -> theory -> Pretty.T list
    32 end;
    33 
    34 structure Display: DISPLAY =
    35 struct
    36 
    37 (** options **)
    38 
    39 val goals_limit = Goal_Display.goals_limit;
    40 val show_consts = Goal_Display.show_consts;
    41 
    42 val show_hyps = Unsynchronized.ref false;    (*false: print meta-hypotheses as dots*)
    43 val show_tags = Unsynchronized.ref false;    (*false: suppress tags*)
    44 
    45 
    46 
    47 (** print thm **)
    48 
    49 fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
    50 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    51 
    52 fun display_status false _ = ""
    53   | display_status true th =
    54       let
    55         val {oracle = oracle0, unfinished, failed} = Thm.status_of th;
    56         val oracle = oracle0 andalso (not (! quick_and_dirty) orelse ! show_hyps);
    57       in
    58         if failed then "!!"
    59         else if oracle andalso unfinished then "!?"
    60         else if oracle then "!"
    61         else if unfinished then "?"
    62         else ""
    63       end;
    64 
    65 fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps', show_status} raw_th =
    66   let
    67     val th = Thm.strip_shyps raw_th;
    68     val {hyps, tpairs, prop, ...} = Thm.rep_thm th;
    69     val xshyps = Thm.extra_shyps th;
    70     val tags = Thm.get_tags th;
    71 
    72     val q = if quote then Pretty.quote else I;
    73     val prt_term = q o Syntax.pretty_term ctxt;
    74 
    75     val asms = map Thm.term_of (Assumption.all_assms_of ctxt);
    76     val hyps' = if ! show_hyps then hyps else subtract (op aconv) asms hyps;
    77     val status = display_status show_status th;
    78 
    79     val hlen = length xshyps + length hyps' + length tpairs;
    80     val hsymbs =
    81       if hlen = 0 andalso status = "" then []
    82       else if ! show_hyps orelse show_hyps' then
    83         [Pretty.brk 2, Pretty.list "[" "]"
    84           (map (q o Goal_Display.pretty_flexpair ctxt) tpairs @ map prt_term hyps' @
    85            map (Syntax.pretty_sort ctxt) xshyps @
    86             (if status = "" then [] else [Pretty.str status]))]
    87       else [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ status ^ "]")];
    88     val tsymbs =
    89       if null tags orelse not (! show_tags) then []
    90       else [Pretty.brk 1, pretty_tags tags];
    91   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    92 
    93 fun pretty_thm_aux ctxt show_status =
    94   pretty_thm_raw ctxt {quote = false, show_hyps = true, show_status = show_status};
    95 
    96 fun pretty_thm ctxt = pretty_thm_aux ctxt true;
    97 
    98 fun pretty_thm_global thy =
    99   pretty_thm_raw (Syntax.init_pretty_global thy)
   100     {quote = false, show_hyps = false, show_status = true};
   101 
   102 fun pretty_thm_without_context th = pretty_thm_global (Thm.theory_of_thm th) th;
   103 
   104 val string_of_thm = Pretty.string_of oo pretty_thm;
   105 val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
   106 val string_of_thm_without_context = Pretty.string_of o pretty_thm_without_context;
   107 
   108 
   109 (* multiple theorems *)
   110 
   111 fun pretty_thms_aux ctxt flag [th] = pretty_thm_aux ctxt flag th
   112   | pretty_thms_aux ctxt flag ths =
   113       Pretty.blk (0, Pretty.fbreaks (map (pretty_thm_aux ctxt flag) ths));
   114 
   115 fun pretty_thms ctxt = pretty_thms_aux ctxt true;
   116 
   117 
   118 
   119 (** print theory **)
   120 
   121 val print_syntax = Syntax.print_syntax o Sign.syn_of;
   122 
   123 
   124 (* pretty_full_theory *)
   125 
   126 fun pretty_full_theory verbose thy =
   127   let
   128     val ctxt = Syntax.init_pretty_global thy;
   129 
   130     fun prt_cls c = Syntax.pretty_sort ctxt [c];
   131     fun prt_sort S = Syntax.pretty_sort ctxt S;
   132     fun prt_arity t (c, Ss) = Syntax.pretty_arity ctxt (t, Ss, [c]);
   133     fun prt_typ ty = Pretty.quote (Syntax.pretty_typ ctxt ty);
   134     val prt_typ_no_tvars = prt_typ o Logic.unvarifyT_global;
   135     fun prt_term t = Pretty.quote (Syntax.pretty_term ctxt t);
   136     val prt_term_no_vars = prt_term o Logic.unvarify_global;
   137     fun prt_const (c, ty) = [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ_no_tvars ty];
   138     val prt_const' = Defs.pretty_const (Syntax.pp ctxt);
   139 
   140     fun pretty_classrel (c, []) = prt_cls c
   141       | pretty_classrel (c, cs) = Pretty.block
   142           (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
   143             Pretty.commas (map prt_cls cs));
   144 
   145     fun pretty_default S = Pretty.block
   146       [Pretty.str "default sort:", Pretty.brk 1, prt_sort S];
   147 
   148     val tfrees = map (fn v => TFree (v, []));
   149     fun pretty_type syn (t, (Type.LogicalType n)) =
   150           if syn then NONE
   151           else SOME (prt_typ (Type (t, tfrees (Name.invents Name.context Name.aT n))))
   152       | pretty_type syn (t, (Type.Abbreviation (vs, U, syn'))) =
   153           if syn <> syn' then NONE
   154           else SOME (Pretty.block
   155             [prt_typ (Type (t, tfrees vs)), Pretty.str " =", Pretty.brk 1, prt_typ U])
   156       | pretty_type syn (t, Type.Nonterminal) =
   157           if not syn then NONE
   158           else SOME (prt_typ (Type (t, [])));
   159 
   160     val pretty_arities = maps (fn (t, ars) => map (prt_arity t) ars);
   161 
   162     fun pretty_abbrev (c, (ty, t)) = Pretty.block
   163       (prt_const (c, ty) @ [Pretty.str " ==", Pretty.brk 1, prt_term_no_vars t]);
   164 
   165     fun pretty_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term_no_vars t];
   166 
   167     fun pretty_finals reds = Pretty.block
   168       (Pretty.str "final:" :: Pretty.brk 1 :: Pretty.commas (map (prt_const' o fst) reds));
   169 
   170     fun pretty_reduct (lhs, rhs) = Pretty.block
   171       ([prt_const' lhs, Pretty.str "  ->", Pretty.brk 2] @
   172         Pretty.commas (map prt_const' (sort_wrt #1 rhs)));
   173 
   174     fun pretty_restrict (const, name) =
   175       Pretty.block ([prt_const' const, Pretty.brk 2, Pretty.str ("(from " ^ quote name ^ ")")]);
   176 
   177     val axioms = (Theory.axiom_space thy, Theory.axiom_table thy);
   178     val defs = Theory.defs_of thy;
   179     val {restricts, reducts} = Defs.dest defs;
   180     val {naming = _, syn = _, tsig, consts} = Sign.rep_sg thy;
   181     val {constants, constraints} = Consts.dest consts;
   182     val extern_const = Name_Space.extern (#1 constants);
   183     val {classes, default, types, ...} = Type.rep_tsig tsig;
   184     val (class_space, class_algebra) = classes;
   185     val classes = Sorts.classes_of class_algebra;
   186     val arities = Sorts.arities_of class_algebra;
   187 
   188     val clsses = Name_Space.dest_table (class_space, Symtab.make (Graph.dest classes));
   189     val tdecls = Name_Space.dest_table types;
   190     val arties = Name_Space.dest_table (Sign.type_space thy, arities);
   191 
   192     fun prune_const c = not verbose andalso Consts.is_concealed consts c;
   193     val cnsts = Name_Space.extern_table (#1 constants,
   194       Symtab.make (filter_out (prune_const o fst) (Symtab.dest (#2 constants))));
   195 
   196     val log_cnsts = map_filter (fn (c, (ty, NONE)) => SOME (c, ty) | _ => NONE) cnsts;
   197     val abbrevs = map_filter (fn (c, (ty, SOME t)) => SOME (c, (ty, t)) | _ => NONE) cnsts;
   198     val cnstrs = Name_Space.extern_table constraints;
   199 
   200     val axms = Name_Space.extern_table axioms;
   201 
   202     val (reds0, (reds1, reds2)) = filter_out (prune_const o fst o fst) reducts
   203       |> map (fn (lhs, rhs) =>
   204         (apfst extern_const lhs, map (apfst extern_const) (filter_out (prune_const o fst) rhs)))
   205       |> sort_wrt (#1 o #1)
   206       |> List.partition (null o #2)
   207       ||> List.partition (Defs.plain_args o #2 o #1);
   208     val rests = restricts |> map (apfst (apfst extern_const)) |> sort_wrt (#1 o #1);
   209   in
   210     [Pretty.strs ("names:" :: Context.display_names thy)] @
   211     [Pretty.big_list "classes:" (map pretty_classrel clsses),
   212       pretty_default default,
   213       Pretty.big_list "syntactic types:" (map_filter (pretty_type true) tdecls),
   214       Pretty.big_list "logical types:" (map_filter (pretty_type false) tdecls),
   215       Pretty.big_list "type arities:" (pretty_arities arties),
   216       Pretty.big_list "logical consts:" (map (Pretty.block o prt_const) log_cnsts),
   217       Pretty.big_list "abbreviations:" (map pretty_abbrev abbrevs),
   218       Pretty.big_list "constraints:" (map (Pretty.block o prt_const) cnstrs),
   219       Pretty.big_list "axioms:" (map pretty_axm axms),
   220       Pretty.strs ("oracles:" :: Thm.extern_oracles thy),
   221       Pretty.big_list "definitions:"
   222         [pretty_finals reds0,
   223          Pretty.big_list "non-overloaded:" (map pretty_reduct reds1),
   224          Pretty.big_list "overloaded:" (map pretty_reduct reds2),
   225          Pretty.big_list "pattern restrictions:" (map pretty_restrict rests)]]
   226   end;
   227 
   228 end;
   229 
   230 structure Basic_Display: BASIC_DISPLAY = Display;
   231 open Basic_Display;