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