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