]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/equality.ml
- new given_clause
[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 (* $Id: inference.ml 6245 2006-04-05 12:07:51Z tassi $ *)
27
28 type rule = SuperpositionRight | SuperpositionLeft | Demodulation
29 type uncomparable = int -> int 
30 type equality =
31     uncomparable *       (* trick to break structural equality *)
32     int  *               (* weight *)
33     proof * 
34     (Cic.term *          (* type *)
35      Cic.term *          (* left side *)
36      Cic.term *          (* right side *)
37      Utils.comparison) * (* ordering *)  
38     Cic.metasenv  *      (* environment for metas *)
39     int                  (* id *)
40 and proof = 
41   | Exact of Cic.term
42   | Step of Subst.substitution * (rule * int*(Utils.pos*int)* Cic.term) 
43             (* subst, (rule,eq1, eq2,predicate) *)  
44 and goal_proof = (Utils.pos * int * Subst.substitution * Cic.term) list
45 ;;
46
47 (* globals *)
48 let maxid = ref 0;;
49 let id_to_eq = Hashtbl.create 1024;;
50
51 let freshid () =
52   incr maxid; !maxid
53 ;;
54
55 let reset () = 
56   maxid := 0;
57   Hashtbl.clear  id_to_eq
58 ;;
59
60 let uncomparable = fun _ -> 0
61
62 let mk_equality (weight,p,(ty,l,r,o),m) =
63   let id = freshid () in
64   let eq = (uncomparable,weight,p,(ty,l,r,o),m,id) in
65   Hashtbl.add id_to_eq id eq;
66   eq
67 ;;
68
69 let mk_tmp_equality (weight,(ty,l,r,o),m) =
70   let id = -1 in
71   uncomparable,weight,Exact (Cic.Implicit None),(ty,l,r,o),m,id
72 ;;
73
74
75 let open_equality (_,weight,proof,(ty,l,r,o),m,id) = 
76   (weight,proof,(ty,l,r,o),m,id)
77
78 let string_of_equality ?env eq =
79   match env with
80   | None ->
81       let w, _, (ty, left, right, o), _ , id = open_equality eq in
82       Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s" 
83               id w (CicPp.ppterm ty)
84               (CicPp.ppterm left) 
85               (Utils.string_of_comparison o) (CicPp.ppterm right)
86   | Some (_, context, _) -> 
87       let names = Utils.names_of_context context in
88       let w, _, (ty, left, right, o), _ , id = open_equality eq in
89       Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s" 
90               id w (CicPp.pp ty names)
91               (CicPp.pp left names) (Utils.string_of_comparison o)
92               (CicPp.pp right names)
93 ;;
94
95 let compare (_,_,_,s1,_,_) (_,_,_,s2,_,_) =
96   Pervasives.compare s1 s2
97 ;;
98
99 let proof_of_id id =
100   try
101     let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
102       p,l,r
103   with
104       Not_found -> assert false
105
106
107 let string_of_proof ?(names=[]) p gp = 
108   let str_of_rule = function
109     | SuperpositionRight -> "SupR"
110     | SuperpositionLeft -> "SupL"
111     | Demodulation -> "Demod"
112   in
113   let str_of_pos = function
114     | Utils.Left -> "left"
115     | Utils.Right -> "right"
116   in
117   let fst3 (x,_,_) = x in
118   let rec aux margin name = 
119     let prefix = String.make margin ' ' ^ name ^ ": " in function 
120     | Exact t -> 
121         Printf.sprintf "%sExact (%s)\n" 
122           prefix (CicPp.pp t names)
123     | Step (subst,(rule,eq1,(pos,eq2),pred)) -> 
124         Printf.sprintf "%s%s(%s|%d with %d dir %s pred %s))\n"
125           prefix (str_of_rule rule) (Subst.ppsubst ~names subst) eq1 eq2 (str_of_pos pos) 
126           (CicPp.pp pred names)^ 
127         aux (margin+1) (Printf.sprintf "%d" eq1) (fst3 (proof_of_id eq1)) ^ 
128         aux (margin+1) (Printf.sprintf "%d" eq2) (fst3 (proof_of_id eq2)) 
129   in
130   aux 0 "" p ^ 
131   String.concat "\n" 
132     (List.map 
133       (fun (pos,i,s,t) -> 
134         (Printf.sprintf 
135           "GOAL: %s %d %s %s\n" 
136             (str_of_pos pos) i (Subst.ppsubst ~names s) (CicPp.pp t names)) ^ 
137         aux 1 (Printf.sprintf "%d " i) (fst3 (proof_of_id i)))
138       gp)
139 ;;
140
141 let rec depend eq id =
142   let (_,p,(_,_,_,_),_,ideq) = open_equality eq in
143   if id = ideq then true else  
144   match p with
145       Exact _ -> false
146     | Step (_,(_,id1,(_,id2),_)) ->
147         let eq1 = Hashtbl.find id_to_eq id1 in
148         let eq2 = Hashtbl.find id_to_eq id2 in  
149         depend eq1 id || depend eq2 id
150 ;;
151
152 let ppsubst = Subst.ppsubst ~names:[];;
153
154 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
155 let build_ens uri termlist =
156   let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
157   match obj with
158   | Cic.Constant (_, _, _, uris, _) ->
159       assert (List.length uris <= List.length termlist);
160       let rec aux = function
161         | [], tl -> [], tl
162         | (uri::uris), (term::tl) ->
163             let ens, args = aux (uris, tl) in
164             (uri, term)::ens, args
165         | _, _ -> assert false
166       in
167       aux (uris, termlist)
168   | _ -> assert false
169 ;;
170
171 let mk_sym uri ty t1 t2 p =
172   let ens, args =  build_ens uri [ty;t1;t2;p] in
173     Cic.Appl (Cic.Const(uri, ens) :: args)
174 ;;
175
176 let mk_trans uri ty t1 t2 t3 p12 p23 =
177   let ens, args = build_ens uri [ty;t1;t2;t3;p12;p23] in
178     Cic.Appl (Cic.Const (uri, ens) :: args)
179 ;;
180
181 let mk_eq_ind uri ty what pred p1 other p2 =
182  Cic.Appl [Cic.Const (uri, []); ty; what; pred; p1; other; p2]
183 ;;
184
185 let p_of_sym ens tl =
186   let args = List.map snd ens @ tl in
187   match args with 
188     | [_;_;_;p] -> p 
189     | _ -> assert false 
190 ;;
191
192 let open_trans ens tl =
193   let args = List.map snd ens @ tl in
194   match args with 
195     | [ty;l;m;r;p1;p2] -> ty,l,m,r,p1,p2
196     | _ -> assert false   
197 ;;
198
199 let open_eq_ind args =
200   match args with 
201   | [ty;l;pred;pl;r;pleqr] -> ty,l,pred,pl,r,pleqr
202   | _ -> assert false   
203 ;;
204
205 let open_pred pred =
206   match pred with 
207   | Cic.Lambda (_,ty,(Cic.Appl [Cic.MutInd (uri, 0,_);_;l;r])) 
208      when LibraryObjects.is_eq_URI uri -> ty,uri,l,r
209   | _ -> prerr_endline (CicPp.ppterm pred); assert false   
210 ;;
211
212 let is_not_fixed t =
213    CicSubstitution.subst (Cic.Implicit None) t <>
214    CicSubstitution.subst (Cic.Rel 1) t
215 ;;
216
217
218 let canonical t = 
219   let rec remove_refl t =
220     match t with
221     | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
222           when LibraryObjects.is_trans_eq_URI uri_trans ->
223           let ty,l,m,r,p1,p2 = open_trans ens tl in
224             (match p1,p2 with
225               | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 -> 
226                   remove_refl p2
227               | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] -> 
228                   remove_refl p1
229               | _ -> Cic.Appl (List.map remove_refl args))
230     | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
231     | _ -> t
232   in
233   let rec canonical t =
234     match t with
235       | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
236           when LibraryObjects.is_sym_eq_URI uri_sym ->
237           (match p_of_sym ens tl with
238              | Cic.Appl ((Cic.Const(uri,ens))::tl)
239                  when LibraryObjects.is_sym_eq_URI uri -> 
240                    canonical (p_of_sym ens tl)
241              | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
242                  when LibraryObjects.is_trans_eq_URI uri_trans ->
243                  let ty,l,m,r,p1,p2 = open_trans ens tl in
244                    mk_trans uri_trans ty r m l 
245                      (canonical (mk_sym uri_sym ty m r p2)) 
246                      (canonical (mk_sym uri_sym ty l m p1))
247              | Cic.Appl (((Cic.Const(uri_ind,ens)) as he)::tl) 
248                  when LibraryObjects.is_eq_ind_URI uri_ind || 
249                       LibraryObjects.is_eq_ind_r_URI uri_ind ->
250                  let ty, what, pred, p1, other, p2 =
251                    match tl with
252                    | [ty;what;pred;p1;other;p2] -> ty, what, pred, p1, other, p2
253                    | _ -> assert false
254                  in
255                  let pred,l,r = 
256                    match pred with
257                    | Cic.Lambda (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;l;r])
258                        when LibraryObjects.is_eq_URI uri ->
259                          Cic.Lambda 
260                            (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;r;l]),l,r
261                    | _ -> 
262                        prerr_endline (CicPp.ppterm pred);
263                        assert false
264                  in
265                  let l = CicSubstitution.subst what l in
266                  let r = CicSubstitution.subst what r in
267                  Cic.Appl 
268                    [he;ty;what;pred;
269                     canonical (mk_sym uri_sym ty l r p1);other;canonical p2]
270              | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
271                  when LibraryObjects.is_eq_URI uri -> t
272              | _ -> Cic.Appl (List.map canonical args))
273       | Cic.Appl l -> Cic.Appl (List.map canonical l)
274       | _ -> t
275   in
276   remove_refl (canonical t)
277 ;;
278   
279 let ty_of_lambda = function
280   | Cic.Lambda (_,ty,_) -> ty
281   | _ -> assert false 
282 ;;
283
284 let compose_contexts ctx1 ctx2 = 
285   ProofEngineReduction.replace_lifting 
286     ~equality:(=) ~what:[Cic.Rel 1] ~with_what:[ctx2] ~where:ctx1
287 ;;
288
289 let put_in_ctx ctx t = 
290   ProofEngineReduction.replace_lifting
291     ~equality:(=) ~what:[Cic.Rel 1] ~with_what:[t] ~where:ctx
292 ;;
293
294 let mk_eq uri ty l r =
295   Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r]
296 ;;
297
298 let mk_refl uri ty t = 
299   Cic.Appl [Cic.MutConstruct(uri,0,1,[]);ty;t]
300 ;;
301
302 let open_eq = function 
303   | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
304       uri, ty, l ,r
305   | _ -> assert false
306 ;;
307
308 let contextualize uri ty left right t = 
309   (* aux [uri] [ty] [left] [right] [ctx] [t] 
310    * 
311    * the parameters validate this invariant  
312    *   t: eq(uri) ty left right
313    * that is used only by the base case
314    *
315    * ctx is a term with an open (Rel 1). (Rel 1) is the empty context
316    *)
317     let rec aux uri ty left right ctx_d = function
318       | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
319         when LibraryObjects.is_eq_ind_URI uri_ind || 
320              LibraryObjects.is_eq_ind_r_URI uri_ind ->
321           let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
322           let ty2,eq,lp,rp = open_pred pred in 
323           let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
324           let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
325           let is_not_fixed_lp = is_not_fixed lp in
326           let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
327           (* extract the context and the fixed term from the predicate *)
328           let m, ctx_c = 
329             let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
330             (* they were under a lambda *)
331             let m =  CicSubstitution.subst (Cic.Implicit None) m in
332             let ctx_c = CicSubstitution.subst (Cic.Rel 1) ctx_c in
333             m, ctx_c          
334           in
335           (* create the compound context and put the terms under it *)
336           let ctx_dc = compose_contexts ctx_d ctx_c in
337           let dc_what = put_in_ctx ctx_dc what in
338           let dc_other = put_in_ctx ctx_dc other in
339           (* m is already in ctx_c so it is put in ctx_d only *)
340           let d_m = put_in_ctx ctx_d m in
341           (* we also need what in ctx_c *)
342           let c_what = put_in_ctx ctx_c what in
343           (* now put the proofs in the compound context *)
344           let p1 = (* p1: dc_what = d_m *)
345             if is_not_fixed_lp then 
346               aux uri ty1 c_what m ctx_d p1 
347             else
348               mk_sym uri_sym ty d_m dc_what
349                 (aux uri ty1 m c_what ctx_d p1)
350           in
351           let p2 = (* p2: dc_other = dc_what *)
352             if avoid_eq_ind then
353               mk_sym uri_sym ty dc_what dc_other
354                 (aux uri ty1 what other ctx_dc p2)
355             else
356               aux uri ty1 other what ctx_dc p2
357           in
358           (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
359              if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
360           let a,b,c,paeqb,pbeqc =
361             if is_not_fixed_lp then
362               dc_other,dc_what,d_m,p2,p1
363             else
364               d_m,dc_what,dc_other,
365                 (mk_sym uri_sym ty dc_what d_m p1),
366                 (mk_sym uri_sym ty dc_other dc_what p2)
367           in
368           mk_trans uri_trans ty a b c paeqb pbeqc
369     | t -> 
370         let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
371         let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in
372         let pred = 
373           (* ctx_d will go under a lambda, but put_in_ctx substitutes Rel 1 *)
374           let ctx_d = CicSubstitution.lift_from 2 1 ctx_d in (* bleah *)
375           let r = put_in_ctx ctx_d (CicSubstitution.lift 1 left) in
376           let l = ctx_d in
377           let lty = CicSubstitution.lift 1 ty in 
378           Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r))
379         in
380         let d_left = put_in_ctx ctx_d left in
381         let d_right = put_in_ctx ctx_d right in
382         let refl_eq = mk_refl uri ty d_left in
383         mk_sym uri_sym ty d_right d_left
384           (mk_eq_ind uri_ind ty left pred refl_eq right t)
385   in
386   let empty_context = Cic.Rel 1 in
387   aux uri ty left right empty_context t
388 ;;
389
390 let contextualize_rewrites t ty = 
391   let eq,ty,l,r = open_eq ty in
392   contextualize eq ty l r t
393 ;;
394   
395 let build_proof_step subst p1 p2 pos l r pred =
396   let p1 = Subst.apply_subst subst p1 in
397   let p2 = Subst.apply_subst subst p2 in
398   let l  = Subst.apply_subst subst l in
399   let r  = Subst.apply_subst subst r in
400   let pred = Subst.apply_subst subst pred in
401   let ty,body = 
402     match pred with
403       | Cic.Lambda (_,ty,body) -> ty,body 
404       | _ -> assert false
405   in
406   let what, other = 
407     if pos = Utils.Left then l,r else r,l
408   in
409     match pos with
410       | Utils.Left ->
411         mk_eq_ind (Utils.eq_ind_URI ()) ty what pred p1 other p2
412       | Utils.Right ->
413         mk_eq_ind (Utils.eq_ind_r_URI ()) ty what pred p1 other p2
414 ;;
415
416 let build_proof_term proof =
417   let rec aux = function
418      | Exact term -> term
419      | Step (subst,(_, id1, (pos,id2), pred)) ->
420          let p,_,_ = proof_of_id id1 in
421          let p1 = aux p in
422          let p,l,r = proof_of_id id2 in
423          let p2 = aux p in
424            build_proof_step subst p1 p2 pos l r pred
425   in
426    aux proof
427 ;;
428
429 let wfo goalproof proof =
430   let rec aux acc id =
431     let p,_,_ = proof_of_id id in
432     match p with
433     | Exact _ -> if (List.mem id acc) then acc else id :: acc
434     | Step (_,(_,id1, (_,id2), _)) -> 
435         let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
436         let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
437         id :: acc
438   in
439   let acc = 
440     match proof with
441       | Exact _ -> []
442       | Step (_,(_,id1, (_,id2), _)) -> aux (aux [] id1) id2
443   in 
444   List.fold_left (fun acc (_,id,_,_) -> aux acc id) acc goalproof
445 ;;
446
447 let string_of_id names id = 
448   try
449     let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
450     match p with
451     | Exact t -> 
452         Printf.sprintf "%d = %s: %s = %s" id
453           (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
454     | Step (_,(step,id1, (_,id2), _) ) ->
455         Printf.sprintf "%6d: %s %6d %6d   %s = %s" id
456           (if step = SuperpositionRight then "SupR" else "Demo") 
457           id1 id2 (CicPp.pp l names) (CicPp.pp r names)
458   with
459       Not_found -> assert false
460
461 let pp_proof names goalproof proof =
462   String.concat "\n" (List.map (string_of_id names) (wfo goalproof proof)) ^ 
463   "\ngoal is demodulated with " ^ 
464     (String.concat " " 
465       ((List.map (fun (_,i,_,_) -> string_of_int i) goalproof)))
466 ;;
467
468 let build_goal_proof l initial ty =
469   let proof = 
470    List.fold_left 
471    (fun  current_proof (pos,id,subst,pred) -> 
472       let p,l,r = proof_of_id id in
473       let p = build_proof_term p in
474       let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
475         build_proof_step subst current_proof p pos l r pred)
476    initial l
477   in
478   proof 
479   (*canonical (contextualize_rewrites proof ty)*)
480 ;;
481
482 let refl_proof ty term = 
483   Cic.Appl 
484     [Cic.MutConstruct 
485        (LibraryObjects.eq_URI (), 0, 1, []);
486        ty; term]
487 ;;
488
489 let metas_of_proof p = 
490   Utils.metas_of_term (build_proof_term p)
491 ;;
492
493 let relocate newmeta menv =
494   let subst, metasenv, newmeta = 
495     List.fold_right 
496       (fun (i, context, ty) (subst, menv, maxmeta) ->         
497         let irl = [] (*
498          CicMkImplicit.identity_relocation_list_for_metavariable context *)
499         in
500         let newsubst = Subst.buildsubst i context (Cic.Meta(maxmeta,irl)) ty subst in
501         let newmeta = maxmeta, context, ty in
502         newsubst, newmeta::menv, maxmeta+1) 
503       menv (Subst.empty_subst, [], newmeta+1)
504   in
505   let metasenv = Subst.apply_subst_metasenv subst metasenv in
506   let subst = Subst.flatten_subst subst in
507   subst, metasenv, newmeta
508
509
510 let fix_metas newmeta eq = 
511   let w, p, (ty, left, right, o), menv,_ = open_equality eq in
512   (* debug 
513   let _ , eq = 
514     fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
515   prerr_endline (string_of_equality eq); *)
516   let subst, metasenv, newmeta = relocate newmeta menv in
517   let ty = Subst.apply_subst subst ty in
518   let left = Subst.apply_subst subst left in
519   let right = Subst.apply_subst subst right in
520   let fix_proof = function
521     | Exact p -> Exact (Subst.apply_subst subst p)
522     | Step (s,(r,id1,(pos,id2),pred)) -> 
523         Step (Subst.concat_substs s subst,(r,id1,(pos,id2), pred))
524   in
525   let p = fix_proof p in
526   let eq = mk_equality (w, p, (ty, left, right, o), metasenv) in
527   (* debug prerr_endline (string_of_equality eq); *)
528   newmeta+1, eq  
529
530 exception NotMetaConvertible;;
531
532 let meta_convertibility_aux table t1 t2 =
533   let module C = Cic in
534   let rec aux ((table_l, table_r) as table) t1 t2 =
535     match t1, t2 with
536     | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
537         let m1_binding, table_l =
538           try List.assoc m1 table_l, table_l
539           with Not_found -> m2, (m1, m2)::table_l
540         and m2_binding, table_r =
541           try List.assoc m2 table_r, table_r
542           with Not_found -> m1, (m2, m1)::table_r
543         in
544         if (m1_binding <> m2) || (m2_binding <> m1) then
545           raise NotMetaConvertible
546         else (
547           try
548             List.fold_left2
549               (fun res t1 t2 ->
550                  match t1, t2 with
551                  | None, Some _ | Some _, None -> raise NotMetaConvertible
552                  | None, None -> res
553                  | Some t1, Some t2 -> (aux res t1 t2))
554               (table_l, table_r) tl1 tl2
555           with Invalid_argument _ ->
556             raise NotMetaConvertible
557         )
558     | C.Var (u1, ens1), C.Var (u2, ens2)
559     | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
560         aux_ens table ens1 ens2
561     | C.Cast (s1, t1), C.Cast (s2, t2)
562     | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
563     | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
564     | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
565         let table = aux table s1 s2 in
566         aux table t1 t2
567     | C.Appl l1, C.Appl l2 -> (
568         try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
569         with Invalid_argument _ -> raise NotMetaConvertible
570       )
571     | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
572         when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
573     | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
574         when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
575         aux_ens table ens1 ens2
576     | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
577         when (UriManager.eq u1 u2) && i1 = i2 ->
578         let table = aux table s1 s2 in
579         let table = aux table t1 t2 in (
580           try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
581           with Invalid_argument _ -> raise NotMetaConvertible
582         )
583     | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
584         try
585           List.fold_left2
586             (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
587                if i1 <> i2 then raise NotMetaConvertible
588                else
589                  let res = (aux res s1 s2) in aux res t1 t2)
590             table il1 il2
591         with Invalid_argument _ -> raise NotMetaConvertible
592       )
593     | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
594         try
595           List.fold_left2
596             (fun res (n1, s1, t1) (n2, s2, t2) ->
597                let res = aux res s1 s2 in aux res t1 t2)
598             table il1 il2
599         with Invalid_argument _ -> raise NotMetaConvertible
600       )
601     | t1, t2 when t1 = t2 -> table
602     | _, _ -> raise NotMetaConvertible
603         
604   and aux_ens table ens1 ens2 =
605     let cmp (u1, t1) (u2, t2) =
606       Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
607     in
608     let ens1 = List.sort cmp ens1
609     and ens2 = List.sort cmp ens2 in
610     try
611       List.fold_left2
612         (fun res (u1, t1) (u2, t2) ->
613            if not (UriManager.eq u1 u2) then raise NotMetaConvertible
614            else aux res t1 t2)
615         table ens1 ens2
616     with Invalid_argument _ -> raise NotMetaConvertible
617   in
618   aux table t1 t2
619 ;;
620
621
622 let meta_convertibility_eq eq1 eq2 =
623   let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
624   let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
625   if ty <> ty' then
626     false
627   else if (left = left') && (right = right') then
628     true
629   else if (left = right') && (right = left') then
630     true
631   else
632     try
633       let table = meta_convertibility_aux ([], []) left left' in
634       let _ = meta_convertibility_aux table right right' in
635       true
636     with NotMetaConvertible ->
637       try
638         let table = meta_convertibility_aux ([], []) left right' in
639         let _ = meta_convertibility_aux table right left' in
640         true
641       with NotMetaConvertible ->
642         false
643 ;;
644
645
646 let meta_convertibility t1 t2 =
647   if t1 = t2 then
648     true
649   else
650     try
651       ignore(meta_convertibility_aux ([], []) t1 t2);
652       true
653     with NotMetaConvertible ->
654       false
655 ;;
656
657 exception TermIsNotAnEquality;;
658
659 let term_is_equality term =
660   let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
661   match term with
662   | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
663   | _ -> false
664 ;;
665
666 let equality_of_term proof term =
667   let eq_uri = LibraryObjects.eq_URI () in
668   let iseq uri = UriManager.eq uri eq_uri in
669   match term with
670   | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
671       let o = !Utils.compare_terms t1 t2 in
672       let stat = (ty,t1,t2,o) in
673       let w = Utils.compute_equality_weight stat in
674       let e = mk_equality (w, Exact proof, stat,[]) in
675       e
676   | _ ->
677       raise TermIsNotAnEquality
678 ;;
679
680 let is_weak_identity eq = 
681   let _,_,(_,left, right,_),_,_ = open_equality eq in
682   left = right || meta_convertibility left right 
683 ;;
684
685 let is_identity (_, context, ugraph) eq = 
686   let _,_,(ty,left,right,_),menv,_ = open_equality eq in
687   left = right ||
688   (* (meta_convertibility left right)) *)
689   fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
690 ;;
691
692
693 let term_of_equality equality =
694   let _, _, (ty, left, right, _), menv, _= open_equality equality in
695   let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
696   let argsno = List.length menv in
697   let t =
698     CicSubstitution.lift argsno
699       (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
700   in
701   snd (
702     List.fold_right
703       (fun (i,_,ty) (n, t) ->
704          let name = Cic.Name ("X" ^ (string_of_int n)) in
705          let ty = CicSubstitution.lift (n-1) ty in
706          let t = 
707            ProofEngineReduction.replace
708              ~equality:eq ~what:[i]
709              ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
710          in
711            (n-1, Cic.Prod (name, ty, t)))
712       menv (argsno, t))
713 ;;
714