]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/pp.ml
a125101bcf212563b6b6a08f7b8c0cb50a6884e9
[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.SuperpositionRight -> "SupR"
40   | Terms.SuperpositionLeft -> "SupL"
41   | Terms.Demodulation -> "Demod"
42 ;;
43
44 let string_of_direction = function
45     | Terms.Left2Right -> "Left to right"
46     | Terms.Right2Left -> "Right to left"
47     | Terms.Nodir -> "No direction"
48 ;;
49
50 let pp_substitution ~formatter:f subst =
51      (List.iter
52        (fun (i, t) ->
53           (Format.fprintf f "?%d -> " i;
54            pp_foterm f t)
55        )
56        subst)
57 ;;
58
59 let pp_proof bag ~formatter:f p =
60   let rec aux eq = function
61     | Terms.Exact t -> 
62         Format.fprintf f "%d: Exact (" eq;
63         pp_foterm f t;
64         Format.fprintf f ")@;";
65     | Terms.Step (rule,eq1,eq2,dir,pos,subst) ->
66         Format.fprintf f "%d: %s("
67           eq (string_of_rule rule);
68         Format.fprintf f "|%d with %d dir %s))" eq1 eq2
69           (string_of_direction dir);
70         let (_, _, _, proof1) = Terms.M.find eq1 bag in
71         let (_, _, _, proof2) = Terms.M.find eq2 bag in
72           Format.fprintf f "@[<v 2>";
73           aux eq1 proof1;           
74           aux eq2 proof2;
75           Format.fprintf f "@]";
76   in
77     Format.fprintf f "@[<v>";
78     aux 0 p;
79     Format.fprintf f "@]"
80 ;;
81
82 let string_of_comparison = function
83   | Terms.Lt -> "=<="
84   | Terms.Gt -> "=>="
85   | Terms.Eq -> "==="
86   | Terms.Incomparable -> "=?="
87
88 let pp_unit_clause ~formatter:f c =
89   let (id, l, vars, proof) = c in
90     Format.fprintf f "Id : %d, " id ;
91     match l with
92       | Terms.Predicate t ->
93           pp_foterm f t
94       | Terms.Equation (lhs, rhs, ty, comp) ->
95           Format.fprintf f "{";
96           pp_foterm f ty;
97           Format.fprintf f "}: ";
98           pp_foterm f lhs;
99           Format.fprintf f " %s " (string_of_comparison comp);
100           pp_foterm f rhs;
101           Format.fprintf f " [%s] by %s"
102             (String.concat ", " (List.map string_of_int vars))
103             (match proof with
104             | Terms.Exact _ -> "axiom"
105             | Terms.Step (rule, id1, id2, _, p, _) -> 
106                  Printf.sprintf "%s %d with %d at %s" 
107                   (string_of_rule rule)
108                   id1 id2 (String.concat
109                  "," (List.map string_of_int p)))
110
111 ;;
112
113 let pp_bag ~formatter:f bag = 
114   Format.fprintf f "@[<v>";
115   Terms.M.iter 
116   (fun _ c -> pp_unit_clause ~formatter:f c;Format.fprintf f "@;") bag;
117   Format.fprintf f "@]"
118 ;;
119
120 (* String buffer implementation *)
121 let on_buffer f t = 
122   let buff = Buffer.create 100 in
123   let formatter = Format.formatter_of_buffer buff in
124   f ~formatter:formatter t;
125   Format.fprintf formatter "@?";
126   Buffer.contents buff
127 ;;
128
129 let pp_bag =
130   on_buffer pp_bag
131 ;;
132
133 let pp_foterm =
134  on_buffer pp_foterm
135 ;;
136
137 let pp_substitution =
138   on_buffer pp_substitution
139 ;;
140
141 let pp_proof bag =
142   on_buffer (pp_proof bag)
143 ;;
144
145 let pp_unit_clause =
146   on_buffer pp_unit_clause
147 ;;
148
149 end