1 (* cOpyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* $Id: inference.ml 6245 2006-04-05 12:07:51Z tassi $ *)
28 type rule = SuperpositionRight | SuperpositionLeft | Demodulation
29 type uncomparable = int -> int
31 uncomparable * (* trick to break structural equality *)
34 (Cic.term * (* type *)
35 Cic.term * (* left side *)
36 Cic.term * (* right side *)
37 Utils.comparison) * (* ordering *)
38 Cic.metasenv * (* environment for metas *)
42 | Step of Subst.substitution * (rule * int*(Utils.pos*int)* Cic.term)
43 (* subst, (rule,eq1, eq2,predicate) *)
44 and goal_proof = (rule * Utils.pos * int * Subst.substitution * Cic.term) list
49 let id_to_eq = Hashtbl.create 1024;;
57 Hashtbl.clear id_to_eq
60 let uncomparable = fun _ -> 0
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;
69 let mk_tmp_equality (weight,(ty,l,r,o),m) =
71 uncomparable,weight,Exact (Cic.Implicit None),(ty,l,r,o),m,id
75 let open_equality (_,weight,proof,(ty,l,r,o),m,id) =
76 (weight,proof,(ty,l,r,o),m,id)
78 let string_of_rule = function
79 | SuperpositionRight -> "SupR"
80 | SuperpositionLeft -> "SupL"
81 | Demodulation -> "Demod"
84 let string_of_equality ?env eq =
87 let w, _, (ty, left, right, o), m , id = open_equality eq in
88 Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]"
89 id w (CicPp.ppterm ty)
91 (Utils.string_of_comparison o) (CicPp.ppterm right)
92 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
93 | Some (_, context, _) ->
94 let names = Utils.names_of_context context in
95 let w, _, (ty, left, right, o), m , id = open_equality eq in
96 Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]"
97 id w (CicPp.pp ty names)
98 (CicPp.pp left names) (Utils.string_of_comparison o)
99 (CicPp.pp right names)
100 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
103 let compare (_,_,_,s1,_,_) (_,_,_,s2,_,_) =
104 Pervasives.compare s1 s2
109 let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
112 Not_found -> assert false
115 let string_of_proof ?(names=[]) p gp =
116 let str_of_pos = function
117 | Utils.Left -> "left"
118 | Utils.Right -> "right"
120 let fst3 (x,_,_) = x in
121 let rec aux margin name =
122 let prefix = String.make margin ' ' ^ name ^ ": " in function
124 Printf.sprintf "%sExact (%s)\n"
125 prefix (CicPp.pp t names)
126 | Step (subst,(rule,eq1,(pos,eq2),pred)) ->
127 Printf.sprintf "%s%s(%s|%d with %d dir %s pred %s))\n"
128 prefix (string_of_rule rule) (Subst.ppsubst ~names subst) eq1 eq2 (str_of_pos pos)
129 (CicPp.pp pred names)^
130 aux (margin+1) (Printf.sprintf "%d" eq1) (fst3 (proof_of_id eq1)) ^
131 aux (margin+1) (Printf.sprintf "%d" eq2) (fst3 (proof_of_id eq2))
136 (fun (r,pos,i,s,t) ->
138 "GOAL: %s %s %d %s %s\n" (string_of_rule r)
139 (str_of_pos pos) i (Subst.ppsubst ~names s) (CicPp.pp t names)) ^
140 aux 1 (Printf.sprintf "%d " i) (fst3 (proof_of_id i)))
144 let rec depend eq id seen =
145 let (_,p,(_,_,_,_),_,ideq) = open_equality eq in
146 if List.mem ideq seen then
153 | Exact _ -> false,seen
154 | Step (_,(_,id1,(_,id2),_)) ->
155 let seen = ideq::seen in
156 let eq1 = Hashtbl.find id_to_eq id1 in
157 let eq2 = Hashtbl.find id_to_eq id2 in
158 let b1,seen = depend eq1 id seen in
159 if b1 then b1,seen else depend eq2 id seen
162 let depend eq id = fst (depend eq id []);;
164 let ppsubst = Subst.ppsubst ~names:[];;
166 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
167 let build_ens uri termlist =
168 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
170 | Cic.Constant (_, _, _, uris, _) ->
171 assert (List.length uris <= List.length termlist);
172 let rec aux = function
174 | (uri::uris), (term::tl) ->
175 let ens, args = aux (uris, tl) in
176 (uri, term)::ens, args
177 | _, _ -> assert false
183 let mk_sym uri ty t1 t2 p =
184 let ens, args = build_ens uri [ty;t1;t2;p] in
185 Cic.Appl (Cic.Const(uri, ens) :: args)
188 let mk_trans uri ty t1 t2 t3 p12 p23 =
189 let ens, args = build_ens uri [ty;t1;t2;t3;p12;p23] in
190 Cic.Appl (Cic.Const (uri, ens) :: args)
193 let mk_eq_ind uri ty what pred p1 other p2 =
194 Cic.Appl [Cic.Const (uri, []); ty; what; pred; p1; other; p2]
197 let p_of_sym ens tl =
198 let args = List.map snd ens @ tl in
204 let open_trans ens tl =
205 let args = List.map snd ens @ tl in
207 | [ty;l;m;r;p1;p2] -> ty,l,m,r,p1,p2
211 let open_sym ens tl =
212 let args = List.map snd ens @ tl in
214 | [ty;l;r;p] -> ty,l,r,p
218 let open_eq_ind args =
220 | [ty;l;pred;pl;r;pleqr] -> ty,l,pred,pl,r,pleqr
226 | Cic.Lambda (_,ty,(Cic.Appl [Cic.MutInd (uri, 0,_);_;l;r]))
227 when LibraryObjects.is_eq_URI uri -> ty,uri,l,r
228 | _ -> prerr_endline (CicPp.ppterm pred); assert false
232 CicSubstitution.subst (Cic.Implicit None) t <>
233 CicSubstitution.subst (Cic.Rel 1) t
238 let rec remove_refl t =
240 | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
241 when LibraryObjects.is_trans_eq_URI uri_trans ->
242 let ty,l,m,r,p1,p2 = open_trans ens tl in
244 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 ->
246 | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] ->
248 | _ -> Cic.Appl (List.map remove_refl args))
249 | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
250 | Cic.LetIn (name,bo,rest) ->
251 Cic.LetIn (name,remove_refl bo,remove_refl rest)
254 let rec canonical t =
256 | Cic.LetIn(name,bo,rest) -> Cic.LetIn(name,canonical bo,canonical rest)
257 | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
258 when LibraryObjects.is_sym_eq_URI uri_sym ->
259 (match p_of_sym ens tl with
260 | Cic.Appl ((Cic.Const(uri,ens))::tl)
261 when LibraryObjects.is_sym_eq_URI uri ->
262 canonical (p_of_sym ens tl)
263 | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
264 when LibraryObjects.is_trans_eq_URI uri_trans ->
265 let ty,l,m,r,p1,p2 = open_trans ens tl in
266 mk_trans uri_trans ty r m l
267 (canonical (mk_sym uri_sym ty m r p2))
268 (canonical (mk_sym uri_sym ty l m p1))
269 | Cic.Appl (((Cic.Const(uri_ind,ens)) as he)::tl)
270 when LibraryObjects.is_eq_ind_URI uri_ind ||
271 LibraryObjects.is_eq_ind_r_URI uri_ind ->
272 let ty, what, pred, p1, other, p2 =
274 | [ty;what;pred;p1;other;p2] -> ty, what, pred, p1, other, p2
279 | Cic.Lambda (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;l;r])
280 when LibraryObjects.is_eq_URI uri ->
282 (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;r;l]),l,r
284 prerr_endline (CicPp.ppterm pred);
287 let l = CicSubstitution.subst what l in
288 let r = CicSubstitution.subst what r in
291 canonical (mk_sym uri_sym ty l r p1);other;canonical p2]
292 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
293 when LibraryObjects.is_eq_URI uri -> t
294 | _ -> Cic.Appl (List.map canonical args))
295 | Cic.Appl l -> Cic.Appl (List.map canonical l)
298 remove_refl (canonical t)
301 let ty_of_lambda = function
302 | Cic.Lambda (_,ty,_) -> ty
306 let compose_contexts ctx1 ctx2 =
307 ProofEngineReduction.replace_lifting
308 ~equality:(=) ~what:[Cic.Rel 1] ~with_what:[ctx2] ~where:ctx1
311 let put_in_ctx ctx t =
312 ProofEngineReduction.replace_lifting
313 ~equality:(=) ~what:[Cic.Rel 1] ~with_what:[t] ~where:ctx
316 let mk_eq uri ty l r =
317 Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r]
320 let mk_refl uri ty t =
321 Cic.Appl [Cic.MutConstruct(uri,0,1,[]);ty;t]
324 let open_eq = function
325 | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
330 let contextualize uri ty left right t =
331 (* aux [uri] [ty] [left] [right] [ctx] [t]
333 * the parameters validate this invariant
334 * t: eq(uri) ty left right
335 * that is used only by the base case
337 * ctx is a term with an open (Rel 1). (Rel 1) is the empty context
339 let rec aux uri ty left right ctx_d = function
340 | Cic.Appl ((Cic.Const(uri_sym,ens))::tl)
341 when LibraryObjects.is_sym_eq_URI uri_sym ->
342 let ty,l,r,p = open_sym ens tl in
343 mk_sym uri_sym ty l r (aux uri ty l r ctx_d p)
344 | Cic.LetIn (name,body,rest) ->
345 (* we should go in body *)
346 Cic.LetIn (name,body,aux uri ty left right ctx_d rest)
347 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
348 when LibraryObjects.is_eq_ind_URI uri_ind ||
349 LibraryObjects.is_eq_ind_r_URI uri_ind ->
350 let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
351 let ty2,eq,lp,rp = open_pred pred in
352 let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
353 let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
354 let is_not_fixed_lp = is_not_fixed lp in
355 let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
356 (* extract the context and the fixed term from the predicate *)
358 let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
359 (* they were under a lambda *)
360 let m = CicSubstitution.subst (Cic.Implicit None) m in
361 let ctx_c = CicSubstitution.subst (Cic.Rel 1) ctx_c in
364 (* create the compound context and put the terms under it *)
365 let ctx_dc = compose_contexts ctx_d ctx_c in
366 let dc_what = put_in_ctx ctx_dc what in
367 let dc_other = put_in_ctx ctx_dc other in
368 (* m is already in ctx_c so it is put in ctx_d only *)
369 let d_m = put_in_ctx ctx_d m in
370 (* we also need what in ctx_c *)
371 let c_what = put_in_ctx ctx_c what in
372 (* now put the proofs in the compound context *)
373 let p1 = (* p1: dc_what = d_m *)
374 if is_not_fixed_lp then
375 aux uri ty1 c_what m ctx_d p1
377 mk_sym uri_sym ty d_m dc_what
378 (aux uri ty1 m c_what ctx_d p1)
380 let p2 = (* p2: dc_other = dc_what *)
382 mk_sym uri_sym ty dc_what dc_other
383 (aux uri ty1 what other ctx_dc p2)
385 aux uri ty1 other what ctx_dc p2
387 (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
388 if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
389 let a,b,c,paeqb,pbeqc =
390 if is_not_fixed_lp then
391 dc_other,dc_what,d_m,p2,p1
393 d_m,dc_what,dc_other,
394 (mk_sym uri_sym ty dc_what d_m p1),
395 (mk_sym uri_sym ty dc_other dc_what p2)
397 mk_trans uri_trans ty a b c paeqb pbeqc
399 let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
400 let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in
402 (* ctx_d will go under a lambda, but put_in_ctx substitutes Rel 1 *)
403 let ctx_d = CicSubstitution.lift_from 2 1 ctx_d in (* bleah *)
404 let r = put_in_ctx ctx_d (CicSubstitution.lift 1 left) in
406 let lty = CicSubstitution.lift 1 ty in
407 Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r))
409 let d_left = put_in_ctx ctx_d left in
410 let d_right = put_in_ctx ctx_d right in
411 let refl_eq = mk_refl uri ty d_left in
412 mk_sym uri_sym ty d_right d_left
413 (mk_eq_ind uri_ind ty left pred refl_eq right t)
415 let empty_context = Cic.Rel 1 in
416 aux uri ty left right empty_context t
419 let contextualize_rewrites t ty =
420 let eq,ty,l,r = open_eq ty in
421 contextualize eq ty l r t
424 let build_proof_step ?(sym=false) lift subst p1 p2 pos l r pred =
425 let p1 = Subst.apply_subst_lift lift subst p1 in
426 let p2 = Subst.apply_subst_lift lift subst p2 in
427 let l = CicSubstitution.lift lift l in
428 let l = Subst.apply_subst_lift lift subst l in
429 let r = CicSubstitution.lift lift r in
430 let r = Subst.apply_subst_lift lift subst r in
431 let pred = CicSubstitution.lift lift pred in
432 let pred = Subst.apply_subst_lift lift subst pred in
435 | Cic.Lambda (_,ty,body) -> ty,body
439 if pos = Utils.Left then l,r else r,l
444 mk_eq_ind (Utils.eq_ind_URI ()) ty what pred p1 other p2
446 mk_eq_ind (Utils.eq_ind_r_URI ()) ty what pred p1 other p2
450 let eq,_,pl,pr = open_eq body in
451 LibraryObjects.sym_eq_URI ~eq, pl, pr
453 let l = CicSubstitution.subst other pl in
454 let r = CicSubstitution.subst other pr in
460 let parametrize_proof p l r ty =
461 let parameters = CicUtil.metas_of_term p
462 @ CicUtil.metas_of_term l
463 @ CicUtil.metas_of_term r
464 in (* ?if they are under a lambda? *)
466 HExtlib.list_uniq (List.sort Pervasives.compare parameters)
468 let what = List.map (fun (i,l) -> Cic.Meta (i,l)) parameters in
469 let with_what, lift_no =
470 List.fold_right (fun _ (acc,n) -> ((Cic.Rel n)::acc),n+1) what ([],1)
472 let p = CicSubstitution.lift (lift_no-1) p in
474 ProofEngineReduction.replace_lifting
475 ~equality:(fun t1 t2 ->
476 match t1,t2 with Cic.Meta (i,_),Cic.Meta(j,_) -> i=j | _ -> false)
477 ~what ~with_what ~where:p
479 let ty_of_m _ = ty (*function
480 | Cic.Meta (i,_) -> List.assoc i menv
481 | _ -> assert false *)
485 (fun (instance,p,n) m ->
488 (Cic.Name ("x"^string_of_int n),
489 CicSubstitution.lift (lift_no - n - 1) (ty_of_m m),
495 let instance = match args with | [x] -> x | _ -> Cic.Appl args in
499 let wfo goalproof proof id =
501 let p,_,_ = proof_of_id id in
503 | Exact _ -> if (List.mem id acc) then acc else id :: acc
504 | Step (_,(_,id1, (_,id2), _)) ->
505 let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
506 let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
512 | Step (_,(_,id1, (_,id2), _)) -> aux (aux [id] id1) id2
514 List.fold_left (fun acc (_,_,id,_,_) -> aux acc id) acc goalproof
517 let string_of_id names id =
518 if id = 0 then "" else
520 let (_,p,(_,l,r,_),m,_) = open_equality (Hashtbl.find id_to_eq id) in
523 Printf.sprintf "%d = %s: %s = %s [%s]" id
524 (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
525 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
526 | Step (_,(step,id1, (_,id2), _) ) ->
527 Printf.sprintf "%6d: %s %6d %6d %s = %s [%s]" id
528 (string_of_rule step)
529 id1 id2 (CicPp.pp l names) (CicPp.pp r names)
530 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
532 Not_found -> assert false
534 let pp_proof names goalproof proof subst id initial_goal =
535 String.concat "\n" (List.map (string_of_id names) (wfo goalproof proof id)) ^
538 (fst (List.fold_right
539 (fun (r,pos,i,s,pred) (acc,g) ->
540 let _,_,left,right = open_eq g in
543 | Utils.Left -> CicReduction.head_beta_reduce (Cic.Appl[pred;right])
544 | Utils.Right -> CicReduction.head_beta_reduce (Cic.Appl[pred;left])
546 let ty = Subst.apply_subst s ty in
547 ("("^ string_of_rule r ^ " " ^ string_of_int i^") -> "
548 ^ CicPp.pp ty names) :: acc,ty) goalproof ([],initial_goal)))) ^
549 "\nand then subsumed by " ^ string_of_int id ^ " when " ^ Subst.ppsubst subst
552 (* returns the list of ids that should be factorized *)
553 let get_duplicate_step_in_wfo l p =
554 let ol = List.rev l in
555 let h = Hashtbl.create 13 in
556 (* NOTE: here the n parameter is an approximation of the dependency
557 between equations. To do things seriously we should maintain a
558 dependency graph. This approximation is not perfect. *)
560 let p,_,_ = proof_of_id i in
565 let (pos,no) = Hashtbl.find h i in
566 Hashtbl.replace h i (pos,no+1);
568 with Not_found -> Hashtbl.add h i (n,1);true
570 let rec aux n = function
572 | Step (_,(_,i1,(_,i2),_)) ->
573 let go_on_1 = add i1 n in
574 let go_on_2 = add i2 n in
576 (if go_on_1 then aux (n+1) (let p,_,_ = proof_of_id i1 in p) else n+1)
577 (if go_on_2 then aux (n+1) (let p,_,_ = proof_of_id i2 in p) else n+1)
582 (fun acc (_,_,id,_,_) -> aux acc (let p,_,_ = proof_of_id id in p))
585 (* now h is complete *)
586 let proofs = Hashtbl.fold (fun k (pos,count) acc->(k,pos,count)::acc) h [] in
587 let proofs = List.filter (fun (_,_,c) -> c > 1) proofs in
589 List.sort (fun (_,c1,_) (_,c2,_) -> Pervasives.compare c2 c1) proofs
591 List.map (fun (i,_,_) -> i) proofs
594 let build_proof_term h lift proof =
595 let proof_of_id aux id =
596 let p,l,r = proof_of_id id in
597 try List.assoc id h,l,r with Not_found -> aux p, l, r
599 let rec aux = function
600 | Exact term -> CicSubstitution.lift lift term
601 | Step (subst,(rule, id1, (pos,id2), pred)) ->
602 let p1,_,_ = proof_of_id aux id1 in
603 let p2,l,r = proof_of_id aux id2 in
606 | SuperpositionRight -> Cic.Name ("SupR" ^ Utils.string_of_pos pos)
607 | Demodulation -> Cic.Name ("DemEq"^ Utils.string_of_pos pos)
612 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
615 let p = build_proof_step lift subst p1 p2 pos l r pred in
616 (* let cond = (not (List.mem 302 (Utils.metas_of_term p)) || id1 = 8 || id1 = 132) in
618 prerr_endline ("ERROR " ^ string_of_int id1 ^ " " ^ string_of_int id2);
625 let build_goal_proof l initial ty se =
626 let se = List.map (fun i -> Cic.Meta (i,[])) se in
627 let lets = get_duplicate_step_in_wfo l initial in
628 let letsno = List.length lets in
629 let _,mty,_,_ = open_eq ty in
630 let lift_list l = List.map (fun (i,t) -> i,CicSubstitution.lift 1 t) l
635 let p,l,r = proof_of_id id in
636 let cic = build_proof_term h n p in
637 let real_cic,instance =
638 parametrize_proof cic l r (CicSubstitution.lift n mty)
640 let h = (id, instance)::lift_list h in
641 acc@[id,real_cic],n+1,h)
645 let rec aux se current_proof = function
646 | [] -> current_proof,se
647 | (rule,pos,id,subst,pred)::tl ->
648 let p,l,r = proof_of_id id in
649 let p = build_proof_term h letsno p in
650 let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
653 | SuperpositionLeft -> Cic.Name ("SupL" ^ Utils.string_of_pos pos)
654 | Demodulation -> Cic.Name ("DemG"^ Utils.string_of_pos pos)
659 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
663 build_proof_step letsno subst current_proof p pos l r pred
665 let proof,se = aux se proof tl in
666 Subst.apply_subst_lift letsno subst proof,
667 List.map (fun x -> Subst.apply_subst_lift letsno subst x) se
669 aux se (build_proof_term h letsno initial) l
672 let initial = proof in
674 (fun (id,cic) (n,p) ->
677 Cic.Name ("H"^string_of_int id),
679 lets (letsno-1,initial)
681 (*canonical (contextualize_rewrites proof (CicSubstitution.lift letsno ty))*)proof, se
684 let refl_proof ty term =
687 (LibraryObjects.eq_URI (), 0, 1, []);
691 let metas_of_proof p =
692 let p = build_proof_term [] 0 p in
693 Utils.metas_of_term p
696 let relocate newmeta menv to_be_relocated =
697 let subst, newmetasenv, newmeta =
699 (fun i (subst, metasenv, maxmeta) ->
700 let _,context,ty = CicUtil.lookup_meta i menv in
702 let newmeta = Cic.Meta(maxmeta,irl) in
703 let newsubst = Subst.buildsubst i context newmeta ty subst in
704 newsubst, (maxmeta,context,ty)::metasenv, maxmeta+1)
705 to_be_relocated (Subst.empty_subst, [], newmeta+1)
707 let menv = Subst.apply_subst_metasenv subst menv @ newmetasenv in
711 let fix_metas newmeta eq =
712 let w, p, (ty, left, right, o), menv,_ = open_equality eq in
713 let to_be_relocated =
715 (List.sort Pervasives.compare
716 (Utils.metas_of_term left @ Utils.metas_of_term right))
718 let subst, metasenv, newmeta = relocate newmeta menv to_be_relocated in
719 let ty = Subst.apply_subst subst ty in
720 let left = Subst.apply_subst subst left in
721 let right = Subst.apply_subst subst right in
722 let fix_proof = function
723 | Exact p -> Exact (Subst.apply_subst subst p)
724 | Step (s,(r,id1,(pos,id2),pred)) ->
725 Step (Subst.concat s subst,(r,id1,(pos,id2), pred))
727 let p = fix_proof p in
728 let eq' = mk_equality (w, p, (ty, left, right, o), metasenv) in
731 exception NotMetaConvertible;;
733 let meta_convertibility_aux table t1 t2 =
734 let module C = Cic in
735 let rec aux ((table_l, table_r) as table) t1 t2 =
737 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
738 let m1_binding, table_l =
739 try List.assoc m1 table_l, table_l
740 with Not_found -> m2, (m1, m2)::table_l
741 and m2_binding, table_r =
742 try List.assoc m2 table_r, table_r
743 with Not_found -> m1, (m2, m1)::table_r
745 if (m1_binding <> m2) || (m2_binding <> m1) then
746 raise NotMetaConvertible
752 | None, Some _ | Some _, None -> raise NotMetaConvertible
754 | Some t1, Some t2 -> (aux res t1 t2))
755 (table_l, table_r) tl1 tl2
756 with Invalid_argument _ ->
757 raise NotMetaConvertible
759 | C.Var (u1, ens1), C.Var (u2, ens2)
760 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
761 aux_ens table ens1 ens2
762 | C.Cast (s1, t1), C.Cast (s2, t2)
763 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
764 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
765 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
766 let table = aux table s1 s2 in
768 | C.Appl l1, C.Appl l2 -> (
769 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
770 with Invalid_argument _ -> raise NotMetaConvertible
772 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
773 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
774 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
775 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
776 aux_ens table ens1 ens2
777 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
778 when (UriManager.eq u1 u2) && i1 = i2 ->
779 let table = aux table s1 s2 in
780 let table = aux table t1 t2 in (
781 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
782 with Invalid_argument _ -> raise NotMetaConvertible
784 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
787 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
788 if i1 <> i2 then raise NotMetaConvertible
790 let res = (aux res s1 s2) in aux res t1 t2)
792 with Invalid_argument _ -> raise NotMetaConvertible
794 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
797 (fun res (n1, s1, t1) (n2, s2, t2) ->
798 let res = aux res s1 s2 in aux res t1 t2)
800 with Invalid_argument _ -> raise NotMetaConvertible
802 | t1, t2 when t1 = t2 -> table
803 | _, _ -> raise NotMetaConvertible
805 and aux_ens table ens1 ens2 =
806 let cmp (u1, t1) (u2, t2) =
807 Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
809 let ens1 = List.sort cmp ens1
810 and ens2 = List.sort cmp ens2 in
813 (fun res (u1, t1) (u2, t2) ->
814 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
817 with Invalid_argument _ -> raise NotMetaConvertible
823 let meta_convertibility_eq eq1 eq2 =
824 let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
825 let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
828 else if (left = left') && (right = right') then
830 else if (left = right') && (right = left') then
834 let table = meta_convertibility_aux ([], []) left left' in
835 let _ = meta_convertibility_aux table right right' in
837 with NotMetaConvertible ->
839 let table = meta_convertibility_aux ([], []) left right' in
840 let _ = meta_convertibility_aux table right left' in
842 with NotMetaConvertible ->
847 let meta_convertibility t1 t2 =
852 ignore(meta_convertibility_aux ([], []) t1 t2);
854 with NotMetaConvertible ->
858 exception TermIsNotAnEquality;;
860 let term_is_equality term =
861 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
863 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
867 let equality_of_term proof term =
868 let eq_uri = LibraryObjects.eq_URI () in
869 let iseq uri = UriManager.eq uri eq_uri in
871 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
872 let o = !Utils.compare_terms t1 t2 in
873 let stat = (ty,t1,t2,o) in
874 let w = Utils.compute_equality_weight stat in
875 let e = mk_equality (w, Exact proof, stat,[]) in
878 raise TermIsNotAnEquality
881 let is_weak_identity eq =
882 let _,_,(_,left, right,_),_,_ = open_equality eq in
883 left = right || meta_convertibility left right
886 let is_identity (_, context, ugraph) eq =
887 let _,_,(ty,left,right,_),menv,_ = open_equality eq in
889 (* (meta_convertibility left right)) *)
890 fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
894 let term_of_equality equality =
895 let _, _, (ty, left, right, _), menv, _= open_equality equality in
896 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
897 let argsno = List.length menv in
899 CicSubstitution.lift argsno
900 (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
904 (fun (i,_,ty) (n, t) ->
905 let name = Cic.Name ("X" ^ (string_of_int n)) in
906 let ty = CicSubstitution.lift (n-1) ty in
908 ProofEngineReduction.replace
909 ~equality:eq ~what:[i]
910 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
912 (n-1, Cic.Prod (name, ty, t)))