]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/equality.ml
Added the computation of max_weight for equations in proofs.
[helm.git] / components / tactics / paramodulation / equality.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 _profiler = <:profiler<_profiler>>;;
27
28 (* $Id: inference.ml 6245 2006-04-05 12:07:51Z tassi $ *)
29
30 type rule = SuperpositionRight | SuperpositionLeft | Demodulation
31 type uncomparable = int -> int 
32 type equality =
33     uncomparable *       (* trick to break structural equality *)
34     int  *               (* weight *)
35     proof * 
36     (Cic.term *          (* type *)
37      Cic.term *          (* left side *)
38      Cic.term *          (* right side *)
39      Utils.comparison) * (* ordering *)  
40     Cic.metasenv  *      (* environment for metas *)
41     int                  (* id *)
42 and proof = 
43   | Exact of Cic.term
44   | Step of Subst.substitution * (rule * int*(Utils.pos*int)* Cic.term) 
45             (* subst, (rule,eq1, eq2,predicate) *)  
46 and goal_proof = (rule * Utils.pos * int * Subst.substitution * Cic.term) list
47 ;;
48
49 type goal = goal_proof * Cic.metasenv * Cic.term
50
51 (* globals *)
52 let maxid = ref 0;;
53 let id_to_eq = Hashtbl.create 1024;;
54
55 let freshid () =
56   incr maxid; !maxid
57 ;;
58
59 let reset () = 
60   maxid := 0;
61   Hashtbl.clear  id_to_eq
62 ;;
63
64 let uncomparable = fun _ -> 0
65
66 let mk_equality (weight,p,(ty,l,r,o),m) =
67   let id = freshid () in
68   let eq = (uncomparable,weight,p,(ty,l,r,o),m,id) in
69   Hashtbl.add id_to_eq id eq;
70
71   eq
72 ;;
73
74 let mk_tmp_equality (weight,(ty,l,r,o),m) =
75   let id = -1 in
76   uncomparable,weight,Exact (Cic.Implicit None),(ty,l,r,o),m,id
77 ;;
78
79
80 let open_equality (_,weight,proof,(ty,l,r,o),m,id) = 
81   (weight,proof,(ty,l,r,o),m,id)
82
83 let string_of_rule = function
84   | SuperpositionRight -> "SupR"
85   | SuperpositionLeft -> "SupL"
86   | Demodulation -> "Demod"
87 ;;
88
89 let string_of_equality ?env eq =
90   match env with
91   | None ->
92       let w, _, (ty, left, right, o), m , id = open_equality eq in
93       Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]" 
94               id w (CicPp.ppterm ty)
95               (CicPp.ppterm left) 
96               (Utils.string_of_comparison o) (CicPp.ppterm right)
97         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
98   | Some (_, context, _) -> 
99       let names = Utils.names_of_context context in
100       let w, _, (ty, left, right, o), m , id = open_equality eq in
101       Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]" 
102               id w (CicPp.pp ty names)
103               (CicPp.pp left names) (Utils.string_of_comparison o)
104               (CicPp.pp right names)
105         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
106 ;;
107
108 let compare (_,_,_,s1,_,_) (_,_,_,s2,_,_) =
109   Pervasives.compare s1 s2
110 ;;
111
112 let rec max_weight_in_proof current =
113   function
114    | Exact _ -> current
115    | Step (_, (_,id1,(_,id2),_)) ->
116        let eq1 = Hashtbl.find id_to_eq id1 in
117        let eq2 = Hashtbl.find id_to_eq id2 in  
118        let (w1,p1,(_,_,_,_),_,_) = open_equality eq1 in
119        let (w2,p2,(_,_,_,_),_,_) = open_equality eq2 in
120        let current = max current w1 in
121        let current = max_weight_in_proof current p1 in
122        let current = max current w2 in
123        max_weight_in_proof current p2
124
125 let max_weight_in_goal_proof =
126   List.fold_left 
127     (fun current (_,_,id,_,_) ->
128        let eq = Hashtbl.find id_to_eq id in
129        let (w,p,(_,_,_,_),_,_) = open_equality eq in
130        let current = max current w in
131        max_weight_in_proof current p)
132
133 let max_weight goal_proof proof =
134   let current = max_weight_in_proof 0 proof in
135   max_weight_in_goal_proof current goal_proof
136
137 let proof_of_id id =
138   try
139     let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
140       p,l,r
141   with
142       Not_found -> assert false
143
144
145 let string_of_proof ?(names=[]) p gp = 
146   let str_of_pos = function
147     | Utils.Left -> "left"
148     | Utils.Right -> "right"
149   in
150   let fst3 (x,_,_) = x in
151   let rec aux margin name = 
152     let prefix = String.make margin ' ' ^ name ^ ": " in function 
153     | Exact t -> 
154         Printf.sprintf "%sExact (%s)\n" 
155           prefix (CicPp.pp t names)
156     | Step (subst,(rule,eq1,(pos,eq2),pred)) -> 
157         Printf.sprintf "%s%s(%s|%d with %d dir %s pred %s))\n"
158           prefix (string_of_rule rule) (Subst.ppsubst ~names subst) eq1 eq2 (str_of_pos pos) 
159           (CicPp.pp pred names)^ 
160         aux (margin+1) (Printf.sprintf "%d" eq1) (fst3 (proof_of_id eq1)) ^ 
161         aux (margin+1) (Printf.sprintf "%d" eq2) (fst3 (proof_of_id eq2)) 
162   in
163   aux 0 "" p ^ 
164   String.concat "\n" 
165     (List.map 
166       (fun (r,pos,i,s,t) -> 
167         (Printf.sprintf 
168           "GOAL: %s %s %d %s %s\n" (string_of_rule r)
169             (str_of_pos pos) i (Subst.ppsubst ~names s) (CicPp.pp t names)) ^ 
170         aux 1 (Printf.sprintf "%d " i) (fst3 (proof_of_id i)))
171       gp)
172 ;;
173
174 let rec depend eq id seen =
175   let (_,p,(_,_,_,_),_,ideq) = open_equality eq in
176   if List.mem ideq seen then 
177     false,seen
178   else
179     if id = ideq then 
180       true,seen
181     else  
182       match p with
183       | Exact _ -> false,seen
184       | Step (_,(_,id1,(_,id2),_)) ->
185           let seen = ideq::seen in
186           let eq1 = Hashtbl.find id_to_eq id1 in
187           let eq2 = Hashtbl.find id_to_eq id2 in  
188           let b1,seen = depend eq1 id seen in
189           if b1 then b1,seen else depend eq2 id seen
190 ;;
191
192 let depend eq id = fst (depend eq id []);;
193
194 let ppsubst = Subst.ppsubst ~names:[];;
195
196 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
197 let build_ens uri termlist =
198   let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
199   match obj with
200   | Cic.Constant (_, _, _, uris, _) ->
201       assert (List.length uris <= List.length termlist);
202       let rec aux = function
203         | [], tl -> [], tl
204         | (uri::uris), (term::tl) ->
205             let ens, args = aux (uris, tl) in
206             (uri, term)::ens, args
207         | _, _ -> assert false
208       in
209       aux (uris, termlist)
210   | _ -> assert false
211 ;;
212
213 let mk_sym uri ty t1 t2 p =
214   let ens, args =  build_ens uri [ty;t1;t2;p] in
215     Cic.Appl (Cic.Const(uri, ens) :: args)
216 ;;
217
218 let mk_trans uri ty t1 t2 t3 p12 p23 =
219   let ens, args = build_ens uri [ty;t1;t2;t3;p12;p23] in
220     Cic.Appl (Cic.Const (uri, ens) :: args)
221 ;;
222
223 let mk_eq_ind uri ty what pred p1 other p2 =
224  Cic.Appl [Cic.Const (uri, []); ty; what; pred; p1; other; p2]
225 ;;
226
227 let p_of_sym ens tl =
228   let args = List.map snd ens @ tl in
229   match args with 
230     | [_;_;_;p] -> p 
231     | _ -> assert false 
232 ;;
233
234 let open_trans ens tl =
235   let args = List.map snd ens @ tl in
236   match args with 
237     | [ty;l;m;r;p1;p2] -> ty,l,m,r,p1,p2
238     | _ -> assert false   
239 ;;
240
241 let open_sym ens tl =
242   let args = List.map snd ens @ tl in
243   match args with 
244     | [ty;l;r;p] -> ty,l,r,p
245     | _ -> assert false   
246 ;;
247
248 let open_eq_ind args =
249   match args with 
250   | [ty;l;pred;pl;r;pleqr] -> ty,l,pred,pl,r,pleqr
251   | _ -> assert false   
252 ;;
253
254 let open_pred pred =
255   match pred with 
256   | Cic.Lambda (_,_,(Cic.Appl [Cic.MutInd (uri, 0,_);ty;l;r])) 
257      when LibraryObjects.is_eq_URI uri -> ty,uri,l,r
258   | _ -> prerr_endline (CicPp.ppterm pred); assert false   
259 ;;
260
261 let is_not_fixed t =
262    CicSubstitution.subst (Cic.Implicit None) t <>
263    CicSubstitution.subst (Cic.Rel 1) t
264 ;;
265
266 let head_of_apply = function | Cic.Appl (hd::_) -> hd | t -> t;;
267 let tail_of_apply = function | Cic.Appl (_::tl) -> tl | t -> [];;
268 let count_args t = List.length (tail_of_apply t);;
269 let rec build_nat = 
270   let u = UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind" in
271   function
272     | 0 -> Cic.MutConstruct(u,0,1,[])
273     | n -> 
274         Cic.Appl [Cic.MutConstruct(u,0,2,[]);build_nat (n-1)]
275 ;;
276 let tyof context menv t =
277   try
278     fst(CicTypeChecker.type_of_aux' menv context t CicUniv.empty_ugraph)
279   with
280   | CicTypeChecker.TypeCheckerFailure _
281   | CicTypeChecker.AssertFailure _ -> assert false
282 ;;
283 let rec lambdaof left context = function
284   | Cic.Prod (n,s,t) ->
285       Cic.Lambda (n,s,lambdaof left context t)
286   | Cic.Appl [Cic.MutInd (uri, 0,_);ty;l;r] 
287       when LibraryObjects.is_eq_URI uri -> if left then l else r
288   | t -> 
289       let names = Utils.names_of_context context in
290       prerr_endline ("lambdaof: " ^ (CicPp.pp t names));
291       assert false
292 ;;
293
294 let canonical t context menv = 
295   let rec remove_refl t =
296     match t with
297     | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
298           when LibraryObjects.is_trans_eq_URI uri_trans ->
299           let ty,l,m,r,p1,p2 = open_trans ens tl in
300             (match p1,p2 with
301               | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 -> 
302                   remove_refl p2
303               | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] -> 
304                   remove_refl p1
305               | _ -> Cic.Appl (List.map remove_refl args))
306     | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
307     | Cic.LetIn (name,bo,rest) ->
308         Cic.LetIn (name,remove_refl bo,remove_refl rest)
309     | _ -> t
310   in
311   let rec canonical context t =
312     match t with
313       | Cic.LetIn(name,bo,rest) -> 
314           let context' = (Some (name,Cic.Def (bo,None)))::context in
315           Cic.LetIn(name,canonical context bo,canonical context' rest)
316       | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
317           when LibraryObjects.is_sym_eq_URI uri_sym ->
318           (match p_of_sym ens tl with
319              | Cic.Appl ((Cic.Const(uri,ens))::tl)
320                  when LibraryObjects.is_sym_eq_URI uri -> 
321                    canonical context (p_of_sym ens tl)
322              | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
323                  when LibraryObjects.is_trans_eq_URI uri_trans ->
324                  let ty,l,m,r,p1,p2 = open_trans ens tl in
325                    mk_trans uri_trans ty r m l 
326                      (canonical context (mk_sym uri_sym ty m r p2)) 
327                      (canonical context (mk_sym uri_sym ty l m p1))
328              | Cic.Appl (([Cic.Const(uri_feq,ens);ty1;ty2;f;x;y;p])) ->
329                  
330                  let eq_f_sym = 
331                    Cic.Const (UriManager.uri_of_string
332                      "cic:/matita/logic/equality/eq_f1.con",[]) 
333                  in
334                  Cic.Appl (([eq_f_sym;ty1;ty2;f;x;y;p]))  
335
336 (*
337                  let sym_eq = Cic.Const(uri_sym,ens) in
338                  let eq_f = Cic.Const(uri_feq,[]) in
339                  let b = Cic.MutConstruct (UriManager.uri_of_string
340                    "cic:/matita/datatypes/bool/bool.ind",0,1,[])
341                  in
342                  let u = ty1 in
343                  let ctx = f in
344                  let n = build_nat (count_args p) in
345                  let h = head_of_apply p in
346                  let predl = lambdaof true context (tyof context menv h) in 
347                  let predr = lambdaof false context (tyof context menv h) in
348                  let args = tail_of_apply p in
349                  let appl = 
350                    Cic.Appl
351                     ([Cic.Const(UriManager.uri_of_string
352                       "cic:/matita/paramodulation/rewrite.con",[]);
353                       eq; sym_eq; eq_f; b; u; ctx; n; predl; predr; h] @
354                       args)
355                  in
356                  appl
357 *)
358 (*
359              | Cic.Appl (((Cic.Const(uri_ind,ens)) as he)::tl) 
360                  when LibraryObjects.is_eq_ind_URI uri_ind || 
361                       LibraryObjects.is_eq_ind_r_URI uri_ind ->
362                  let ty, what, pred, p1, other, p2 =
363                    match tl with
364                    | [ty;what;pred;p1;other;p2] -> ty, what, pred, p1, other, p2
365                    | _ -> assert false
366                  in
367                  let pred,l,r = 
368                    match pred with
369                    | Cic.Lambda (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;l;r])
370                        when LibraryObjects.is_eq_URI uri ->
371                          Cic.Lambda 
372                            (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;r;l]),l,r
373                    | _ -> 
374                        prerr_endline (CicPp.ppterm pred);
375                        assert false
376                  in
377                  let l = CicSubstitution.subst what l in
378                  let r = CicSubstitution.subst what r in
379                  Cic.Appl 
380                    [he;ty;what;pred;
381                     canonical (mk_sym uri_sym ty l r p1);other;canonical p2]
382 *)
383              | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
384                  when LibraryObjects.is_eq_URI uri -> t
385              | _ -> Cic.Appl (List.map (canonical context) args))
386       | Cic.Appl l -> Cic.Appl (List.map (canonical context) l)
387       | _ -> t
388   in
389   remove_refl (canonical context t)
390 ;;
391   
392 let ty_of_lambda = function
393   | Cic.Lambda (_,ty,_) -> ty
394   | _ -> assert false 
395 ;;
396
397 let compose_contexts ctx1 ctx2 = 
398   ProofEngineReduction.replace_lifting 
399     ~equality:(=) ~what:[Cic.Implicit(Some `Hole)] ~with_what:[ctx2] ~where:ctx1
400 ;;
401
402 let put_in_ctx ctx t = 
403   ProofEngineReduction.replace_lifting
404     ~equality:(=) ~what:[Cic.Implicit (Some `Hole)] ~with_what:[t] ~where:ctx
405 ;;
406
407 let mk_eq uri ty l r =
408   Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r]
409 ;;
410
411 let mk_refl uri ty t = 
412   Cic.Appl [Cic.MutConstruct(uri,0,1,[]);ty;t]
413 ;;
414
415 let open_eq = function 
416   | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
417       uri, ty, l ,r
418   | _ -> assert false
419 ;;
420
421 let mk_feq uri_feq ty ty1 left pred right t = 
422   Cic.Appl [Cic.Const(uri_feq,[]);ty;ty1;pred;left;right;t]
423 ;;
424
425 let contextualize uri ty left right t = 
426   let hole = Cic.Implicit (Some `Hole) in
427   (* aux [uri] [ty] [left] [right] [ctx] [t] 
428    * 
429    * the parameters validate this invariant  
430    *   t: eq(uri) ty left right
431    * that is used only by the base case
432    *
433    * ctx is a term with an hole. Cic.Implicit(Some `Hole) is the empty context
434    * ty_ctx is the type of ctx_d
435    *)
436     let rec aux uri ty left right ctx_d ctx_ty = function
437       | Cic.Appl ((Cic.Const(uri_sym,ens))::tl) 
438         when LibraryObjects.is_sym_eq_URI uri_sym  ->
439           let ty,l,r,p = open_sym ens tl in
440           mk_sym uri_sym ty l r (aux uri ty l r ctx_d ctx_ty p)
441       | Cic.LetIn (name,body,rest) ->
442           (* we should go in body *)
443           Cic.LetIn (name,body,aux uri ty left right ctx_d ctx_ty rest)
444       | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
445         when LibraryObjects.is_eq_ind_URI uri_ind || 
446              LibraryObjects.is_eq_ind_r_URI uri_ind ->
447           let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
448           let ty2,eq,lp,rp = open_pred pred in 
449           let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
450           let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
451           let is_not_fixed_lp = is_not_fixed lp in
452           let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
453           (* extract the context and the fixed term from the predicate *)
454           let m, ctx_c, ty2 = 
455             let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
456             (* they were under a lambda *)
457             let m =  CicSubstitution.subst hole m in
458             let ctx_c = CicSubstitution.subst hole ctx_c in
459             let ty2 = CicSubstitution.subst hole ty2 in
460             m, ctx_c, ty2          
461           in
462           (* create the compound context and put the terms under it *)
463           let ctx_dc = compose_contexts ctx_d ctx_c in
464           let dc_what = put_in_ctx ctx_dc what in
465           let dc_other = put_in_ctx ctx_dc other in
466           (* m is already in ctx_c so it is put in ctx_d only *)
467           let d_m = put_in_ctx ctx_d m in
468           (* we also need what in ctx_c *)
469           let c_what = put_in_ctx ctx_c what in
470           (* now put the proofs in the compound context *)
471           let p1 = (* p1: dc_what = d_m *)
472             if is_not_fixed_lp then 
473               aux uri ty2 c_what m ctx_d ctx_ty p1 
474             else
475               mk_sym uri_sym ctx_ty d_m dc_what
476                 (aux uri ty2 m c_what ctx_d ctx_ty p1)
477           in
478           let p2 = (* p2: dc_other = dc_what *)
479             if avoid_eq_ind then
480               mk_sym uri_sym ctx_ty dc_what dc_other
481                 (aux uri ty1 what other ctx_dc ctx_ty p2)
482             else
483               aux uri ty1 other what ctx_dc ctx_ty p2
484           in
485           (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
486              if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
487           let a,b,c,paeqb,pbeqc =
488             if is_not_fixed_lp then
489               dc_other,dc_what,d_m,p2,p1
490             else
491               d_m,dc_what,dc_other,
492                 (mk_sym uri_sym ctx_ty dc_what d_m p1),
493                 (mk_sym uri_sym ctx_ty dc_other dc_what p2)
494           in
495           mk_trans uri_trans ctx_ty a b c paeqb pbeqc
496     | t when ctx_d = hole -> t 
497     | t -> 
498 (*         let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in *)
499 (*         let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in *)
500         let uri_feq = 
501           UriManager.uri_of_string "cic:/matita/logic/equality/eq_f.con"
502         in
503         let pred = 
504 (*           let r = CicSubstitution.lift 1 (put_in_ctx ctx_d left) in *)
505           let l = 
506             let ctx_d = CicSubstitution.lift 1 ctx_d in
507             put_in_ctx ctx_d (Cic.Rel 1)
508           in
509 (*           let lty = CicSubstitution.lift 1 ctx_ty in  *)
510 (*           Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r)) *)
511           Cic.Lambda (Cic.Name "foo",ty,l)
512         in
513 (*         let d_left = put_in_ctx ctx_d left in *)
514 (*         let d_right = put_in_ctx ctx_d right in *)
515 (*         let refl_eq = mk_refl uri ctx_ty d_left in *)
516 (*         mk_sym uri_sym ctx_ty d_right d_left *)
517 (*           (mk_eq_ind uri_ind ty left pred refl_eq right t) *)
518           (mk_feq uri_feq ty ctx_ty left pred right t)
519   in
520   aux uri ty left right hole ty t
521 ;;
522
523 let contextualize_rewrites t ty = 
524   let eq,ty,l,r = open_eq ty in
525   contextualize eq ty l r t
526 ;;
527
528 let add_subst subst =
529   function
530     | Exact t -> Exact (Subst.apply_subst subst t)
531     | Step (s,(rule, id1, (pos,id2), pred)) -> 
532         Step (Subst.concat subst s,(rule, id1, (pos,id2), pred))
533 ;;
534         
535 let build_proof_step eq lift subst p1 p2 pos l r pred =
536   let p1 = Subst.apply_subst_lift lift subst p1 in
537   let p2 = Subst.apply_subst_lift lift subst p2 in
538   let l  = CicSubstitution.lift lift l in
539   let l = Subst.apply_subst_lift lift subst l in
540   let r  = CicSubstitution.lift lift r in
541   let r = Subst.apply_subst_lift lift subst r in
542   let pred = CicSubstitution.lift lift pred in
543   let pred = Subst.apply_subst_lift lift subst pred in
544   let ty,body = 
545     match pred with
546       | Cic.Lambda (_,ty,body) -> ty,body 
547       | _ -> assert false
548   in
549   let what, other = 
550     if pos = Utils.Left then l,r else r,l
551   in
552   let p =
553     match pos with
554       | Utils.Left ->
555         mk_eq_ind (LibraryObjects.eq_ind_URI ~eq) ty what pred p1 other p2
556       | Utils.Right ->
557         mk_eq_ind (LibraryObjects.eq_ind_r_URI ~eq) ty what pred p1 other p2
558   in
559     p
560 ;;
561
562 let parametrize_proof p l r ty = 
563   let uniq l = HExtlib.list_uniq (List.sort Pervasives.compare l) in
564   let mot = CicUtil.metas_of_term_set in
565   let parameters = uniq (mot p @ mot l @ mot r) in 
566   (* ?if they are under a lambda? *)
567   let parameters = 
568     HExtlib.list_uniq (List.sort Pervasives.compare parameters) 
569   in
570   let what = List.map (fun (i,l) -> Cic.Meta (i,l)) parameters in 
571   let with_what, lift_no = 
572     List.fold_right (fun _ (acc,n) -> ((Cic.Rel n)::acc),n+1) what ([],1) 
573   in
574   let p = CicSubstitution.lift (lift_no-1) p in
575   let p = 
576     ProofEngineReduction.replace_lifting
577     ~equality:(fun t1 t2 -> 
578       match t1,t2 with Cic.Meta (i,_),Cic.Meta(j,_) -> i=j | _ -> false) 
579     ~what ~with_what ~where:p
580   in
581   let ty_of_m _ = ty (*function 
582     | Cic.Meta (i,_) -> List.assoc i menv 
583     | _ -> assert false *)
584   in
585   let args, proof,_ = 
586     List.fold_left 
587       (fun (instance,p,n) m -> 
588         (instance@[m],
589         Cic.Lambda 
590           (Cic.Name ("x"^string_of_int n),
591           CicSubstitution.lift (lift_no - n - 1) (ty_of_m m),
592           p),
593         n+1)) 
594       ([Cic.Rel 1],p,1) 
595       what
596   in
597   let instance = match args with | [x] -> x | _ -> Cic.Appl args in
598   proof, instance
599 ;;
600
601 let wfo goalproof proof id =
602   let rec aux acc id =
603     let p,_,_ = proof_of_id id in
604     match p with
605     | Exact _ -> if (List.mem id acc) then acc else id :: acc
606     | Step (_,(_,id1, (_,id2), _)) -> 
607         let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
608         let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
609         id :: acc
610   in
611   let acc = 
612     match proof with
613       | Exact _ -> [id]
614       | Step (_,(_,id1, (_,id2), _)) -> aux (aux [id] id1) id2
615   in 
616   List.fold_left (fun acc (_,_,id,_,_) -> aux acc id) acc goalproof
617 ;;
618
619 let string_of_id names id = 
620   if id = 0 then "" else 
621   try
622     let (_,p,(_,l,r,_),m,_) = open_equality (Hashtbl.find id_to_eq id) in
623     match p with
624     | Exact t -> 
625         Printf.sprintf "%d = %s: %s = %s [%s]" id
626           (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
627         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
628     | Step (_,(step,id1, (_,id2), _) ) ->
629         Printf.sprintf "%6d: %s %6d %6d   %s = %s [%s]" id
630           (string_of_rule step)
631           id1 id2 (CicPp.pp l names) (CicPp.pp r names)
632         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
633   with
634       Not_found -> assert false
635
636 let pp_proof names goalproof proof subst id initial_goal =
637   String.concat "\n" (List.map (string_of_id names) (wfo goalproof proof id)) ^ 
638   "\ngoal:\n   " ^ 
639     (String.concat "\n   " 
640       (fst (List.fold_right
641         (fun (r,pos,i,s,pred) (acc,g) -> 
642           let _,_,left,right = open_eq g in
643           let ty = 
644             match pos with 
645             | Utils.Left -> CicReduction.head_beta_reduce (Cic.Appl[pred;right])
646             | Utils.Right -> CicReduction.head_beta_reduce (Cic.Appl[pred;left])
647           in
648           let ty = Subst.apply_subst s ty in
649           ("("^ string_of_rule r ^ " " ^ string_of_int i^") -> "
650           ^ CicPp.pp ty names) :: acc,ty) goalproof ([],initial_goal)))) ^
651   "\nand then subsumed by " ^ string_of_int id ^ " when " ^ Subst.ppsubst subst
652 ;;
653
654 module OT = 
655   struct
656     type t = int
657     let compare = Pervasives.compare
658   end
659
660 module M = Map.Make(OT)
661
662 let rec find_deps m i = 
663   if M.mem i m then m
664   else 
665     let p,_,_ = proof_of_id i in
666     match p with
667     | Exact _ -> M.add i [] m
668     | Step (_,(_,id1,(_,id2),_)) -> 
669         let m = find_deps m id1 in
670         let m = find_deps m id2 in
671         (* without the uniq there is a stack overflow doing concatenation *)
672         let xxx = [id1;id2] @ M.find id1 m @ M.find id2 m in 
673         let xxx = HExtlib.list_uniq (List.sort Pervasives.compare xxx) in
674         M.add i xxx m
675 ;;
676
677 let topological_sort l = 
678   (* build the partial order relation *)
679   let m = List.fold_left (fun m i -> find_deps m i) M.empty l in
680   let m = (* keep only deps inside l *) 
681     List.fold_left 
682       (fun m' i ->
683         M.add i (List.filter (fun x -> List.mem x l) (M.find i m)) m') 
684       M.empty l 
685   in
686   let m = M.map (fun x -> Some x) m in
687   (* utils *)
688   let keys m = M.fold (fun i _ acc -> i::acc) m [] in
689   let split l m = List.filter (fun i -> M.find i m = Some []) l in
690   let purge l m = 
691     M.mapi 
692       (fun k v -> if List.mem k l then None else 
693          match v with
694          | None -> None
695          | Some ll -> Some (List.filter (fun i -> not (List.mem i l)) ll)) 
696       m
697   in
698   let rec aux m res = 
699       let keys = keys m in
700       let ok = split keys m in
701       let m = purge ok m in
702       let res = ok @ res in
703       if ok = [] then res else aux m res
704   in
705   let rc = List.rev (aux m []) in
706   rc
707 ;;
708   
709
710 (* returns the list of ids that should be factorized *)
711 let get_duplicate_step_in_wfo l p =
712   let ol = List.rev l in
713   let h = Hashtbl.create 13 in
714   (* NOTE: here the n parameter is an approximation of the dependency 
715      between equations. To do things seriously we should maintain a 
716      dependency graph. This approximation is not perfect. *)
717   let add i = 
718     let p,_,_ = proof_of_id i in 
719     match p with 
720     | Exact _ -> true
721     | _ -> 
722         try 
723           let no = Hashtbl.find h i in
724           Hashtbl.replace h i (no+1);
725           false
726         with Not_found -> Hashtbl.add h i 1;true
727   in
728   let rec aux = function
729     | Exact _ -> ()
730     | Step (_,(_,i1,(_,i2),_)) -> 
731         let go_on_1 = add i1 in
732         let go_on_2 = add i2 in
733         if go_on_1 then aux (let p,_,_ = proof_of_id i1 in p);
734         if go_on_2 then aux (let p,_,_ = proof_of_id i2 in p)
735   in
736   aux p;
737   List.iter
738     (fun (_,_,id,_,_) -> aux (let p,_,_ = proof_of_id id in p))
739     ol;
740   (* now h is complete *)
741   let proofs = Hashtbl.fold (fun k count acc-> (k,count)::acc) h [] in
742   let proofs = List.filter (fun (_,c) -> c > 1) proofs in
743   let res = topological_sort (List.map (fun (i,_) -> i) proofs) in
744   res
745 ;;
746
747 let build_proof_term eq h lift proof =
748   let proof_of_id aux id =
749     let p,l,r = proof_of_id id in
750     try List.assoc id h,l,r with Not_found -> aux p, l, r
751   in
752   let rec aux = function
753      | Exact term -> 
754          CicSubstitution.lift lift term
755      | Step (subst,(rule, id1, (pos,id2), pred)) ->
756          let p1,_,_ = proof_of_id aux id1 in
757          let p2,l,r = proof_of_id aux id2 in
758          let varname = 
759            match rule with
760            | SuperpositionRight -> Cic.Name ("SupR" ^ Utils.string_of_pos pos) 
761            | Demodulation -> Cic.Name ("DemEq"^ Utils.string_of_pos pos)
762            | _ -> assert false
763          in
764          let pred = 
765            match pred with
766            | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
767            | _ -> assert false
768          in
769          let p = build_proof_step eq lift subst p1 p2 pos l r pred in
770 (*         let cond =  (not (List.mem 302 (Utils.metas_of_term p)) || id1 = 8 || id1 = 132) in
771            if not cond then
772              prerr_endline ("ERROR " ^ string_of_int id1 ^ " " ^ string_of_int id2);
773            assert cond;*)
774            p
775   in
776    aux proof
777 ;;
778
779 let build_goal_proof eq l initial ty se context menv =
780   let se = List.map (fun i -> Cic.Meta (i,[])) se in 
781   let lets = get_duplicate_step_in_wfo l initial in
782   let letsno = List.length lets in
783   let _,mty,_,_ = open_eq ty in
784   let lift_list l = List.map (fun (i,t) -> i,CicSubstitution.lift 1 t) l in
785   let lets,_,h = 
786     List.fold_left
787       (fun (acc,n,h) id -> 
788         let p,l,r = proof_of_id id in
789         let cic = build_proof_term eq h n p in
790         let real_cic,instance = 
791           parametrize_proof cic l r (CicSubstitution.lift n mty)
792         in
793         let h = (id, instance)::lift_list h in
794         acc@[id,real_cic],n+1,h) 
795       ([],0,[]) lets
796   in
797   let proof,se = 
798     let rec aux se current_proof = function
799       | [] -> current_proof,se
800       | (rule,pos,id,subst,pred)::tl ->
801           let p,l,r = proof_of_id id in
802            let p = build_proof_term eq h letsno p in
803            let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
804          let varname = 
805            match rule with
806            | SuperpositionLeft -> Cic.Name ("SupL" ^ Utils.string_of_pos pos) 
807            | Demodulation -> Cic.Name ("DemG"^ Utils.string_of_pos pos)
808            | _ -> assert false
809          in
810          let pred = 
811            match pred with
812            | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
813            | _ -> assert false
814          in
815            let proof = 
816              build_proof_step eq letsno subst current_proof p pos l r pred
817            in
818            let proof,se = aux se proof tl in
819            Subst.apply_subst_lift letsno subst proof,
820            List.map (fun x -> Subst.apply_subst_lift letsno subst x) se
821     in
822     aux se (build_proof_term eq h letsno initial) l
823   in
824   let n,proof = 
825     let initial = proof in
826     List.fold_right
827       (fun (id,cic) (n,p) -> 
828         n-1,
829         Cic.LetIn (
830           Cic.Name ("H"^string_of_int id),
831           cic, p))
832     lets (letsno-1,initial)
833   in
834    canonical 
835      (contextualize_rewrites proof (CicSubstitution.lift letsno ty))
836      context menv,
837    se 
838 ;;
839
840 let refl_proof eq_uri ty term = 
841   Cic.Appl [Cic.MutConstruct (eq_uri, 0, 1, []); ty; term]
842 ;;
843
844 let metas_of_proof p =
845   let eq = 
846     match LibraryObjects.eq_URI () with
847     | Some u -> u 
848     | None -> 
849         raise 
850           (ProofEngineTypes.Fail 
851             (lazy "No default equality defined when calling metas_of_proof"))
852   in
853   let p = build_proof_term eq [] 0 p in
854   Utils.metas_of_term p
855 ;;
856
857 let remove_local_context eq =
858    let w, p, (ty, left, right, o), menv,id = open_equality eq in
859    let p = Utils.remove_local_context p in
860    let ty = Utils.remove_local_context ty in
861    let left = Utils.remove_local_context left in
862    let right = Utils.remove_local_context right in
863    w, p, (ty, left, right, o), menv, id
864 ;;
865
866 let relocate newmeta menv to_be_relocated =
867   let subst, newmetasenv, newmeta = 
868     List.fold_right 
869       (fun i (subst, metasenv, maxmeta) ->         
870         let _,context,ty = CicUtil.lookup_meta i menv in
871         let irl = [] in
872         let newmeta = Cic.Meta(maxmeta,irl) in
873         let newsubst = Subst.buildsubst i context newmeta ty subst in
874         newsubst, (maxmeta,context,ty)::metasenv, maxmeta+1) 
875       to_be_relocated (Subst.empty_subst, [], newmeta+1)
876   in
877   let menv = Subst.apply_subst_metasenv subst menv @ newmetasenv in
878   subst, menv, newmeta
879
880 let fix_metas_goal newmeta goal =
881   let (proof, menv, ty) = goal in
882   let to_be_relocated = 
883     HExtlib.list_uniq (List.sort Pervasives.compare (Utils.metas_of_term ty))
884   in
885   let subst, menv, newmeta = relocate newmeta menv to_be_relocated in
886   let ty = Subst.apply_subst subst ty in
887   let proof = 
888     match proof with
889     | [] -> assert false (* is a nonsense to relocate the initial goal *)
890     | (r,pos,id,s,p) :: tl -> (r,pos,id,Subst.concat subst s,p) :: tl
891   in
892   newmeta+1,(proof, menv, ty)
893 ;;
894
895 let fix_metas newmeta eq = 
896   let w, p, (ty, left, right, o), menv,_ = open_equality eq in
897   let to_be_relocated = 
898 (* List.map (fun i ,_,_ -> i) menv *)
899     HExtlib.list_uniq 
900       (List.sort Pervasives.compare 
901          (Utils.metas_of_term left @ Utils.metas_of_term right)) 
902   in
903   let subst, metasenv, newmeta = relocate newmeta menv to_be_relocated in
904   let ty = Subst.apply_subst subst ty in
905   let left = Subst.apply_subst subst left in
906   let right = Subst.apply_subst subst right in
907   let fix_proof = function
908     | Exact p -> Exact (Subst.apply_subst subst p)
909     | Step (s,(r,id1,(pos,id2),pred)) -> 
910         Step (Subst.concat s subst,(r,id1,(pos,id2), pred))
911   in
912   let p = fix_proof p in
913   let eq' = mk_equality (w, p, (ty, left, right, o), metasenv) in
914   newmeta+1, eq'  
915
916 exception NotMetaConvertible;;
917
918 let meta_convertibility_aux table t1 t2 =
919   let module C = Cic in
920   let rec aux ((table_l, table_r) as table) t1 t2 =
921     match t1, t2 with
922     | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
923         let tl1, tl2 = [],[] in
924         let m1_binding, table_l =
925           try List.assoc m1 table_l, table_l
926           with Not_found -> m2, (m1, m2)::table_l
927         and m2_binding, table_r =
928           try List.assoc m2 table_r, table_r
929           with Not_found -> m1, (m2, m1)::table_r
930         in
931         if (m1_binding <> m2) || (m2_binding <> m1) then
932           raise NotMetaConvertible
933         else (
934           try
935             List.fold_left2
936               (fun res t1 t2 ->
937                  match t1, t2 with
938                  | None, Some _ | Some _, None -> raise NotMetaConvertible
939                  | None, None -> res
940                  | Some t1, Some t2 -> (aux res t1 t2))
941               (table_l, table_r) tl1 tl2
942           with Invalid_argument _ ->
943             raise NotMetaConvertible
944         )
945     | C.Var (u1, ens1), C.Var (u2, ens2)
946     | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
947         aux_ens table ens1 ens2
948     | C.Cast (s1, t1), C.Cast (s2, t2)
949     | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
950     | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
951     | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
952         let table = aux table s1 s2 in
953         aux table t1 t2
954     | C.Appl l1, C.Appl l2 -> (
955         try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
956         with Invalid_argument _ -> raise NotMetaConvertible
957       )
958     | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
959         when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
960     | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
961         when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
962         aux_ens table ens1 ens2
963     | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
964         when (UriManager.eq u1 u2) && i1 = i2 ->
965         let table = aux table s1 s2 in
966         let table = aux table t1 t2 in (
967           try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
968           with Invalid_argument _ -> raise NotMetaConvertible
969         )
970     | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
971         try
972           List.fold_left2
973             (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
974                if i1 <> i2 then raise NotMetaConvertible
975                else
976                  let res = (aux res s1 s2) in aux res t1 t2)
977             table il1 il2
978         with Invalid_argument _ -> raise NotMetaConvertible
979       )
980     | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
981         try
982           List.fold_left2
983             (fun res (n1, s1, t1) (n2, s2, t2) ->
984                let res = aux res s1 s2 in aux res t1 t2)
985             table il1 il2
986         with Invalid_argument _ -> raise NotMetaConvertible
987       )
988     | t1, t2 when t1 = t2 -> table
989     | _, _ -> raise NotMetaConvertible
990         
991   and aux_ens table ens1 ens2 =
992     let cmp (u1, t1) (u2, t2) =
993       Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
994     in
995     let ens1 = List.sort cmp ens1
996     and ens2 = List.sort cmp ens2 in
997     try
998       List.fold_left2
999         (fun res (u1, t1) (u2, t2) ->
1000            if not (UriManager.eq u1 u2) then raise NotMetaConvertible
1001            else aux res t1 t2)
1002         table ens1 ens2
1003     with Invalid_argument _ -> raise NotMetaConvertible
1004   in
1005   aux table t1 t2
1006 ;;
1007
1008
1009 let meta_convertibility_eq eq1 eq2 =
1010   let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
1011   let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
1012   if ty <> ty' then
1013     false
1014   else if (left = left') && (right = right') then
1015     true
1016   else if (left = right') && (right = left') then
1017     true
1018   else
1019     try
1020       let table = meta_convertibility_aux ([], []) left left' in
1021       let _ = meta_convertibility_aux table right right' in
1022       true
1023     with NotMetaConvertible ->
1024       try
1025         let table = meta_convertibility_aux ([], []) left right' in
1026         let _ = meta_convertibility_aux table right left' in
1027         true
1028       with NotMetaConvertible ->
1029         false
1030 ;;
1031
1032
1033 let meta_convertibility t1 t2 =
1034   if t1 = t2 then
1035     true
1036   else
1037     try
1038       ignore(meta_convertibility_aux ([], []) t1 t2);
1039       true
1040     with NotMetaConvertible ->
1041       false
1042 ;;
1043
1044 exception TermIsNotAnEquality;;
1045
1046 let term_is_equality term =
1047   match term with
1048   | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] 
1049     when LibraryObjects.is_eq_URI uri -> true
1050   | _ -> false
1051 ;;
1052
1053 let equality_of_term proof term =
1054   match term with
1055   | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] 
1056     when LibraryObjects.is_eq_URI uri ->
1057       let o = !Utils.compare_terms t1 t2 in
1058       let stat = (ty,t1,t2,o) in
1059       let w = Utils.compute_equality_weight stat in
1060       let e = mk_equality (w, Exact proof, stat,[]) in
1061       e
1062   | _ ->
1063       raise TermIsNotAnEquality
1064 ;;
1065
1066 let is_weak_identity eq = 
1067   let _,_,(_,left, right,_),_,_ = open_equality eq in
1068   left = right || meta_convertibility left right 
1069 ;;
1070
1071 let is_identity (_, context, ugraph) eq = 
1072   let _,_,(ty,left,right,_),menv,_ = open_equality eq in
1073   left = right ||
1074   (* (meta_convertibility left right)) *)
1075   fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
1076 ;;
1077
1078
1079 let term_of_equality eq_uri equality =
1080   let _, _, (ty, left, right, _), menv, _= open_equality equality in
1081   let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1082   let argsno = List.length menv in
1083   let t =
1084     CicSubstitution.lift argsno
1085       (Cic.Appl [Cic.MutInd (eq_uri, 0, []); ty; left; right])
1086   in
1087   snd (
1088     List.fold_right
1089       (fun (i,_,ty) (n, t) ->
1090          let name = Cic.Name ("X" ^ (string_of_int n)) in
1091          let ty = CicSubstitution.lift (n-1) ty in
1092          let t = 
1093            ProofEngineReduction.replace
1094              ~equality:eq ~what:[i]
1095              ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1096          in
1097            (n-1, Cic.Prod (name, ty, t)))
1098       menv (argsno, t))
1099 ;;
1100
1101 let symmetric eq_ty l id uri m =
1102   let eq = Cic.MutInd(uri,0,[]) in
1103   let pred = 
1104     Cic.Lambda (Cic.Name "Sym",eq_ty,
1105      Cic.Appl [CicSubstitution.lift 1 eq ;
1106                CicSubstitution.lift 1 eq_ty;
1107                Cic.Rel 1;CicSubstitution.lift 1 l]) 
1108   in
1109   let prefl = 
1110     Exact (Cic.Appl
1111       [Cic.MutConstruct(uri,0,1,[]);eq_ty;l]) 
1112   in
1113   let id1 = 
1114     let eq = mk_equality (0,prefl,(eq_ty,l,l,Utils.Eq),m) in
1115     let (_,_,_,_,id) = open_equality eq in
1116     id
1117   in
1118   Step(Subst.empty_subst,
1119     (Demodulation,id1,(Utils.Left,id),pred))
1120 ;;
1121
1122 module IntOT = struct
1123   type t = int
1124   let compare = Pervasives.compare
1125 end
1126
1127 module IntSet = Set.Make(IntOT);;
1128
1129 let n_purged = ref 0;;
1130
1131 let collect alive1 alive2 alive3 =
1132   let _ = <:start<collect>> in
1133   let deps_of id = 
1134     let p,_,_ = proof_of_id id in  
1135     match p with
1136     | Exact _ -> IntSet.empty
1137     | Step (_,(_,id1,(_,id2),_)) ->
1138           IntSet.add id1 (IntSet.add id2 IntSet.empty)
1139   in
1140   let rec close s = 
1141     let news = IntSet.fold (fun id s -> IntSet.union (deps_of id) s) s s in
1142     if IntSet.equal news s then s else close news
1143   in
1144   let l_to_s s l = List.fold_left (fun s x -> IntSet.add x s) s l in
1145   let alive_set = l_to_s (l_to_s (l_to_s IntSet.empty alive2) alive1) alive3 in
1146   let closed_alive_set = close alive_set in
1147   let to_purge = 
1148     Hashtbl.fold 
1149       (fun k _ s -> 
1150         if not (IntSet.mem k closed_alive_set) then
1151           k::s else s) id_to_eq []
1152   in
1153   n_purged := !n_purged + List.length to_purge;
1154   List.iter (Hashtbl.remove id_to_eq) to_purge;
1155   let _ = <:stop<collect>> in ()  
1156 ;;
1157
1158 let id_of e = 
1159   let _,_,_,_,id = open_equality e in id
1160 ;;
1161
1162 let get_stats () = 
1163   <:show<Equality.>> ^ 
1164   "# of purged eq by the collector: " ^ string_of_int !n_purged ^ "\n"
1165 ;;