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