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
34 uncomparable * (* trick to break structural equality *)
37 (Cic.term * (* type *)
38 Cic.term * (* left side *)
39 Cic.term * (* right side *)
40 Utils.comparison) * (* ordering *)
41 Cic.metasenv * (* environment for metas *)
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
49 (* the hashtbl eq_id -> proof, max_eq_id *)
50 type equality_bag = (int,equality) Hashtbl.t * int ref
52 type goal = goal_proof * Cic.metasenv * Cic.term
55 let mk_equality_bag () =
56 Hashtbl.create 1024, ref 0
63 let add_to_bag (id_to_eq,_) id eq =
64 Hashtbl.add id_to_eq id eq
67 let uncomparable = fun _ -> 0
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
76 let mk_tmp_equality (weight,(ty,l,r,o),m) =
78 uncomparable,weight,Exact (Cic.Implicit None),(ty,l,r,o),m,id
82 let open_equality (_,weight,proof,(ty,l,r,o),m,id) =
83 (weight,proof,(ty,l,r,o),m,id)
85 let string_of_rule = function
86 | SuperpositionRight -> "SupR"
87 | SuperpositionLeft -> "SupL"
88 | Demodulation -> "Demod"
91 let string_of_equality ?env eq =
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)
98 (Utils.string_of_comparison o) (CicPp.ppterm right)
99 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
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))
112 let compare (_,_,_,s1,_,_) (_,_,_,s2,_,_) =
113 Pervasives.compare s1 s2
116 let rec max_weight_in_proof ((id_to_eq,_) as bag) 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
129 let max_weight_in_goal_proof ((id_to_eq,_) as bag) =
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)
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
141 let proof_of_id (id_to_eq,_) id =
143 let (_,p,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
146 Not_found -> assert false
149 let string_of_proof ?(names=[]) bag p gp =
150 let str_of_pos = function
151 | Utils.Left -> "left"
152 | Utils.Right -> "right"
154 let fst3 (x,_,_) = x in
155 let rec aux margin name =
156 let prefix = String.make margin ' ' ^ name ^ ": " in function
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))
170 (fun (r,pos,i,s,t) ->
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)))
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
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
196 let depend bag eq id = fst (depend bag eq id []);;
198 let ppsubst = Subst.ppsubst ~names:[];;
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
204 | Cic.Constant (_, _, _, uris, _) ->
205 (* assert (List.length uris <= List.length termlist); *)
206 let rec aux = function
208 | (uri::uris), (term::tl) ->
209 let ens, args = aux (uris, tl) in
210 (uri, term)::ens, args
211 | _, _ -> assert false
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)
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)
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)
232 let p_of_sym ens tl =
233 let args = List.map snd ens @ tl in
239 let open_trans ens tl =
240 let args = List.map snd ens @ tl in
242 | [ty;l;m;r;p1;p2] -> ty,l,m,r,p1,p2
246 let open_sym ens tl =
247 let args = List.map snd ens @ tl in
249 | [ty;l;r;p] -> ty,l,r,p
253 let open_eq_ind args =
255 | [ty;l;pred;pl;r;pleqr] -> ty,l,pred,pl,r,pleqr
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
267 CicSubstitution.subst (Cic.Implicit None) t <>
268 CicSubstitution.subst (Cic.Rel 1) t
271 let canonical t context menv =
272 let remove_cycles t =
275 Cic.Appl (Cic.Const (uri_trans,_)::_)
276 when LibraryObjects.is_trans_eq_URI uri_trans ->
281 Cic.Appl (Cic.Const (uri_trans,ens)::tl)
282 when LibraryObjects.is_trans_eq_URI uri_trans ->
283 let ty,l,m,r,p1,p2 = open_trans ens tl in
284 (if is_transitive p1 then fst (collect p1) else [l,p1]) @
285 (if is_transitive p2 then fst (collect p2) else [m,p2]),
287 | t -> assert false in
288 let rec cut_to_last_duplicate l acc =
291 | (l',p)::tl when l=l' ->
293 prerr_endline ("!!! RISPARMIO " ^ string_of_int (List.length acc) ^ " PASSI");
294 cut_to_last_duplicate l [l',p] tl
296 cut_to_last_duplicate l ((l',p)::acc) tl
300 (l,_)::_::_ as steps, ((r,uri_trans,ty) as last) ->
301 (match cut_to_last_duplicate l [] steps with
302 (l,p1)::((m,_)::_::_ as tl) ->
303 mk_trans uri_trans ty l m r p1 (rebuild (tl,last))
304 | [l,p1 ; m,p2] -> mk_trans uri_trans ty l m r p1 p2
306 | [] -> assert false)
309 if is_transitive t then
314 let rec remove_refl t =
316 | Cic.Appl (((Cic.Const(uri_trans,ens))::tl) as args)
317 when LibraryObjects.is_trans_eq_URI uri_trans ->
318 let ty,l,m,r,p1,p2 = open_trans ens tl in
320 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_],p2 ->
322 | p1,Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] ->
324 | _ -> Cic.Appl (List.map remove_refl args))
325 | Cic.Appl l -> Cic.Appl (List.map remove_refl l)
326 | Cic.LetIn (name,bo,rest) ->
327 Cic.LetIn (name,remove_refl bo,remove_refl rest)
330 let rec canonical_trough_lambda context = function
331 | Cic.Lambda(name,ty,bo) ->
332 let context' = (Some (name,Cic.Decl ty))::context in
333 Cic.Lambda(name,ty,canonical_trough_lambda context' bo)
334 | t -> canonical context t
336 and canonical context t =
338 | Cic.LetIn(name,bo,rest) ->
339 let bo = canonical_trough_lambda context bo in
340 let context' = (Some (name,Cic.Def (bo,None)))::context in
341 Cic.LetIn(name,bo,canonical context' rest)
342 | Cic.Appl (((Cic.Const(uri_sym,ens))::tl) as args)
343 when LibraryObjects.is_sym_eq_URI uri_sym ->
344 (match p_of_sym ens tl with
345 | Cic.Appl ((Cic.Const(uri,ens))::tl)
346 when LibraryObjects.is_sym_eq_URI uri ->
347 canonical context (p_of_sym ens tl)
348 | Cic.Appl ((Cic.Const(uri_trans,ens))::tl)
349 when LibraryObjects.is_trans_eq_URI uri_trans ->
350 let ty,l,m,r,p1,p2 = open_trans ens tl in
351 mk_trans uri_trans ty r m l
352 (canonical context (mk_sym uri_sym ty m r p2))
353 (canonical context (mk_sym uri_sym ty l m p1))
354 | Cic.Appl (([Cic.Const(uri_feq,ens);ty1;ty2;f;x;y;p]))
355 when LibraryObjects.is_eq_f_URI uri_feq ->
356 let eq = LibraryObjects.eq_URI_of_eq_f_URI uri_feq in
358 Cic.Const (LibraryObjects.eq_f_sym_URI ~eq, [])
360 let rc = Cic.Appl [eq_f_sym;ty1;ty2;f;x;y;p] in
361 prerr_endline ("CANONICAL " ^ CicPp.ppterm rc);
363 | Cic.Appl [Cic.MutConstruct (uri, 0, 1,_);_;_] as t
364 when LibraryObjects.is_eq_URI uri -> t
365 | _ -> Cic.Appl (List.map (canonical context) args))
366 | Cic.Appl l -> Cic.Appl (List.map (canonical context) l)
369 remove_cycles (remove_refl (canonical context t))
372 let compose_contexts ctx1 ctx2 =
373 ProofEngineReduction.replace_lifting
374 ~equality:(=) ~what:[Cic.Implicit(Some `Hole)] ~with_what:[ctx2] ~where:ctx1
377 let put_in_ctx ctx t =
378 ProofEngineReduction.replace_lifting
379 ~equality:(=) ~what:[Cic.Implicit (Some `Hole)] ~with_what:[t] ~where:ctx
382 let mk_eq uri ty l r =
383 let ens, args = build_ens uri [ty; l; r] in
384 Cic.Appl (Cic.MutInd(uri,0,ens) :: args)
387 let mk_refl uri ty t =
388 let ens, args = build_ens uri [ty; t] in
389 Cic.Appl (Cic.MutConstruct(uri,0,1,ens) :: args)
392 let open_eq = function
393 | Cic.Appl [Cic.MutInd(uri,0,[]);ty;l;r] when LibraryObjects.is_eq_URI uri ->
398 let mk_feq uri_feq ty ty1 left pred right t =
399 let ens, args = build_ens uri_feq [ty;ty1;pred;left;right;t] in
400 Cic.Appl (Cic.Const(uri_feq,ens) :: args)
403 let rec look_ahead aux = function
404 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl) as t
405 when LibraryObjects.is_eq_ind_URI uri_ind ||
406 LibraryObjects.is_eq_ind_r_URI uri_ind ->
407 let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
408 let ty2,eq,lp,rp = open_pred pred in
409 let hole = Cic.Implicit (Some `Hole) in
410 let ty2 = CicSubstitution.subst hole ty2 in
411 aux ty1 (CicSubstitution.subst other lp) (CicSubstitution.subst other rp) hole ty2 t
412 | Cic.Lambda (n,s,t) -> Cic.Lambda (n,s,look_ahead aux t)
416 let contextualize uri ty left right t =
417 let hole = Cic.Implicit (Some `Hole) in
418 (* aux [uri] [ty] [left] [right] [ctx] [ctx_ty] [t]
420 * the parameters validate this invariant
421 * t: eq(uri) ty left right
422 * that is used only by the base case
424 * ctx is a term with an hole. Cic.Implicit(Some `Hole) is the empty context
425 * ctx_ty is the type of ctx
427 let rec aux uri ty left right ctx_d ctx_ty t =
429 | Cic.Appl ((Cic.Const(uri_sym,ens))::tl)
430 when LibraryObjects.is_sym_eq_URI uri_sym ->
431 let ty,l,r,p = open_sym ens tl in
432 mk_sym uri_sym ty l r (aux uri ty l r ctx_d ctx_ty p)
433 | Cic.LetIn (name,body,rest) ->
434 Cic.LetIn (name,look_ahead (aux uri) body, aux uri ty left right ctx_d ctx_ty rest)
435 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
436 when LibraryObjects.is_eq_ind_URI uri_ind ||
437 LibraryObjects.is_eq_ind_r_URI uri_ind ->
438 let ty1,what,pred,p1,other,p2 = open_eq_ind tl in
439 let ty2,eq,lp,rp = open_pred pred in
440 let uri_trans = LibraryObjects.trans_eq_URI ~eq:uri in
441 let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in
442 let is_not_fixed_lp = is_not_fixed lp in
443 let avoid_eq_ind = LibraryObjects.is_eq_ind_URI uri_ind in
444 (* extract the context and the fixed term from the predicate *)
446 let m, ctx_c = if is_not_fixed_lp then rp,lp else lp,rp in
447 (* they were under a lambda *)
448 let m = CicSubstitution.subst hole m in
449 let ctx_c = CicSubstitution.subst hole ctx_c in
450 let ty2 = CicSubstitution.subst hole ty2 in
453 (* create the compound context and put the terms under it *)
454 let ctx_dc = compose_contexts ctx_d ctx_c in
455 let dc_what = put_in_ctx ctx_dc what in
456 let dc_other = put_in_ctx ctx_dc other in
457 (* m is already in ctx_c so it is put in ctx_d only *)
458 let d_m = put_in_ctx ctx_d m in
459 (* we also need what in ctx_c *)
460 let c_what = put_in_ctx ctx_c what in
461 (* now put the proofs in the compound context *)
462 let p1 = (* p1: dc_what = d_m *)
463 if is_not_fixed_lp then
464 aux uri ty2 c_what m ctx_d ctx_ty p1
466 mk_sym uri_sym ctx_ty d_m dc_what
467 (aux uri ty2 m c_what ctx_d ctx_ty p1)
469 let p2 = (* p2: dc_other = dc_what *)
471 mk_sym uri_sym ctx_ty dc_what dc_other
472 (aux uri ty1 what other ctx_dc ctx_ty p2)
474 aux uri ty1 other what ctx_dc ctx_ty p2
476 (* if pred = \x.C[x]=m --> t : C[other]=m --> trans other what m
477 if pred = \x.m=C[x] --> t : m=C[other] --> trans m what other *)
478 let a,b,c,paeqb,pbeqc =
479 if is_not_fixed_lp then
480 dc_other,dc_what,d_m,p2,p1
482 d_m,dc_what,dc_other,
483 (mk_sym uri_sym ctx_ty dc_what d_m p1),
484 (mk_sym uri_sym ctx_ty dc_other dc_what p2)
486 mk_trans uri_trans ctx_ty a b c paeqb pbeqc
487 | t when ctx_d = hole -> t
489 (* let uri_sym = LibraryObjects.sym_eq_URI ~eq:uri in *)
490 (* let uri_ind = LibraryObjects.eq_ind_URI ~eq:uri in *)
492 let uri_feq = LibraryObjects.eq_f_URI ~eq:uri in
494 (* let r = CicSubstitution.lift 1 (put_in_ctx ctx_d left) in *)
496 let ctx_d = CicSubstitution.lift 1 ctx_d in
497 put_in_ctx ctx_d (Cic.Rel 1)
499 (* let lty = CicSubstitution.lift 1 ctx_ty in *)
500 (* Cic.Lambda (Cic.Name "foo",ty,(mk_eq uri lty l r)) *)
501 Cic.Lambda (Cic.Name "foo",ty,l)
503 (* let d_left = put_in_ctx ctx_d left in *)
504 (* let d_right = put_in_ctx ctx_d right in *)
505 (* let refl_eq = mk_refl uri ctx_ty d_left in *)
506 (* mk_sym uri_sym ctx_ty d_right d_left *)
507 (* (mk_eq_ind uri_ind ty left pred refl_eq right t) *)
508 (mk_feq uri_feq ty ctx_ty left pred right t)
510 aux uri ty left right hole ty t
513 let contextualize_rewrites t ty =
514 let eq,ty,l,r = open_eq ty in
515 contextualize eq ty l r t
518 let add_subst subst =
520 | Exact t -> Exact (Subst.apply_subst subst t)
521 | Step (s,(rule, id1, (pos,id2), pred)) ->
522 Step (Subst.concat subst s,(rule, id1, (pos,id2), pred))
525 let build_proof_step eq lift subst p1 p2 pos l r pred =
526 let p1 = Subst.apply_subst_lift lift subst p1 in
527 let p2 = Subst.apply_subst_lift lift subst p2 in
528 let l = CicSubstitution.lift lift l in
529 let l = Subst.apply_subst_lift lift subst l in
530 let r = CicSubstitution.lift lift r in
531 let r = Subst.apply_subst_lift lift subst r in
532 let pred = CicSubstitution.lift lift pred in
533 let pred = Subst.apply_subst_lift lift subst pred in
536 | Cic.Lambda (_,ty,body) -> ty,body
540 if pos = Utils.Left then l,r else r,l
545 mk_eq_ind (LibraryObjects.eq_ind_URI ~eq) ty what pred p1 other p2
547 mk_eq_ind (LibraryObjects.eq_ind_r_URI ~eq) ty what pred p1 other p2
552 let parametrize_proof p l r =
553 let uniq l = HExtlib.list_uniq (List.sort (fun (i,_) (j,_) -> Pervasives.compare i j) l) in
554 let mot = CicUtil.metas_of_term_set in
555 let parameters = uniq (mot p @ mot l @ mot r) in
556 (* ?if they are under a lambda? *)
559 HExtlib.list_uniq (List.sort Pervasives.compare parameters)
562 (* resorts l such that *hopefully* dependencies can be inferred *)
563 let guess_dependency p l =
565 | Cic.Appl ((Cic.Const(uri_ind,ens))::tl)
566 when LibraryObjects.is_eq_ind_URI uri_ind ||
567 LibraryObjects.is_eq_ind_r_URI uri_ind ->
568 let ty,_,_,_,_,_ = open_eq_ind tl in
569 let metas = CicUtil.metas_of_term ty in
571 List.partition (fun (i,_) -> List.exists (fun (j,_) -> j=i) metas) l
576 let parameters = guess_dependency p parameters in
577 let what = List.map (fun (i,l) -> Cic.Meta (i,l)) parameters in
578 let with_what, lift_no =
579 List.fold_right (fun _ (acc,n) -> ((Cic.Rel n)::acc),n+1) what ([],1)
581 let p = CicSubstitution.lift (lift_no-1) p in
583 ProofEngineReduction.replace_lifting
584 ~equality:(fun t1 t2 ->
585 match t1,t2 with Cic.Meta (i,_),Cic.Meta(j,_) -> i=j | _ -> false)
586 ~what ~with_what ~where:p
588 let ty_of_m _ = Cic.Implicit (Some `Type) in
591 (fun (instance,p,n) m ->
594 (Cic.Name ("X"^string_of_int n),
595 CicSubstitution.lift (lift_no - n - 1) (ty_of_m m),
601 let instance = match args with | [x] -> x | _ -> Cic.Appl args in
605 let wfo bag goalproof proof id =
607 let p,_,_ = proof_of_id bag id in
609 | Exact _ -> if (List.mem id acc) then acc else id :: acc
610 | Step (_,(_,id1, (_,id2), _)) ->
611 let acc = if not (List.mem id1 acc) then aux acc id1 else acc in
612 let acc = if not (List.mem id2 acc) then aux acc id2 else acc in
618 | Step (_,(_,id1, (_,id2), _)) -> aux (aux [id] id1) id2
620 List.fold_left (fun acc (_,_,id,_,_) -> aux acc id) acc goalproof
623 let string_of_id (id_to_eq,_) names id =
624 if id = 0 then "" else
626 let (_,p,(t,l,r,_),m,_) = open_equality (Hashtbl.find id_to_eq id) in
629 Printf.sprintf "%d = %s: %s = %s [%s]" id
630 (CicPp.pp t names) (CicPp.pp l names) (CicPp.pp r names)
632 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
633 | Step (_,(step,id1, (dir,id2), p) ) ->
634 Printf.sprintf "%6d: %s %6d %6d %s =(%s) %s [%s]" id
635 (string_of_rule step)
636 id1 id2 (CicPp.pp l names) (CicPp.pp t names) (CicPp.pp r names)
637 (String.concat ", " (List.map (fun (i,_,_) -> string_of_int i) m))
640 Not_found -> assert false
642 let pp_proof bag names goalproof proof subst id initial_goal =
643 String.concat "\n" (List.map (string_of_id bag names) (wfo bag goalproof proof id)) ^
646 (fst (List.fold_right
647 (fun (r,pos,i,s,pred) (acc,g) ->
648 let _,_,left,right = open_eq g in
651 | Utils.Left -> CicReduction.head_beta_reduce (Cic.Appl[pred;right])
652 | Utils.Right -> CicReduction.head_beta_reduce (Cic.Appl[pred;left])
654 let ty = Subst.apply_subst s ty in
655 ("("^ string_of_rule r ^ " " ^ string_of_int i^") -> "
656 ^ CicPp.pp ty names) :: acc,ty) goalproof ([],initial_goal)))) ^
657 "\nand then subsumed by " ^ string_of_int id ^ " when " ^ Subst.ppsubst subst
663 let compare = Pervasives.compare
666 module M = Map.Make(OT)
668 let rec find_deps bag m i =
671 let p,_,_ = proof_of_id bag i in
673 | Exact _ -> M.add i [] m
674 | Step (_,(_,id1,(_,id2),_)) ->
675 let m = find_deps bag m id1 in
676 let m = find_deps bag m id2 in
677 (* without the uniq there is a stack overflow doing concatenation *)
678 let xxx = [id1;id2] @ M.find id1 m @ M.find id2 m in
679 let xxx = HExtlib.list_uniq (List.sort Pervasives.compare xxx) in
683 let topological_sort bag l =
684 (* build the partial order relation *)
685 let m = List.fold_left (fun m i -> find_deps bag m i) M.empty l in
686 let m = (* keep only deps inside l *)
689 M.add i (List.filter (fun x -> List.mem x l) (M.find i m)) m')
692 let m = M.map (fun x -> Some x) m in
694 let keys m = M.fold (fun i _ acc -> i::acc) m [] in
695 let split l m = List.filter (fun i -> M.find i m = Some []) l in
698 (fun k v -> if List.mem k l then None else
701 | Some ll -> Some (List.filter (fun i -> not (List.mem i l)) ll))
706 let ok = split keys m in
707 let m = purge ok m in
708 let res = ok @ res in
709 if ok = [] then res else aux m res
711 let rc = List.rev (aux m []) in
716 (* returns the list of ids that should be factorized *)
717 let get_duplicate_step_in_wfo bag l p =
718 let ol = List.rev l in
719 let h = Hashtbl.create 13 in
720 (* NOTE: here the n parameter is an approximation of the dependency
721 between equations. To do things seriously we should maintain a
722 dependency graph. This approximation is not perfect. *)
724 let p,_,_ = proof_of_id bag i in
729 let no = Hashtbl.find h i in
730 Hashtbl.replace h i (no+1);
732 with Not_found -> Hashtbl.add h i 1;true
734 let rec aux = function
736 | Step (_,(_,i1,(_,i2),_)) ->
737 let go_on_1 = add i1 in
738 let go_on_2 = add i2 in
739 if go_on_1 then aux (let p,_,_ = proof_of_id bag i1 in p);
740 if go_on_2 then aux (let p,_,_ = proof_of_id bag i2 in p)
744 (fun (_,_,id,_,_) -> aux (let p,_,_ = proof_of_id bag id in p))
746 (* now h is complete *)
747 let proofs = Hashtbl.fold (fun k count acc-> (k,count)::acc) h [] in
748 let proofs = List.filter (fun (_,c) -> c > 1) proofs in
749 let res = topological_sort bag (List.map (fun (i,_) -> i) proofs) in
753 let build_proof_term bag eq h lift proof =
754 let proof_of_id aux id =
755 let p,l,r = proof_of_id bag id in
756 try List.assoc id h,l,r with Not_found -> aux p, l, r
758 let rec aux = function
760 CicSubstitution.lift lift term
761 | Step (subst,(rule, id1, (pos,id2), pred)) ->
762 let p1,_,_ = proof_of_id aux id1 in
763 let p2,l,r = proof_of_id aux id2 in
766 | SuperpositionRight -> Cic.Name ("SupR" ^ Utils.string_of_pos pos)
767 | Demodulation -> Cic.Name ("DemEq"^ Utils.string_of_pos pos)
772 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
775 let p = build_proof_step eq lift subst p1 p2 pos l r pred in
776 (* let cond = (not (List.mem 302 (Utils.metas_of_term p)) || id1 = 8 || id1 = 132) in
778 prerr_endline ("ERROR " ^ string_of_int id1 ^ " " ^ string_of_int id2);
785 let build_goal_proof bag eq l initial ty se context menv =
786 let se = List.map (fun i -> Cic.Meta (i,[])) se in
787 let lets = get_duplicate_step_in_wfo bag l initial in
788 let letsno = List.length lets in
789 let lift_list l = List.map (fun (i,t) -> i,CicSubstitution.lift 1 t) l in
793 let p,l,r = proof_of_id bag id in
794 let cic = build_proof_term bag eq h n p in
795 let real_cic,instance =
796 parametrize_proof cic l r
798 let h = (id, instance)::lift_list h in
799 acc@[id,real_cic],n+1,h)
803 let rec aux se current_proof = function
804 | [] -> current_proof,se
805 | (rule,pos,id,subst,pred)::tl ->
806 let p,l,r = proof_of_id bag id in
807 let p = build_proof_term bag eq h letsno p in
808 let pos = if pos = Utils.Left then Utils.Right else Utils.Left in
811 | SuperpositionLeft -> Cic.Name ("SupL" ^ Utils.string_of_pos pos)
812 | Demodulation -> Cic.Name ("DemG"^ Utils.string_of_pos pos)
817 | Cic.Lambda (_,a,b) -> Cic.Lambda (varname,a,b)
821 build_proof_step eq letsno subst current_proof p pos l r pred
823 let proof,se = aux se proof tl in
824 Subst.apply_subst_lift letsno subst proof,
825 List.map (fun x -> Subst.apply_subst(*_lift letsno*) subst x) se
827 aux se (build_proof_term bag eq h letsno initial) l
830 let initial = proof in
832 (fun (id,cic) (n,p) ->
835 Cic.Name ("H"^string_of_int id),
837 lets (letsno-1,initial)
840 (contextualize_rewrites proof (CicSubstitution.lift letsno ty))
845 let refl_proof eq_uri ty term =
846 Cic.Appl [Cic.MutConstruct (eq_uri, 0, 1, []); ty; term]
849 let metas_of_proof bag p =
851 match LibraryObjects.eq_URI () with
855 (ProofEngineTypes.Fail
856 (lazy "No default equality defined when calling metas_of_proof"))
858 let p = build_proof_term bag eq [] 0 p in
859 Utils.metas_of_term p
862 let remove_local_context eq =
863 let w, p, (ty, left, right, o), menv,id = open_equality eq in
864 let p = Utils.remove_local_context p in
865 let ty = Utils.remove_local_context ty in
866 let left = Utils.remove_local_context left in
867 let right = Utils.remove_local_context right in
868 w, p, (ty, left, right, o), menv, id
871 let relocate newmeta menv to_be_relocated =
872 let subst, newmetasenv, newmeta =
874 (fun i (subst, metasenv, maxmeta) ->
875 let _,context,ty = CicUtil.lookup_meta i menv in
877 let newmeta = Cic.Meta(maxmeta,irl) in
878 let newsubst = Subst.buildsubst i context newmeta ty subst in
879 newsubst, (maxmeta,context,ty)::metasenv, maxmeta+1)
880 to_be_relocated (Subst.empty_subst, [], newmeta+1)
882 let menv = Subst.apply_subst_metasenv subst menv @ newmetasenv in
885 let fix_metas_goal newmeta goal =
886 let (proof, menv, ty) = goal in
887 let to_be_relocated =
888 HExtlib.list_uniq (List.sort Pervasives.compare (Utils.metas_of_term ty))
890 let subst, menv, newmeta = relocate newmeta menv to_be_relocated in
891 let ty = Subst.apply_subst subst ty in
894 | [] -> assert false (* is a nonsense to relocate the initial goal *)
895 | (r,pos,id,s,p) :: tl -> (r,pos,id,Subst.concat subst s,p) :: tl
897 newmeta+1,(proof, menv, ty)
900 let fix_metas bag newmeta eq =
901 let w, p, (ty, left, right, o), menv,_ = open_equality eq in
902 let to_be_relocated =
903 (* List.map (fun i ,_,_ -> i) menv *)
905 (List.sort Pervasives.compare
906 (Utils.metas_of_term left @ Utils.metas_of_term right @
907 Utils.metas_of_term ty))
909 let subst, metasenv, newmeta = relocate newmeta menv to_be_relocated in
910 let ty = Subst.apply_subst subst ty in
911 let left = Subst.apply_subst subst left in
912 let right = Subst.apply_subst subst right in
913 let fix_proof = function
914 | Exact p -> Exact (Subst.apply_subst subst p)
915 | Step (s,(r,id1,(pos,id2),pred)) ->
916 Step (Subst.concat s subst,(r,id1,(pos,id2), pred))
918 let p = fix_proof p in
919 let eq' = mk_equality bag (w, p, (ty, left, right, o), metasenv) in
922 exception NotMetaConvertible;;
924 let meta_convertibility_aux table t1 t2 =
925 let module C = Cic in
926 let rec aux ((table_l,table_r) as table) t1 t2 =
928 | C.Meta (m1, tl1), C.Meta (m2, tl2) when m1 = m2 -> table
929 | C.Meta (m1, tl1), C.Meta (m2, tl2) when m1 < m2 -> aux table t2 t1
930 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
931 let m1_binding, table_l =
932 try List.assoc m1 table_l, table_l
933 with Not_found -> m2, (m1, m2)::table_l
934 and m2_binding, table_r =
935 try List.assoc m2 table_r, table_r
936 with Not_found -> m1, (m2, m1)::table_r
938 if (m1_binding <> m2) || (m2_binding <> m1) then
939 raise NotMetaConvertible
941 | C.Var (u1, ens1), C.Var (u2, ens2)
942 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
943 aux_ens table ens1 ens2
944 | C.Cast (s1, t1), C.Cast (s2, t2)
945 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
946 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
947 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
948 let table = aux table s1 s2 in
950 | C.Appl l1, C.Appl l2 -> (
951 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
952 with Invalid_argument _ -> raise NotMetaConvertible
954 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
955 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
956 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
957 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
958 aux_ens table ens1 ens2
959 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
960 when (UriManager.eq u1 u2) && i1 = i2 ->
961 let table = aux table s1 s2 in
962 let table = aux table t1 t2 in (
963 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
964 with Invalid_argument _ -> raise NotMetaConvertible
966 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
969 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
970 if i1 <> i2 then raise NotMetaConvertible
972 let res = (aux res s1 s2) in aux res t1 t2)
974 with Invalid_argument _ -> raise NotMetaConvertible
976 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
979 (fun res (n1, s1, t1) (n2, s2, t2) ->
980 let res = aux res s1 s2 in aux res t1 t2)
982 with Invalid_argument _ -> raise NotMetaConvertible
984 | t1, t2 when t1 = t2 -> table
985 | _, _ -> raise NotMetaConvertible
987 and aux_ens table ens1 ens2 =
988 let cmp (u1, t1) (u2, t2) =
989 Pervasives.compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
991 let ens1 = List.sort cmp ens1
992 and ens2 = List.sort cmp ens2 in
995 (fun res (u1, t1) (u2, t2) ->
996 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
999 with Invalid_argument _ -> raise NotMetaConvertible
1005 let meta_convertibility_eq eq1 eq2 =
1006 let _, _, (ty, left, right, _), _,_ = open_equality eq1 in
1007 let _, _, (ty', left', right', _), _,_ = open_equality eq2 in
1010 else if (left = left') && (right = right') then
1012 else if (left = right') && (right = left') then
1016 let table = meta_convertibility_aux ([],[]) left left' in
1017 let _ = meta_convertibility_aux table right right' in
1019 with NotMetaConvertible ->
1021 let table = meta_convertibility_aux ([],[]) left right' in
1022 let _ = meta_convertibility_aux table right left' in
1024 with NotMetaConvertible ->
1029 let meta_convertibility t1 t2 =
1034 ignore(meta_convertibility_aux ([],[]) t1 t2);
1036 with NotMetaConvertible ->
1040 exception TermIsNotAnEquality;;
1042 let term_is_equality term =
1044 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
1045 when LibraryObjects.is_eq_URI uri -> true
1049 let equality_of_term bag proof term =
1051 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2]
1052 when LibraryObjects.is_eq_URI uri ->
1053 let o = !Utils.compare_terms t1 t2 in
1054 let stat = (ty,t1,t2,o) in
1055 let w = Utils.compute_equality_weight stat in
1056 let e = mk_equality bag (w, Exact proof, stat,[]) in
1059 raise TermIsNotAnEquality
1062 let is_weak_identity eq =
1063 let _,_,(_,left, right,_),_,_ = open_equality eq in
1065 (* doing metaconv here is meaningless *)
1068 let is_identity (_, context, ugraph) eq =
1069 let _,_,(ty,left,right,_),menv,_ = open_equality eq in
1070 (* doing metaconv here is meaningless *)
1072 (* fst (CicReduction.are_convertible ~metasenv:menv context left right ugraph)
1077 let term_of_equality eq_uri equality =
1078 let _, _, (ty, left, right, _), menv, _= open_equality equality in
1079 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1080 let argsno = List.length menv in
1082 CicSubstitution.lift argsno
1083 (Cic.Appl [Cic.MutInd (eq_uri, 0, []); ty; left; right])
1087 (fun (i,_,ty) (n, t) ->
1088 let name = Cic.Name ("X" ^ (string_of_int n)) in
1089 let ty = CicSubstitution.lift (n-1) ty in
1091 ProofEngineReduction.replace
1092 ~equality:eq ~what:[i]
1093 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1095 (n-1, Cic.Prod (name, ty, t)))
1099 let symmetric bag eq_ty l id uri m =
1100 let eq = Cic.MutInd(uri,0,[]) in
1102 Cic.Lambda (Cic.Name "Sym",eq_ty,
1103 Cic.Appl [CicSubstitution.lift 1 eq ;
1104 CicSubstitution.lift 1 eq_ty;
1105 Cic.Rel 1;CicSubstitution.lift 1 l])
1109 [Cic.MutConstruct(uri,0,1,[]);eq_ty;l])
1112 let eq = mk_equality bag (0,prefl,(eq_ty,l,l,Utils.Eq),m) in
1113 let (_,_,_,_,id) = open_equality eq in
1116 Step(Subst.empty_subst,
1117 (Demodulation,id1,(Utils.Left,id),pred))
1120 module IntOT = struct
1122 let compare = Pervasives.compare
1125 module IntSet = Set.Make(IntOT);;
1127 let n_purged = ref 0;;
1129 let collect ((id_to_eq,_) as bag) alive1 alive2 alive3 =
1130 (* let _ = <:start<collect>> in *)
1132 let p,_,_ = proof_of_id bag id in
1134 | Exact _ -> IntSet.empty
1135 | Step (_,(_,id1,(_,id2),_)) ->
1136 IntSet.add id1 (IntSet.add id2 IntSet.empty)
1139 let news = IntSet.fold (fun id s -> IntSet.union (deps_of id) s) s s in
1140 if IntSet.equal news s then s else close news
1142 let l_to_s s l = List.fold_left (fun s x -> IntSet.add x s) s l in
1143 let alive_set = l_to_s (l_to_s (l_to_s IntSet.empty alive2) alive1) alive3 in
1144 let closed_alive_set = close alive_set in
1148 if not (IntSet.mem k closed_alive_set) then
1149 k::s else s) id_to_eq []
1151 n_purged := !n_purged + List.length to_purge;
1152 List.iter (Hashtbl.remove id_to_eq) to_purge;
1153 (* let _ = <:stop<collect>> in () *)
1157 let _,_,_,_,id = open_equality e in id
1160 let get_stats () = ""
1162 <:show<Equality.>> ^
1163 "# of purged eq by the collector: " ^ string_of_int !n_purged ^ "\n"
1167 let rec pp_proofterm name t context =
1168 let rec skip_lambda tys ctx = function
1169 | Cic.Lambda (n,s,t) -> skip_lambda (s::tys) ((Some n)::ctx) t
1174 | Cic.Name s1 -> Cic.Name (s ^ s1)
1177 let rec skip_letin ctx = function
1178 | Cic.LetIn (n,b,t) ->
1179 pp_proofterm (Some (rename "Lemma " n)) b ctx::
1180 skip_letin ((Some n)::ctx) t
1182 let ppterm t = CicPp.pp t ctx in
1183 let rec pp inner = function
1184 | Cic.Appl [Cic.Const (uri,[]);_;l;m;r;p1;p2]
1185 when Pcre.pmatch ~pat:"trans_eq" (UriManager.string_of_uri uri)->
1187 (" " ^ ppterm l) :: pp true p1 @
1188 [ " = " ^ ppterm m ] @ pp true p2 @
1189 [ " = " ^ ppterm r ]
1192 [ " = " ^ ppterm m ] @ pp true p2
1193 | Cic.Appl [Cic.Const (uri,[]);_;l;m;p]
1194 when Pcre.pmatch ~pat:"sym_eq" (UriManager.string_of_uri uri)->
1196 | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
1197 when Pcre.pmatch ~pat:"eq_f" (UriManager.string_of_uri uri)->
1199 | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
1200 when Pcre.pmatch ~pat:"eq_f1" (UriManager.string_of_uri uri)->
1202 | Cic.Appl [Cic.MutConstruct (uri,_,_,[]);_;_;t;p]
1203 when Pcre.pmatch ~pat:"ex.ind" (UriManager.string_of_uri uri)->
1204 [ "witness " ^ ppterm t ] @ pp true p
1205 | Cic.Appl (t::_) ->[ " [by " ^ ppterm t ^ "]"]
1206 | t ->[ " [by " ^ ppterm t ^ "]"]
1208 let rec compat = function
1209 | a::b::tl -> (b ^ a) :: compat tl
1213 let compat l = List.hd l :: compat (List.tl l) in
1214 compat (pp false t) @ ["";""]
1216 let names, tys, body = skip_lambda [] context t in
1217 let ppname name = (match name with Some (Cic.Name s) -> s | _ -> "") in
1218 ppname name ^ ":\n" ^
1219 (if context = [] then
1220 let rec pp_l ctx = function
1222 " " ^ ppname name ^ ": " ^ CicPp.pp t ctx ^ "\n" ^
1226 pp_l [] (List.rev (List.combine tys names))
1229 String.concat "\n" (skip_letin names body)
1232 let pp_proofterm t =
1234 pp_proofterm (Some (Cic.Name "Hypothesis")) t []