]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/utils.ml
fixed bug in compute_equality_weight caused by wrong assumption on function
[helm.git] / helm / ocaml / paramodulation / utils.ml
1 let debug = true;;
2
3 let debug_print = if debug then prerr_endline else ignore;;
4
5
6 let print_metasenv metasenv =
7   String.concat "\n--------------------------\n"
8     (List.map (fun (i, context, term) ->
9                  (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
10                    "\n] " ^  (CicPp.ppterm term))
11        metasenv)
12 ;;
13
14
15 let print_subst ?(prefix="\n") subst =
16   String.concat prefix
17     (List.map
18        (fun (i, (c, t, ty)) ->
19           Printf.sprintf "?%d -> %s : %s" i
20             (CicPp.ppterm t) (CicPp.ppterm ty))
21        subst)
22 ;;  
23
24 (* (weight of constants, [(meta, weight_of_meta)]) *)
25 type weight = int * (int * int) list;;
26
27 let string_of_weight (cw, mw) =
28   let s =
29     String.concat ", "
30       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
31   in
32   Printf.sprintf "[%d; %s]" cw s
33
34
35 let weight_of_term ?(consider_metas=true) term =
36   (* ALB: what to consider as a variable? I think "variables" in our case are
37      Metas and maybe Rels... *)
38   let module C = Cic in
39   let vars_dict = Hashtbl.create 5 in
40   let rec aux = function
41     | C.Meta (metano, _) when consider_metas ->
42         (try
43            let oldw = Hashtbl.find vars_dict metano in
44            Hashtbl.replace vars_dict metano (oldw+1)
45          with Not_found ->
46            Hashtbl.add vars_dict metano 1);
47         0
48     | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
49                   
50     | C.Var (_, ens)
51     | C.Const (_, ens)
52     | C.MutInd (_, _, ens)
53     | C.MutConstruct (_, _, _, ens) ->
54         List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
55           
56     | C.Cast (t1, t2)
57     | C.Lambda (_, t1, t2)
58     | C.Prod (_, t1, t2)
59     | C.LetIn (_, t1, t2) ->
60         let w1 = aux t1 in
61         let w2 = aux t2 in
62         w1 + w2 + 1
63           
64     | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
65         
66     | C.MutCase (_, _, outt, t, pl) ->
67         let w1 = aux outt in
68         let w2 = aux t in
69         let w3 = List.fold_left (+) 0 (List.map aux pl) in
70         w1 + w2 + w3 + 1
71           
72     | C.Fix (_, fl) ->
73         List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
74           
75     | C.CoFix (_, fl) ->
76         List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
77           
78     | _ -> 1
79   in
80   let w = aux term in
81   let l =
82     Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
83   let compare w1 w2 = 
84     match w1, w2 with
85     | (m1, _), (m2, _) -> m2 - m1 
86   in
87   (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
88 ;;
89
90
91 module OrderedInt = struct
92   type t = int
93
94   let compare = Pervasives.compare
95 end
96
97 module IntSet = Set.Make(OrderedInt)
98
99 let compute_equality_weight ty left right =
100 (*   let metasw = ref IntSet.empty in *)
101   let metasw = ref 0 in
102   let weight_of t =
103     let w, m = (weight_of_term ~consider_metas:true(* false *) t) in
104 (*     let mw = List.fold_left (fun mw (_, c) -> mw + 2 * c) 0 m in *)
105 (*     metasw := !metasw + mw; *)
106     metasw := !metasw + (2 * (List.length m));
107 (*     metasw := List.fold_left (fun s (i, _) -> IntSet.add i s) !metasw m; *)
108     w
109   in
110   let w = (weight_of ty) + (weight_of left) + (weight_of right) in
111   w + !metasw
112 (*     (4 * IntSet.cardinal !metasw) *)
113 ;;
114
115
116 (* returns a "normalized" version of the polynomial weight wl (with type
117  * weight list), i.e. a list sorted ascending by meta number,
118  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
119  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
120  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
121 let normalize_weight maxmeta (cw, wl) =
122 (*   Printf.printf "normalize_weight: %d, %s\n" maxmeta *)
123 (*     (string_of_weight (cw, wl)); *)
124   let rec aux = function
125     | 0 -> []
126     | m -> (m, 0)::(aux (m-1))
127   in
128   let tmpl = aux maxmeta in
129   let wl =
130     List.sort
131       (fun (m, _) (n, _) -> Pervasives.compare m n)
132       (List.fold_left
133          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
134   in
135   (cw, wl)
136 ;;
137
138
139 let normalize_weights (cw1, wl1) (cw2, wl2) =
140   let rec aux wl1 wl2 =
141     match wl1, wl2 with
142     | [], [] -> [], []
143     | (m, w)::tl1, (n, w')::tl2 when m = n ->
144         let res1, res2 = aux tl1 tl2 in
145         (m, w)::res1, (n, w')::res2
146     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
147         let res1, res2 = aux tl1 wl2 in
148         (m, w)::res1, (m, 0)::res2
149     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
150         let res1, res2 = aux wl1 tl2 in
151         (n, 0)::res1, (n, w')::res2
152     | [], (n, w)::tl2 ->
153         let res1, res2 = aux [] tl2 in
154         (n, 0)::res1, (n, w)::res2
155     | (m, w)::tl1, [] ->
156         let res1, res2 = aux tl1 [] in
157         (m, w)::res1, (m, 0)::res2
158     | _, _ -> assert false
159   in
160   let cmp (m, _) (n, _) = compare m n in
161   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
162   (cw1, wl1), (cw2, wl2)
163 ;;
164
165         
166 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
167     
168 let string_of_comparison = function
169   | Lt -> "<"
170   | Le -> "<="
171   | Gt -> ">"
172   | Ge -> ">="
173   | Eq -> "="
174   | Incomparable -> "I"
175
176
177 let compare_weights ?(normalize=false)
178     ((h1, w1) as weight1) ((h2, w2) as weight2)=
179   let (h1, w1), (h2, w2) =
180     if normalize then
181 (*       let maxmeta =  *)
182 (*         let maxmeta l = *)
183 (*           try *)
184 (*             match List.hd l with *)
185 (*             | (m, _) -> m *)
186 (*           with Failure _ -> 0 *)
187 (*         in *)
188 (*         max (maxmeta w1) (maxmeta w2) *)
189 (*       in *)
190 (*       (normalize_weight maxmeta (h1, w1)), (normalize_weight maxmeta (h2, w2)) *)
191       normalize_weights weight1 weight2
192     else
193       (h1, w1), (h2, w2)
194   in
195   let res, diffs =
196     try
197       List.fold_left2
198         (fun ((lt, eq, gt), diffs) w1 w2 ->
199            match w1, w2 with
200            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
201                let diffs = (w1 - w2) + diffs in 
202                let r = compare w1 w2 in
203                if r < 0 then (lt+1, eq, gt), diffs
204                else if r = 0 then (lt, eq+1, gt), diffs
205                else (lt, eq, gt+1), diffs
206            | (meta1, w1), (meta2, w2) ->
207                Printf.printf "HMMM!!!! %s, %s\n"
208                  (string_of_weight weight1) (string_of_weight weight2);
209                assert false)
210         ((0, 0, 0), 0) w1 w2
211     with Invalid_argument _ ->
212       Printf.printf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
213         (string_of_weight (h1, w1)) (string_of_weight weight1)
214         (string_of_weight (h2, w2)) (string_of_weight weight2)
215         (string_of_bool normalize);
216       assert false
217   in
218   let hdiff = h1 - h2 in
219   match res with
220   | (0, _, 0) ->
221       if hdiff < 0 then Lt
222       else if hdiff > 0 then Gt
223       else Eq (* Incomparable *)
224   | (m, _, 0) ->
225       if hdiff <= 0 then 
226         if m > 0 || hdiff < 0 then Lt
227         else if diffs >= (- hdiff) then Le else Incomparable
228       else 
229         if diffs >= (- hdiff) then Le else Incomparable
230   | (0, _, m) ->
231       if hdiff >= 0 then 
232         if m > 0 || hdiff > 0 then Gt
233         else if (- diffs) >= hdiff then Ge else Incomparable
234       else
235         if (- diffs) >= hdiff then Ge else Incomparable
236   | (m, _, n) when m > 0 && n > 0 ->
237       Incomparable
238   | _ -> assert false
239 ;;
240
241
242 let rec aux_ordering ?(recursion=true) t1 t2 =
243   let module C = Cic in
244   let compare_uris u1 u2 =
245     let res =
246       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
247     if res < 0 then Lt
248     else if res = 0 then Eq
249     else Gt
250   in
251   match t1, t2 with
252   | C.Meta _, _
253   | _, C.Meta _ -> Incomparable
254
255   | t1, t2 when t1 = t2 -> Eq
256
257   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
258   | C.Rel _, _ -> Lt
259   | _, C.Rel _ -> Gt
260
261   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
262   | C.Const _, _ -> Lt
263   | _, C.Const _ -> Gt
264
265   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
266   | C.MutInd _, _ -> Lt
267   | _, C.MutInd _ -> Gt
268
269   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
270       compare_uris u1 u2
271   | C.MutConstruct _, _ -> Lt
272   | _, C.MutConstruct _ -> Gt
273
274   | C.Appl l1, C.Appl l2 when recursion ->
275       let rec cmp t1 t2 =
276         match t1, t2 with
277         | [], [] -> Eq
278         | _, [] -> Gt
279         | [], _ -> Lt
280         | hd1::tl1, hd2::tl2 ->
281             let o = aux_ordering hd1 hd2 in
282             if o = Eq then cmp tl1 tl2
283             else o
284       in
285       cmp l1 l2
286   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
287       aux_ordering h1 h2
288         
289   | t1, t2 ->
290       debug_print (
291         Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
292           (CicPp.ppterm t1) (CicPp.ppterm t2));
293       Incomparable
294 ;;
295
296
297 (* w1, w2 are the weights, they should already be normalized... *)
298 let nonrec_kbo_w (t1, w1) (t2, w2) =
299   match compare_weights w1 w2 with
300   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
301   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
302   | Eq -> aux_ordering t1 t2
303   | res -> res
304 ;;
305
306     
307 let nonrec_kbo t1 t2 =
308   let w1 = weight_of_term t1 in
309   let w2 = weight_of_term t2 in
310   match compare_weights ~normalize:true w1 w2 with
311   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
312   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
313   | Eq -> aux_ordering t1 t2
314   | res -> res
315 ;;
316
317
318 let rec kbo t1 t2 =
319 (*   debug_print ( *)
320 (*     Printf.sprintf "kbo %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2)); *)
321 (*   if t1 = t2 then *)
322 (*     Eq *)
323 (*   else *)
324     let aux = aux_ordering ~recursion:false in
325     let w1 = weight_of_term t1
326     and w2 = weight_of_term t2 in
327     let rec cmp t1 t2 =
328       match t1, t2 with
329       | [], [] -> Eq
330       | _, [] -> Gt
331       | [], _ -> Lt
332       | hd1::tl1, hd2::tl2 ->
333           let o =
334 (*             debug_print ( *)
335 (*               Printf.sprintf "recursion kbo on %s %s" *)
336 (*                 (CicPp.ppterm hd1) (CicPp.ppterm hd2)); *)
337             kbo hd1 hd2
338           in
339           if o = Eq then cmp tl1 tl2
340           else o
341     in
342     let comparison = compare_weights ~normalize:true w1 w2 in
343 (*     debug_print ( *)
344 (*       Printf.sprintf "Weights are: %s %s: %s" *)
345 (*         (string_of_weight w1) (string_of_weight w2) *)
346 (*         (string_of_comparison comparison)); *)
347     match comparison with
348     | Le ->
349         let r = aux t1 t2 in
350 (*         debug_print ("HERE! " ^ (string_of_comparison r)); *)
351         if r = Lt then Lt
352         else if r = Eq then (
353           match t1, t2 with
354           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
355               if cmp tl1 tl2 = Lt then Lt else Incomparable
356           | _, _ ->  Incomparable
357         ) else Incomparable
358     | Ge ->
359         let r = aux t1 t2 in
360         if r = Gt then Gt
361         else if r = Eq then (
362           match t1, t2 with
363           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
364               if cmp tl1 tl2 = Gt then Gt else Incomparable
365           | _, _ ->  Incomparable
366         ) else Incomparable
367     | Eq ->
368         let r = aux t1 t2 in
369         if r = Eq then (
370           match t1, t2 with
371           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
372 (*               if cmp tl1 tl2 = Gt then Gt else Incomparable *)
373               cmp tl1 tl2
374           | _, _ ->  Incomparable
375         ) else r 
376     | res -> res
377 ;;
378           
379
380 let names_of_context context = 
381   List.map
382     (function
383        | None -> None
384        | Some (n, e) -> Some n)
385     context
386 ;;
387
388
389 module OrderedTerm =
390 struct
391   type t = Cic.term
392       
393   let compare = Pervasives.compare
394 end
395
396 module TermSet = Set.Make(OrderedTerm);;
397 module TermMap = Map.Make(OrderedTerm);;
398
399 let symbols_of_term term =
400   let module C = Cic in
401   let rec aux map = function
402     | C.Meta _ -> map
403     | C.Appl l ->
404         List.fold_left (fun res t -> (aux res t)) map l
405     | t ->
406         let map = 
407           try
408             let c = TermMap.find t map in
409             TermMap.add t (c+1) map
410           with Not_found ->
411             TermMap.add t 1 map
412         in
413         map
414   in
415   aux TermMap.empty term
416 ;;
417
418
419 let metas_of_term term =
420   let module C = Cic in
421   let rec aux = function
422     | C.Meta _ as t -> TermSet.singleton t
423     | C.Appl l ->
424         List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
425     | t -> TermSet.empty (* TODO: maybe add other cases? *)
426   in
427   aux term
428 ;;
429
430
431 let rec lpo t1 t2 =
432   let module C = Cic in
433   match t1, t2 with
434   | t1, t2 when t1 = t2 -> Eq
435   | t1, (C.Meta _ as m) ->
436       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
437   | (C.Meta _ as m), t2 ->
438       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
439   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
440       let res =
441         let f o r t =
442           if r then true else
443             match lpo t o with
444             | Gt | Eq -> true
445             | _ -> false
446         in
447         let res1 = List.fold_left (f t2) false tl1 in
448         if res1 then Gt
449         else let res2 = List.fold_left (f t1) false tl2 in
450         if res2 then Lt
451         else Incomparable
452       in
453       if res <> Incomparable then
454         res
455       else
456         let f o r t =
457           if not r then false else
458             match lpo o t with
459             | Gt -> true
460             | _ -> false
461         in
462         match aux_ordering hd1 hd2 with
463         | Gt ->
464             let res = List.fold_left (f t1) false tl2 in
465             if res then Gt
466             else Incomparable
467         | Lt ->
468             let res = List.fold_left (f t2) false tl1 in
469             if res then Lt
470             else Incomparable
471         | Eq -> (
472             let lex_res =
473               try
474                 List.fold_left2
475                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
476                   Eq tl1 tl2
477               with Invalid_argument _ ->
478                 Incomparable
479             in
480             match lex_res with
481             | Gt ->
482                 if List.fold_left (f t1) false tl2 then Gt
483                 else Incomparable
484             | Lt ->
485                 if List.fold_left (f t2) false tl1 then Lt
486                 else Incomparable
487             | _ -> Incomparable
488           )
489         | _ -> Incomparable
490     )
491   | t1, t2 -> aux_ordering t1 t2
492 ;;
493
494
495 (* settable by the user... *)
496 let compare_terms = ref nonrec_kbo;;
497
498
499 type equality_sign = Negative | Positive;;
500
501 let string_of_sign = function
502   | Negative -> "Negative"
503   | Positive -> "Positive"
504 ;;
505
506
507 type pos = Left | Right 
508
509 let string_of_pos = function
510   | Left -> "Left"
511   | Right -> "Right"
512 ;;