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