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 (* let _profiler = <:profiler<_profiler>>;; *)
28 (* $Id: inference.ml 6245 2006-04-05 12:07:51Z tassi $ *)
30 type rule = SuperpositionRight | SuperpositionLeft | Demodulation
31 type uncomparable = int -> int
33 uncomparable * (* trick to break structural equality *)
36 (Cic.term * (* type *)
37 Cic.term * (* left side *)
38 Cic.term * (* right side *)
39 Utils.comparison) * (* ordering *)
40 Cic.metasenv * (* environment for metas *)
44 | Step of Subst.substitution * (rule * int*(Utils.pos*int)* Cic.term)
45 (* subst, (rule,eq1, eq2,predicate) *)
46 and goal_proof = (rule * Utils.pos * int * Subst.substitution * Cic.term) list
49 type goal = goal_proof * Cic.metasenv * Cic.term
53 let id_to_eq = Hashtbl.create 1024;;
61 Hashtbl.clear id_to_eq
64 let uncomparable = fun _ -> 0
66 let mk_equality (weight,p,(ty,l,r,o),m) =
67 let id = freshid () in
68 let eq = (uncomparable,weight,p,(ty,l,r,o),m,id) in
69 Hashtbl.add id_to_eq id eq;
74 let mk_tmp_equality (weight,(ty,l,r,o),m) =
76 uncomparable,weight,Exact (Cic.Implicit None),(ty,l,r,o),m,id
80 let open_equality (_,weight,proof,(ty,l,r,o),m,id) =
81 (weight,proof,(ty,l,r,o),m,id)
83 let string_of_rule = function
84 | SuperpositionRight -> "SupR"
85 | SuperpositionLeft -> "SupL"
86 | Demodulation -> "Demod"
89 let string_of_equality ?env eq =
92 let w, _, (ty, left, right, o), m , id = open_equality eq in
93 Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]"
94 id w (CicPp.ppterm ty)
96 (Utils.string_of_comparison o) (CicPp.ppterm right)
97 (*(String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))*)
99 | Some (_, context, _) ->
100 let names = Utils.names_of_context context in
101 let w, _, (ty, left, right, o), m , id = open_equality eq in
102 Printf.sprintf "Id: %d, Weight: %d, {%s}: %s =(%s) %s [%s]"
103 id w (CicPp.pp ty names)
104 (CicPp.pp left names) (Utils.string_of_comparison o)
105 (CicPp.pp right names)
106 (* (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m)) *)
110 let compare (_,_,_,s1,_,_) (_,_,_,s2,_,_) =
111 Pervasives.compare s1 s2
114 let rec max_weight_in_proof current =
117 | Step (_, (_,id1,(_,id2),_)) ->
118 let eq1 = Hashtbl.find id_to_eq id1 in
119 let eq2 = Hashtbl.find id_to_eq id2 in
120 let (w1,p1,(_,_,_,_),_,_) = open_equality eq1 in
121 let (w2,p2,(_,_,_,_),_,_) = open_equality eq2 in
122 let current = max current w1 in
123 let current = max_weight_in_proof current p1 in
124 let current = max current w2 in
125 max_weight_in_proof current p2
127 let max_weight_in_goal_proof =
129 (fun current (_,_,id,_,_) ->
130 let eq = Hashtbl.find id_to_eq id in
131 let (w,p,(_,_,_,_),_,_) = open_equality eq in
132 let current = max current w in
133 max_weight_in_proof current p)
135 let max_weight goal_proof proof =
136 let current = max_weight_in_proof 0 proof in
137 max_weight_in_goal_proof current goal_proof
141 let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
144 Not_found -> assert false
147 let string_of_proof ?(names=[]) p gp =
148 let str_of_pos = function
149 | Utils.Left -> "left"
150 | Utils.Right -> "right"
152 let fst3 (x,_,_) = x in
153 let rec aux margin name =
154 let prefix = String.make margin ' ' ^ name ^ ": " in function
156 Printf.sprintf "%sExact (%s)\n"
157 prefix (CicPp.pp t names)
158 | Step (subst,(rule,eq1,(pos,eq2),pred)) ->
159 Printf.sprintf "%s%s(%s|%d with %d dir %s pred %s))\n"
160 prefix (string_of_rule rule) (Subst.ppsubst ~names subst) eq1 eq2 (str_of_pos pos)
161 (CicPp.pp pred names)^
162 aux (margin+1) (Printf.sprintf "%d" eq1) (fst3 (proof_of_id eq1)) ^
163 aux (margin+1) (Printf.sprintf "%d" eq2) (fst3 (proof_of_id eq2))
168 (fun (r,pos,i,s,t) ->
170 "GOAL: %s %s %d %s %s\n" (string_of_rule r)
171 (str_of_pos pos) i (Subst.ppsubst ~names s) (CicPp.pp t names)) ^
172 aux 1 (Printf.sprintf "%d " i) (fst3 (proof_of_id i)))
176 let rec depend eq id seen =
177 let (_,p,(_,_,_,_),_,ideq) = open_equality eq in
178 if List.mem ideq seen then
185 | Exact _ -> false,seen
186 | Step (_,(_,id1,(_,id2),_)) ->
187 let seen = ideq::seen in
188 let eq1 = Hashtbl.find id_to_eq id1 in
189 let eq2 = Hashtbl.find id_to_eq id2 in
190 let b1,seen = depend eq1 id seen in
191 if b1 then b1,seen else depend eq2 id seen
194 let depend eq id = fst (depend eq id []);;
196 let ppsubst = Subst.ppsubst ~names:[];;
198 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
199 let build_ens uri termlist =
200 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
202 | Cic.Constant (_, _, _, uris, _) ->
203 assert (List.length uris <= List.length termlist);
204 let rec aux = function
206 | (uri::uris), (term::tl) ->
207 let ens, args = aux (uris, tl) in
208 (uri, term)::ens, args
209 | _, _ -> assert false
215 let mk_sym uri ty t1 t2 p =
216 let ens, args = build_ens uri [ty;t1;t2;p] in
217 Cic.Appl (Cic.Const(uri, ens) :: args)
220 let mk_trans uri ty t1 t2 t3 p12 p23 =
221 let ens, args = build_ens uri [ty;t1;t2;t3;p12;p23] in
222 Cic.Appl (Cic.Const (uri, ens) :: args)
225 let mk_eq_ind uri ty what pred p1 other p2 =
226 Cic.Appl [Cic.Const (uri, []); ty; what; pred; p1; other; p2]
229 let p_of_sym ens tl =
230 let args = List.map snd ens @ tl in
236 let open_trans ens tl =
237 let args = List.map snd ens @ tl in
239 | [ty;l;m;r;p1;p2] -> ty,l,m,r,p1,p2
243 let open_sym ens tl =
244 let args = List.map snd ens @ tl in
246 | [ty;l;r;p] -> ty,l,r,p
250 let open_eq_ind args =
252 | [ty;l;pred;pl;r;pleqr] -> ty,l,pred,pl,r,pleqr
258 | Cic.Lambda (_,_,(Cic.Appl [Cic.MutInd (uri, 0,_);ty;l;r]))
259 when LibraryObjects.is_eq_URI uri -> ty,uri,l,r
260 | _ -> prerr_endline (CicPp.ppterm pred); assert false
264 CicSubstitution.subst (Cic.Implicit None) t <>
265 CicSubstitution.subst (Cic.Rel 1) t
268 let head_of_apply = function | Cic.Appl (hd::_) -> hd | t -> t;;
269 let tail_of_apply = function | Cic.Appl (_::tl) -> tl | t -> [];;
270 let count_args t = List.length (tail_of_apply t);;
272 let u = UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind" in
274 | 0 -> Cic.MutConstruct(u,0,1,[])
276 Cic.Appl [Cic.MutConstruct(u,0,2,[]);build_nat (n-1)]
278 let tyof context menv t =
280 fst(CicTypeChecker.type_of_aux' menv context t CicUniv.empty_ugraph)
282 | CicTypeChecker.TypeCheckerFailure _
283 | CicTypeChecker.AssertFailure _ -> assert false
285 let rec lambdaof left context = function
286 | Cic.Prod (n,s,t) ->
287 Cic.Lambda (n,s,lambdaof left context t)
288 | Cic.Appl [Cic.MutInd (uri, 0,_);ty;l;r]
289 when LibraryObjects.is_eq_URI uri -> if left then l else r
291 let names = Utils.names_of_context context in
292 prerr_endline ("lambdaof: " ^ (CicPp.pp t names));
296 let canonical t context menv =
297 let rec remove_refl t =
299 | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
300 when LibraryObjects.is_trans_eq_URI uri_trans ->
301 let ty,l,m,r,p1,p2 = open_trans ens tl in
303 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 ->
305 | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] ->
307 | _ -> Cic.Appl (List.map remove_refl args))
308 | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
309 | Cic.LetIn (name,bo,rest) ->
310 Cic.LetIn (name,remove_refl bo,remove_refl rest)
313 let rec canonical context t =
315 | Cic.LetIn(name,bo,rest) ->
316 let context' = (Some (name,Cic.Def (bo,None)))::context in
317 Cic.LetIn(name,canonical context bo,canonical context' rest)
318 | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
319 when LibraryObjects.is_sym_eq_URI uri_sym ->
320 (match p_of_sym ens tl with
321 | Cic.Appl ((Cic.Const(uri,ens))::tl)
322 when LibraryObjects.is_sym_eq_URI uri ->
323 canonical context (p_of_sym ens tl)
324 | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
325 when LibraryObjects.is_trans_eq_URI uri_trans ->
326 let ty,l,m,r,p1,p2 = open_trans ens tl in
327 mk_trans uri_trans ty r m l
328 (canonical context (mk_sym uri_sym ty m r p2))
329 (canonical context (mk_sym uri_sym ty l m p1))
330 | Cic.Appl (([Cic.Const(uri_feq,ens);ty1;ty2;f;x;y;p])) ->
331 let eq = LibraryObjects.eq_URI_of_eq_f_URI uri_feq in
333 Cic.Const (LibraryObjects.eq_f_sym_URI ~eq, [])
335 Cic.Appl (([eq_f_sym;ty1;ty2;f;x;y;p]))
338 let sym_eq = Cic.Const(uri_sym,ens) in
339 let eq_f = Cic.Const(uri_feq,[]) in
340 let b = Cic.MutConstruct (UriManager.uri_of_string
341 "cic:/matita/datatypes/bool/bool.ind",0,1,[])
345 let n = build_nat (count_args p) in
346 let h = head_of_apply p in
347 let predl = lambdaof true context (tyof context menv h) in
348 let predr = lambdaof false context (tyof context menv h) in
349 let args = tail_of_apply p in
352 ([Cic.Const(UriManager.uri_of_string
353 "cic:/matita/paramodulation/rewrite.con",[]);
354 eq; sym_eq; eq_f; b; u; ctx; n; predl; predr; h] @
360 | Cic.Appl (((Cic.Const(uri_ind,ens)) as he)::tl)
361 when LibraryObjects.is_eq_ind_URI uri_ind ||
362 LibraryObjects.is_eq_ind_r_URI uri_ind ->
363 let ty, what, pred, p1, other, p2 =
365 | [ty;what;pred;p1;other;p2] -> ty, what, pred, p1, other, p2
370 | Cic.Lambda (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;l;r])
371 when LibraryObjects.is_eq_URI uri ->
373 (name,s,Cic.Appl [Cic.MutInd(uri,0,ens);ty;r;l]),l,r
375 prerr_endline (CicPp.ppterm pred);
378 let l = CicSubstitution.subst what l in
379 let r = CicSubstitution.subst what r in
382 canonical (mk_sym uri_sym ty l r p1);other;canonical p2]
384 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
385 when LibraryObjects.is_eq_URI uri -> t
386 | _ -> Cic.Appl (List.map (canonical context) args))
387 | Cic.Appl l -> Cic.Appl (List.map (canonical context) l)
390 remove_refl (canonical context t)
393 let ty_of_lambda = function
394 | Cic.Lambda (_,ty,_) -> ty
398 let compose_contexts ctx1 ctx2 =
399 ProofEngineReduction.replace_lifting
400 ~equality:(=) ~what:[Cic.Implicit(Some `Hole)] ~with_what:[ctx2] ~where:ctx1
403 let put_in_ctx ctx t =
404 ProofEngineReduction.replace_lifting
405 ~equality:(=) ~what:[Cic.Implicit (Some `Hole)] ~with_what:[t] ~where:ctx
408 let mk_eq uri ty l r =
409 Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r]
412 let mk_refl uri ty t =
413 Cic.Appl [Cic.MutConstruct(uri,0,1,[]);ty;t]
416 let open_eq = function
417 | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
422 let mk_feq uri_feq ty ty1 left pred right t =
423 Cic.Appl [Cic.Const(uri_feq,[]);ty;ty1;pred;left;right;t]
426 let rec look_ahead aux = function
427 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl) as t
428 when LibraryObjects.is_eq_ind_URI uri_ind ||
429 LibraryObjects.is_eq_ind_r_URI uri_ind ->
430 let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
431 let ty2,eq,lp,rp = open_pred pred in
432 let hole = Cic.Implicit (Some `Hole) in
433 let ty2 = CicSubstitution.subst hole ty2 in
434 aux ty1 (CicSubstitution.subst other lp) (CicSubstitution.subst other rp) hole ty2 t
435 | Cic.Lambda (n,s,t) -> Cic.Lambda (n,s,look_ahead aux t)
439 let contextualize uri ty left right t =
440 let hole = Cic.Implicit (Some `Hole) in
441 (* aux [uri] [ty] [left] [right] [ctx] [ctx_ty] [t]
443 * the parameters validate this invariant
444 * t: eq(uri) ty left right
445 * that is used only by the base case
447 * ctx is a term with an hole. Cic.Implicit(Some `Hole) is the empty context
448 * ctx_ty is the type of ctx
450 let rec aux uri ty left right ctx_d ctx_ty = function
451 | Cic.Appl ((Cic.Const(uri_sym,ens))::tl)
452 when LibraryObjects.is_sym_eq_URI uri_sym ->
453 let ty,l,r,p = open_sym ens tl in
454 mk_sym uri_sym ty l r (aux uri ty l r ctx_d ctx_ty p)
455 | Cic.LetIn (name,body,rest) ->
456 Cic.LetIn (name,look_ahead (aux uri) body, aux uri ty left right ctx_d ctx_ty rest)
457 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
458 when LibraryObjects.is_eq_ind_URI uri_ind ||
459 LibraryObjects.is_eq_ind_r_URI uri_ind ->
460 let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
461 let ty2,eq,lp,rp = open_pred pred in
462 let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
463 let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
464 let is_not_fixed_lp = is_not_fixed lp in
465 let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
466 (* extract the context and the fixed term from the predicate *)
468 let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
469 (* they were under a lambda *)
470 let m = CicSubstitution.subst hole m in
471 let ctx_c = CicSubstitution.subst hole ctx_c in
472 let ty2 = CicSubstitution.subst hole ty2 in
475 (* create the compound context and put the terms under it *)
476 let ctx_dc = compose_contexts ctx_d ctx_c in
477 let dc_what = put_in_ctx ctx_dc what in
478 let dc_other = put_in_ctx ctx_dc other in
479 (* m is already in ctx_c so it is put in ctx_d only *)
480 let d_m = put_in_ctx ctx_d m in
481 (* we also need what in ctx_c *)
482 let c_what = put_in_ctx ctx_c what in
483 (* now put the proofs in the compound context *)
484 let p1 = (* p1: dc_what = d_m *)
485 if is_not_fixed_lp then
486 aux uri ty2 c_what m ctx_d ctx_ty p1
488 mk_sym uri_sym ctx_ty d_m dc_what
489 (aux uri ty2 m c_what ctx_d ctx_ty p1)
491 let p2 = (* p2: dc_other = dc_what *)
493 mk_sym uri_sym ctx_ty dc_what dc_other
494 (aux uri ty1 what other ctx_dc ctx_ty p2)
496 aux uri ty1 other what ctx_dc ctx_ty p2
498 (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
499 if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
500 let a,b,c,paeqb,pbeqc =
501 if is_not_fixed_lp then
502 dc_other,dc_what,d_m,p2,p1
504 d_m,dc_what,dc_other,
505 (mk_sym uri_sym ctx_ty dc_what d_m p1),
506 (mk_sym uri_sym ctx_ty dc_other dc_what p2)
508 mk_trans uri_trans ctx_ty a b c paeqb pbeqc
509 | t when ctx_d = hole -> t
511 (* let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in *)
512 (* let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in *)
514 let uri_feq = LibraryObjects.eq_f_URI ~eq:uri in
516 (* let r = CicSubstitution.lift 1 (put_in_ctx ctx_d left) in *)
518 let ctx_d = CicSubstitution.lift 1 ctx_d in
519 put_in_ctx ctx_d (Cic.Rel 1)
521 (* let lty = CicSubstitution.lift 1 ctx_ty in *)
522 (* Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r)) *)
523 Cic.Lambda (Cic.Name "foo",ty,l)
525 (* let d_left = put_in_ctx ctx_d left in *)
526 (* let d_right = put_in_ctx ctx_d right in *)
527 (* let refl_eq = mk_refl uri ctx_ty d_left in *)
528 (* mk_sym uri_sym ctx_ty d_right d_left *)
529 (* (mk_eq_ind uri_ind ty left pred refl_eq right t) *)
530 (mk_feq uri_feq ty ctx_ty left pred right t)
532 aux uri ty left right hole ty t
535 let contextualize_rewrites t ty =
536 let eq,ty,l,r = open_eq ty in
537 contextualize eq ty l r t
540 let add_subst subst =
542 | Exact t -> Exact (Subst.apply_subst subst t)
543 | Step (s,(rule, id1, (pos,id2), pred)) ->
544 Step (Subst.concat subst s,(rule, id1, (pos,id2), pred))
547 let build_proof_step eq lift subst p1 p2 pos l r pred =
548 let p1 = Subst.apply_subst_lift lift subst p1 in
549 let p2 = Subst.apply_subst_lift lift subst p2 in
550 let l = CicSubstitution.lift lift l in
551 let l = Subst.apply_subst_lift lift subst l in
552 let r = CicSubstitution.lift lift r in
553 let r = Subst.apply_subst_lift lift subst r in
554 let pred = CicSubstitution.lift lift pred in
555 let pred = Subst.apply_subst_lift lift subst pred in
558 | Cic.Lambda (_,ty,body) -> ty,body
562 if pos = Utils.Left then l,r else r,l
567 mk_eq_ind (LibraryObjects.eq_ind_URI ~eq) ty what pred p1 other p2
569 mk_eq_ind (LibraryObjects.eq_ind_r_URI ~eq) ty what pred p1 other p2
574 let parametrize_proof p l r ty =
575 let uniq l = HExtlib.list_uniq (List.sort Pervasives.compare l) in
576 let mot = CicUtil.metas_of_term_set in
577 let parameters = uniq (mot p @ mot l @ mot r) in
578 (* ?if they are under a lambda? *)
581 HExtlib.list_uniq (List.sort Pervasives.compare parameters)
584 let what = List.map (fun (i,l) -> Cic.Meta (i,l)) parameters in
585 let with_what, lift_no =
586 List.fold_right (fun _ (acc,n) -> ((Cic.Rel n)::acc),n+1) what ([],1)
588 let p = CicSubstitution.lift (lift_no-1) p in
590 ProofEngineReduction.replace_lifting
591 ~equality:(fun t1 t2 ->
592 match t1,t2 with Cic.Meta (i,_),Cic.Meta(j,_) -> i=j | _ -> false)
593 ~what ~with_what ~where:p
595 let ty_of_m _ = ty (*function
596 | Cic.Meta (i,_) -> List.assoc i menv
597 | _ -> assert false *)
601 (fun (instance,p,n) m ->
604 (Cic.Name ("X"^string_of_int n),
605 CicSubstitution.lift (lift_no - n - 1) (ty_of_m m),
611 let instance = match args with | [x] -> x | _ -> Cic.Appl args in
615 let wfo goalproof proof id =
617 let p,_,_ = proof_of_id id in
619 | Exact _ -> if (List.mem id acc) then acc else id :: acc
620 | Step (_,(_,id1, (_,id2), _)) ->
621 let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
622 let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
628 | Step (_,(_,id1, (_,id2), _)) -> aux (aux [id] id1) id2
630 List.fold_left (fun acc (_,_,id,_,_) -> aux acc id) acc goalproof
633 let string_of_id names id =
634 if id = 0 then "" else
636 let (_,p,(_,l,r,_),m,_) = open_equality (Hashtbl.find id_to_eq id) in
639 Printf.sprintf "%d = %s: %s = %s [%s]" id
640 (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
642 (* (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m)) *)
643 | Step (_,(step,id1, (_,id2), _) ) ->
644 Printf.sprintf "%6d: %s %6d %6d %s = %s [%s]" id
645 (string_of_rule step)
646 id1 id2 (CicPp.pp l names) (CicPp.pp r names)
647 (* (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m)) *)
650 Not_found -> assert false
652 let pp_proof names goalproof proof subst id initial_goal =
653 String.concat "\n" (List.map (string_of_id names) (wfo goalproof proof id)) ^
656 (fst (List.fold_right
657 (fun (r,pos,i,s,pred) (acc,g) ->
658 let _,_,left,right = open_eq g in
661 | Utils.Left -> CicReduction.head_beta_reduce (Cic.Appl[pred;right])
662 | Utils.Right -> CicReduction.head_beta_reduce (Cic.Appl[pred;left])
664 let ty = Subst.apply_subst s ty in
665 ("("^ string_of_rule r ^ " " ^ string_of_int i^") -> "
666 ^ CicPp.pp ty names) :: acc,ty) goalproof ([],initial_goal)))) ^
667 "\nand then subsumed by " ^ string_of_int id ^ " when " ^ Subst.ppsubst subst
673 let compare = Pervasives.compare
676 module M = Map.Make(OT)
678 let rec find_deps m i =
681 let p,_,_ = proof_of_id i in
683 | Exact _ -> M.add i [] m
684 | Step (_,(_,id1,(_,id2),_)) ->
685 let m = find_deps m id1 in
686 let m = find_deps m id2 in
687 (* without the uniq there is a stack overflow doing concatenation *)
688 let xxx = [id1;id2] @ M.find id1 m @ M.find id2 m in
689 let xxx = HExtlib.list_uniq (List.sort Pervasives.compare xxx) in
693 let topological_sort l =
694 (* build the partial order relation *)
695 let m = List.fold_left (fun m i -> find_deps m i) M.empty l in
696 let m = (* keep only deps inside l *)
699 M.add i (List.filter (fun x -> List.mem x l) (M.find i m)) m')
702 let m = M.map (fun x -> Some x) m in
704 let keys m = M.fold (fun i _ acc -> i::acc) m [] in
705 let split l m = List.filter (fun i -> M.find i m = Some []) l in
708 (fun k v -> if List.mem k l then None else
711 | Some ll -> Some (List.filter (fun i -> not (List.mem i l)) ll))
716 let ok = split keys m in
717 let m = purge ok m in
718 let res = ok @ res in
719 if ok = [] then res else aux m res
721 let rc = List.rev (aux m []) in
726 (* returns the list of ids that should be factorized *)
727 let get_duplicate_step_in_wfo l p =
728 let ol = List.rev l in
729 let h = Hashtbl.create 13 in
730 (* NOTE: here the n parameter is an approximation of the dependency
731 between equations. To do things seriously we should maintain a
732 dependency graph. This approximation is not perfect. *)
734 let p,_,_ = proof_of_id i in
739 let no = Hashtbl.find h i in
740 Hashtbl.replace h i (no+1);
742 with Not_found -> Hashtbl.add h i 1;true
744 let rec aux = function
746 | Step (_,(_,i1,(_,i2),_)) ->
747 let go_on_1 = add i1 in
748 let go_on_2 = add i2 in
749 if go_on_1 then aux (let p,_,_ = proof_of_id i1 in p);
750 if go_on_2 then aux (let p,_,_ = proof_of_id i2 in p)
754 (fun (_,_,id,_,_) -> aux (let p,_,_ = proof_of_id id in p))
756 (* now h is complete *)
757 let proofs = Hashtbl.fold (fun k count acc-> (k,count)::acc) h [] in
758 let proofs = List.filter (fun (_,c) -> c > 1) proofs in
759 let res = topological_sort (List.map (fun (i,_) -> i) proofs) in
763 let build_proof_term eq h lift proof =
764 let proof_of_id aux id =
765 let p,l,r = proof_of_id id in
766 try List.assoc id h,l,r with Not_found -> aux p, l, r
768 let rec aux = function
770 CicSubstitution.lift lift term
771 | Step (subst,(rule, id1, (pos,id2), pred)) ->
772 let p1,_,_ = proof_of_id aux id1 in
773 let p2,l,r = proof_of_id aux id2 in
776 | SuperpositionRight -> Cic.Name ("SupR" ^ Utils.string_of_pos pos)
777 | Demodulation -> Cic.Name ("DemEq"^ Utils.string_of_pos pos)
782 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
785 let p = build_proof_step eq lift subst p1 p2 pos l r pred in
786 (* let cond = (not (List.mem 302 (Utils.metas_of_term p)) || id1 = 8 || id1 = 132) in
788 prerr_endline ("ERROR " ^ string_of_int id1 ^ " " ^ string_of_int id2);
795 let build_goal_proof eq l initial ty se context menv =
796 let se = List.map (fun i -> Cic.Meta (i,[])) se in
797 let lets = get_duplicate_step_in_wfo l initial in
798 let letsno = List.length lets in
799 let _,mty,_,_ = open_eq ty in
800 let lift_list l = List.map (fun (i,t) -> i,CicSubstitution.lift 1 t) l in
804 let p,l,r = proof_of_id id in
805 let cic = build_proof_term eq h n p in
806 let real_cic,instance =
807 parametrize_proof cic l r (CicSubstitution.lift n mty)
809 let h = (id, instance)::lift_list h in
810 acc@[id,real_cic],n+1,h)
814 let rec aux se current_proof = function
815 | [] -> current_proof,se
816 | (rule,pos,id,subst,pred)::tl ->
817 let p,l,r = proof_of_id id in
818 let p = build_proof_term eq h letsno p in
819 let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
822 | SuperpositionLeft -> Cic.Name ("SupL" ^ Utils.string_of_pos pos)
823 | Demodulation -> Cic.Name ("DemG"^ Utils.string_of_pos pos)
828 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
832 build_proof_step eq letsno subst current_proof p pos l r pred
834 let proof,se = aux se proof tl in
835 Subst.apply_subst_lift letsno subst proof,
836 List.map (fun x -> Subst.apply_subst_lift letsno subst x) se
838 aux se (build_proof_term eq h letsno initial) l
841 let initial = proof in
843 (fun (id,cic) (n,p) ->
846 Cic.Name ("H"^string_of_int id),
848 lets (letsno-1,initial)
851 (contextualize_rewrites proof (CicSubstitution.lift letsno ty))
856 let refl_proof eq_uri ty term =
857 Cic.Appl [Cic.MutConstruct (eq_uri, 0, 1, []); ty; term]
860 let metas_of_proof p =
862 match LibraryObjects.eq_URI () with
866 (ProofEngineTypes.Fail
867 (lazy "No default equality defined when calling metas_of_proof"))
869 let p = build_proof_term eq [] 0 p in
870 Utils.metas_of_term p
873 let remove_local_context eq =
874 let w, p, (ty, left, right, o), menv,id = open_equality eq in
875 let p = Utils.remove_local_context p in
876 let ty = Utils.remove_local_context ty in
877 let left = Utils.remove_local_context left in
878 let right = Utils.remove_local_context right in
879 w, p, (ty, left, right, o), menv, id
882 let relocate newmeta menv to_be_relocated =
883 let subst, newmetasenv, newmeta =
885 (fun i (subst, metasenv, maxmeta) ->
886 let _,context,ty = CicUtil.lookup_meta i menv in
888 let newmeta = Cic.Meta(maxmeta,irl) in
889 let newsubst = Subst.buildsubst i context newmeta ty subst in
890 newsubst, (maxmeta,context,ty)::metasenv, maxmeta+1)
891 to_be_relocated (Subst.empty_subst, [], newmeta+1)
893 let menv = Subst.apply_subst_metasenv subst menv @ newmetasenv in
896 let fix_metas_goal newmeta goal =
897 let (proof, menv, ty) = goal in
898 let to_be_relocated =
899 HExtlib.list_uniq (List.sort Pervasives.compare (Utils.metas_of_term ty))
901 let subst, menv, newmeta = relocate newmeta menv to_be_relocated in
902 let ty = Subst.apply_subst subst ty in
905 | [] -> assert false (* is a nonsense to relocate the initial goal *)
906 | (r,pos,id,s,p) :: tl -> (r,pos,id,Subst.concat subst s,p) :: tl
908 newmeta+1,(proof, menv, ty)
911 let fix_metas newmeta eq =
912 let w, p, (ty, left, right, o), menv,_ = open_equality eq in
913 let to_be_relocated =
914 (* List.map (fun i ,_,_ -> i) menv *)
916 (List.sort Pervasives.compare
917 (Utils.metas_of_term left @ Utils.metas_of_term right))
919 let subst, metasenv, newmeta = relocate newmeta menv to_be_relocated in
920 let ty = Subst.apply_subst subst ty in
921 let left = Subst.apply_subst subst left in
922 let right = Subst.apply_subst subst right in
923 let fix_proof = function
924 | Exact p -> Exact (Subst.apply_subst subst p)
925 | Step (s,(r,id1,(pos,id2),pred)) ->
926 Step (Subst.concat s subst,(r,id1,(pos,id2), pred))
928 let p = fix_proof p in
929 let eq' = mk_equality (w, p, (ty, left, right, o), metasenv) in
932 exception NotMetaConvertible;;
934 let meta_convertibility_aux table t1 t2 =
935 let module C = Cic in
936 let rec aux ((table_l, table_r) as table) t1 t2 =
938 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
939 let tl1, tl2 = [],[] in
940 let m1_binding, table_l =
941 try List.assoc m1 table_l, table_l
942 with Not_found -> m2, (m1, m2)::table_l
943 and m2_binding, table_r =
944 try List.assoc m2 table_r, table_r
945 with Not_found -> m1, (m2, m1)::table_r
947 if (m1_binding <> m2) || (m2_binding <> m1) then
948 raise NotMetaConvertible
954 | None, Some _ | Some _, None -> raise NotMetaConvertible
956 | Some t1, Some t2 -> (aux res t1 t2))
957 (table_l, table_r) tl1 tl2
958 with Invalid_argument _ ->
959 raise NotMetaConvertible
961 | C.Var (u1, ens1), C.Var (u2, ens2)
962 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
963 aux_ens table ens1 ens2
964 | C.Cast (s1, t1), C.Cast (s2, t2)
965 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
966 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
967 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
968 let table = aux table s1 s2 in
970 | C.Appl l1, C.Appl l2 -> (
971 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
972 with Invalid_argument _ -> raise NotMetaConvertible
974 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
975 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
976 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
977 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
978 aux_ens table ens1 ens2
979 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
980 when (UriManager.eq u1 u2) && i1 = i2 ->
981 let table = aux table s1 s2 in
982 let table = aux table t1 t2 in (
983 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
984 with Invalid_argument _ -> raise NotMetaConvertible
986 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
989 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
990 if i1 <> i2 then raise NotMetaConvertible
992 let res = (aux res s1 s2) in aux res t1 t2)
994 with Invalid_argument _ -> raise NotMetaConvertible
996 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
999 (fun res (n1, s1, t1) (n2, s2, t2) ->
1000 let res = aux res s1 s2 in aux res t1 t2)
1002 with Invalid_argument _ -> raise NotMetaConvertible
1004 | t1, t2 when t1 = t2 -> table
1005 | _, _ -> raise NotMetaConvertible
1007 and aux_ens table ens1 ens2 =
1008 let cmp (u1, t1) (u2, t2) =
1009 Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
1011 let ens1 = List.sort cmp ens1
1012 and ens2 = List.sort cmp ens2 in
1015 (fun res (u1, t1) (u2, t2) ->
1016 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
1019 with Invalid_argument _ -> raise NotMetaConvertible
1025 let meta_convertibility_eq eq1 eq2 =
1026 let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
1027 let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
1030 else if (left = left') && (right = right') then
1032 else if (left = right') && (right = left') then
1036 let table = meta_convertibility_aux ([], []) left left' in
1037 let _ = meta_convertibility_aux table right right' in
1039 with NotMetaConvertible ->
1041 let table = meta_convertibility_aux ([], []) left right' in
1042 let _ = meta_convertibility_aux table right left' in
1044 with NotMetaConvertible ->
1049 let meta_convertibility t1 t2 =
1054 ignore(meta_convertibility_aux ([], []) t1 t2);
1056 with NotMetaConvertible ->
1060 exception TermIsNotAnEquality;;
1062 let term_is_equality term =
1064 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
1065 when LibraryObjects.is_eq_URI uri -> true
1069 let equality_of_term proof term =
1071 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2]
1072 when LibraryObjects.is_eq_URI uri ->
1073 let o = !Utils.compare_terms t1 t2 in
1074 let stat = (ty,t1,t2,o) in
1075 let w = Utils.compute_equality_weight stat in
1076 let e = mk_equality (w, Exact proof, stat,[]) in
1079 raise TermIsNotAnEquality
1082 let is_weak_identity eq =
1083 let _,_,(_,left, right,_),_,_ = open_equality eq in
1084 left = right || meta_convertibility left right
1087 let is_identity (_, context, ugraph) eq =
1088 let _,_,(ty,left,right,_),menv,_ = open_equality eq in
1090 (* (meta_convertibility left right)) *)
1091 fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
1095 let term_of_equality eq_uri equality =
1096 let _, _, (ty, left, right, _), menv, _= open_equality equality in
1097 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1098 let argsno = List.length menv in
1100 CicSubstitution.lift argsno
1101 (Cic.Appl [Cic.MutInd (eq_uri, 0, []); ty; left; right])
1105 (fun (i,_,ty) (n, t) ->
1106 let name = Cic.Name ("X" ^ (string_of_int n)) in
1107 let ty = CicSubstitution.lift (n-1) ty in
1109 ProofEngineReduction.replace
1110 ~equality:eq ~what:[i]
1111 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1113 (n-1, Cic.Prod (name, ty, t)))
1117 let symmetric eq_ty l id uri m =
1118 let eq = Cic.MutInd(uri,0,[]) in
1120 Cic.Lambda (Cic.Name "Sym",eq_ty,
1121 Cic.Appl [CicSubstitution.lift 1 eq ;
1122 CicSubstitution.lift 1 eq_ty;
1123 Cic.Rel 1;CicSubstitution.lift 1 l])
1127 [Cic.MutConstruct(uri,0,1,[]);eq_ty;l])
1130 let eq = mk_equality (0,prefl,(eq_ty,l,l,Utils.Eq),m) in
1131 let (_,_,_,_,id) = open_equality eq in
1134 Step(Subst.empty_subst,
1135 (Demodulation,id1,(Utils.Left,id),pred))
1138 module IntOT = struct
1140 let compare = Pervasives.compare
1143 module IntSet = Set.Make(IntOT);;
1145 let n_purged = ref 0;;
1147 let collect alive1 alive2 alive3 =
1148 (* let _ = <:start<collect>> in *)
1150 let p,_,_ = proof_of_id id in
1152 | Exact _ -> IntSet.empty
1153 | Step (_,(_,id1,(_,id2),_)) ->
1154 IntSet.add id1 (IntSet.add id2 IntSet.empty)
1157 let news = IntSet.fold (fun id s -> IntSet.union (deps_of id) s) s s in
1158 if IntSet.equal news s then s else close news
1160 let l_to_s s l = List.fold_left (fun s x -> IntSet.add x s) s l in
1161 let alive_set = l_to_s (l_to_s (l_to_s IntSet.empty alive2) alive1) alive3 in
1162 let closed_alive_set = close alive_set in
1166 if not (IntSet.mem k closed_alive_set) then
1167 k::s else s) id_to_eq []
1169 n_purged := !n_purged + List.length to_purge;
1170 List.iter (Hashtbl.remove id_to_eq) to_purge;
1171 (* let _ = <:stop<collect>> in () *)
1175 let _,_,_,_,id = open_equality e in id
1178 let get_stats () = ""
1180 <:show<Equality.>> ^
1181 "# of purged eq by the collector: " ^ string_of_int !n_purged ^ "\n"
1185 let rec pp_proofterm name t context =
1186 let rec skip_lambda tys ctx = function
1187 | Cic.Lambda (n,s,t) -> skip_lambda (s::tys) ((Some n)::ctx) t
1192 | Cic.Name s1 -> Cic.Name (s ^ s1)
1195 let rec skip_letin ctx = function
1196 | Cic.LetIn (n,b,t) ->
1197 pp_proofterm (Some (rename "Lemma " n)) b ctx::
1198 skip_letin ((Some n)::ctx) t
1200 let ppterm t = CicPp.pp t ctx in
1201 let rec pp inner = function
1202 | Cic.Appl [Cic.Const (uri,[]);_;l;m;r;p1;p2]
1203 when Pcre.pmatch ~pat:"trans_eq" (UriManager.string_of_uri uri)->
1205 (" " ^ ppterm l) :: pp true p1 @
1206 [ " = " ^ ppterm m ] @ pp true p2 @
1207 [ " = " ^ ppterm r ]
1210 [ " = " ^ ppterm m ] @ pp true p2
1211 | Cic.Appl [Cic.Const (uri,[]);_;l;m;p]
1212 when Pcre.pmatch ~pat:"sym_eq" (UriManager.string_of_uri uri)->
1214 | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
1215 when Pcre.pmatch ~pat:"eq_f" (UriManager.string_of_uri uri)->
1217 | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
1218 when Pcre.pmatch ~pat:"eq_f1" (UriManager.string_of_uri uri)->
1220 | Cic.Appl [Cic.MutConstruct (uri,_,_,[]);_;_;t;p]
1221 when Pcre.pmatch ~pat:"ex.ind" (UriManager.string_of_uri uri)->
1222 [ "witness " ^ ppterm t ] @ pp true p
1223 | Cic.Appl (t::_) ->[ " [by " ^ ppterm t ^ "]"]
1224 | t ->[ " [by " ^ ppterm t ^ "]"]
1226 let rec compat = function
1227 | a::b::tl -> (b ^ a) :: compat tl
1231 let compat l = List.hd l :: compat (List.tl l) in
1232 compat (pp false t) @ ["";""]
1234 let names, tys, body = skip_lambda [] context t in
1235 let ppname name = (match name with Some (Cic.Name s) -> s | _ -> "") in
1236 ppname name ^ ":\n" ^
1237 (if context = [] then
1238 let rec pp_l ctx = function
1240 " " ^ ppname name ^ ": " ^ CicPp.pp t ctx ^ "\n" ^
1244 pp_l [] (List.rev (List.combine tys names))
1247 String.concat "\n" (skip_letin names body)
1250 let pp_proofterm t =
1252 pp_proofterm (Some (Cic.Name "Hypothesis")) t []