]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/pp.ml
first proof reconstruction attempt, still bugged since it
[helm.git] / helm / software / components / ng_paramodulation / pp.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 module Pp (B : Terms.Blob) = struct
15
16 (* Main pretty printing functions *)
17
18 let pp_foterm ~formatter:f t =
19   let rec aux ?(toplevel=false) = function
20     | Terms.Leaf x ->
21         Format.fprintf f "%s" (B.pp x)
22     | Terms.Var i ->
23         Format.fprintf f "?%d" i
24     | Terms.Node (hd::tl) ->
25         Format.fprintf f "@[<hov 2>";
26         if not toplevel then Format.fprintf f "(";
27         aux hd;
28         List.iter (fun x -> Format.fprintf f "@;";
29                      aux x) tl;
30         if not toplevel then Format.fprintf f ")";
31         Format.fprintf f "@]"
32     | _ ->
33         assert false
34   in
35     aux ~toplevel:true t
36 ;;
37
38 let string_of_rule = function
39   | Terms.Superposition -> "Super"
40   | Terms.Demodulation ->  "Demod"
41 ;;
42
43 let string_of_direction = function
44     | Terms.Left2Right -> "Left to right"
45     | Terms.Right2Left -> "Right to left"
46     | Terms.Nodir -> "No direction"
47 ;;
48
49 let pp_substitution ~formatter:f subst =
50      (List.iter
51        (fun (i, t) ->
52           (Format.fprintf f "?%d -> " i;
53            pp_foterm f t)
54        )
55        subst)
56 ;;
57
58 let pp_proof bag ~formatter:f p =
59   let rec aux eq = function
60     | Terms.Exact t -> 
61         Format.fprintf f "%d: Exact (" eq;
62         pp_foterm f t;
63         Format.fprintf f ")@;";
64     | Terms.Step (rule,eq1,eq2,dir,pos,subst) ->
65         Format.fprintf f "%d: %s("
66           eq (string_of_rule rule);
67         Format.fprintf f "|%d with %d dir %s))" eq1 eq2
68           (string_of_direction dir);
69         let (_, _, _, proof1) = Terms.M.find eq1 bag in
70         let (_, _, _, proof2) = Terms.M.find eq2 bag in
71           Format.fprintf f "@[<v 2>";
72           aux eq1 proof1;           
73           aux eq2 proof2;
74           Format.fprintf f "@]";
75   in
76     Format.fprintf f "@[<v>";
77     aux 0 p;
78     Format.fprintf f "@]"
79 ;;
80
81 let string_of_comparison = function
82   | Terms.Lt -> "=<="
83   | Terms.Gt -> "=>="
84   | Terms.Eq -> "==="
85   | Terms.Incomparable -> "=?="
86
87 let pp_unit_clause ~formatter:f c =
88   let (id, l, vars, proof) = c in
89     Format.fprintf f "Id : %3d, " id ;
90     match l with
91       | Terms.Predicate t ->
92           pp_foterm f t
93       | Terms.Equation (lhs, rhs, ty, comp) ->
94           Format.fprintf f "@[<hv>{";
95           pp_foterm f ty;
96           Format.fprintf f "}:@;@[<hv>";
97           pp_foterm f lhs;
98           Format.fprintf f "@;%s@;" (string_of_comparison comp);
99           pp_foterm f rhs;
100           Format.fprintf f "@]@;[%s] by %s@]"
101             (String.concat ", " (List.map string_of_int vars))
102             (match proof with
103             | Terms.Exact _ -> "axiom"
104             | Terms.Step (rule, id1, id2, _, p, _) -> 
105                  Printf.sprintf "%s %d with %d at %s" 
106                   (string_of_rule rule)
107                   id1 id2 (String.concat
108                  "," (List.map string_of_int p)))
109 ;;
110
111 let pp_bag ~formatter:f bag = 
112   Format.fprintf f "@[<v>";
113   Terms.M.iter 
114   (fun _ c -> pp_unit_clause ~formatter:f c;Format.fprintf f "@;") bag;
115   Format.fprintf f "@]"
116 ;;
117
118 (* String buffer implementation *)
119 let on_buffer f t = 
120   let buff = Buffer.create 100 in
121   let formatter = Format.formatter_of_buffer buff in
122   f ~formatter:formatter t;
123   Format.fprintf formatter "@?";
124   Buffer.contents buff
125 ;;
126
127 let pp_bag =
128   on_buffer pp_bag
129 ;;
130
131 let pp_foterm =
132  on_buffer pp_foterm
133 ;;
134
135 let pp_substitution =
136   on_buffer pp_substitution
137 ;;
138
139 let pp_proof bag =
140   on_buffer (pp_proof bag)
141 ;;
142
143 let pp_unit_clause =
144   on_buffer pp_unit_clause
145 ;;
146
147 end