]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/equality.ml
7123c134a93a98056655f6cee83e55600b559945
[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
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 rec remove_refl t =
273     match t with
274     | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
275           when LibraryObjects.is_trans_eq_URI uri_trans ->
276           let ty,l,m,r,p1,p2 = open_trans ens tl in
277             (match p1,p2 with
278               | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 -> 
279                   remove_refl p2
280               | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] -> 
281                   remove_refl p1
282               | _ -> Cic.Appl (List.map remove_refl args))
283     | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
284     | Cic.LetIn (name,bo,rest) ->
285         Cic.LetIn (name,remove_refl bo,remove_refl rest)
286     | _ -> t
287   in
288   let rec canonical context t =
289     match t with
290       | Cic.LetIn(name,bo,rest) -> 
291           let context' = (Some (name,Cic.Def (bo,None)))::context in
292           Cic.LetIn(name,canonical context bo,canonical context' rest)
293       | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
294           when LibraryObjects.is_sym_eq_URI uri_sym ->
295           (match p_of_sym ens tl with
296              | Cic.Appl ((Cic.Const(uri,ens))::tl)
297                  when LibraryObjects.is_sym_eq_URI uri -> 
298                    canonical context (p_of_sym ens tl)
299              | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
300                  when LibraryObjects.is_trans_eq_URI uri_trans ->
301                  let ty,l,m,r,p1,p2 = open_trans ens tl in
302                    mk_trans uri_trans ty r m l 
303                      (canonical context (mk_sym uri_sym ty m r p2)) 
304                      (canonical context (mk_sym uri_sym ty l m p1))
305              | Cic.Appl (([Cic.Const(uri_feq,ens);ty1;ty2;f;x;y;p])) ->
306                  let eq = LibraryObjects.eq_URI_of_eq_f_URI uri_feq in
307                  let eq_f_sym =
308                    Cic.Const (LibraryObjects.eq_f_sym_URI ~eq, [])
309                  in
310                  Cic.Appl (([eq_f_sym;ty1;ty2;f;x;y;p]))  
311              | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
312                  when LibraryObjects.is_eq_URI uri -> t
313              | _ -> Cic.Appl (List.map (canonical context) args))
314       | Cic.Appl l -> Cic.Appl (List.map (canonical context) l)
315       | _ -> t
316   in
317   remove_refl (canonical context t)
318 ;;
319   
320 let compose_contexts ctx1 ctx2 = 
321   ProofEngineReduction.replace_lifting 
322     ~equality:(=) ~what:[Cic.Implicit(Some `Hole)] ~with_what:[ctx2] ~where:ctx1
323 ;;
324
325 let put_in_ctx ctx t = 
326   ProofEngineReduction.replace_lifting
327     ~equality:(=) ~what:[Cic.Implicit (Some `Hole)] ~with_what:[t] ~where:ctx
328 ;;
329
330 let mk_eq uri ty l r =
331   let ens, args = build_ens uri [ty; l; r] in
332   Cic.Appl (Cic.MutInd(uri,0,ens) :: args)
333 ;;
334
335 let mk_refl uri ty t = 
336   let ens, args = build_ens uri [ty; t] in
337   Cic.Appl (Cic.MutConstruct(uri,0,1,ens) :: args)
338 ;;
339
340 let open_eq = function 
341   | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
342       uri, ty, l ,r
343   | _ -> assert false
344 ;;
345
346 let mk_feq uri_feq ty ty1 left pred right t = 
347   let ens, args = build_ens uri_feq [ty;ty1;pred;left;right;t] in
348   Cic.Appl (Cic.Const(uri_feq,ens) :: args)
349 ;;
350
351 let rec look_ahead aux = function
352   | Cic.Appl ((Cic.Const(uri_ind,ens))::tl) as t
353         when LibraryObjects.is_eq_ind_URI uri_ind || 
354              LibraryObjects.is_eq_ind_r_URI uri_ind ->
355           let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
356           let ty2,eq,lp,rp = open_pred pred in 
357           let hole = Cic.Implicit (Some `Hole) in
358           let ty2 = CicSubstitution.subst hole ty2 in
359           aux ty1 (CicSubstitution.subst other lp) (CicSubstitution.subst other rp) hole ty2 t
360   | Cic.Lambda (n,s,t) -> Cic.Lambda (n,s,look_ahead aux t)
361   | t -> t
362 ;;
363
364 let contextualize uri ty left right t = 
365   let hole = Cic.Implicit (Some `Hole) in
366   (* aux [uri] [ty] [left] [right] [ctx] [ctx_ty] [t] 
367    * 
368    * the parameters validate this invariant  
369    *   t: eq(uri) ty left right
370    * that is used only by the base case
371    *
372    * ctx is a term with an hole. Cic.Implicit(Some `Hole) is the empty context
373    * ctx_ty is the type of ctx
374    *)
375     let rec aux uri ty left right ctx_d ctx_ty = function
376       | Cic.Appl ((Cic.Const(uri_sym,ens))::tl) 
377         when LibraryObjects.is_sym_eq_URI uri_sym  ->
378           let ty,l,r,p = open_sym ens tl in
379           mk_sym uri_sym ty l r (aux uri ty l r ctx_d ctx_ty p)
380       | Cic.LetIn (name,body,rest) ->
381           Cic.LetIn (name,look_ahead (aux uri) body, aux uri ty left right ctx_d ctx_ty rest)
382       | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
383         when LibraryObjects.is_eq_ind_URI uri_ind || 
384              LibraryObjects.is_eq_ind_r_URI uri_ind ->
385           let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
386           let ty2,eq,lp,rp = open_pred pred in 
387           let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
388           let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
389           let is_not_fixed_lp = is_not_fixed lp in
390           let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
391           (* extract the context and the fixed term from the predicate *)
392           let m, ctx_c, ty2 = 
393             let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
394             (* they were under a lambda *)
395             let m =  CicSubstitution.subst hole m in
396             let ctx_c = CicSubstitution.subst hole ctx_c in
397             let ty2 = CicSubstitution.subst hole ty2 in
398             m, ctx_c, ty2          
399           in
400           (* create the compound context and put the terms under it *)
401           let ctx_dc = compose_contexts ctx_d ctx_c in
402           let dc_what = put_in_ctx ctx_dc what in
403           let dc_other = put_in_ctx ctx_dc other in
404           (* m is already in ctx_c so it is put in ctx_d only *)
405           let d_m = put_in_ctx ctx_d m in
406           (* we also need what in ctx_c *)
407           let c_what = put_in_ctx ctx_c what in
408           (* now put the proofs in the compound context *)
409           let p1 = (* p1: dc_what = d_m *)
410             if is_not_fixed_lp then 
411               aux uri ty2 c_what m ctx_d ctx_ty p1 
412             else
413               mk_sym uri_sym ctx_ty d_m dc_what
414                 (aux uri ty2 m c_what ctx_d ctx_ty p1)
415           in
416           let p2 = (* p2: dc_other = dc_what *)
417             if avoid_eq_ind then
418               mk_sym uri_sym ctx_ty dc_what dc_other
419                 (aux uri ty1 what other ctx_dc ctx_ty p2)
420             else
421               aux uri ty1 other what ctx_dc ctx_ty p2
422           in
423           (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
424              if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
425           let a,b,c,paeqb,pbeqc =
426             if is_not_fixed_lp then
427               dc_other,dc_what,d_m,p2,p1
428             else
429               d_m,dc_what,dc_other,
430                 (mk_sym uri_sym ctx_ty dc_what d_m p1),
431                 (mk_sym uri_sym ctx_ty dc_other dc_what p2)
432           in
433           mk_trans uri_trans ctx_ty a b c paeqb pbeqc
434     | t when ctx_d = hole -> t 
435     | t -> 
436 (*         let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in *)
437 (*         let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in *)
438
439         let uri_feq = LibraryObjects.eq_f_URI ~eq:uri in
440         let pred = 
441 (*           let r = CicSubstitution.lift 1 (put_in_ctx ctx_d left) in *)
442           let l = 
443             let ctx_d = CicSubstitution.lift 1 ctx_d in
444             put_in_ctx ctx_d (Cic.Rel 1)
445           in
446 (*           let lty = CicSubstitution.lift 1 ctx_ty in  *)
447 (*           Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r)) *)
448           Cic.Lambda (Cic.Name "foo",ty,l)
449         in
450 (*         let d_left = put_in_ctx ctx_d left in *)
451 (*         let d_right = put_in_ctx ctx_d right in *)
452 (*         let refl_eq = mk_refl uri ctx_ty d_left in *)
453 (*         mk_sym uri_sym ctx_ty d_right d_left *)
454 (*           (mk_eq_ind uri_ind ty left pred refl_eq right t) *)
455           (mk_feq uri_feq ty ctx_ty left pred right t)
456   in
457   aux uri ty left right hole ty t
458 ;;
459
460 let contextualize_rewrites t ty = 
461   let eq,ty,l,r = open_eq ty in
462   contextualize eq ty l r t
463 ;;
464
465 let add_subst subst =
466   function
467     | Exact t -> Exact (Subst.apply_subst subst t)
468     | Step (s,(rule, id1, (pos,id2), pred)) -> 
469         Step (Subst.concat subst s,(rule, id1, (pos,id2), pred))
470 ;;
471         
472 let build_proof_step eq lift subst p1 p2 pos l r pred =
473   let p1 = Subst.apply_subst_lift lift subst p1 in
474   let p2 = Subst.apply_subst_lift lift subst p2 in
475   let l  = CicSubstitution.lift lift l in
476   let l = Subst.apply_subst_lift lift subst l in
477   let r  = CicSubstitution.lift lift r in
478   let r = Subst.apply_subst_lift lift subst r in
479   let pred = CicSubstitution.lift lift pred in
480   let pred = Subst.apply_subst_lift lift subst pred in
481   let ty,body = 
482     match pred with
483       | Cic.Lambda (_,ty,body) -> ty,body 
484       | _ -> assert false
485   in
486   let what, other = 
487     if pos = Utils.Left then l,r else r,l
488   in
489   let p =
490     match pos with
491       | Utils.Left ->
492         mk_eq_ind (LibraryObjects.eq_ind_URI ~eq) ty what pred p1 other p2
493       | Utils.Right ->
494         mk_eq_ind (LibraryObjects.eq_ind_r_URI ~eq) ty what pred p1 other p2
495   in
496     p
497 ;;
498
499 let parametrize_proof p l r ty = 
500   let uniq l = HExtlib.list_uniq (List.sort Pervasives.compare l) in
501   let mot = CicUtil.metas_of_term_set in
502   let parameters = uniq (mot p @ mot l @ mot r) in 
503   (* ?if they are under a lambda? *)
504 (*
505   let parameters = 
506     HExtlib.list_uniq (List.sort Pervasives.compare parameters) 
507   in
508 *)
509   let what = List.map (fun (i,l) -> Cic.Meta (i,l)) parameters in 
510   let with_what, lift_no = 
511     List.fold_right (fun _ (acc,n) -> ((Cic.Rel n)::acc),n+1) what ([],1) 
512   in
513   let p = CicSubstitution.lift (lift_no-1) p in
514   let p = 
515     ProofEngineReduction.replace_lifting
516     ~equality:(fun t1 t2 -> 
517       match t1,t2 with Cic.Meta (i,_),Cic.Meta(j,_) -> i=j | _ -> false) 
518     ~what ~with_what ~where:p
519   in
520   let ty_of_m _ = ty (*function 
521     | Cic.Meta (i,_) -> List.assoc i menv 
522     | _ -> assert false *)
523   in
524   let args, proof,_ = 
525     List.fold_left 
526       (fun (instance,p,n) m -> 
527         (instance@[m],
528         Cic.Lambda 
529           (Cic.Name ("X"^string_of_int n),
530           CicSubstitution.lift (lift_no - n - 1) (ty_of_m m),
531           p),
532         n+1)) 
533       ([Cic.Rel 1],p,1) 
534       what
535   in
536   let instance = match args with | [x] -> x | _ -> Cic.Appl args in
537   proof, instance
538 ;;
539
540 let wfo bag goalproof proof id =
541   let rec aux acc id =
542     let p,_,_ = proof_of_id bag id in
543     match p with
544     | Exact _ -> if (List.mem id acc) then acc else id :: acc
545     | Step (_,(_,id1, (_,id2), _)) -> 
546         let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
547         let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
548         id :: acc
549   in
550   let acc = 
551     match proof with
552       | Exact _ -> [id]
553       | Step (_,(_,id1, (_,id2), _)) -> aux (aux [id] id1) id2
554   in 
555   List.fold_left (fun acc (_,_,id,_,_) -> aux acc id) acc goalproof
556 ;;
557
558 let string_of_id (id_to_eq,_) names id = 
559   if id = 0 then "" else 
560   try
561     let (_,p,(_,l,r,_),m,_) = open_equality (Hashtbl.find id_to_eq id) in
562     match p with
563     | Exact t -> 
564         Printf.sprintf "%d = %s: %s = %s [%s]" id
565           (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
566           "..."
567 (*         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m)) *)
568     | Step (_,(step,id1, (_,id2), _) ) ->
569         Printf.sprintf "%6d: %s %6d %6d   %s = %s [%s]" id
570           (string_of_rule step)
571           id1 id2 (CicPp.pp l names) (CicPp.pp r names)
572 (*         (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m)) *)
573           "..."
574   with
575       Not_found -> assert false
576
577 let pp_proof bag names goalproof proof subst id initial_goal =
578   String.concat "\n" (List.map (string_of_id bag names) (wfo bag goalproof proof id)) ^ 
579   "\ngoal:\n   " ^ 
580     (String.concat "\n   " 
581       (fst (List.fold_right
582         (fun (r,pos,i,s,pred) (acc,g) -> 
583           let _,_,left,right = open_eq g in
584           let ty = 
585             match pos with 
586             | Utils.Left -> CicReduction.head_beta_reduce (Cic.Appl[pred;right])
587             | Utils.Right -> CicReduction.head_beta_reduce (Cic.Appl[pred;left])
588           in
589           let ty = Subst.apply_subst s ty in
590           ("("^ string_of_rule r ^ " " ^ string_of_int i^") -> "
591           ^ CicPp.pp ty names) :: acc,ty) goalproof ([],initial_goal)))) ^
592   "\nand then subsumed by " ^ string_of_int id ^ " when " ^ Subst.ppsubst subst
593 ;;
594
595 module OT = 
596   struct
597     type t = int
598     let compare = Pervasives.compare
599   end
600
601 module M = Map.Make(OT)
602
603 let rec find_deps bag m i = 
604   if M.mem i m then m
605   else 
606     let p,_,_ = proof_of_id bag i in
607     match p with
608     | Exact _ -> M.add i [] m
609     | Step (_,(_,id1,(_,id2),_)) -> 
610         let m = find_deps bag m id1 in
611         let m = find_deps bag m id2 in
612         (* without the uniq there is a stack overflow doing concatenation *)
613         let xxx = [id1;id2] @ M.find id1 m @ M.find id2 m in 
614         let xxx = HExtlib.list_uniq (List.sort Pervasives.compare xxx) in
615         M.add i xxx m
616 ;;
617
618 let topological_sort bag l = 
619   (* build the partial order relation *)
620   let m = List.fold_left (fun m i -> find_deps bag m i) M.empty l in
621   let m = (* keep only deps inside l *) 
622     List.fold_left 
623       (fun m' i ->
624         M.add i (List.filter (fun x -> List.mem x l) (M.find i m)) m') 
625       M.empty l 
626   in
627   let m = M.map (fun x -> Some x) m in
628   (* utils *)
629   let keys m = M.fold (fun i _ acc -> i::acc) m [] in
630   let split l m = List.filter (fun i -> M.find i m = Some []) l in
631   let purge l m = 
632     M.mapi 
633       (fun k v -> if List.mem k l then None else 
634          match v with
635          | None -> None
636          | Some ll -> Some (List.filter (fun i -> not (List.mem i l)) ll)) 
637       m
638   in
639   let rec aux m res = 
640       let keys = keys m in
641       let ok = split keys m in
642       let m = purge ok m in
643       let res = ok @ res in
644       if ok = [] then res else aux m res
645   in
646   let rc = List.rev (aux m []) in
647   rc
648 ;;
649   
650
651 (* returns the list of ids that should be factorized *)
652 let get_duplicate_step_in_wfo bag l p =
653   let ol = List.rev l in
654   let h = Hashtbl.create 13 in
655   (* NOTE: here the n parameter is an approximation of the dependency 
656      between equations. To do things seriously we should maintain a 
657      dependency graph. This approximation is not perfect. *)
658   let add i = 
659     let p,_,_ = proof_of_id bag i in 
660     match p with 
661     | Exact _ -> true
662     | _ -> 
663         try 
664           let no = Hashtbl.find h i in
665           Hashtbl.replace h i (no+1);
666           false
667         with Not_found -> Hashtbl.add h i 1;true
668   in
669   let rec aux = function
670     | Exact _ -> ()
671     | Step (_,(_,i1,(_,i2),_)) -> 
672         let go_on_1 = add i1 in
673         let go_on_2 = add i2 in
674         if go_on_1 then aux (let p,_,_ = proof_of_id bag i1 in p);
675         if go_on_2 then aux (let p,_,_ = proof_of_id bag i2 in p)
676   in
677   aux p;
678   List.iter
679     (fun (_,_,id,_,_) -> aux (let p,_,_ = proof_of_id bag id in p))
680     ol;
681   (* now h is complete *)
682   let proofs = Hashtbl.fold (fun k count acc-> (k,count)::acc) h [] in
683   let proofs = List.filter (fun (_,c) -> c > 1) proofs in
684   let res = topological_sort bag (List.map (fun (i,_) -> i) proofs) in
685   res
686 ;;
687
688 let build_proof_term bag eq h lift proof =
689   let proof_of_id aux id =
690     let p,l,r = proof_of_id bag id in
691     try List.assoc id h,l,r with Not_found -> aux p, l, r
692   in
693   let rec aux = function
694      | Exact term -> 
695          CicSubstitution.lift lift term
696      | Step (subst,(rule, id1, (pos,id2), pred)) ->
697          let p1,_,_ = proof_of_id aux id1 in
698          let p2,l,r = proof_of_id aux id2 in
699          let varname = 
700            match rule with
701            | SuperpositionRight -> Cic.Name ("SupR" ^ Utils.string_of_pos pos) 
702            | Demodulation -> Cic.Name ("DemEq"^ Utils.string_of_pos pos)
703            | _ -> assert false
704          in
705          let pred = 
706            match pred with
707            | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
708            | _ -> assert false
709          in
710          let p = build_proof_step eq lift subst p1 p2 pos l r pred in
711 (*         let cond =  (not (List.mem 302 (Utils.metas_of_term p)) || id1 = 8 || id1 = 132) in
712            if not cond then
713              prerr_endline ("ERROR " ^ string_of_int id1 ^ " " ^ string_of_int id2);
714            assert cond;*)
715            p
716   in
717    aux proof
718 ;;
719
720 let build_goal_proof bag eq l initial ty se context menv =
721   let se = List.map (fun i -> Cic.Meta (i,[])) se in 
722   let lets = get_duplicate_step_in_wfo bag l initial in
723   let letsno = List.length lets in
724   let _,mty,_,_ = open_eq ty in
725   let lift_list l = List.map (fun (i,t) -> i,CicSubstitution.lift 1 t) l in
726   let lets,_,h = 
727     List.fold_left
728       (fun (acc,n,h) id -> 
729         let p,l,r = proof_of_id bag id in
730         let cic = build_proof_term bag eq h n p in
731         let real_cic,instance = 
732           parametrize_proof cic l r (CicSubstitution.lift n mty)
733         in
734         let h = (id, instance)::lift_list h in
735         acc@[id,real_cic],n+1,h) 
736       ([],0,[]) lets
737   in
738   let proof,se = 
739     let rec aux se current_proof = function
740       | [] -> current_proof,se
741       | (rule,pos,id,subst,pred)::tl ->
742           let p,l,r = proof_of_id bag id in
743            let p = build_proof_term bag eq h letsno p in
744            let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
745          let varname = 
746            match rule with
747            | SuperpositionLeft -> Cic.Name ("SupL" ^ Utils.string_of_pos pos) 
748            | Demodulation -> Cic.Name ("DemG"^ Utils.string_of_pos pos)
749            | _ -> assert false
750          in
751          let pred = 
752            match pred with
753            | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
754            | _ -> assert false
755          in
756            let proof = 
757              build_proof_step eq letsno subst current_proof p pos l r pred
758            in
759            let proof,se = aux se proof tl in
760            Subst.apply_subst_lift letsno subst proof,
761            List.map (fun x -> Subst.apply_subst(*_lift letsno*) subst x) se
762     in
763     aux se (build_proof_term bag eq h letsno initial) l
764   in
765   let n,proof = 
766     let initial = proof in
767     List.fold_right
768       (fun (id,cic) (n,p) -> 
769         n-1,
770         Cic.LetIn (
771           Cic.Name ("H"^string_of_int id),
772           cic, p))
773     lets (letsno-1,initial)
774   in
775    canonical 
776      (contextualize_rewrites proof (CicSubstitution.lift letsno ty))
777      context menv,
778    se 
779 ;;
780
781 let refl_proof eq_uri ty term = 
782   Cic.Appl [Cic.MutConstruct (eq_uri, 0, 1, []); ty; term]
783 ;;
784
785 let metas_of_proof bag p =
786   let eq = 
787     match LibraryObjects.eq_URI () with
788     | Some u -> u 
789     | None -> 
790         raise 
791           (ProofEngineTypes.Fail 
792             (lazy "No default equality defined when calling metas_of_proof"))
793   in
794   let p = build_proof_term bag eq [] 0 p in
795   Utils.metas_of_term p
796 ;;
797
798 let remove_local_context eq =
799    let w, p, (ty, left, right, o), menv,id = open_equality eq in
800    let p = Utils.remove_local_context p in
801    let ty = Utils.remove_local_context ty in
802    let left = Utils.remove_local_context left in
803    let right = Utils.remove_local_context right in
804    w, p, (ty, left, right, o), menv, id
805 ;;
806
807 let relocate newmeta menv to_be_relocated =
808   let subst, newmetasenv, newmeta = 
809     List.fold_right 
810       (fun i (subst, metasenv, maxmeta) ->         
811         let _,context,ty = CicUtil.lookup_meta i menv in
812         let irl = [] in
813         let newmeta = Cic.Meta(maxmeta,irl) in
814         let newsubst = Subst.buildsubst i context newmeta ty subst in
815         newsubst, (maxmeta,context,ty)::metasenv, maxmeta+1) 
816       to_be_relocated (Subst.empty_subst, [], newmeta+1)
817   in
818   let menv = Subst.apply_subst_metasenv subst menv @ newmetasenv in
819   subst, menv, newmeta
820
821 let fix_metas_goal newmeta goal =
822   let (proof, menv, ty) = goal in
823   let to_be_relocated = 
824     HExtlib.list_uniq (List.sort Pervasives.compare (Utils.metas_of_term ty))
825   in
826   let subst, menv, newmeta = relocate newmeta menv to_be_relocated in
827   let ty = Subst.apply_subst subst ty in
828   let proof = 
829     match proof with
830     | [] -> assert false (* is a nonsense to relocate the initial goal *)
831     | (r,pos,id,s,p) :: tl -> (r,pos,id,Subst.concat subst s,p) :: tl
832   in
833   newmeta+1,(proof, menv, ty)
834 ;;
835
836 let fix_metas bag newmeta eq = 
837   let w, p, (ty, left, right, o), menv,_ = open_equality eq in
838   let to_be_relocated = 
839 (* List.map (fun i ,_,_ -> i) menv *)
840     HExtlib.list_uniq 
841       (List.sort Pervasives.compare 
842          (Utils.metas_of_term left @ Utils.metas_of_term right)) 
843   in
844   let subst, metasenv, newmeta = relocate newmeta menv to_be_relocated in
845   let ty = Subst.apply_subst subst ty in
846   let left = Subst.apply_subst subst left in
847   let right = Subst.apply_subst subst right in
848   let fix_proof = function
849     | Exact p -> Exact (Subst.apply_subst subst p)
850     | Step (s,(r,id1,(pos,id2),pred)) -> 
851         Step (Subst.concat s subst,(r,id1,(pos,id2), pred))
852   in
853   let p = fix_proof p in
854   let eq' = mk_equality bag (w, p, (ty, left, right, o), metasenv) in
855   newmeta+1, eq'  
856
857 exception NotMetaConvertible;;
858
859 let meta_convertibility_aux table t1 t2 =
860   let module C = Cic in
861   let rec aux ((table_l,table_r) as table) t1 t2 =
862     match t1, t2 with
863     | C.Meta (m1, tl1), C.Meta (m2, tl2) when m1 = m2 -> table
864     | C.Meta (m1, tl1), C.Meta (m2, tl2) when m1 < m2 -> aux table t2 t1
865     | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
866         let m1_binding, table_l =
867           try List.assoc m1 table_l, table_l
868           with Not_found -> m2, (m1, m2)::table_l
869         and m2_binding, table_r =
870           try List.assoc m2 table_r, table_r
871           with Not_found -> m1, (m2, m1)::table_r
872         in
873         if (m1_binding <> m2) || (m2_binding <> m1) then
874           raise NotMetaConvertible
875         else table_l,table_r
876     | C.Var (u1, ens1), C.Var (u2, ens2)
877     | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
878         aux_ens table ens1 ens2
879     | C.Cast (s1, t1), C.Cast (s2, t2)
880     | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
881     | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
882     | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
883         let table = aux table s1 s2 in
884         aux table t1 t2
885     | C.Appl l1, C.Appl l2 -> (
886         try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
887         with Invalid_argument _ -> raise NotMetaConvertible
888       )
889     | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
890         when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
891     | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
892         when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
893         aux_ens table ens1 ens2
894     | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
895         when (UriManager.eq u1 u2) && i1 = i2 ->
896         let table = aux table s1 s2 in
897         let table = aux table t1 t2 in (
898           try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
899           with Invalid_argument _ -> raise NotMetaConvertible
900         )
901     | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
902         try
903           List.fold_left2
904             (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
905                if i1 <> i2 then raise NotMetaConvertible
906                else
907                  let res = (aux res s1 s2) in aux res t1 t2)
908             table il1 il2
909         with Invalid_argument _ -> raise NotMetaConvertible
910       )
911     | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
912         try
913           List.fold_left2
914             (fun res (n1, s1, t1) (n2, s2, t2) ->
915                let res = aux res s1 s2 in aux res t1 t2)
916             table il1 il2
917         with Invalid_argument _ -> raise NotMetaConvertible
918       )
919     | t1, t2 when t1 = t2 -> table
920     | _, _ -> raise NotMetaConvertible
921         
922   and aux_ens table ens1 ens2 =
923     let cmp (u1, t1) (u2, t2) =
924       Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
925     in
926     let ens1 = List.sort cmp ens1
927     and ens2 = List.sort cmp ens2 in
928     try
929       List.fold_left2
930         (fun res (u1, t1) (u2, t2) ->
931            if not (UriManager.eq u1 u2) then raise NotMetaConvertible
932            else aux res t1 t2)
933         table ens1 ens2
934     with Invalid_argument _ -> raise NotMetaConvertible
935   in
936   aux table t1 t2
937 ;;
938
939
940 let meta_convertibility_eq eq1 eq2 =
941   let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
942   let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
943   if ty <> ty' then
944     false
945   else if (left = left') && (right = right') then
946     true
947   else if (left = right') && (right = left') then
948     true
949   else
950     try
951       let table = meta_convertibility_aux ([],[]) left left' in
952       let _ = meta_convertibility_aux table right right' in
953       true
954     with NotMetaConvertible ->
955       try
956         let table = meta_convertibility_aux ([],[]) left right' in
957         let _ = meta_convertibility_aux table right left' in
958         true
959       with NotMetaConvertible ->
960         false
961 ;;
962
963
964 let meta_convertibility t1 t2 =
965   if t1 = t2 then
966     true
967   else
968     try
969       ignore(meta_convertibility_aux ([],[]) t1 t2);
970       true
971     with NotMetaConvertible ->
972       false
973 ;;
974
975 exception TermIsNotAnEquality;;
976
977 let term_is_equality term =
978   match term with
979   | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] 
980     when LibraryObjects.is_eq_URI uri -> true
981   | _ -> false
982 ;;
983
984 let equality_of_term bag proof term =
985   match term with
986   | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] 
987     when LibraryObjects.is_eq_URI uri ->
988       let o = !Utils.compare_terms t1 t2 in
989       let stat = (ty,t1,t2,o) in
990       let w = Utils.compute_equality_weight stat in
991       let e = mk_equality bag (w, Exact proof, stat,[]) in
992       e
993   | _ ->
994       raise TermIsNotAnEquality
995 ;;
996
997 let is_weak_identity eq = 
998   let _,_,(_,left, right,_),_,_ = open_equality eq in
999    left = right 
1000    (* doing metaconv here is meaningless *)
1001 ;;
1002
1003 let is_identity (_, context, ugraph) eq = 
1004   let _,_,(ty,left,right,_),menv,_ = open_equality eq in
1005   (* doing metaconv here is meaningless *)
1006   left = right
1007 (*   fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
1008  *   *)
1009 ;;
1010
1011
1012 let term_of_equality eq_uri equality =
1013   let _, _, (ty, left, right, _), menv, _= open_equality equality in
1014   let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1015   let argsno = List.length menv in
1016   let t =
1017     CicSubstitution.lift argsno
1018       (Cic.Appl [Cic.MutInd (eq_uri, 0, []); ty; left; right])
1019   in
1020   snd (
1021     List.fold_right
1022       (fun (i,_,ty) (n, t) ->
1023          let name = Cic.Name ("X" ^ (string_of_int n)) in
1024          let ty = CicSubstitution.lift (n-1) ty in
1025          let t = 
1026            ProofEngineReduction.replace
1027              ~equality:eq ~what:[i]
1028              ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1029          in
1030            (n-1, Cic.Prod (name, ty, t)))
1031       menv (argsno, t))
1032 ;;
1033
1034 let symmetric bag eq_ty l id uri m =
1035   let eq = Cic.MutInd(uri,0,[]) in
1036   let pred = 
1037     Cic.Lambda (Cic.Name "Sym",eq_ty,
1038      Cic.Appl [CicSubstitution.lift 1 eq ;
1039                CicSubstitution.lift 1 eq_ty;
1040                Cic.Rel 1;CicSubstitution.lift 1 l]) 
1041   in
1042   let prefl = 
1043     Exact (Cic.Appl
1044       [Cic.MutConstruct(uri,0,1,[]);eq_ty;l]) 
1045   in
1046   let id1 = 
1047     let eq = mk_equality bag (0,prefl,(eq_ty,l,l,Utils.Eq),m) in
1048     let (_,_,_,_,id) = open_equality eq in
1049     id
1050   in
1051   Step(Subst.empty_subst,
1052     (Demodulation,id1,(Utils.Left,id),pred))
1053 ;;
1054
1055 module IntOT = struct
1056   type t = int
1057   let compare = Pervasives.compare
1058 end
1059
1060 module IntSet = Set.Make(IntOT);;
1061
1062 let n_purged = ref 0;;
1063
1064 let collect ((id_to_eq,_) as bag) alive1 alive2 alive3 =
1065 (*   let _ = <:start<collect>> in *)
1066   let deps_of id = 
1067     let p,_,_ = proof_of_id bag id in  
1068     match p with
1069     | Exact _ -> IntSet.empty
1070     | Step (_,(_,id1,(_,id2),_)) ->
1071           IntSet.add id1 (IntSet.add id2 IntSet.empty)
1072   in
1073   let rec close s = 
1074     let news = IntSet.fold (fun id s -> IntSet.union (deps_of id) s) s s in
1075     if IntSet.equal news s then s else close news
1076   in
1077   let l_to_s s l = List.fold_left (fun s x -> IntSet.add x s) s l in
1078   let alive_set = l_to_s (l_to_s (l_to_s IntSet.empty alive2) alive1) alive3 in
1079   let closed_alive_set = close alive_set in
1080   let to_purge = 
1081     Hashtbl.fold 
1082       (fun k _ s -> 
1083         if not (IntSet.mem k closed_alive_set) then
1084           k::s else s) id_to_eq []
1085   in
1086   n_purged := !n_purged + List.length to_purge;
1087   List.iter (Hashtbl.remove id_to_eq) to_purge;
1088 (*   let _ = <:stop<collect>> in ()   *)
1089 ;;
1090
1091 let id_of e = 
1092   let _,_,_,_,id = open_equality e in id
1093 ;;
1094
1095 let get_stats () = "" 
1096 (*
1097   <:show<Equality.>> ^ 
1098   "# of purged eq by the collector: " ^ string_of_int !n_purged ^ "\n"
1099 *)
1100 ;;
1101
1102 let rec pp_proofterm name t context = 
1103   let rec skip_lambda tys ctx = function
1104     | Cic.Lambda (n,s,t) -> skip_lambda (s::tys) ((Some n)::ctx) t
1105     | t -> ctx,tys,t
1106   in
1107   let rename s name = 
1108     match name with 
1109     | Cic.Name s1 -> Cic.Name (s ^ s1)
1110     | _ -> assert false
1111   in
1112   let rec skip_letin ctx = function
1113     | Cic.LetIn (n,b,t) -> 
1114         pp_proofterm (Some (rename "Lemma " n)) b ctx:: 
1115           skip_letin ((Some n)::ctx) t
1116     | t -> 
1117         let ppterm t = CicPp.pp t ctx in
1118         let rec pp inner = function
1119           | Cic.Appl [Cic.Const (uri,[]);_;l;m;r;p1;p2] 
1120               when Pcre.pmatch ~pat:"trans_eq" (UriManager.string_of_uri uri)->
1121                 if not inner then
1122                   ("     " ^ ppterm l) :: pp true p1 @ 
1123                             [ "   = " ^ ppterm m ] @ pp true p2 @ 
1124                             [ "   = " ^ ppterm r ]
1125                 else
1126                    pp true p1 @ 
1127                             [ "   = " ^ ppterm m ] @ pp true p2 
1128           | Cic.Appl [Cic.Const (uri,[]);_;l;m;p] 
1129               when Pcre.pmatch ~pat:"sym_eq" (UriManager.string_of_uri uri)->
1130                 pp true p
1131           | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p] 
1132               when Pcre.pmatch ~pat:"eq_f" (UriManager.string_of_uri uri)->
1133                 pp true p
1134           | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p] 
1135               when Pcre.pmatch ~pat:"eq_f1" (UriManager.string_of_uri uri)->
1136                 pp true p
1137           | Cic.Appl [Cic.MutConstruct (uri,_,_,[]);_;_;t;p] 
1138               when Pcre.pmatch ~pat:"ex.ind" (UriManager.string_of_uri uri)->
1139                       [ "witness " ^ ppterm t ] @ pp true p
1140           | Cic.Appl (t::_) ->[ " [by " ^ ppterm t ^ "]"]
1141           | t ->[ " [by " ^ ppterm t ^ "]"]
1142         in
1143         let rec compat = function
1144           | a::b::tl -> (b ^ a) :: compat tl
1145           | h::[] -> [h]
1146           | [] -> []
1147         in
1148         let compat l = List.hd l :: compat (List.tl l) in
1149         compat (pp false t) @ ["";""]
1150   in      
1151   let names, tys, body = skip_lambda [] context t in
1152   let ppname name = (match name with Some (Cic.Name s) -> s | _ -> "") in
1153   ppname name ^ ":\n" ^
1154   (if context = [] then
1155      let rec pp_l ctx = function
1156           | (t,name)::tl -> 
1157               "   " ^ ppname name ^ ": " ^ CicPp.pp t ctx ^ "\n" ^ 
1158               pp_l (name::ctx) tl
1159           | [] -> "\n\n"
1160      in
1161        pp_l [] (List.rev (List.combine tys names))
1162    else "")
1163     ^
1164   String.concat "\n" (skip_letin names body)
1165 ;;
1166
1167 let pp_proofterm t = 
1168   "\n\n" ^ 
1169   pp_proofterm (Some (Cic.Name "Hypothesis")) t []
1170 ;;
1171