]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/utils.ml
6ee45f235478a78db8a6a682ed77eacb175a55c9
[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 hdiff <= 0 then 
237         if m > 0 || hdiff < 0 then Lt
238         else if diffs >= (- hdiff) then Le else Incomparable
239       else 
240         if diffs >= (- hdiff) then Le else Incomparable
241   | (0, _, m) ->
242       if hdiff >= 0 then 
243         if m > 0 || hdiff > 0 then Gt
244         else if (- diffs) >= hdiff then Ge else Incomparable
245       else
246         if (- diffs) >= hdiff then Ge else Incomparable
247   | (m, _, n) when m > 0 && n > 0 ->
248       Incomparable
249   | _ -> assert false
250 ;;
251
252
253 let rec aux_ordering ?(recursion=true) t1 t2 =
254   let module C = Cic in
255   let compare_uris u1 u2 =
256     let res =
257       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
258     if res < 0 then Lt
259     else if res = 0 then Eq
260     else Gt
261   in
262   match t1, t2 with
263   | C.Meta _, _
264   | _, C.Meta _ -> Incomparable
265
266   | t1, t2 when t1 = t2 -> Eq
267
268   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
269   | C.Rel _, _ -> Lt
270   | _, C.Rel _ -> Gt
271
272   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
273   | C.Const _, _ -> Lt
274   | _, C.Const _ -> Gt
275
276   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
277   | C.MutInd _, _ -> Lt
278   | _, C.MutInd _ -> Gt
279
280   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
281       compare_uris u1 u2
282   | C.MutConstruct _, _ -> Lt
283   | _, C.MutConstruct _ -> Gt
284
285   | C.Appl l1, C.Appl l2 when recursion ->
286       let rec cmp t1 t2 =
287         match t1, t2 with
288         | [], [] -> Eq
289         | _, [] -> Gt
290         | [], _ -> Lt
291         | hd1::tl1, hd2::tl2 ->
292             let o = aux_ordering hd1 hd2 in
293             if o = Eq then cmp tl1 tl2
294             else o
295       in
296       cmp l1 l2
297   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
298       aux_ordering h1 h2
299         
300   | t1, t2 ->
301       debug_print
302         (lazy
303            (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
304               (CicPp.ppterm t1) (CicPp.ppterm t2)));
305       Incomparable
306 ;;
307
308
309 (* w1, w2 are the weights, they should already be normalized... *)
310 let nonrec_kbo_w (t1, w1) (t2, w2) =
311   match compare_weights w1 w2 with
312   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
313   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
314   | Eq -> aux_ordering t1 t2
315   | res -> res
316 ;;
317
318     
319 let nonrec_kbo t1 t2 =
320   let w1 = weight_of_term t1 in
321   let w2 = weight_of_term t2 in
322   match compare_weights ~normalize:true w1 w2 with
323   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
324   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
325   | Eq -> aux_ordering t1 t2
326   | res -> res
327 ;;
328
329
330 let rec kbo t1 t2 =
331   let aux = aux_ordering ~recursion:false in
332   let w1 = weight_of_term t1
333   and w2 = weight_of_term t2 in
334   let rec cmp t1 t2 =
335     match t1, t2 with
336     | [], [] -> Eq
337     | _, [] -> Gt
338     | [], _ -> Lt
339     | hd1::tl1, hd2::tl2 ->
340         let o =
341           kbo hd1 hd2
342         in
343         if o = Eq then cmp tl1 tl2
344         else o
345   in
346   let comparison = compare_weights ~normalize:true w1 w2 in
347   match comparison with
348   | Le ->
349       let r = aux t1 t2 in
350       if r = Lt then Lt
351       else if r = Eq then (
352         match t1, t2 with
353         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
354             if cmp tl1 tl2 = Lt then Lt else Incomparable
355         | _, _ ->  Incomparable
356       ) else Incomparable
357   | Ge ->
358       let r = aux t1 t2 in
359       if r = Gt then Gt
360       else if r = Eq then (
361         match t1, t2 with
362         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
363             if cmp tl1 tl2 = Gt then Gt else Incomparable
364         | _, _ ->  Incomparable
365       ) else Incomparable
366   | Eq ->
367       let r = aux t1 t2 in
368       if r = Eq then (
369         match t1, t2 with
370         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
371             cmp tl1 tl2
372         | _, _ ->  Incomparable
373       ) else r 
374   | res -> res
375 ;;
376           
377 let rec ao t1 t2 =
378   let get_hd t =
379     match t with
380         Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
381       | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) -> 
382           Some(uri,tyno,cno)
383       | _ -> None in
384   let aux = aux_ordering ~recursion:false in
385   let w1 = weight_of_term t1
386   and w2 = weight_of_term t2 in
387   let rec cmp t1 t2 =
388     match t1, t2 with
389     | [], [] -> Eq
390     | _, [] -> Gt
391     | [], _ -> Lt
392     | hd1::tl1, hd2::tl2 ->
393         let o =
394           ao hd1 hd2
395         in
396         if o = Eq then cmp tl1 tl2
397         else o
398   in
399   match get_hd t1, get_hd t2 with
400       Some(_),None -> Lt
401     | None,Some(_) -> Gt
402     | _ ->
403         let comparison = compare_weights ~normalize:true w1 w2 in
404           match comparison with
405             | Le ->
406                 let r = aux t1 t2 in
407                   if r = Lt then Lt
408                   else if r = Eq then (
409                     match t1, t2 with
410                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
411                           if cmp tl1 tl2 = Lt then Lt else Incomparable
412                       | _, _ ->  Incomparable
413                   ) else Incomparable
414             | Ge ->
415                 let r = aux t1 t2 in
416                   if r = Gt then Gt
417                   else if r = Eq then (
418                     match t1, t2 with
419                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
420                           if cmp tl1 tl2 = Gt then Gt else Incomparable
421                       | _, _ ->  Incomparable
422                   ) else Incomparable
423             | Eq ->
424                 let r = aux t1 t2 in
425                   if r = Eq then (
426                     match t1, t2 with
427                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
428                           cmp tl1 tl2
429                       | _, _ ->  Incomparable
430                   ) else r 
431             | res -> res
432 ;;
433
434 let names_of_context context = 
435   List.map
436     (function
437        | None -> None
438        | Some (n, e) -> Some n)
439     context
440 ;;
441
442
443 module OrderedTerm =
444 struct
445   type t = Cic.term
446       
447   let compare = Pervasives.compare
448 end
449
450 module TermSet = Set.Make(OrderedTerm);;
451 module TermMap = Map.Make(OrderedTerm);;
452
453 let symbols_of_term term =
454   let module C = Cic in
455   let rec aux map = function
456     | C.Meta _ -> map
457     | C.Appl l ->
458         List.fold_left (fun res t -> (aux res t)) map l
459     | t ->
460         let map = 
461           try
462             let c = TermMap.find t map in
463             TermMap.add t (c+1) map
464           with Not_found ->
465             TermMap.add t 1 map
466         in
467         map
468   in
469   aux TermMap.empty term
470 ;;
471
472
473 let metas_of_term term =
474   let module C = Cic in
475   let rec aux = function
476     | C.Meta _ as t -> TermSet.singleton t
477     | C.Appl l ->
478         List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
479     | t -> TermSet.empty (* TODO: maybe add other cases? *)
480   in
481   aux term
482 ;;
483
484
485 let rec lpo t1 t2 =
486   let module C = Cic in
487   match t1, t2 with
488   | t1, t2 when t1 = t2 -> Eq
489   | t1, (C.Meta _ as m) ->
490       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
491   | (C.Meta _ as m), t2 ->
492       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
493   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
494       let res =
495         let f o r t =
496           if r then true else
497             match lpo t o with
498             | Gt | Eq -> true
499             | _ -> false
500         in
501         let res1 = List.fold_left (f t2) false tl1 in
502         if res1 then Gt
503         else let res2 = List.fold_left (f t1) false tl2 in
504         if res2 then Lt
505         else Incomparable
506       in
507       if res <> Incomparable then
508         res
509       else
510         let f o r t =
511           if not r then false else
512             match lpo o t with
513             | Gt -> true
514             | _ -> false
515         in
516         match aux_ordering hd1 hd2 with
517         | Gt ->
518             let res = List.fold_left (f t1) false tl2 in
519             if res then Gt
520             else Incomparable
521         | Lt ->
522             let res = List.fold_left (f t2) false tl1 in
523             if res then Lt
524             else Incomparable
525         | Eq -> (
526             let lex_res =
527               try
528                 List.fold_left2
529                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
530                   Eq tl1 tl2
531               with Invalid_argument _ ->
532                 Incomparable
533             in
534             match lex_res with
535             | Gt ->
536                 if List.fold_left (f t1) false tl2 then Gt
537                 else Incomparable
538             | Lt ->
539                 if List.fold_left (f t2) false tl1 then Lt
540                 else Incomparable
541             | _ -> Incomparable
542           )
543         | _ -> Incomparable
544     )
545   | t1, t2 -> aux_ordering t1 t2
546 ;;
547
548
549 (* settable by the user... *)
550 (* let compare_terms = ref nonrec_kbo;; *)
551 let compare_terms = ref ao;;
552
553 let guarded_simpl context t =
554   let t' = ProofEngineReduction.simpl context t in
555   let simpl_order = !compare_terms t t' in
556   if simpl_order = Gt then 
557     (prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t'));
558      t')
559   else t
560 ;;
561
562 type equality_sign = Negative | Positive;;
563
564 let string_of_sign = function
565   | Negative -> "Negative"
566   | Positive -> "Positive"
567 ;;
568
569
570 type pos = Left | Right 
571
572 let string_of_pos = function
573   | Left -> "Left"
574   | Right -> "Right"
575 ;;
576
577
578 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
579 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
580 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
581 let eq_XURI () =
582   let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
583   UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
584 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())