]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/utils.ml
moved string_of_equality into utils
[helm.git] / helm / ocaml / paramodulation / utils.ml
1 let print_metasenv metasenv =
2   String.concat "\n--------------------------\n"
3     (List.map (fun (i, context, term) ->
4                  (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
5                    "\n] " ^  (CicPp.ppterm term))
6        metasenv)
7 ;;
8
9
10 let print_subst subst =
11   String.concat "\n"
12     (List.map
13        (fun (i, (c, t, ty)) ->
14           Printf.sprintf "?%d -> %s : %s" i
15             (CicPp.ppterm t) (CicPp.ppterm ty))
16        subst)
17 ;;  
18
19 (* (weight of constants, [(meta, weight_of_meta)]) *)
20 type weight = int * (int * int) list;;
21
22 let string_of_weight (cw, mw) =
23   let s =
24     String.concat ", "
25       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
26   in
27   Printf.sprintf "[%d; %s]" cw s
28
29
30 let weight_of_term term =
31   (* ALB: what to consider as a variable? I think "variables" in our case are
32      Metas and maybe Rels... *)
33   let module C = Cic in
34   let vars_dict = Hashtbl.create 5 in
35   let rec aux = function
36     | C.Meta (metano, _) ->
37         (try
38            let oldw = Hashtbl.find vars_dict metano in
39            Hashtbl.replace vars_dict metano (oldw+1)
40          with Not_found ->
41            Hashtbl.add vars_dict metano 1);
42         0
43           
44     | C.Var (_, ens)
45     | C.Const (_, ens)
46     | C.MutInd (_, _, ens)
47     | C.MutConstruct (_, _, _, ens) ->
48         List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
49           
50     | C.Cast (t1, t2)
51     | C.Lambda (_, t1, t2)
52     | C.Prod (_, t1, t2)
53     | C.LetIn (_, t1, t2) ->
54         let w1 = aux t1 in
55         let w2 = aux t2 in
56         w1 + w2 + 1
57           
58     | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
59         
60     | C.MutCase (_, _, outt, t, pl) ->
61         let w1 = aux outt in
62         let w2 = aux t in
63         let w3 = List.fold_left (+) 0 (List.map aux pl) in
64         w1 + w2 + w3 + 1
65           
66     | C.Fix (_, fl) ->
67         List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
68           
69     | C.CoFix (_, fl) ->
70         List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
71           
72     | _ -> 1
73   in
74   let w = aux term in
75   let l =
76     Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
77   let compare w1 w2 = 
78     match w1, w2 with
79     | (m1, _), (m2, _) -> m2 - m1 
80   in
81   (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
82 ;;
83
84
85 (* returns a "normalized" version of the polynomial weight wl (with type
86  * weight list), i.e. a list sorted ascending by meta number,
87  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
88  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
89  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
90 let normalize_weight maxmeta (cw, wl) =
91 (*   Printf.printf "normalize_weight: %d, %s\n" maxmeta *)
92 (*     (string_of_weight (cw, wl)); *)
93   let rec aux = function
94     | 0 -> []
95     | m -> (m, 0)::(aux (m-1))
96   in
97   let tmpl = aux maxmeta in
98   let wl =
99     List.sort
100       (fun (m, _) (n, _) -> Pervasives.compare m n)
101       (List.fold_left
102          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
103   in
104   (cw, wl)
105 ;;
106
107
108 let normalize_weights (cw1, wl1) (cw2, wl2) =
109   let rec aux wl1 wl2 =
110     match wl1, wl2 with
111     | [], [] -> [], []
112     | (m, w)::tl1, (n, w')::tl2 when m = n ->
113         let res1, res2 = aux tl1 tl2 in
114         (m, w)::res1, (n, w')::res2
115     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
116         let res1, res2 = aux tl1 wl2 in
117         (m, w)::res1, (m, 0)::res2
118     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
119         let res1, res2 = aux wl1 tl2 in
120         (n, 0)::res1, (n, w')::res2
121     | [], (n, w)::tl2 ->
122         let res1, res2 = aux [] tl2 in
123         (n, 0)::res1, (n, w)::res2
124     | (m, w)::tl1, [] ->
125         let res1, res2 = aux tl1 [] in
126         (m, w)::res1, (m, 0)::res2
127   in
128   let cmp (m, _) (n, _) = compare m n in
129   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
130   (cw1, wl1), (cw2, wl2)
131 ;;
132
133         
134 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
135     
136 let string_of_comparison = function
137   | Lt -> "<"
138   | Le -> "<="
139   | Gt -> ">"
140   | Ge -> ">="
141   | Eq -> "="
142   | Incomparable -> "I"
143
144
145 let compare_weights ?(normalize=false)
146     ((h1, w1) as weight1) ((h2, w2) as weight2)=
147   let (h1, w1), (h2, w2) =
148     if normalize then
149 (*       let maxmeta =  *)
150 (*         let maxmeta l = *)
151 (*           try *)
152 (*             match List.hd l with *)
153 (*             | (m, _) -> m *)
154 (*           with Failure _ -> 0 *)
155 (*         in *)
156 (*         max (maxmeta w1) (maxmeta w2) *)
157 (*       in *)
158 (*       (normalize_weight maxmeta (h1, w1)), (normalize_weight maxmeta (h2, w2)) *)
159       normalize_weights weight1 weight2
160     else
161       (h1, w1), (h2, w2)
162   in
163   let res, diffs =
164     try
165       List.fold_left2
166         (fun ((lt, eq, gt), diffs) w1 w2 ->
167            match w1, w2 with
168            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
169                let diffs = (w1 - w2) + diffs in 
170                let r = compare w1 w2 in
171                if r < 0 then (lt+1, eq, gt), diffs
172                else if r = 0 then (lt, eq+1, gt), diffs
173                else (lt, eq, gt+1), diffs
174            | (meta1, w1), (meta2, w2) ->
175                Printf.printf "HMMM!!!! %s, %s\n"
176                  (string_of_weight weight1) (string_of_weight weight2);
177                assert false)
178         ((0, 0, 0), 0) w1 w2
179     with Invalid_argument _ ->
180       Printf.printf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
181         (string_of_weight (h1, w1)) (string_of_weight weight1)
182         (string_of_weight (h2, w2)) (string_of_weight weight2)
183         (string_of_bool normalize);
184       assert false
185   in
186   let hdiff = h1 - h2 in
187   match res with
188   | (0, _, 0) ->
189       if hdiff < 0 then Lt
190       else if hdiff > 0 then Gt
191       else Eq (* Incomparable *)
192   | (m, _, 0) ->
193       if hdiff <= 0 then 
194         if m > 0 || hdiff < 0 then Lt
195         else if diffs >= (- hdiff) then Le else Incomparable
196       else 
197         if diffs >= (- hdiff) then Le else Incomparable
198   | (0, _, m) ->
199       if hdiff >= 0 then 
200         if m > 0 || hdiff > 0 then Gt
201         else if (- diffs) >= hdiff then Ge else Incomparable
202       else
203         if (- diffs) >= hdiff then Ge else Incomparable
204   | (m, _, n) when m > 0 && n > 0 ->
205       Incomparable
206 ;;
207
208
209 let rec aux_ordering t1 t2 =
210   let module C = Cic in
211   let compare_uris u1 u2 =
212     let res =
213       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
214     if res < 0 then Lt
215     else if res = 0 then Eq
216     else Gt
217   in
218   match t1, t2 with
219   | C.Meta _, _
220   | _, C.Meta _ -> Incomparable
221
222   | t1, t2 when t1 = t2 -> Eq
223
224   | C.Rel n, C.Rel m -> if n > m then Lt else Gt (* ALB: changed < to > *)
225   | C.Rel _, _ -> Lt
226   | _, C.Rel _ -> Gt
227
228   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
229   | C.Const _, _ -> Lt
230   | _, C.Const _ -> Gt
231
232   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
233   | C.MutInd _, _ -> Lt
234   | _, C.MutInd _ -> Gt
235
236   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
237       compare_uris u1 u2
238   | C.MutConstruct _, _ -> Lt
239   | _, C.MutConstruct _ -> Gt
240
241   | C.Appl l1, C.Appl l2 ->
242       let rec cmp t1 t2 =
243         match t1, t2 with
244         | [], [] -> Eq
245         | _, [] -> Gt
246         | [], _ -> Lt
247         | hd1::tl1, hd2::tl2 ->
248             let o = aux_ordering hd1 hd2 in
249             if o = Eq then cmp tl1 tl2
250             else o
251       in
252       cmp l1 l2
253         
254   | t1, t2 ->
255       Printf.printf "These two terms are not comparable:\n%s\n%s\n\n"
256         (CicPp.ppterm t1) (CicPp.ppterm t2);
257       Incomparable
258 ;;
259
260
261 (* w1, w2 are the weights, they should already be normalized... *)
262 let nonrec_kbo_w (t1, w1) (t2, w2) =
263   match compare_weights w1 w2 with
264   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
265   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
266   | Eq -> aux_ordering t1 t2
267   | res -> res
268 ;;
269
270     
271 let nonrec_kbo t1 t2 =
272   let w1 = weight_of_term t1 in
273   let w2 = weight_of_term t2 in
274   match compare_weights ~normalize:true w1 w2 with
275   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
276   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
277   | Eq -> aux_ordering t1 t2
278   | res -> res
279 ;;
280
281
282 let names_of_context context = 
283   List.map
284     (function
285        | None -> None
286        | Some (n, e) -> Some n)
287     context
288 ;;
289
290
291 let string_of_equality ?env =
292   match env with
293   | None -> (
294       function
295         | _, (ty, left, right), _, _ ->
296             Printf.sprintf "{%s}: %s = %s" (CicPp.ppterm ty)
297               (CicPp.ppterm left) (CicPp.ppterm right)
298     )
299   | Some (_, context, _) -> (
300       let names = names_of_context context in
301       function
302         | _, (ty, left, right), _, _ ->
303             Printf.sprintf "{%s}: %s = %s" (CicPp.pp ty names)
304               (CicPp.pp left names) (CicPp.pp right names)
305     )
306 ;;
307
308