1 (* Copyright (C) 2002, HELM Team.
4 * This file is part of HELM, an Hypertextual, Electronic
5 * Library of Mathematics, developed at the Computer Science
6 * Department, University of Bologna, Italy.
8 * HELM is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * HELM is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with HELM; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
32 if debug then prerr_endline (Lazy.force s);;
35 let mk_irl ctx = CicMkImplicit.identity_relocation_list_for_metavariable ctx;;
36 let ugraph = CicUniv.oblivion_ugraph;;
37 let typeof = CicTypeChecker.type_of_aux';;
39 let names = List.map (function None -> None | Some (x,_) -> Some x) ctx in
42 let is_propositional context sort =
43 match CicReduction.whd context sort with
45 | Cic.Sort (Cic.CProp _) -> true
48 let is_in_prop context subst metasenv ty =
49 let sort,u = typeof ~subst metasenv context ty CicUniv.oblivion_ugraph in
50 is_propositional context sort
53 exception NotConvertible;;
55 let check_proof_is_valid proof metasenv context goalty =
59 let ty,u = typeof metasenv context proof CicUniv.oblivion_ugraph in
60 let b,_ = CicReduction.are_convertible context ty goalty u in
61 if not b then raise NotConvertible else b
64 List.map (function None -> None | Some (x,_) -> Some x) context
66 debug_print (lazy ("PROOF:" ^ CicPp.pp proof names));
67 (* debug_print (lazy ("PROOFTY:" ^ CicPp.pp ty names)); *)
68 debug_print (lazy ("GOAL:" ^ CicPp.pp goalty names));
69 debug_print (lazy ("MENV:" ^ CicMetaSubst.ppmetasenv [] metasenv));
75 let assert_proof_is_valid proof metasenv context goalty =
76 assert (check_proof_is_valid proof metasenv context goalty)
79 let assert_subst_are_disjoint subst subst' =
82 (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst')
87 let split_goals_in_prop metasenv subst gl =
90 let _,context,ty = CicUtil.lookup_meta g metasenv in
92 let sort,u = typeof ~subst metasenv context ty ugraph in
93 is_propositional context sort
95 | CicTypeChecker.AssertFailure s
96 | CicTypeChecker.TypeCheckerFailure s ->
98 (lazy ("NON TIPA" ^ ppterm context (CicMetaSubst.apply_subst subst ty)));
101 (* FIXME... they should type! *)
105 let split_goals_with_metas metasenv subst gl =
108 let _,context,ty = CicUtil.lookup_meta g metasenv in
109 let ty = CicMetaSubst.apply_subst subst ty in
110 CicUtil.is_meta_closed ty)
114 let order_new_goals metasenv subst open_goals ppterm =
115 let prop,rest = split_goals_in_prop metasenv subst open_goals in
116 let closed_prop, open_prop = split_goals_with_metas metasenv subst prop in
117 let closed_type, open_type = split_goals_with_metas metasenv subst rest in
119 (List.map (fun x -> x,P) (open_prop @ closed_prop))
121 (List.map (fun x -> x,T) (open_type @ closed_type))
126 let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty,sort) open_goals
128 debug_print (lazy (" OPEN: "^
132 | (i,t,P) -> string_of_int i ^ ":"^ppterm t^ "Prop"
133 | (i,t,T) -> string_of_int i ^ ":"^ppterm t^ "Type")
138 let is_an_equational_goal = function
139 | Cic.Appl [Cic.MutInd(u,_,_);_;_;_] when LibraryObjects.is_eq_URI u -> true
143 type auto_params = Cic.term list * (string * string) list
145 let elems = ref [] ;;
147 (* closing a term w.r.t. its metavariables
148 very naif version: it does not take dependencies properly into account *)
150 let naif_closure ?(prefix_name="xxx_") t metasenv context =
151 let in_term t (i,_,_) =
152 List.exists (fun (j,_) -> j=i) (CicUtil.metas_of_term t)
154 let metasenv = List.filter (in_term t) metasenv in
155 let metasenv = ProofEngineHelpers.sort_metasenv metasenv in
156 let n = List.length metasenv in
157 let what = List.map (fun (i,cc,ty) -> Cic.Meta(i,[])) metasenv in
160 (fun (i,acc) (_,cc,ty) -> (i-1,Cic.Rel i::acc))
163 let t = CicSubstitution.lift n t in
165 ProofEngineReduction.replace_lifting
166 ~equality:(fun c t1 t2 ->
168 | Cic.Meta(i,_),Cic.Meta(j,_) -> i = j
170 ~context ~what ~with_what ~where:t
174 (fun (n,t) (_,cc,ty) ->
175 n-1, Cic.Lambda(Cic.Name (prefix_name^string_of_int n),
176 CicSubstitution.lift n ty,t))
179 t, List.length metasenv
182 let lambda_close ?prefix_name t menv ctx =
183 let t, num_lambdas = naif_closure ?prefix_name t menv ctx in
185 (fun (t,i) -> function
186 | None -> CicSubstitution.subst (Cic.Implicit None) t,i (* delift *)
187 | Some (name, Cic.Decl ty) -> Cic.Lambda (name, ty, t),i+1
188 | Some (name, Cic.Def (bo, ty)) -> Cic.LetIn (name, bo, ty, t),i+1)
192 (* functions for retrieving theorems *)
194 exception FillingFailure of AutoCache.cache * int
196 let rec unfold context = function
197 | Cic.Prod(name,s,t) ->
198 let t' = unfold ((Some (name,Cic.Decl s))::context) t in
200 | t -> ProofEngineReduction.unfold context t
202 let find_library_theorems dbd proof goal =
203 let univ = MetadataQuery.universe_of_goal ~dbd false proof goal in
204 let terms = List.map CicUtil.term_of_uri univ in
207 (t,fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph)))
210 let find_context_theorems context metasenv =
213 (fun (res,i) ctxentry ->
215 | Some (_,Cic.Decl t) ->
216 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
217 | Some (_,Cic.Def (_,t)) ->
218 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
223 let rec is_an_equality = function
224 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
225 when (LibraryObjects.is_eq_URI uri) -> true
226 | Cic.Prod (_, _, t) -> is_an_equality t
230 let partition_equalities =
231 List.partition (fun (_,ty) -> is_an_equality ty)
234 let default_auto maxm _ _ cache _ _ _ _ = [],cache,maxm ;;
236 (* giusto per provare che succede
237 let is_unit_equation context metasenv oldnewmeta term =
238 let head, metasenv, args, newmeta =
239 TermUtil.saturate_term oldnewmeta metasenv context term 0
242 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
244 Some (args,metasenv,newmetas,head,newmeta) *)
246 let is_unit_equation context metasenv oldnewmeta term =
247 let head, metasenv, args, newmeta =
248 TermUtil.saturate_term oldnewmeta metasenv context term 0
250 let propositional_args =
254 let _,_,mt = CicUtil.lookup_meta i metasenv in
256 CicTypeChecker.type_of_aux' metasenv context mt
257 CicUniv.oblivion_ugraph
259 if is_propositional context sort then Some i else None
263 if propositional_args = [] then
265 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
267 Some (args,metasenv,newmetas,head,newmeta)
271 let get_candidates skip_trie_filtering universe cache t =
272 let t = if skip_trie_filtering then Cic.Meta(0,[]) else t in
274 (Universe.get_candidates universe t)@(AutoCache.get_candidates cache t)
277 (lazy ("candidates for " ^ (CicPp.ppterm t) ^ " = " ^
278 (String.concat "\n" (List.map CicPp.ppterm candidates)))) in
279 debug_print debug_msg;
283 let only signature context metasenv t =
286 CicTypeChecker.type_of_aux' metasenv context t CicUniv.oblivion_ugraph
288 let consts = MetadataConstraints.constants_of ty in
289 let b = MetadataConstraints.UriManagerSet.subset consts signature in
290 (* if b then (prerr_endline ("keeping " ^ (CicPp.ppterm t)); b) *)
293 let ty' = unfold context ty in
294 let consts' = MetadataConstraints.constants_of ty' in
295 let b = MetadataConstraints.UriManagerSet.subset consts' signature in
297 if not b then prerr_endline ("filtering " ^ (CicPp.ppterm t))
298 else prerr_endline ("keeping " ^ (CicPp.ppterm t));
302 | CicTypeChecker.TypeCheckerFailure _ -> assert false
303 | ProofEngineTypes.Fail _ -> false (* unfold may fail *)
306 let not_default_eq_term t =
308 let uri = CicUtil.uri_of_term t in
309 not (LibraryObjects.in_eq_URIs uri)
310 with Invalid_argument _ -> true
312 let retrieve_equations dont_filter signature universe cache context metasenv =
313 match LibraryObjects.eq_URI() with
316 let eq_uri = UriManager.strip_xpointer eq_uri in
317 let fake= Cic.Meta(-1,[]) in
318 let fake_eq = Cic.Appl [Cic.MutInd (eq_uri,0, []);fake;fake;fake] in
319 let candidates = get_candidates false universe cache fake_eq in
320 if dont_filter then candidates
321 else let eq_uri = UriManager.uri_of_uriref eq_uri 0 None in
322 (* let candidates = List.filter not_default_eq_term candidates in *)
324 (only (MetadataConstraints.UriManagerSet.add eq_uri signature)
325 context metasenv) candidates
327 let build_equality bag head args proof newmetas maxmeta =
329 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] ->
331 if args = [] then proof else Cic.Appl (proof::args)
333 let o = !Utils.compare_terms t1 t2 in
334 let stat = (ty,t1,t2,o) in
335 (* let w = compute_equality_weight stat in *)
337 let proof = Equality.Exact p in
338 let e = Equality.mk_equality bag (w, proof, stat, newmetas) in
339 (* to clean the local context of metas *)
340 Equality.fix_metas bag maxmeta e
344 let partition_unit_equalities context metasenv newmeta bag equations =
346 (fun (units,other,maxmeta)(t,ty) ->
347 if not (CicUtil.is_meta_closed t && CicUtil.is_meta_closed ty) then
350 ("Skipping " ^ CicMetaSubst.ppterm_in_context ~metasenv [] t context
351 ^ " since it is not meta closed")
353 units,(t,ty)::other,maxmeta
355 match is_unit_equation context metasenv maxmeta ty with
356 | Some (args,metasenv,newmetas,head,newmeta') ->
357 let maxmeta,equality =
358 build_equality bag head args t newmetas newmeta' in
359 equality::units,other,maxmeta
361 units,(t,ty)::other,maxmeta)
362 ([],[],newmeta) equations
365 (Saturation.make_active [],
366 Saturation.make_passive [],
367 Equality.mk_equality_bag)
370 let init_cache_and_tables
371 ?dbd use_library paramod use_context dont_filter universe (proof, goal)
373 (* the local cache in initially empty *)
374 let cache = AutoCache.cache_empty in
375 let _, metasenv, _subst,_, _, _ = proof in
376 let signature = MetadataQuery.signature_of metasenv goal in
377 let newmeta = CicMkImplicit.new_meta metasenv [] in
378 let _,context,_ = CicUtil.lookup_meta goal metasenv in
379 let ct = if use_context then find_context_theorems context metasenv else [] in
381 (lazy ("ho trovato nel contesto " ^ (string_of_int (List.length ct))));
383 match use_library, dbd with
384 | true, Some dbd -> find_library_theorems dbd metasenv goal
388 (lazy ("ho trovato nella libreria " ^ (string_of_int (List.length lt))));
389 let cache = cache_add_list cache context (ct@lt) in
391 retrieve_equations dont_filter (* true *) signature universe cache context metasenv
394 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
399 CicTypeChecker.type_of_aux'
400 metasenv context t CicUniv.oblivion_ugraph
402 (* retrieve_equations could also return flexible terms *)
403 if is_an_equality ty then Some(t,ty)
406 let ty' = unfold context ty in
407 if is_an_equality ty' then Some(t,ty') else None
408 with ProofEngineTypes.Fail _ -> None)
411 let bag = Equality.mk_equality_bag () in
412 let units, other_equalities, newmeta =
413 partition_unit_equalities context metasenv newmeta bag eqs_and_types
415 (* SIMPLIFICATION STEP
417 let env = (metasenv, context, CicUniv.oblivion_ugraph) in
418 let eq_uri = HExtlib.unopt (LibraryObjects.eq_URI()) in
419 Saturation.simplify_equalities bag eq_uri env units
422 let passive = Saturation.make_passive units in
423 let no = List.length units in
424 let active = Saturation.make_active [] in
425 let active,passive,newmeta =
426 if paramod then active,passive,newmeta
428 Saturation.pump_actives
429 context bag newmeta active passive (no+1) infinity
431 (active,passive,bag),cache,newmeta
433 let fill_hypothesis context metasenv oldnewmeta term tables (universe:Universe.universe) cache auto fast =
434 let head, metasenv, args, newmeta =
435 TermUtil.saturate_term oldnewmeta metasenv context term 0
437 let propositional_args =
441 let _,_,mt = CicUtil.lookup_meta i metasenv in
443 CicTypeChecker.type_of_aux' metasenv context mt
444 CicUniv.oblivion_ugraph
446 if is_propositional context sort then Some i else None
450 let results,cache,newmeta =
451 if propositional_args = [] then
452 let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
453 [args,metasenv,newmetas,head,newmeta],cache,newmeta
457 None,metasenv,term,term (* term non e' significativo *)
461 {AutoTypes.default_flags() with
462 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
463 maxwidth = 2;maxdepth = 2;
464 use_paramod=true;use_only_paramod=false}
466 {AutoTypes.default_flags() with
467 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
468 maxwidth = 2;maxdepth = 4;
469 use_paramod=true;use_only_paramod=false}
471 match auto newmeta tables universe cache context metasenv propositional_args flags with
472 | [],cache,newmeta -> raise (FillingFailure (cache,newmeta))
473 | substs,cache,newmeta ->
477 CicMetaSubst.apply_subst_metasenv subst metasenv
479 let head = CicMetaSubst.apply_subst subst head in
481 List.filter (fun (i,_,_) ->i >= oldnewmeta) metasenv
483 let args = List.map (CicMetaSubst.apply_subst subst) args in
484 let newm = CicMkImplicit.new_meta metasenv subst in
485 args,metasenv,newmetas,head,max newm newmeta)
486 substs, cache, newmeta
488 results,cache,newmeta
490 let build_equalities auto context metasenv tables universe cache newmeta equations =
492 (fun (facts,cache,newmeta) (t,ty) ->
493 (* in any case we add the equation to the cache *)
494 let cache = AutoCache.cache_add_list cache context [(t,ty)] in
496 let saturated,cache,newmeta =
497 fill_hypothesis context metasenv newmeta ty tables universe cache auto true
499 let (active,passive,bag) = tables in
500 let eqs,bag,newmeta =
502 (fun (acc,bag,newmeta) (args,metasenv,newmetas,head,newmeta') ->
503 let maxmeta,equality =
504 build_equality bag head args t newmetas newmeta'
506 equality::acc,bag,maxmeta)
507 ([],bag,newmeta) saturated
509 (eqs@facts, cache, newmeta)
510 with FillingFailure (cache,newmeta) ->
511 (* if filling hypothesis fails we add the equation to
513 (facts,cache,newmeta)
515 ([],cache,newmeta) equations
517 let close_more tables maxmeta context status auto universe cache =
518 let (active,passive,bag) = tables in
519 let proof, goalno = status in
520 let _, metasenv,_subst,_,_, _ = proof in
521 let signature = MetadataQuery.signature_of metasenv goalno in
523 retrieve_equations false signature universe cache context metasenv
529 CicTypeChecker.type_of_aux' metasenv context t
530 CicUniv.oblivion_ugraph in
531 (* retrieve_equations could also return flexible terms *)
532 if is_an_equality ty then Some(t,ty) else None)
534 let units, cache, maxm =
535 build_equalities auto context metasenv tables universe cache maxmeta eqs_and_types in
536 debug_print (lazy (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
537 string_of_int maxm));
539 (fun e -> debug_print (lazy (Equality.string_of_equality e)))
541 debug_print (lazy ">>>>>>>>>>>>>>>>>>>>>>");
542 let passive = Saturation.add_to_passive units passive in
543 let no = List.length units in
544 debug_print (lazy ("No = " ^ (string_of_int no)));
545 let active,passive,newmeta =
546 Saturation.pump_actives context bag maxm active passive (no+1) infinity
548 (active,passive,bag),cache,newmeta
550 let find_context_equalities
551 maxmeta bag context proof (universe:Universe.universe) cache
553 let module C = Cic in
554 let module S = CicSubstitution in
555 let module T = CicTypeChecker in
556 let _,metasenv,_subst,_,_, _ = proof in
557 let newmeta = max (ProofEngineHelpers.new_meta_of_proof ~proof) maxmeta in
558 (* if use_auto is true, we try to close the hypothesis of equational
559 statements using auto; a naif, and probably wrong approach *)
560 let rec aux cache index newmeta = function
561 | [] -> [], newmeta,cache
562 | (Some (_, C.Decl (term)))::tl ->
565 (Printf.sprintf "Examining: %d (%s)" index (CicPp.ppterm term)));
566 let do_find context term =
568 | C.Prod (name, s, t) when is_an_equality t ->
571 let term = S.lift index term in
572 let saturated,cache,newmeta =
573 fill_hypothesis context metasenv newmeta term
574 empty_tables universe cache default_auto false
578 (fun (acc,newmeta) (args,metasenv,newmetas,head,newmeta') ->
579 let newmeta, equality =
581 bag head args (Cic.Rel index) newmetas (max newmeta newmeta')
583 equality::acc, newmeta + 1)
584 ([],newmeta) saturated
587 with FillingFailure (cache,newmeta) ->
589 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
590 when LibraryObjects.is_eq_URI uri ->
591 let term = S.lift index term in
593 build_equality bag term [] (Cic.Rel index) [] newmeta
595 [e], (newmeta+1),cache
596 | _ -> [], newmeta, cache
598 let eqs, newmeta, cache = do_find context term in
599 let rest, newmeta,cache = aux cache (index+1) newmeta tl in
600 List.map (fun x -> index,x) eqs @ rest, newmeta, cache
602 aux cache (index+1) newmeta tl
604 let il, maxm, cache =
605 aux cache 1 newmeta context
607 let indexes, equalities = List.split il in
608 indexes, equalities, maxm, cache
611 (********** PARAMETERS PASSING ***************)
613 let bool params name default =
615 let s = List.assoc name params in
616 if s = "" || s = "1" || s = "true" || s = "yes" || s = "on" then true
617 else if s = "0" || s = "false" || s = "no" || s= "off" then false
619 let msg = "Unrecognized value for parameter "^name^"\n" in
620 let msg = msg^"Accepted values are 1,true,yes,on and 0,false,no,off" in
621 raise (ProofEngineTypes.Fail (lazy msg))
622 with Not_found -> default
625 let string params name default =
626 try List.assoc name params with
627 | Not_found -> default
630 let int params name default =
631 try int_of_string (List.assoc name params) with
632 | Not_found -> default
634 raise (ProofEngineTypes.Fail (lazy (name ^ " must be an integer")))
637 let flags_of_params params ?(for_applyS=false) () =
638 let int = int params in
639 let bool = bool params in
640 let close_more = bool "close_more" false in
641 let use_paramod = bool "use_paramod" true in
642 let skip_trie_filtering = bool "skip_trie_filtering" false in
643 let skip_context = bool "skip_context" false in
644 let use_only_paramod =
645 if for_applyS then true else bool "paramodulation" false in
646 let use_library = bool "library"
647 ((AutoTypes.default_flags()).AutoTypes.use_library) in
648 let depth = int "depth" ((AutoTypes.default_flags()).AutoTypes.maxdepth) in
649 let width = int "width" ((AutoTypes.default_flags()).AutoTypes.maxwidth) in
650 let size = int "size" ((AutoTypes.default_flags()).AutoTypes.maxsize) in
651 let gsize = int "gsize" ((AutoTypes.default_flags()).AutoTypes.maxgoalsizefactor) in
652 let do_type = bool "type" false in
653 let timeout = int "timeout" 0 in
654 { AutoTypes.maxdepth =
655 if use_only_paramod then 2 else depth;
656 AutoTypes.maxwidth = width;
657 AutoTypes.maxsize = size;
660 if for_applyS then Unix.gettimeofday () +. 30.0
664 Unix.gettimeofday() +. (float_of_int timeout);
665 AutoTypes.use_library = use_library;
666 AutoTypes.use_paramod = use_paramod;
667 AutoTypes.use_only_paramod = use_only_paramod;
668 AutoTypes.close_more = close_more;
669 AutoTypes.dont_cache_failures = false;
670 AutoTypes.maxgoalsizefactor = gsize;
671 AutoTypes.do_types = do_type;
672 AutoTypes.skip_trie_filtering = skip_trie_filtering;
673 AutoTypes.skip_context = skip_context;
676 let universe_of_params metasenv context universe tl =
677 if tl = [] then universe else
681 fst (CicTypeChecker.type_of_aux' metasenv context term
682 CicUniv.oblivion_ugraph))
685 Universe.index_list Universe.empty context (List.combine tl tys)
689 (***************** applyS *******************)
691 let new_metasenv_and_unify_and_t
692 dbd flags universe proof goal ?tables newmeta' metasenv'
693 context term' ty termty goal_arity
695 let ppterm = ppterm context in
696 let (consthead,newmetasenv,arguments,_) =
697 TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in
699 match arguments with [] -> term' | _ -> Cic.Appl (term'::arguments)
701 let proof',oldmetasenv =
702 let (puri,metasenv,_subst,pbo,pty, attrs) = proof in
703 (puri,newmetasenv,_subst,pbo,pty, attrs),metasenv
705 let goal_for_paramod =
706 match LibraryObjects.eq_URI () with
708 Cic.Appl [Cic.MutInd (uri,0,[]); Cic.Sort Cic.Prop; consthead; ty]
709 | None -> raise (ProofEngineTypes.Fail (lazy "No equality defined"))
711 let newmeta = CicMkImplicit.new_meta newmetasenv (*subst*) [] in
712 let metasenv_for_paramod = (newmeta,context,goal_for_paramod)::newmetasenv in
714 let uri,_,_subst,p,ty, attrs = proof' in
715 uri,metasenv_for_paramod,_subst,p,ty, attrs
717 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
719 ProofEngineTypes.apply_tactic
720 (EqualityTactics.rewrite_tac ~direction:`RightToLeft
721 ~pattern:(ProofEngineTypes.conclusion_pattern None)
722 (Cic.Meta(newmeta,irl)) [])
725 let goal = match goals with [g] -> g | _ -> assert false in
727 ProofEngineTypes.apply_tactic
728 (PrimitiveTactics.apply_tac term'')
731 let (active, passive,bag), cache, maxm =
732 init_cache_and_tables ~dbd flags.use_library false (* was true *)
737 Saturation.given_clause bag maxm (proof'''',newmeta) active passive
741 raise (ProofEngineTypes.Fail (lazy ("FIXME: propaga le tabelle")))
742 | Some (_,proof''''',_), active,passive,_ ->
744 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
745 ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m), active, passive
749 ("SUBSUMPTION SU: " ^ string_of_int newmeta ^ " " ^ ppterm goal_for_paramod));
751 Saturation.all_subsumed bag maxm (proof'''',newmeta) active passive
754 raise (ProofEngineTypes.Fail (lazy("BUM")))
755 else let (_,proof''''',_) = List.hd res in
756 proof''''',ProofEngineHelpers.compare_metasenvs ~oldmetasenv
757 ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m), active, passive
761 let rec count_prods context ty =
762 match CicReduction.whd context ty with
763 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
767 ~dbd ~term ~subst ~universe ?tables ~params:(univ,params) (proof, goal)
769 let module T = CicTypeChecker in
770 let module R = CicReduction in
771 let module C = Cic in
772 let (_,metasenv,_subst,_,_, _) = proof in
773 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
774 let flags = flags_of_params params ~for_applyS:true () in
775 let universe = universe_of_params metasenv context universe univ in
776 let newmeta = CicMkImplicit.new_meta metasenv subst in
777 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
779 C.Var (uri,exp_named_subst) ->
780 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
781 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
784 exp_named_subst_diff,newmeta',newmetasenvfragment,
785 C.Var (uri,exp_named_subst')
786 | C.Const (uri,exp_named_subst) ->
787 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
788 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
791 exp_named_subst_diff,newmeta',newmetasenvfragment,
792 C.Const (uri,exp_named_subst')
793 | C.MutInd (uri,tyno,exp_named_subst) ->
794 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
795 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
798 exp_named_subst_diff,newmeta',newmetasenvfragment,
799 C.MutInd (uri,tyno,exp_named_subst')
800 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
801 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
802 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
805 exp_named_subst_diff,newmeta',newmetasenvfragment,
806 C.MutConstruct (uri,tyno,consno,exp_named_subst')
807 | _ -> [],newmeta,[],term
809 let metasenv' = metasenv@newmetasenvfragment in
811 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.oblivion_ugraph
813 let termty = CicSubstitution.subst_vars exp_named_subst_diff termty in
814 let goal_arity = count_prods context ty in
815 let proof, gl, active, passive =
816 new_metasenv_and_unify_and_t dbd flags universe proof goal ?tables
817 newmeta' metasenv' context term' ty termty goal_arity
819 proof, gl, active, passive
822 (****************** AUTO ********************)
827 let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
830 let calculate_timeout flags =
831 if flags.timeout = 0. then
832 (debug_print (lazy "AUTO WITH NO TIMEOUT");
833 {flags with timeout = infinity})
837 let is_equational_case goalty flags =
838 let ensure_equational t =
839 if is_an_equational_goal t then true
842 let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
843 raise (ProofEngineTypes.Fail (lazy msg))
846 (flags.use_paramod && is_an_equational_goal goalty) ||
847 (flags.use_only_paramod && ensure_equational goalty)
850 let cache_add_success sort cache k v =
851 if sort = P then cache_add_success cache k v else cache_remove_underinspection
856 type menv = Cic.metasenv
857 type subst = Cic.substitution
858 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
859 let candidate_no = ref 0;;
860 type candidate = int * Cic.term Lazy.t
861 type cache = AutoCache.cache
863 Saturation.active_table * Saturation.passive_table * Equality.equality_bag
866 (* the goal (mainly for depth) and key of the goal *)
867 goal * AutoCache.cache_key
869 (* goal has to be proved *)
871 (* goal has to be cached as a success obtained using candidate as the first
873 | S of goal * AutoCache.cache_key * candidate * int
875 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
876 menv * subst * int * op list * op list * fail list
878 (* list of computations that may lead to the solution: all op list will
879 * end with the same (S(g,_)) *)
882 (* menv, subst, alternatives, tables, cache, maxmeta *)
883 | Proved of menv * subst * elem list * tables * cache * int
884 | Gaveup of tables * cache * int
887 (* the status exported to the external observer *)
889 (* context, (goal,candidate) list, and_list, history *)
890 Cic.context * (int * Cic.term * bool * int * (int * Cic.term Lazy.t) list) list *
891 (int * Cic.term * int) list * Cic.term Lazy.t list
894 let rec aux acc = function
895 | (D g)::tl -> aux (acc@[g]) tl
901 List.filter (function (_,_,P) -> true | _ -> false) l
905 let rec aux acc = function
906 | (D g)::tl -> aux (acc@[g]) tl
907 | (S _)::tl -> aux acc tl
913 let calculate_goal_ty (goalno,_,_) s m =
915 let _,cc,goalty = CicUtil.lookup_meta goalno m in
916 (* XXX applicare la subst al contesto? *)
917 Some (cc, CicMetaSubst.apply_subst s goalty)
918 with CicUtil.Meta_not_found i when i = goalno -> None
921 let calculate_closed_goal_ty (goalno,_,_) s =
923 let cc,_,goalty = List.assoc goalno s in
924 (* XXX applicare la subst al contesto? *)
925 Some (cc, CicMetaSubst.apply_subst s goalty)
926 with Not_found -> None
929 let pp_status ctx status =
931 let names = Utils.names_of_context ctx in
934 ProofEngineReduction.replace
935 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
936 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
940 let string_of_do m s (gi,_,_ as g) d =
941 match calculate_goal_ty g s m with
942 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
943 | None -> Printf.sprintf "D(%d, _, %d)" gi d
945 let string_of_s m su k (ci,ct) gi =
946 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp (Lazy.force ct)) ci
948 let string_of_ol m su l =
952 | D (g,d,s) -> string_of_do m su (g,d,s) d
953 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
956 let string_of_fl m s fl =
958 (List.map (fun ((i,_,_),ty) ->
959 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
961 let rec aux = function
963 | (m,s,_,_,ol,fl)::tl ->
964 Printf.eprintf "< [%s] ;;; [%s]>\n"
965 (string_of_ol m s ol) (string_of_fl m s fl);
968 Printf.eprintf "-------------------------- status -------------------\n";
970 Printf.eprintf "-----------------------------------------------------\n";
973 let auto_status = ref [] ;;
974 let auto_context = ref [];;
975 let in_pause = ref false;;
976 let pause b = in_pause := b;;
977 let cond = Condition.create ();;
978 let mutex = Mutex.create ();;
979 let hint = ref None;;
980 let prune_hint = ref [];;
982 let step _ = Condition.signal cond;;
983 let give_hint n = hint := Some n;;
984 let give_prune_hint hint =
985 prune_hint := hint :: !prune_hint
992 Condition.wait cond mutex;
997 let get_auto_status _ =
998 let status = !auto_status in
999 let and_list,elems,last =
1002 | (m,s,_,don,gl,fail)::tl ->
1005 (fun (id,d,_ as g) ->
1006 match calculate_goal_ty g s m with
1007 | Some (_,x) -> Some (id,x,d) | None -> None)
1011 (* these are the S goalsin the or list *)
1014 (fun (m,s,_,don,gl,fail) ->
1016 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
1017 (List.rev don @ gl))
1020 (* this function eats id from a list l::[id,x] returning x, l *)
1021 let eat_tail_if_eq id l =
1022 let rec aux (s, l) = function
1024 | ((id1,_,_),k1,c)::tl when id = id1 ->
1026 | None -> aux (Some c,l) tl
1027 | Some _ -> assert false)
1028 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
1030 let c, l = aux (None, []) l in
1033 let eat_in_parallel id l =
1034 let rec aux (b,eaten, new_l as acc) l =
1038 match eat_tail_if_eq id l with
1039 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
1040 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
1044 let rec eat_all rows l =
1048 match List.rev elem with
1049 | ((to_eat,depth,_),k,_)::next_lunch ->
1050 let b, eaten, l = eat_in_parallel to_eat l in
1051 let eaten = HExtlib.list_uniq eaten in
1052 let eaten = List.rev eaten in
1053 let b = true (* List.hd (List.rev b) *) in
1054 let rows = rows @ [to_eat,k,b,depth,eaten] in
1056 | [] -> eat_all rows or_list
1058 eat_all [] (List.rev orlist)
1062 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
1065 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
1066 and_list, rows, history
1068 !auto_context, elems, and_list, last
1071 (* Works if there is no dependency over proofs *)
1072 let is_a_green_cut goalty =
1073 CicUtil.is_meta_closed goalty
1075 let rec first_s = function
1076 | (D _)::tl -> first_s tl
1077 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
1080 let list_union l1 l2 =
1081 (* TODO ottimizzare compare *)
1082 HExtlib.list_uniq (List.sort compare (l1 @ l1))
1084 let rec eq_todo l1 l2 =
1086 | (D g1) :: tl1,(D g2) :: tl2 when g1=g2 -> eq_todo tl1 tl2
1087 | (S (g1,k1,(c1,lt1),i1)) :: tl1, (S (g2,k2,(c2,lt2),i2)) :: tl2
1088 when i1 = i2 && g1 = g2 && k1 = k2 && c1 = c2 ->
1089 if Lazy.force lt1 = Lazy.force lt2 then eq_todo tl1 tl2 else false
1093 let eat_head todo id fl orlist =
1094 let rec aux acc = function
1096 | (m, s, _, _, todo1, fl1)::tl as orlist ->
1097 let rec aux1 todo1 =
1098 match first_s todo1 with
1099 | None -> orlist, acc
1100 | Some (((gno,_,_),_,_,_), todo11) ->
1101 (* TODO confronto tra todo da ottimizzare *)
1102 if gno = id && eq_todo todo11 todo then
1103 aux (list_union fl1 acc) tl
1111 let close_proof p ty menv context =
1113 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
1115 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
1116 naif_closure p menv context
1118 (* XXX capire bene quando aggiungere alla cache *)
1119 let add_to_cache_and_del_from_orlist_if_green_cut
1120 g s m cache key todo orlist fl ctx size minsize
1122 let cache = cache_remove_underinspection cache key in
1123 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
1125 match calculate_closed_goal_ty g s with
1126 | None -> assert false
1127 | Some (canonical_ctx , gty) ->
1128 let goalno,depth,sort = g in
1129 let irl = mk_irl canonical_ctx in
1130 let goal = Cic.Meta(goalno, irl) in
1131 let proof = CicMetaSubst.apply_subst s goal in
1132 let green_proof, closed_proof =
1133 let b = is_a_green_cut proof in
1135 b, (* close_proof proof gty m ctx *) proof
1139 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
1140 if is_a_green_cut key then
1141 (* if the initia goal was closed, we cut alternatives *)
1142 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
1143 let orlist, fl = eat_head todo goalno fl orlist in
1145 if size < minsize then
1146 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
1148 (* if the proof is closed we cache it *)
1149 if green_proof then cache_add_success cache key proof
1150 else (* cache_add_success cache key closed_proof *)
1151 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
1153 cache, orlist, fl, true
1156 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
1157 if size < minsize then
1158 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
1159 (* if the substituted goal and the proof are closed we cache it *)
1160 if is_a_green_cut gty then
1161 if green_proof then cache_add_success cache gty proof
1162 else (* cache_add_success cache gty closed_proof *)
1163 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
1167 CicTypeChecker.type_of_aux' ~subst:s
1168 m ctx closed_proof CicUniv.oblivion_ugraph
1170 if is_a_green_cut ty then
1171 cache_add_success cache ty closed_proof
1174 | CicTypeChecker.TypeCheckerFailure _ ->*)
1175 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
1177 cache, orlist, fl, false
1179 let close_failures (fl : fail list) (cache : cache) =
1181 (fun cache ((gno,depth,_),gty) ->
1182 debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
1183 cache_add_failure cache gty depth)
1186 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
1187 let entry = goalno, (canonical_ctx, t,ty) in
1188 assert_subst_are_disjoint subst [entry];
1189 let subst = entry :: subst in
1191 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1196 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
1197 None,metasenv,subst ,(lazy (Cic.Meta(goalno,mk_irl context))),goalty, []
1200 tables maxm cache depth fake_proof goalno goalty subst context
1203 let active,passive,bag = tables in
1204 let ppterm = ppterm context in
1205 let status = (fake_proof,goalno) in
1206 if flags.use_only_paramod then
1208 debug_print (lazy ("PARAMODULATION SU: " ^
1209 string_of_int goalno ^ " " ^ ppterm goalty ));
1210 let goal_steps, saturation_steps, timeout =
1211 max_int,max_int,flags.timeout
1215 Saturation.given_clause bag maxm status active passive
1216 goal_steps saturation_steps timeout
1218 | None, active, passive, maxmeta ->
1219 [], (active,passive,bag), cache, maxmeta, flags
1220 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
1222 assert_subst_are_disjoint subst subst';
1223 let subst = subst@subst' in
1225 order_new_goals metasenv subst open_goals ppterm
1228 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1231 [(!candidate_no,proof),metasenv,subst,open_goals],
1232 (active,passive,bag),
1233 cache, maxmeta, flags
1239 ("SUBSUMPTION SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty));
1241 Saturation.all_subsumed bag maxm status active passive
1243 assert (maxmeta >= maxm);
1246 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
1247 assert_subst_are_disjoint subst subst';
1248 let subst = subst@subst' in
1250 order_new_goals metasenv subst open_goals ppterm
1253 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1256 (!candidate_no,proof),metasenv,subst,open_goals)
1259 res', (active,passive,bag), cache, maxmeta, flags
1264 goalty tables maxm subst fake_proof goalno depth context cand
1266 let ppterm = ppterm context in
1268 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1269 (PrimitiveTactics.apply_with_subst ~subst ~maxmeta:maxm ~term:cand)
1272 debug_print (lazy (" OK: " ^ ppterm cand));
1273 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
1274 let open_goals = order_new_goals metasenv subst open_goals ppterm in
1275 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
1277 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables , maxmeta
1279 | ProofEngineTypes.Fail s -> None,tables, maxm
1280 | CicUnification.Uncertain s -> None,tables, maxm
1283 let sort_new_elems =
1284 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
1285 List.length (prop_only l1) - List.length (prop_only l2))
1288 let applicative_case
1289 tables maxm depth subst fake_proof goalno goalty metasenv context universe
1292 let candidates = get_candidates flags.skip_trie_filtering universe cache goalty in
1293 let tables, elems, maxm =
1295 (fun (tables,elems,maxm) cand ->
1297 try_candidate goalty
1298 tables maxm subst fake_proof goalno depth context cand
1300 | None, tables,maxm -> tables,elems, maxm
1301 | Some x, tables, maxm -> tables,x::elems, maxm)
1302 (tables,[],maxm) candidates
1304 let elems = sort_new_elems elems in
1305 elems, tables, cache, maxm
1308 let equational_and_applicative_case
1309 universe flags m s g gty tables cache maxm context
1311 let goalno, depth, sort = g in
1312 let fake_proof = mk_fake_proof m s g gty context in
1313 if is_equational_case gty flags then
1314 let elems,tables,cache,maxm1, flags =
1315 equational_case tables maxm cache
1316 depth fake_proof goalno gty s context flags
1319 let more_elems, tables, cache, maxm1 =
1320 if flags.use_only_paramod then
1321 [],tables, cache, maxm
1324 tables maxm depth s fake_proof goalno
1325 gty m context universe cache flags
1328 elems@more_elems, tables, cache, maxm, flags
1330 let elems, tables, cache, maxm =
1331 applicative_case tables maxm depth s fake_proof goalno
1332 gty m context universe cache flags
1334 elems, tables, cache, maxm, flags
1336 let rec condition_for_hint i = function
1338 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
1339 | _::tl -> condition_for_hint i tl
1341 let remove_s_from_fl (id,_,_) (fl : fail list) =
1342 let rec aux = function
1344 | ((id1,_,_),_)::tl when id = id1 -> tl
1345 | hd::tl -> hd :: aux tl
1350 let prunable_for_size flags s m todo =
1351 let rec aux b = function
1352 | (S _)::tl -> aux b tl
1353 | (D (_,_,T))::tl -> aux b tl
1355 (match calculate_goal_ty g s m with
1357 | Some (canonical_ctx, gty) ->
1359 Utils.weight_of_term
1360 ~consider_metas:false ~count_metas_occurrences:true gty in
1361 let newb = b || gsize > flags.maxgoalsizefactor in
1368 let prunable ty todo =
1369 let rec aux b = function
1370 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
1371 | (D (_,_,T))::tl -> aux b tl
1379 let prunable menv subst ty todo =
1380 let rec aux = function
1381 | (S(_,k,_,_))::tl ->
1382 (match Equality.meta_convertibility_subst k ty menv with
1385 no_progress variant tl (* || aux tl*))
1386 | (D (_,_,T))::tl -> aux tl
1388 and no_progress variant = function
1389 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
1390 | D ((n,_,P) as g)::tl ->
1391 (match calculate_goal_ty g subst menv with
1392 | None -> no_progress variant tl
1394 (match calculate_goal_ty g variant menv with
1395 | None -> assert false
1397 if gty = gty' then no_progress variant tl
1399 (prerr_endline (string_of_int n);
1400 prerr_endline (CicPp.ppterm gty);
1401 prerr_endline (CicPp.ppterm gty');
1402 prerr_endline "---------- subst";
1403 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv subst);
1404 prerr_endline "---------- variant";
1405 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv variant);
1406 prerr_endline "---------- menv";
1407 prerr_endline (CicMetaSubst.ppmetasenv [] menv);
1408 no_progress variant tl) *)
1410 | _::tl -> no_progress variant tl
1415 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
1417 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
1419 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
1421 let filter_prune_hint l =
1422 let prune = !prune_hint in
1423 prune_hint := []; (* possible race... *)
1424 if prune = [] then l
1425 else List.filter (condition_for_prune_hint prune) l
1427 let auto_main tables maxm context flags universe cache elems =
1428 auto_context := context;
1429 let rec aux tables maxm flags cache (elems : status) =
1430 (* pp_status context elems; *)
1431 (* DEBUGGING CODE: uncomment these two lines to stop execution at each iteration
1432 auto_status := elems;
1435 let elems = filter_prune_hint elems in
1437 | (m, s, size, don, todo, fl)::orlist when !hint <> None ->
1438 debug_print (lazy "skip");
1440 | Some i when condition_for_hint i todo ->
1441 aux tables maxm flags cache orlist
1444 aux tables maxm flags cache elems)
1446 (* complete failure *)
1447 debug_print (lazy "give up");
1448 Gaveup (tables, cache, maxm)
1449 | (m, s, _, _, [],_)::orlist ->
1450 (* complete success *)
1451 debug_print (lazy "success");
1452 Proved (m, s, orlist, tables, cache, maxm)
1453 | (m, s, size, don, (D (_,_,T))::todo, fl)::orlist
1454 when not flags.AutoTypes.do_types ->
1455 (* skip since not Prop, don't even check if closed by side-effect *)
1456 debug_print (lazy "skip existential goal");
1457 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1458 | (m, s, size, don, (S(g, key, c,minsize) as op)::todo, fl)::orlist ->
1459 (* partial success, cache g and go on *)
1460 let cache, orlist, fl, sibling_pruned =
1461 add_to_cache_and_del_from_orlist_if_green_cut
1462 g s m cache key todo orlist fl context size minsize
1464 debug_print (lazy (AutoCache.cache_print context cache));
1465 let fl = remove_s_from_fl g fl in
1466 let don = if sibling_pruned then don else op::don in
1467 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1468 | (m, s, size, don, todo, fl)::orlist
1469 when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
1470 debug_print (lazy ("FAIL: WIDTH"));
1471 (* too many goals in and generated by last th *)
1472 let cache = close_failures fl cache in
1473 aux tables maxm flags cache orlist
1474 | (m, s, size, don, todo, fl)::orlist when size > flags.maxsize ->
1476 (lazy ("FAIL: SIZE: "^string_of_int size ^
1477 " > " ^ string_of_int flags.maxsize ));
1478 (* we already have a too large proof term *)
1479 let cache = close_failures fl cache in
1480 aux tables maxm flags cache orlist
1481 | _ when Unix.gettimeofday () > flags.timeout ->
1483 debug_print (lazy ("FAIL: TIMEOUT"));
1484 Gaveup (tables, cache, maxm)
1485 | (m, s, size, don, (D (gno,depth,_ as g))::todo, fl)::orlist as status ->
1487 debug_print (lazy "attack goal");
1488 match calculate_goal_ty g s m with
1490 (* closed by side effect *)
1491 debug_print (lazy ("SUCCESS: SIDE EFFECT: " ^ string_of_int gno));
1492 aux tables maxm flags cache ((m,s,size,don,todo, fl)::orlist)
1493 | Some (canonical_ctx, gty) ->
1495 Utils.weight_of_term ~consider_metas:false ~count_metas_occurrences:true gty
1497 if gsize > flags.maxgoalsizefactor then
1498 (debug_print (lazy ("FAIL: SIZE: goal: "^string_of_int gsize));
1499 aux tables maxm flags cache orlist)
1500 else if prunable_for_size flags s m todo then
1501 (debug_print (lazy ("POTO at depth: "^(string_of_int depth)));
1502 aux tables maxm flags cache orlist)
1504 (* still to be proved *)
1505 (debug_print (lazy ("EXAMINE: "^CicPp.ppterm gty));
1506 match cache_examine cache gty with
1507 | Failed_in d when d >= depth ->
1509 debug_print (lazy ("FAIL: DEPTH (cache): "^string_of_int gno));
1510 let cache = close_failures fl cache in
1511 aux tables maxm flags cache orlist
1512 | UnderInspection ->
1514 debug_print (lazy ("FAIL: LOOP: " ^ string_of_int gno));
1515 let cache = close_failures fl cache in
1516 aux tables maxm flags cache orlist
1518 debug_print (lazy ("SUCCESS: CACHE HIT: " ^ string_of_int gno));
1519 let s, m = put_in_subst s m g canonical_ctx t gty in
1520 aux tables maxm flags cache ((m, s, size, don,todo, fl)::orlist)
1522 | Failed_in _ when depth > 0 ->
1523 ( (* more depth or is the first time we see the goal *)
1524 if prunable m s gty todo then
1526 "FAIL: LOOP: one father is equal"));
1527 aux tables maxm flags cache orlist)
1529 let cache = cache_add_underinspection cache gty depth in
1530 auto_status := status;
1533 (lazy ("INSPECTING: " ^
1534 string_of_int gno ^ "("^ string_of_int size ^ "): "^
1536 (* elems are possible computations for proving gty *)
1537 let elems, tables, cache, maxm, flags =
1538 equational_and_applicative_case
1539 universe flags m s g gty tables cache maxm context
1542 (* this goal has failed *)
1543 let cache = close_failures ((g,gty)::fl) cache in
1544 aux tables maxm flags cache orlist
1546 (* elems = (cand,m,s,gl) *)
1547 let size_gl l = List.length
1548 (List.filter (function (_,_,P) -> true | _ -> false) l)
1551 let inj_gl gl = List.map (fun g -> D g) gl in
1552 let rec map = function
1553 | [] -> assert false
1554 | (cand,m,s,gl)::[] ->
1555 (* in the last one we add the failure *)
1557 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1559 (* we are the last in OR, we fail on g and
1560 * also on all failures implied by g *)
1561 (m,s, size + size_gl gl, don, todo, (g,gty)::fl)
1563 | (cand,m,s,gl)::tl ->
1564 (* we add the S step after gl and before todo *)
1566 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1568 (* since we are not the last in OR, we do not
1570 (m,s, size + size_gl gl, don, todo, []) :: map tl
1574 aux tables maxm flags cache elems)
1577 debug_print (lazy ("FAIL: DEPTH: " ^ string_of_int gno));
1578 let cache = close_failures fl cache in
1579 aux tables maxm flags cache orlist)
1581 (aux tables maxm flags cache elems : auto_result)
1586 auto_all_solutions maxm tables universe cache context metasenv gl flags
1588 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1591 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
1593 let elems = [metasenv,[],1,[],goals,[]] in
1594 let rec aux tables maxm solutions cache elems flags =
1595 match auto_main tables maxm context flags universe cache elems with
1596 | Gaveup (tables,cache,maxm) ->
1597 solutions,cache,maxm
1598 | Proved (metasenv,subst,others,tables,cache,maxm) ->
1599 if Unix.gettimeofday () > flags.timeout then
1600 ((subst,metasenv)::solutions), cache, maxm
1602 aux tables maxm ((subst,metasenv)::solutions) cache others flags
1604 let rc = aux tables maxm [] cache elems flags in
1606 | [],cache,maxm -> [],cache,maxm
1607 | solutions,cache,maxm ->
1610 (fun (subst,newmetasenv) ->
1612 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
1614 if opened = [] then Some subst else None)
1617 solutions,cache,maxm
1620 (******************* AUTO ***************)
1622 let auto flags metasenv tables universe cache context metasenv gl =
1623 let initial_time = Unix.gettimeofday() in
1624 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1625 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
1626 let elems = [metasenv,[],1,[],goals,[]] in
1627 match auto_main tables 0 context flags universe cache elems with
1628 | Proved (metasenv,subst,_, tables,cache,_) ->
1630 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1631 Some (subst,metasenv), cache
1632 | Gaveup (tables,cache,maxm) ->
1634 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1638 let applyS_tac ~dbd ~term ~params ~universe =
1639 ProofEngineTypes.mk_tactic
1643 apply_smart ~dbd ~term ~subst:[] ~params ~universe status
1647 | CicUnification.UnificationFailure msg
1648 | CicTypeChecker.TypeCheckerFailure msg ->
1649 raise (ProofEngineTypes.Fail msg))
1651 let auto_tac ~(dbd:HSql.dbd) ~params:(univ,params) ~universe (proof, goal) =
1652 let _,metasenv,_subst,_,_, _ = proof in
1653 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
1654 let flags = flags_of_params params () in
1655 let universe = universe_of_params metasenv context universe univ in
1656 let use_library = flags.use_library in
1657 let tables,cache,newmeta =
1658 init_cache_and_tables ~dbd use_library flags.use_only_paramod (not flags.skip_context)
1659 false universe (proof, goal) in
1660 let tables,cache,newmeta =
1661 if flags.close_more then
1663 tables newmeta context (proof, goal)
1664 auto_all_solutions universe cache
1665 else tables,cache,newmeta in
1666 let initial_time = Unix.gettimeofday() in
1667 let (_,oldmetasenv,_subst,_,_, _) = proof in
1670 metasenv,[],1,[],[D (goal,flags.maxdepth,P)],[]
1672 match auto_main tables newmeta context flags universe cache [elem] with
1673 | Proved (metasenv,subst,_, tables,cache,_) ->
1675 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1676 let proof,metasenv =
1677 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1678 proof goal subst metasenv
1681 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
1682 ~newmetasenv:metasenv
1685 | Gaveup (tables,cache,maxm) ->
1688 string_of_float(Unix.gettimeofday()-.initial_time)));
1689 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
1692 let auto_tac ~dbd ~params ~universe =
1693 ProofEngineTypes.mk_tactic (auto_tac ~params ~dbd ~universe);;
1695 let eq_of_goal = function
1696 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1698 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1701 (* performs steps of rewrite with the universe, obtaining if possible
1703 let solve_rewrite_tac ~universe ~params:(univ,params) (proof,goal as status)=
1704 let _,metasenv,_subst,_,_,_ = proof in
1705 let _,context,ty = CicUtil.lookup_meta goal metasenv in
1706 let steps = int_of_string (string params "steps" "1") in
1707 let universe = universe_of_params metasenv context universe univ in
1708 let eq_uri = eq_of_goal ty in
1709 let (active,passive,bag), cache, maxm =
1710 (* we take the whole universe (no signature filtering) *)
1711 init_cache_and_tables false true false true universe (proof,goal)
1713 let initgoal = [], metasenv, ty in
1715 let equalities = (Saturation.list_of_passive passive) in
1716 (* we demodulate using both actives passives *)
1717 List.fold_left (fun tbl eq -> Indexing.index tbl eq) (snd active) equalities
1719 let env = metasenv,context,CicUniv.oblivion_ugraph in
1720 match Indexing.solve_demodulating bag env table initgoal steps with
1721 | Some (proof, metasenv, newty) ->
1724 | Cic.Appl[Cic.MutInd _;eq_ty;left;_] ->
1725 Equality.Exact (Equality.refl_proof eq_uri eq_ty left)
1729 Equality.build_goal_proof
1730 bag eq_uri proof refl newty [] context metasenv
1732 ProofEngineTypes.apply_tactic
1733 (PrimitiveTactics.apply_tac ~term:proofterm) status
1736 (ProofEngineTypes.Fail (lazy
1737 ("Unable to solve with " ^ string_of_int steps ^ " demodulations")))
1739 let solve_rewrite_tac ~params ~universe () =
1740 ProofEngineTypes.mk_tactic (solve_rewrite_tac ~universe ~params)
1743 (* Demodulate thorem *)
1744 let open_type ty bo =
1745 let rec open_type_aux context ty k args =
1747 | Cic.Prod (n,s,t) ->
1749 FreshNamesGenerator.mk_fresh_name [] context n ~typ:s ~subst:[] in
1750 let entry = match n' with
1751 | Cic.Name _ -> Some (n',(Cic.Decl s))
1752 | Cic.Anonymous -> None
1754 open_type_aux (entry::context) t (k+1) ((Cic.Rel k)::args)
1755 | Cic.LetIn (n,s,sty,t) ->
1756 let entry = Some (n,(Cic.Def (s,sty)))
1758 open_type_aux (entry::context) t (k+1) args
1759 | _ -> context, ty, args
1761 let context, ty, args = open_type_aux [] ty 1 [] in
1763 | [] -> context, ty, bo
1764 | _ -> context, ty, Cic.Appl (bo::args)
1767 let rec close_type bo ty context =
1769 | [] -> assert_proof_is_valid bo [] [] ty; (bo,ty)
1770 | Some (n,(Cic.Decl s))::tl ->
1771 close_type (Cic.Lambda (n,s,bo)) (Cic.Prod (n,s,ty)) tl
1772 | Some (n,(Cic.Def (s,sty)))::tl ->
1773 close_type (Cic.LetIn (n,s,sty,bo)) (Cic.LetIn (n,s,sty,ty)) tl
1777 let is_subsumed univ context ty =
1778 let candidates = Universe.get_candidates univ ty in
1782 | Some found -> Some found
1785 let mk_irl = CicMkImplicit.identity_relocation_list_for_metavariable in
1786 let metasenv = [(0,context,ty)] in
1787 let fake_proof = None,metasenv,[] , (lazy (Cic.Meta(0,mk_irl context))),ty,[] in
1788 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1789 (PrimitiveTactics.apply_with_subst ~subst:[] ~maxmeta:0 ~term:cand) (fake_proof,0)
1791 let prop_goals, other = split_goals_in_prop metasenv subst open_goals in
1792 if prop_goals = [] then Some cand else None
1794 | ProofEngineTypes.Fail s -> None
1795 | CicUnification.Uncertain s -> None
1799 let demodulate_theorem ~universe uri =
1801 match LibraryObjects.eq_URI () with
1803 | None -> raise (ProofEngineTypes.Fail (lazy "equality not declared")) in
1804 let obj,_ = CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
1808 | Cic.Constant(n, _, ty ,_, _) -> open_type ty (Cic.Const(uri,[]))
1809 | _ -> raise (ProofEngineTypes.Fail (lazy "not a theorem"))
1811 if CicUtil.is_closed ty then
1812 raise (ProofEngineTypes.Fail (lazy ("closed term: dangerous reduction")));
1813 let initgoal = [], [], ty in
1814 (* compute the signature *)
1816 let ty_set = MetadataConstraints.constants_of ty in
1817 let hyp_set = MetadataQuery.signature_of_hypothesis context [] in
1818 let set = MetadataConstraints.UriManagerSet.union ty_set hyp_set in
1819 MetadataQuery.close_with_types set [] context
1821 (* retrieve equations from the universe universe *)
1823 retrieve_equations true signature universe AutoCache.cache_empty context []
1826 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
1831 CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph
1833 (* retrieve_equations could also return flexible terms *)
1834 if is_an_equality ty then Some(t,ty)
1837 let ty' = unfold context ty in
1838 if is_an_equality ty' then Some(t,ty') else None
1839 with ProofEngineTypes.Fail _ -> None)
1842 let bag = Equality.mk_equality_bag () in
1844 let units, _, newmeta =
1845 partition_unit_equalities context [] (CicMkImplicit.new_meta [] []) bag eqs_and_types
1849 (fun tbl eq -> Indexing.index tbl eq)
1850 Indexing.empty units
1852 let changed,(newproof,newmetasenv, newty) =
1854 ([],context,CicUniv.oblivion_ugraph) table initgoal in
1857 let oldproof = Equality.Exact bo in
1859 Equality.build_goal_proof (~contextualize:false) (~forward:true) bag
1860 eq_uri newproof oldproof ty [] context newmetasenv
1862 if newmetasenv <> [] then
1863 raise (ProofEngineTypes.Fail (lazy ("metasenv not empty")))
1866 assert_proof_is_valid proofterm newmetasenv context newty;
1867 match is_subsumed universe context newty with
1869 (ProofEngineTypes.Fail (lazy ("subsumed by " ^ CicPp.ppterm t)))
1870 | None -> close_type proofterm newty context
1873 else (* if newty = ty then *)
1874 raise (ProofEngineTypes.Fail (lazy "no progress"))
1875 (*else ProofEngineTypes.apply_tactic
1876 (ReductionTactics.simpl_tac
1877 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1881 (* NEW DEMODULATE *)
1882 let demodulate_tac ~dbd ~universe ~params:(univ, params) (proof,goal)=
1883 let curi,metasenv,_subst,pbo,pty, attrs = proof in
1884 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1885 let universe = universe_of_params metasenv context universe univ in
1886 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1887 let initgoal = [], metasenv, ty in
1889 match LibraryObjects.eq_URI () with
1891 | None -> raise (ProofEngineTypes.Fail (lazy "equality not declared")) in
1892 (* let eq_uri = eq_of_goal ty in *)
1893 let (active,passive,bag), cache, maxm =
1894 init_cache_and_tables
1895 ~dbd false false true true universe (proof,goal)
1897 let equalities = (Saturation.list_of_passive passive) in
1898 (* we demodulate using both actives passives *)
1899 let env = metasenv,context,CicUniv.empty_ugraph in
1900 debug_print (lazy ("PASSIVES:" ^ string_of_int(List.length equalities)));
1901 List.iter (fun e -> debug_print (lazy (Equality.string_of_equality ~env e)))
1905 (fun tbl eq -> Indexing.index tbl eq)
1906 (snd active) equalities
1908 let changed,(newproof,newmetasenv, newty) =
1909 (* Indexing.demodulation_goal bag *)
1911 (metasenv,context,CicUniv.oblivion_ugraph) table initgoal
1915 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1917 Equality.build_goal_proof (~contextualize:false) bag
1918 eq_uri newproof opengoal ty [] context metasenv
1920 let extended_metasenv = (maxm,context,newty)::metasenv in
1921 let extended_status =
1922 (curi,extended_metasenv,_subst,pbo,pty, attrs),goal in
1923 let (status,newgoals) =
1924 ProofEngineTypes.apply_tactic
1925 (PrimitiveTactics.apply_tac ~term:proofterm)
1927 (status,maxm::newgoals)
1929 else (* if newty = ty then *)
1930 raise (ProofEngineTypes.Fail (lazy "no progress"))
1931 (*else ProofEngineTypes.apply_tactic
1932 (ReductionTactics.simpl_tac
1933 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1936 let demodulate_tac ~dbd ~params ~universe =
1937 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~params ~universe);;
1939 let pp_proofterm = Equality.pp_proofterm;;
1941 let revision = "$Revision$";;
1942 let size_and_depth context metasenv t = 100, 100