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