src/Pure/Isar/proof_display.ML
changeset 28092 5886e9359aa8
parent 28087 a9cccdd9d521
child 28210 c164d1892553
equal deleted inserted replaced
28091:50f2d6ba024c 28092:5886e9359aa8
     3     Author:     Makarius
     3     Author:     Makarius
     4 
     4 
     5 Printing of theorems, goals, results etc.
     5 Printing of theorems, goals, results etc.
     6 *)
     6 *)
     7 
     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 =
     8 signature PROOF_DISPLAY =
    15 sig
     9 sig
    16   include BASIC_PROOF_DISPLAY
       
    17   val pprint_context: Proof.context -> pprint_args -> unit
    10   val pprint_context: Proof.context -> pprint_args -> unit
    18   val pprint_typ: theory -> typ -> pprint_args -> unit
    11   val pprint_typ: theory -> typ -> pprint_args -> unit
    19   val pprint_term: theory -> term -> pprint_args -> unit
    12   val pprint_term: theory -> term -> pprint_args -> unit
    20   val pprint_ctyp: ctyp -> pprint_args -> unit
    13   val pprint_ctyp: ctyp -> pprint_args -> unit
    21   val pprint_cterm: cterm -> pprint_args -> unit
    14   val pprint_cterm: cterm -> pprint_args -> unit
    22   val pprint_thm: thm -> pprint_args -> unit
    15   val pprint_thm: thm -> pprint_args -> unit
    23   val print_theorems_diff: theory -> theory -> unit
    16   val print_theorems_diff: theory -> theory -> unit
       
    17   val print_theorems: theory -> unit
    24   val pretty_full_theory: bool -> theory -> Pretty.T
    18   val pretty_full_theory: bool -> theory -> Pretty.T
       
    19   val print_theory: theory -> unit
    25   val string_of_rule: Proof.context -> string -> thm -> string
    20   val string_of_rule: Proof.context -> string -> thm -> string
    26   val print_results: bool -> Proof.context -> ((string * string) * (string * thm list) list) -> unit
    21   val print_results: bool -> Proof.context -> ((string * string) * (string * thm list) list) -> unit
    27   val add_hook: ((string * thm list) list -> unit) -> unit
       
    28   val theory_results: Proof.context -> ((string * string) * (string * thm list) list) -> unit
       
    29   val pretty_consts: Proof.context -> (string * typ -> bool) -> (string * typ) list -> Pretty.T
    22   val pretty_consts: Proof.context -> (string * typ -> bool) -> (string * typ) list -> Pretty.T
    30 end
    23 end
    31 
    24 
    32 structure ProofDisplay: PROOF_DISPLAY =
    25 structure ProofDisplay: PROOF_DISPLAY =
    33 struct
    26 struct
    77 val string_of_rule = Pretty.string_of ooo pretty_rule;
    70 val string_of_rule = Pretty.string_of ooo pretty_rule;
    78 
    71 
    79 
    72 
    80 (* results *)
    73 (* results *)
    81 
    74 
       
    75 local
       
    76 
    82 fun pretty_fact_name (kind, "") = Pretty.str kind
    77 fun pretty_fact_name (kind, "") = Pretty.str kind
    83   | pretty_fact_name (kind, name) = Pretty.block [Pretty.str kind, Pretty.brk 1,
    78   | pretty_fact_name (kind, name) = Pretty.block [Pretty.str kind, Pretty.brk 1,
    84       Pretty.str (NameSpace.base name), Pretty.str ":"];
    79       Pretty.str (NameSpace.base name), Pretty.str ":"];
    85 
       
    86 local
       
    87 
    80 
    88 fun pretty_facts ctxt =
    81 fun pretty_facts ctxt =
    89   flat o (separate [Pretty.fbrk, Pretty.str "and "]) o
    82   flat o (separate [Pretty.fbrk, Pretty.str "and "]) o
    90     map (single o ProofContext.pretty_fact ctxt);
    83     map (single o ProofContext.pretty_fact ctxt);
    91 
    84 
    92 fun pretty_results ctxt ((kind, ""), facts) =
       
    93       Pretty.block (Pretty.str kind :: Pretty.brk 1 :: pretty_facts ctxt facts)
       
    94   | pretty_results ctxt (kind_name, [fact]) = Pretty.blk (1,
       
    95       [pretty_fact_name kind_name, Pretty.fbrk, ProofContext.pretty_fact ctxt fact])
       
    96   | pretty_results ctxt (kind_name, facts) = Pretty.blk (1,
       
    97       [pretty_fact_name kind_name, Pretty.fbrk, Pretty.blk (1,
       
    98         Pretty.str "(" :: pretty_facts ctxt facts @ [Pretty.str ")"])]);
       
    99 
       
   100 in
    85 in
   101 
    86 
   102 fun print_results true = Pretty.writeln oo pretty_results
    87 fun print_results do_print ctxt ((kind, name), facts) =
   103   | print_results false = K (K ());
    88   if not do_print orelse kind = "" orelse kind = Thm.internalK then ()
   104 
    89   else if name = "" then
   105 end;
    90     Pretty.writeln (Pretty.block (Pretty.str kind :: Pretty.brk 1 :: pretty_facts ctxt facts))
   106 
    91   else Pretty.writeln
   107 
    92     (case facts of
   108 (* theory results -- subject to hook *)
    93       [fact] => Pretty.blk (1, [pretty_fact_name (kind, name), Pretty.fbrk,
   109 
    94         ProofContext.pretty_fact ctxt fact])
   110 local val hooks = ref ([]: ((string * thm list) list -> unit) list) in
    95     | _ => Pretty.blk (1, [pretty_fact_name (kind, name), Pretty.fbrk,
   111 
    96         Pretty.blk (1, Pretty.str "(" :: pretty_facts ctxt facts @ [Pretty.str ")"])]));
   112 fun add_hook f = CRITICAL (fn () => change hooks (cons f));
       
   113 fun get_hooks () = CRITICAL (fn () => ! hooks);
       
   114 
       
   115 end;
       
   116 
       
   117 local
       
   118 
       
   119 fun name_results "" res = res
       
   120   | name_results name res =
       
   121       let
       
   122         val n = length (maps snd res);
       
   123         fun name_res (a, ths) i =
       
   124           let
       
   125             val m = length ths;
       
   126             val j = i + m;
       
   127           in
       
   128             if a <> "" then ((a, ths), j)
       
   129             else if m = n then ((name, ths), j)
       
   130             else ((Facts.string_of_ref (Facts.Named ((name, Position.none),
       
   131               SOME ([if m = 1 then Facts.Single i else Facts.FromTo (i, j - 1)]))), ths), j)
       
   132           end;
       
   133       in fst (fold_map name_res res 1) end;
       
   134 
       
   135 in
       
   136 
       
   137 fun theory_results ctxt ((kind, name), res) =
       
   138   if kind = "" orelse kind = Thm.internalK then ()
       
   139   else
       
   140     let
       
   141       val _ = print_results true ctxt ((kind, name), res);
       
   142       val res' = name_results name res;
       
   143       val _ = List.app (fn f => f res') (get_hooks ());
       
   144     in () end;
       
   145 
    97 
   146 end;
    98 end;
   147 
    99 
   148 
   100 
   149 (* consts *)
   101 (* consts *)
   164   | ps => Pretty.chunks [pretty_vars ctxt "parameters" ps, pretty_vars ctxt "constants" cs]);
   116   | ps => Pretty.chunks [pretty_vars ctxt "parameters" ps, pretty_vars ctxt "constants" cs]);
   165 
   117 
   166 end;
   118 end;
   167 
   119 
   168 end;
   120 end;
   169 
       
   170 structure BasicProofDisplay: BASIC_PROOF_DISPLAY = ProofDisplay;
       
   171 open BasicProofDisplay;