src/Pure/Isar/proof_display.ML
author wenzelm
Fri Jan 19 22:08:02 2007 +0100 (2007-01-19)
changeset 22095 07875394618e
parent 22086 cf6019fece63
child 22335 6c4204df6863
permissions -rw-r--r--
moved ML context stuff to from Context to ML_Context;
     1 (*  Title:      Pure/Isar/proof_display.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Printing of theorems, goals, results etc.
     6 *)
     7 
     8 signature BASIC_PROOF_DISPLAY =
     9 sig
    10   val print_theorems: theory -> unit
    11   val print_theory: theory -> unit
    12 end
    13 
    14 signature PROOF_DISPLAY =
    15 sig
    16   include BASIC_PROOF_DISPLAY
    17   val pprint_context: Proof.context -> pprint_args -> unit
    18   val pprint_typ: theory -> typ -> pprint_args -> unit
    19   val pprint_term: theory -> term -> pprint_args -> unit
    20   val pprint_ctyp: ctyp -> pprint_args -> unit
    21   val pprint_cterm: cterm -> pprint_args -> unit
    22   val pprint_thm: thm -> pprint_args -> unit
    23   val print_theorems_diff: theory -> theory -> unit
    24   val print_full_theory: bool -> theory -> unit
    25   val string_of_rule: Proof.context -> string -> thm -> string
    26   val print_results: bool -> Proof.context ->
    27     ((string * string) * (string * thm list) list) -> unit
    28   val present_results: Proof.context ->
    29     ((string * string) * (string * thm list) list) -> unit
    30   val pretty_consts: Proof.context -> (string * typ -> bool) -> (string * typ) list -> Pretty.T
    31 end
    32 
    33 structure ProofDisplay: PROOF_DISPLAY =
    34 struct
    35 
    36 (* toplevel pretty printing *)
    37 
    38 fun pprint_context ctxt = Pretty.pprint
    39  (if ! ProofContext.debug then
    40     Pretty.quote (Pretty.big_list "proof context:" (ProofContext.pretty_context ctxt))
    41   else Pretty.str "<context>");
    42 
    43 fun pprint pretty thy = Pretty.pprint o Pretty.quote o pretty (ProofContext.init thy);
    44 
    45 val pprint_typ = pprint ProofContext.pretty_typ;
    46 val pprint_term = pprint ProofContext.pretty_term;
    47 fun pprint_ctyp cT = pprint_typ (Thm.theory_of_ctyp cT) (Thm.typ_of cT);
    48 fun pprint_cterm ct = pprint_term (Thm.theory_of_cterm ct) (Thm.term_of ct);
    49 val pprint_thm = Pretty.pprint o ProofContext.pretty_thm_legacy true;
    50 
    51 
    52 (* theorems and theory *)
    53 
    54 fun pretty_theorems_diff prev_thms thy =
    55   let
    56     val ctxt = ProofContext.init thy;
    57     val (space, thms) = PureThy.theorems_of thy;
    58     val diff_thmss = Symtab.fold (fn fact =>
    59       if not (Symtab.member Thm.eq_thms prev_thms fact) then cons fact else I) thms [];
    60     val thmss = diff_thmss |> map (apfst (NameSpace.extern space)) |> Library.sort_wrt #1;
    61   in Pretty.big_list "theorems:" (map (ProofContext.pretty_fact ctxt) thmss) end;
    62 
    63 fun print_theorems_diff prev_thy thy =
    64   Pretty.writeln (pretty_theorems_diff (#2 (PureThy.theorems_of prev_thy)) thy);
    65 
    66 fun pretty_theorems thy = pretty_theorems_diff Symtab.empty thy;
    67 
    68 val print_theorems = Pretty.writeln o pretty_theorems;
    69 
    70 fun print_full_theory verbose thy =
    71   Pretty.writeln (Pretty.chunks (Display.pretty_full_theory verbose thy @ [pretty_theorems thy]));
    72 
    73 val print_theory = print_full_theory false;
    74 
    75 
    76 (* refinement rule *)
    77 
    78 fun pretty_rule ctxt s thm =
    79   Pretty.block [Pretty.str (s ^ " attempt to solve goal by exported rule:"),
    80     Pretty.fbrk, ProofContext.pretty_thm ctxt thm];
    81 
    82 val string_of_rule = Pretty.string_of ooo pretty_rule;
    83 
    84 
    85 (* results *)
    86 
    87 local
    88 
    89 fun pretty_facts ctxt =
    90   flat o (separate [Pretty.fbrk, Pretty.str "and "]) o
    91     map (single o ProofContext.pretty_fact ctxt);
    92 
    93 fun pretty_results ctxt ((kind, ""), facts) =
    94       Pretty.block (Pretty.str kind :: Pretty.brk 1 :: pretty_facts ctxt facts)
    95   | pretty_results ctxt ((kind, name), [fact]) = Pretty.blk (1,
    96       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, ProofContext.pretty_fact ctxt fact])
    97   | pretty_results ctxt ((kind, name), facts) = Pretty.blk (1,
    98       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk,
    99         Pretty.blk (1, Pretty.str "(" :: pretty_facts ctxt facts @ [Pretty.str ")"])]);
   100 
   101 fun name_results "" res = res
   102   | name_results name res =
   103       let
   104         val n = length (maps snd res);
   105         fun name_res (a, ths) i =
   106           let
   107             val m = length ths;
   108             val j = i + m;
   109           in
   110             if a <> "" then ((a, ths), j)
   111             else if m = n then ((name, ths), j)
   112             else if m = 1 then
   113               ((PureThy.string_of_thmref (NameSelection (name, [Single i])), ths), j)
   114             else ((PureThy.string_of_thmref (NameSelection (name, [FromTo (i, j - 1)])), ths), j)
   115           end;
   116       in fst (fold_map name_res res 1) end;
   117 
   118 in
   119 
   120 fun print_results true = Pretty.writeln oo pretty_results
   121   | print_results false = K (K ());
   122 
   123 fun present_results ctxt ((kind, name), res) =
   124   if kind = "" orelse kind = Thm.internalK then ()
   125   else (print_results true ctxt ((kind, name), res);
   126     ML_Context.setmp (SOME (Context.Proof ctxt))
   127       (Present.results kind) (name_results name res));
   128 
   129 end;
   130 
   131 
   132 (* consts *)
   133 
   134 local
   135 
   136 fun pretty_var ctxt (x, T) =
   137   Pretty.block [Pretty.str x, Pretty.str " ::", Pretty.brk 1,
   138     Pretty.quote (ProofContext.pretty_typ ctxt T)];
   139 
   140 fun pretty_vars ctxt kind vs = Pretty.big_list kind (map (pretty_var ctxt) vs);
   141 
   142 in
   143 
   144 fun pretty_consts ctxt pred cs =
   145   (case filter pred (#1 (ProofContext.inferred_fixes ctxt)) of
   146     [] => pretty_vars ctxt "constants" cs
   147   | ps => Pretty.chunks [pretty_vars ctxt "parameters" ps, pretty_vars ctxt "constants" cs]);
   148 
   149 end;
   150 
   151 end;
   152 
   153 structure BasicProofDisplay: BASIC_PROOF_DISPLAY = ProofDisplay;
   154 open BasicProofDisplay;