]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/utils.ml
new paramodulation
[helm.git] / helm / ocaml / paramodulation / utils.ml
1 let debug = true;;
2
3 let debug_print s = if debug then prerr_endline (Lazy.force s);;
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   (* Warning: the following let cannot be expanded since it forces the
111      right evaluation order!!!! *)
112   let w = (weight_of ty) + (weight_of left) + (weight_of right) in
113   w + !metasw
114 (*     (4 * IntSet.cardinal !metasw) *)
115 ;;
116
117
118 (* returns a "normalized" version of the polynomial weight wl (with type
119  * weight list), i.e. a list sorted ascending by meta number,
120  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
121  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
122  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
123 let normalize_weight maxmeta (cw, wl) =
124 (*   Printf.printf "normalize_weight: %d, %s\n" maxmeta *)
125 (*     (string_of_weight (cw, wl)); *)
126   let rec aux = function
127     | 0 -> []
128     | m -> (m, 0)::(aux (m-1))
129   in
130   let tmpl = aux maxmeta in
131   let wl =
132     List.sort
133       (fun (m, _) (n, _) -> Pervasives.compare m n)
134       (List.fold_left
135          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
136   in
137   (cw, wl)
138 ;;
139
140
141 let normalize_weights (cw1, wl1) (cw2, wl2) =
142   let rec aux wl1 wl2 =
143     match wl1, wl2 with
144     | [], [] -> [], []
145     | (m, w)::tl1, (n, w')::tl2 when m = n ->
146         let res1, res2 = aux tl1 tl2 in
147         (m, w)::res1, (n, w')::res2
148     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
149         let res1, res2 = aux tl1 wl2 in
150         (m, w)::res1, (m, 0)::res2
151     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
152         let res1, res2 = aux wl1 tl2 in
153         (n, 0)::res1, (n, w')::res2
154     | [], (n, w)::tl2 ->
155         let res1, res2 = aux [] tl2 in
156         (n, 0)::res1, (n, w)::res2
157     | (m, w)::tl1, [] ->
158         let res1, res2 = aux tl1 [] in
159         (m, w)::res1, (m, 0)::res2
160     | _, _ -> assert false
161   in
162   let cmp (m, _) (n, _) = compare m n in
163   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
164   (cw1, wl1), (cw2, wl2)
165 ;;
166
167         
168 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
169     
170 let string_of_comparison = function
171   | Lt -> "<"
172   | Le -> "<="
173   | Gt -> ">"
174   | Ge -> ">="
175   | Eq -> "="
176   | Incomparable -> "I"
177
178
179 let compare_weights ?(normalize=false)
180     ((h1, w1) as weight1) ((h2, w2) as weight2)=
181   let (h1, w1), (h2, w2) =
182     if normalize then
183 (*       let maxmeta =  *)
184 (*         let maxmeta l = *)
185 (*           try *)
186 (*             match List.hd l with *)
187 (*             | (m, _) -> m *)
188 (*           with Failure _ -> 0 *)
189 (*         in *)
190 (*         max (maxmeta w1) (maxmeta w2) *)
191 (*       in *)
192 (*       (normalize_weight maxmeta (h1, w1)), (normalize_weight maxmeta (h2, w2)) *)
193       normalize_weights weight1 weight2
194     else
195       (h1, w1), (h2, w2)
196   in
197   let res, diffs =
198     try
199       List.fold_left2
200         (fun ((lt, eq, gt), diffs) w1 w2 ->
201            match w1, w2 with
202            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
203                let diffs = (w1 - w2) + diffs in 
204                let r = compare w1 w2 in
205                if r < 0 then (lt+1, eq, gt), diffs
206                else if r = 0 then (lt, eq+1, gt), diffs
207                else (lt, eq, gt+1), diffs
208            | (meta1, w1), (meta2, w2) ->
209                Printf.printf "HMMM!!!! %s, %s\n"
210                  (string_of_weight weight1) (string_of_weight weight2);
211                assert false)
212         ((0, 0, 0), 0) w1 w2
213     with Invalid_argument _ ->
214       Printf.printf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
215         (string_of_weight (h1, w1)) (string_of_weight weight1)
216         (string_of_weight (h2, w2)) (string_of_weight weight2)
217         (string_of_bool normalize);
218       assert false
219   in
220   let hdiff = h1 - h2 in
221   match res with
222   | (0, _, 0) ->
223       if hdiff < 0 then Lt
224       else if hdiff > 0 then Gt
225       else Eq (* Incomparable *)
226   | (m, _, 0) ->
227       if hdiff <= 0 then 
228         if m > 0 || hdiff < 0 then Lt
229         else if diffs >= (- hdiff) then Le else Incomparable
230       else 
231         if diffs >= (- hdiff) then Le else Incomparable
232   | (0, _, m) ->
233       if hdiff >= 0 then 
234         if m > 0 || hdiff > 0 then Gt
235         else if (- diffs) >= hdiff then Ge else Incomparable
236       else
237         if (- diffs) >= hdiff then Ge else Incomparable
238   | (m, _, n) when m > 0 && n > 0 ->
239       Incomparable
240   | _ -> assert false
241 ;;
242
243
244 let rec aux_ordering ?(recursion=true) t1 t2 =
245   let module C = Cic in
246   let compare_uris u1 u2 =
247     let res =
248       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
249     if res < 0 then Lt
250     else if res = 0 then Eq
251     else Gt
252   in
253   match t1, t2 with
254   | C.Meta _, _
255   | _, C.Meta _ -> Incomparable
256
257   | t1, t2 when t1 = t2 -> Eq
258
259   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
260   | C.Rel _, _ -> Lt
261   | _, C.Rel _ -> Gt
262
263   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
264   | C.Const _, _ -> Lt
265   | _, C.Const _ -> Gt
266
267   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
268   | C.MutInd _, _ -> Lt
269   | _, C.MutInd _ -> Gt
270
271   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
272       compare_uris u1 u2
273   | C.MutConstruct _, _ -> Lt
274   | _, C.MutConstruct _ -> Gt
275
276   | C.Appl l1, C.Appl l2 when recursion ->
277       let rec cmp t1 t2 =
278         match t1, t2 with
279         | [], [] -> Eq
280         | _, [] -> Gt
281         | [], _ -> Lt
282         | hd1::tl1, hd2::tl2 ->
283             let o = aux_ordering hd1 hd2 in
284             if o = Eq then cmp tl1 tl2
285             else o
286       in
287       cmp l1 l2
288   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
289       aux_ordering h1 h2
290         
291   | t1, t2 ->
292       debug_print
293         (lazy
294            (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
295               (CicPp.ppterm t1) (CicPp.ppterm t2)));
296       Incomparable
297 ;;
298
299
300 (* w1, w2 are the weights, they should already be normalized... *)
301 let nonrec_kbo_w (t1, w1) (t2, w2) =
302   match compare_weights w1 w2 with
303   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
304   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
305   | Eq -> aux_ordering t1 t2
306   | res -> res
307 ;;
308
309     
310 let nonrec_kbo t1 t2 =
311   let w1 = weight_of_term t1 in
312   let w2 = weight_of_term t2 in
313   match compare_weights ~normalize:true w1 w2 with
314   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
315   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
316   | Eq -> aux_ordering t1 t2
317   | res -> res
318 ;;
319
320
321 let rec kbo t1 t2 =
322 (*   debug_print (lazy ( *)
323 (*     Printf.sprintf "kbo %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2))); *)
324 (*   if t1 = t2 then *)
325 (*     Eq *)
326 (*   else *)
327     let aux = aux_ordering ~recursion:false in
328     let w1 = weight_of_term t1
329     and w2 = weight_of_term t2 in
330     let rec cmp t1 t2 =
331       match t1, t2 with
332       | [], [] -> Eq
333       | _, [] -> Gt
334       | [], _ -> Lt
335       | hd1::tl1, hd2::tl2 ->
336           let o =
337 (*             debug_print (lazy ( *)
338 (*               Printf.sprintf "recursion kbo on %s %s" *)
339 (*                 (CicPp.ppterm hd1) (CicPp.ppterm hd2))); *)
340             kbo hd1 hd2
341           in
342           if o = Eq then cmp tl1 tl2
343           else o
344     in
345     let comparison = compare_weights ~normalize:true w1 w2 in
346 (*     debug_print (lazy ( *)
347 (*       Printf.sprintf "Weights are: %s %s: %s" *)
348 (*         (string_of_weight w1) (string_of_weight w2) *)
349 (*         (string_of_comparison comparison))); *)
350     match comparison with
351     | Le ->
352         let r = aux t1 t2 in
353 (*         debug_print (lazy ("HERE! " ^ (string_of_comparison r))); *)
354         if r = Lt then Lt
355         else if r = Eq then (
356           match t1, t2 with
357           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
358               if cmp tl1 tl2 = Lt then Lt else Incomparable
359           | _, _ ->  Incomparable
360         ) else Incomparable
361     | Ge ->
362         let r = aux t1 t2 in
363         if r = Gt then Gt
364         else if r = Eq then (
365           match t1, t2 with
366           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
367               if cmp tl1 tl2 = Gt then Gt else Incomparable
368           | _, _ ->  Incomparable
369         ) else Incomparable
370     | Eq ->
371         let r = aux t1 t2 in
372         if r = Eq then (
373           match t1, t2 with
374           | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
375 (*               if cmp tl1 tl2 = Gt then Gt else Incomparable *)
376               cmp tl1 tl2
377           | _, _ ->  Incomparable
378         ) else r 
379     | res -> res
380 ;;
381           
382
383 let names_of_context context = 
384   List.map
385     (function
386        | None -> None
387        | Some (n, e) -> Some n)
388     context
389 ;;
390
391
392 module OrderedTerm =
393 struct
394   type t = Cic.term
395       
396   let compare = Pervasives.compare
397 end
398
399 module TermSet = Set.Make(OrderedTerm);;
400 module TermMap = Map.Make(OrderedTerm);;
401
402 let symbols_of_term term =
403   let module C = Cic in
404   let rec aux map = function
405     | C.Meta _ -> map
406     | C.Appl l ->
407         List.fold_left (fun res t -> (aux res t)) map l
408     | t ->
409         let map = 
410           try
411             let c = TermMap.find t map in
412             TermMap.add t (c+1) map
413           with Not_found ->
414             TermMap.add t 1 map
415         in
416         map
417   in
418   aux TermMap.empty term
419 ;;
420
421
422 let metas_of_term term =
423   let module C = Cic in
424   let rec aux = function
425     | C.Meta _ as t -> TermSet.singleton t
426     | C.Appl l ->
427         List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
428     | t -> TermSet.empty (* TODO: maybe add other cases? *)
429   in
430   aux term
431 ;;
432
433
434 let rec lpo t1 t2 =
435   let module C = Cic in
436   match t1, t2 with
437   | t1, t2 when t1 = t2 -> Eq
438   | t1, (C.Meta _ as m) ->
439       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
440   | (C.Meta _ as m), t2 ->
441       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
442   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
443       let res =
444         let f o r t =
445           if r then true else
446             match lpo t o with
447             | Gt | Eq -> true
448             | _ -> false
449         in
450         let res1 = List.fold_left (f t2) false tl1 in
451         if res1 then Gt
452         else let res2 = List.fold_left (f t1) false tl2 in
453         if res2 then Lt
454         else Incomparable
455       in
456       if res <> Incomparable then
457         res
458       else
459         let f o r t =
460           if not r then false else
461             match lpo o t with
462             | Gt -> true
463             | _ -> false
464         in
465         match aux_ordering hd1 hd2 with
466         | Gt ->
467             let res = List.fold_left (f t1) false tl2 in
468             if res then Gt
469             else Incomparable
470         | Lt ->
471             let res = List.fold_left (f t2) false tl1 in
472             if res then Lt
473             else Incomparable
474         | Eq -> (
475             let lex_res =
476               try
477                 List.fold_left2
478                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
479                   Eq tl1 tl2
480               with Invalid_argument _ ->
481                 Incomparable
482             in
483             match lex_res with
484             | Gt ->
485                 if List.fold_left (f t1) false tl2 then Gt
486                 else Incomparable
487             | Lt ->
488                 if List.fold_left (f t2) false tl1 then Lt
489                 else Incomparable
490             | _ -> Incomparable
491           )
492         | _ -> Incomparable
493     )
494   | t1, t2 -> aux_ordering t1 t2
495 ;;
496
497
498 (* settable by the user... *)
499 let compare_terms = ref nonrec_kbo;;
500
501
502 type equality_sign = Negative | Positive;;
503
504 let string_of_sign = function
505   | Negative -> "Negative"
506   | Positive -> "Positive"
507 ;;
508
509
510 type pos = Left | Right 
511
512 let string_of_pos = function
513   | Left -> "Left"
514   | Right -> "Right"
515 ;;