]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/utils.ml
0a2cd4502a02cea18d16865ddaf2719f62971265
[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 let print_metasenv metasenv =
31   String.concat "\n--------------------------\n"
32     (List.map (fun (i, context, term) ->
33                  (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
34                    "\n] " ^  (CicPp.ppterm term))
35        metasenv)
36 ;;
37
38
39 let print_subst ?(prefix="\n") subst =
40   String.concat prefix
41     (List.map
42        (fun (i, (c, t, ty)) ->
43           Printf.sprintf "?%d -> %s : %s" i
44             (CicPp.ppterm t) (CicPp.ppterm ty))
45        subst)
46 ;;  
47
48 (* (weight of constants, [(meta, weight_of_meta)]) *)
49 type weight = int * (int * int) list;;
50
51 let string_of_weight (cw, mw) =
52   let s =
53     String.concat ", "
54       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
55   in
56   Printf.sprintf "[%d; %s]" cw s
57
58
59 let weight_of_term ?(consider_metas=true) term =
60   let module C = Cic in
61   let vars_dict = Hashtbl.create 5 in
62   let rec aux = function
63     | C.Meta (metano, _) when consider_metas ->
64         (try
65            let oldw = Hashtbl.find vars_dict metano in
66            Hashtbl.replace vars_dict metano (oldw+1)
67          with Not_found ->
68            Hashtbl.add vars_dict metano 1);
69         0
70     | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
71                   
72     | C.Var (_, ens)
73     | C.Const (_, ens)
74     | C.MutInd (_, _, ens)
75     | C.MutConstruct (_, _, _, ens) ->
76         List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
77           
78     | C.Cast (t1, t2)
79     | C.Lambda (_, t1, t2)
80     | C.Prod (_, t1, t2)
81     | C.LetIn (_, t1, t2) ->
82         let w1 = aux t1 in
83         let w2 = aux t2 in
84         w1 + w2 + 1
85           
86     | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
87         
88     | C.MutCase (_, _, outt, t, pl) ->
89         let w1 = aux outt in
90         let w2 = aux t in
91         let w3 = List.fold_left (+) 0 (List.map aux pl) in
92         w1 + w2 + w3 + 1
93           
94     | C.Fix (_, fl) ->
95         List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
96           
97     | C.CoFix (_, fl) ->
98         List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
99           
100     | _ -> 1
101   in
102   let w = aux term in
103   let l =
104     Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
105   let compare w1 w2 = 
106     match w1, w2 with
107     | (m1, _), (m2, _) -> m2 - m1 
108   in 
109   (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
110 ;;
111
112
113 module OrderedInt = struct
114   type t = int
115
116   let compare = Pervasives.compare
117 end
118
119 module IntSet = Set.Make(OrderedInt)
120
121 let compute_equality_weight ty left right =
122   let metasw = ref 0 in
123   let weight_of t =
124     let w, m = (weight_of_term ~consider_metas:true t) in
125     metasw := !metasw + (2 * (List.length m));
126     w
127   in
128   (* Warning: the following let cannot be expanded since it forces the
129      right evaluation order!!!! *)
130   let w = (weight_of ty) + (weight_of left) + (weight_of right) in
131   w + !metasw
132 ;;
133
134
135 (* returns a "normalized" version of the polynomial weight wl (with type
136  * weight list), i.e. a list sorted ascending by meta number,
137  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
138  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
139  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
140 let normalize_weight maxmeta (cw, wl) =
141   let rec aux = function
142     | 0 -> []
143     | m -> (m, 0)::(aux (m-1))
144   in
145   let tmpl = aux maxmeta in
146   let wl =
147     List.sort
148       (fun (m, _) (n, _) -> Pervasives.compare m n)
149       (List.fold_left
150          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
151   in
152   (cw, wl)
153 ;;
154
155
156 let normalize_weights (cw1, wl1) (cw2, wl2) =
157   let rec aux wl1 wl2 =
158     match wl1, wl2 with
159     | [], [] -> [], []
160     | (m, w)::tl1, (n, w')::tl2 when m = n ->
161         let res1, res2 = aux tl1 tl2 in
162         (m, w)::res1, (n, w')::res2
163     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
164         let res1, res2 = aux tl1 wl2 in
165         (m, w)::res1, (m, 0)::res2
166     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
167         let res1, res2 = aux wl1 tl2 in
168         (n, 0)::res1, (n, w')::res2
169     | [], (n, w)::tl2 ->
170         let res1, res2 = aux [] tl2 in
171         (n, 0)::res1, (n, w)::res2
172     | (m, w)::tl1, [] ->
173         let res1, res2 = aux tl1 [] in
174         (m, w)::res1, (m, 0)::res2
175     | _, _ -> assert false
176   in
177   let cmp (m, _) (n, _) = compare m n in
178   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
179   (cw1, wl1), (cw2, wl2)
180 ;;
181
182         
183 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
184     
185 let string_of_comparison = function
186   | Lt -> "<"
187   | Le -> "<="
188   | Gt -> ">"
189   | Ge -> ">="
190   | Eq -> "="
191   | Incomparable -> "I"
192
193
194 let compare_weights ?(normalize=false)
195     ((h1, w1) as weight1) ((h2, w2) as weight2)=
196   let (h1, w1), (h2, w2) =
197     if normalize then
198       normalize_weights weight1 weight2
199     else
200       (h1, w1), (h2, w2)
201   in
202   let res, diffs =
203     try
204       List.fold_left2
205         (fun ((lt, eq, gt), diffs) w1 w2 ->
206            match w1, w2 with
207            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
208                let diffs = (w1 - w2) + diffs in 
209                let r = compare w1 w2 in
210                if r < 0 then (lt+1, eq, gt), diffs
211                else if r = 0 then (lt, eq+1, gt), diffs
212                else (lt, eq, gt+1), diffs
213            | (meta1, w1), (meta2, w2) ->
214                debug_print
215                  (lazy
216                     (Printf.sprintf "HMMM!!!! %s, %s\n"
217                        (string_of_weight weight1) (string_of_weight weight2)));
218                assert false)
219         ((0, 0, 0), 0) w1 w2
220     with Invalid_argument _ ->
221       debug_print
222         (lazy
223            (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
224               (string_of_weight (h1, w1)) (string_of_weight weight1)
225               (string_of_weight (h2, w2)) (string_of_weight weight2)
226               (string_of_bool normalize)));
227       assert false
228   in
229   let hdiff = h1 - h2 in
230   match res with
231   | (0, _, 0) ->
232       if hdiff < 0 then Lt
233       else if hdiff > 0 then Gt
234       else Eq (* Incomparable *)
235   | (m, _, 0) ->
236       if diffs < (- hdiff) then Lt
237       else if diffs = (- hdiff) then Le else Incomparable
238 (*
239       if hdiff <= 0 then 
240         if m > 0 || hdiff < 0 then Lt
241         else if diffs >= (- hdiff) then Le else Incomparable
242       else 
243         if diffs >= (- hdiff) then Le else Incomparable *)
244   | (0, _, m) ->
245       if (- hdiff) < diffs then Gt
246       else if (- hdiff) = diffs then Ge else Incomparable
247 (*
248       if hdiff >= 0 then 
249         if m > 0 || hdiff > 0 then Gt
250         else if (- diffs) >= hdiff then Ge else Incomparable
251       else
252         if (- diffs) >= hdiff then Ge else Incomparable *)
253   | (m, _, n) when m > 0 && n > 0 ->
254       Incomparable
255   | _ -> assert false
256 ;;
257
258
259 let rec aux_ordering ?(recursion=true) t1 t2 =
260   let module C = Cic in
261   let compare_uris u1 u2 =
262     let res =
263       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
264     if res < 0 then Lt
265     else if res = 0 then Eq
266     else Gt
267   in
268   match t1, t2 with
269   | C.Meta _, _
270   | _, C.Meta _ -> Incomparable
271
272   | t1, t2 when t1 = t2 -> Eq
273
274   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
275   | C.Rel _, _ -> Lt
276   | _, C.Rel _ -> Gt
277
278   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
279   | C.Const _, _ -> Lt
280   | _, C.Const _ -> Gt
281
282   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
283   | C.MutInd _, _ -> Lt
284   | _, C.MutInd _ -> Gt
285
286   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
287       compare_uris u1 u2
288   | C.MutConstruct _, _ -> Lt
289   | _, C.MutConstruct _ -> Gt
290
291   | C.Appl l1, C.Appl l2 when recursion ->
292       let rec cmp t1 t2 =
293         match t1, t2 with
294         | [], [] -> Eq
295         | _, [] -> Gt
296         | [], _ -> Lt
297         | hd1::tl1, hd2::tl2 ->
298             let o = aux_ordering hd1 hd2 in
299             if o = Eq then cmp tl1 tl2
300             else o
301       in
302       cmp l1 l2
303   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
304       aux_ordering h1 h2
305         
306   | t1, t2 ->
307       debug_print
308         (lazy
309            (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
310               (CicPp.ppterm t1) (CicPp.ppterm t2)));
311       Incomparable
312 ;;
313
314
315 (* w1, w2 are the weights, they should already be normalized... *)
316 let nonrec_kbo_w (t1, w1) (t2, w2) =
317   match compare_weights w1 w2 with
318   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
319   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
320   | Eq -> aux_ordering t1 t2
321   | res -> res
322 ;;
323
324     
325 let nonrec_kbo t1 t2 =
326   let w1 = weight_of_term t1 in
327   let w2 = weight_of_term t2 in
328   (* 
329   prerr_endline ("weight1 :"^(string_of_weight w1));
330   prerr_endline ("weight2 :"^(string_of_weight w2)); 
331   *)
332   match compare_weights ~normalize:true w1 w2 with
333   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
334   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
335   | Eq -> aux_ordering t1 t2
336   | res -> res
337 ;;
338
339
340 let rec kbo t1 t2 =
341   let aux = aux_ordering ~recursion:false in
342   let w1 = weight_of_term t1
343   and w2 = weight_of_term t2 in
344   let rec cmp t1 t2 =
345     match t1, t2 with
346     | [], [] -> Eq
347     | _, [] -> Gt
348     | [], _ -> Lt
349     | hd1::tl1, hd2::tl2 ->
350         let o =
351           kbo hd1 hd2
352         in
353         if o = Eq then cmp tl1 tl2
354         else o
355   in
356   let comparison = compare_weights ~normalize:true w1 w2 in
357   match comparison with
358   | Le ->
359       let r = aux t1 t2 in
360       if r = Lt then Lt
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 = Lt then Lt else Incomparable
365         | _, _ ->  Incomparable
366       ) else Incomparable
367   | Ge ->
368       let r = aux t1 t2 in
369       if r = Gt then Gt
370       else if r = Eq then (
371         match t1, t2 with
372         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
373             if cmp tl1 tl2 = Gt then Gt else Incomparable
374         | _, _ ->  Incomparable
375       ) else Incomparable
376   | Eq ->
377       let r = aux t1 t2 in
378       if r = Eq then (
379         match t1, t2 with
380         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
381             cmp tl1 tl2
382         | _, _ ->  Incomparable
383       ) else r 
384   | res -> res
385 ;;
386           
387 let rec ao t1 t2 =
388   let get_hd t =
389     match t with
390         Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
391       | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) -> 
392           Some(uri,tyno,cno)
393       | _ -> None in
394   let aux = aux_ordering ~recursion:false in
395   let w1 = weight_of_term t1
396   and w2 = weight_of_term t2 in
397   let rec cmp t1 t2 =
398     match t1, t2 with
399     | [], [] -> Eq
400     | _, [] -> Gt
401     | [], _ -> Lt
402     | hd1::tl1, hd2::tl2 ->
403         let o =
404           ao hd1 hd2
405         in
406         if o = Eq then cmp tl1 tl2
407         else o
408   in
409   match get_hd t1, get_hd t2 with
410       Some(_),None -> Lt
411     | None,Some(_) -> Gt
412     | _ ->
413         let comparison = compare_weights ~normalize:true w1 w2 in
414           match comparison with
415             | Le ->
416                 let r = aux t1 t2 in
417                   if r = Lt then Lt
418                   else if r = Eq then (
419                     match t1, t2 with
420                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
421                           if cmp tl1 tl2 = Lt then Lt else Incomparable
422                       | _, _ ->  Incomparable
423                   ) else Incomparable
424             | Ge ->
425                 let r = aux t1 t2 in
426                   if r = Gt then Gt
427                   else if r = Eq then (
428                     match t1, t2 with
429                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
430                           if cmp tl1 tl2 = Gt then Gt else Incomparable
431                       | _, _ ->  Incomparable
432                   ) else Incomparable
433             | Eq ->
434                 let r = aux t1 t2 in
435                   if r = Eq then (
436                     match t1, t2 with
437                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
438                           cmp tl1 tl2
439                       | _, _ ->  Incomparable
440                   ) else r 
441             | res -> res
442 ;;
443
444 let names_of_context context = 
445   List.map
446     (function
447        | None -> None
448        | Some (n, e) -> Some n)
449     context
450 ;;
451
452
453 module OrderedTerm =
454 struct
455   type t = Cic.term
456       
457   let compare = Pervasives.compare
458 end
459
460 module TermSet = Set.Make(OrderedTerm);;
461 module TermMap = Map.Make(OrderedTerm);;
462
463 let symbols_of_term term =
464   let module C = Cic in
465   let rec aux map = function
466     | C.Meta _ -> map
467     | C.Appl l ->
468         List.fold_left (fun res t -> (aux res t)) map l
469     | t ->
470         let map = 
471           try
472             let c = TermMap.find t map in
473             TermMap.add t (c+1) map
474           with Not_found ->
475             TermMap.add t 1 map
476         in
477         map
478   in
479   aux TermMap.empty term
480 ;;
481
482
483 let metas_of_term term =
484   let module C = Cic in
485   let rec aux = function
486     | C.Meta _ as t -> TermSet.singleton t
487     | C.Appl l ->
488         List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
489     | t -> TermSet.empty (* TODO: maybe add other cases? *)
490   in
491   aux term
492 ;;
493
494
495 let rec lpo t1 t2 =
496   let module C = Cic in
497   match t1, t2 with
498   | t1, t2 when t1 = t2 -> Eq
499   | t1, (C.Meta _ as m) ->
500       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
501   | (C.Meta _ as m), t2 ->
502       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
503   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
504       let res =
505         let f o r t =
506           if r then true else
507             match lpo t o with
508             | Gt | Eq -> true
509             | _ -> false
510         in
511         let res1 = List.fold_left (f t2) false tl1 in
512         if res1 then Gt
513         else let res2 = List.fold_left (f t1) false tl2 in
514         if res2 then Lt
515         else Incomparable
516       in
517       if res <> Incomparable then
518         res
519       else
520         let f o r t =
521           if not r then false else
522             match lpo o t with
523             | Gt -> true
524             | _ -> false
525         in
526         match aux_ordering hd1 hd2 with
527         | Gt ->
528             let res = List.fold_left (f t1) false tl2 in
529             if res then Gt
530             else Incomparable
531         | Lt ->
532             let res = List.fold_left (f t2) false tl1 in
533             if res then Lt
534             else Incomparable
535         | Eq -> (
536             let lex_res =
537               try
538                 List.fold_left2
539                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
540                   Eq tl1 tl2
541               with Invalid_argument _ ->
542                 Incomparable
543             in
544             match lex_res with
545             | Gt ->
546                 if List.fold_left (f t1) false tl2 then Gt
547                 else Incomparable
548             | Lt ->
549                 if List.fold_left (f t2) false tl1 then Lt
550                 else Incomparable
551             | _ -> Incomparable
552           )
553         | _ -> Incomparable
554     )
555   | t1, t2 -> aux_ordering t1 t2
556 ;;
557
558
559 (* settable by the user... *)
560 let compare_terms = ref nonrec_kbo;; 
561 (* let compare_terms = ref ao;; *)
562
563 let guarded_simpl context t =
564   let t' = ProofEngineReduction.simpl context t in
565   let simpl_order = !compare_terms t t' in
566   if simpl_order = Gt then 
567     (prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t'));
568      t')
569   else t
570 ;;
571
572 type equality_sign = Negative | Positive;;
573
574 let string_of_sign = function
575   | Negative -> "Negative"
576   | Positive -> "Positive"
577 ;;
578
579
580 type pos = Left | Right 
581
582 let string_of_pos = function
583   | Left -> "Left"
584   | Right -> "Right"
585 ;;
586
587
588 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
589 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
590 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
591 let eq_XURI () =
592   let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
593   UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
594 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())