1 (* Copyright (C) 2002, 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/.
31 if debug then prerr_endline (Lazy.force s);;
33 let is_propositional context sort =
34 match CicReduction.whd context sort with
36 | Cic.Sort (Cic.CProp _) -> true
41 type auto_params = Cic.term list * (string * string) list
45 (* closing a term w.r.t. its metavariables
46 very naif version: it does not take dependencies properly into account *)
48 let naif_closure ?(prefix_name="xxx_") t metasenv context =
49 let metasenv = ProofEngineHelpers.sort_metasenv metasenv in
50 let n = List.length metasenv in
51 let what = List.map (fun (i,cc,ty) -> Cic.Meta(i,[])) metasenv in
54 (fun (i,acc) (_,cc,ty) -> (i-1,Cic.Rel i::acc))
57 let t = CicSubstitution.lift n t in
59 ProofEngineReduction.replace_lifting
60 ~equality:(fun c t1 t2 ->
62 | Cic.Meta(i,_),Cic.Meta(j,_) -> i = j
64 ~context ~what ~with_what ~where:t
68 (fun (n,t) (_,cc,ty) ->
69 n-1, Cic.Lambda(Cic.Name (prefix_name^string_of_int n),
70 CicSubstitution.lift n ty,t))
76 let lambda_close ?prefix_name t menv ctx =
77 let t = naif_closure ?prefix_name t menv ctx in
79 (fun (t,i) -> function
80 | None -> CicSubstitution.subst (Cic.Implicit None) t,i (* delift *)
81 | Some (name, Cic.Decl ty) -> Cic.Lambda (name, ty, t),i+1
82 | Some (name, Cic.Def (bo, ty)) -> Cic.LetIn (name, bo, ty, t),i+1)
83 (t,List.length menv) ctx
86 (* functions for retrieving theorems *)
88 exception FillingFailure of AutoCache.cache * int
90 let rec unfold context = function
91 | Cic.Prod(name,s,t) ->
92 let t' = unfold ((Some (name,Cic.Decl s))::context) t in
94 | t -> ProofEngineReduction.unfold context t
96 let find_library_theorems dbd proof goal =
97 let univ = MetadataQuery.universe_of_goal ~dbd false proof goal in
98 let terms = List.map CicUtil.term_of_uri univ in
101 (t,fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph)))
104 let find_context_theorems context metasenv =
107 (fun (res,i) ctxentry ->
109 | Some (_,Cic.Decl t) ->
110 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
111 | Some (_,Cic.Def (_,t)) ->
112 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
117 let rec is_an_equality = function
118 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
119 when (LibraryObjects.is_eq_URI uri) -> true
120 | Cic.Prod (_, _, t) -> is_an_equality t
124 let partition_equalities =
125 List.partition (fun (_,ty) -> is_an_equality ty)
128 let default_auto maxm _ _ cache _ _ _ _ = [],cache,maxm ;;
131 let is_unit_equation context metasenv oldnewmeta term =
132 let head, metasenv, args, newmeta =
133 TermUtil.saturate_term oldnewmeta metasenv context term 0
135 let propositional_args =
139 let _,_,mt = CicUtil.lookup_meta i metasenv in
141 CicTypeChecker.type_of_aux' metasenv context mt
142 CicUniv.oblivion_ugraph
144 if is_propositional context sort then Some i else None
148 if propositional_args = [] then
150 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
152 Some (args,metasenv,newmetas,head,newmeta)
156 let get_candidates universe cache t =
158 (Universe.get_candidates universe t)@(AutoCache.get_candidates cache t)
161 (lazy ("candidates for " ^ (CicPp.ppterm t) ^ " = " ^
162 (String.concat "\n" (List.map CicPp.ppterm candidates)))) in
163 debug_print debug_msg;
167 let only signature context metasenv t =
170 CicTypeChecker.type_of_aux' metasenv context t CicUniv.oblivion_ugraph
172 let consts = MetadataConstraints.constants_of ty in
173 let b = MetadataConstraints.UriManagerSet.subset consts signature in
176 let ty' = unfold context ty in
177 let consts' = MetadataConstraints.constants_of ty' in
178 MetadataConstraints.UriManagerSet.subset consts' signature
180 | CicTypeChecker.TypeCheckerFailure _ -> assert false
181 | ProofEngineTypes.Fail _ -> false (* unfold may fail *)
184 let not_default_eq_term t =
186 let uri = CicUtil.uri_of_term t in
187 not (LibraryObjects.in_eq_URIs uri)
188 with Invalid_argument _ -> true
190 let retrieve_equations dont_filter signature universe cache context metasenv =
191 match LibraryObjects.eq_URI() with
194 let eq_uri = UriManager.strip_xpointer eq_uri in
195 let fake= Cic.Meta(-1,[]) in
196 let fake_eq = Cic.Appl [Cic.MutInd (eq_uri,0, []);fake;fake;fake] in
197 let candidates = get_candidates universe cache fake_eq in
198 if dont_filter then candidates
200 let candidates = List.filter not_default_eq_term candidates in
201 List.filter (only signature context metasenv) candidates
203 let build_equality bag head args proof newmetas maxmeta =
205 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] ->
207 if args = [] then proof else Cic.Appl (proof::args)
209 let o = !Utils.compare_terms t1 t2 in
210 let stat = (ty,t1,t2,o) in
211 (* let w = compute_equality_weight stat in *)
213 let proof = Equality.Exact p in
214 let e = Equality.mk_equality bag (w, proof, stat, newmetas) in
215 (* to clean the local context of metas *)
216 Equality.fix_metas bag maxmeta e
220 let partition_unit_equalities context metasenv newmeta bag equations =
222 (fun (units,other,maxmeta)(t,ty) ->
223 if not (CicUtil.is_meta_closed t && CicUtil.is_meta_closed ty) then
226 ("Skipping " ^ CicMetaSubst.ppterm_in_context ~metasenv [] t context
227 ^ " since it is not meta closed")
229 units,(t,ty)::other,maxmeta
231 match is_unit_equation context metasenv maxmeta ty with
232 | Some (args,metasenv,newmetas,head,newmeta') ->
233 let maxmeta,equality =
234 build_equality bag head args t newmetas newmeta' in
235 equality::units,other,maxmeta
237 units,(t,ty)::other,maxmeta)
238 ([],[],newmeta) equations
241 (Saturation.make_active [],
242 Saturation.make_passive [],
243 Equality.mk_equality_bag)
245 let init_cache_and_tables
246 ?dbd use_library paramod use_context dont_filter universe (proof, goal)
248 (* the local cache in initially empty *)
249 let cache = AutoCache.cache_empty in
250 let _, metasenv, _subst,_, _, _ = proof in
251 let signature = MetadataQuery.signature_of metasenv goal in
252 let newmeta = CicMkImplicit.new_meta metasenv [] in
253 let _,context,_ = CicUtil.lookup_meta goal metasenv in
254 let ct = if use_context then find_context_theorems context metasenv else [] in
256 (lazy ("ho trovato nel contesto " ^ (string_of_int (List.length ct))));
258 match use_library, dbd with
259 | true, Some dbd -> find_library_theorems dbd metasenv goal
263 (lazy ("ho trovato nella libreria " ^ (string_of_int (List.length lt))));
264 let cache = cache_add_list cache context (ct@lt) in
266 retrieve_equations dont_filter signature universe cache context metasenv
269 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
274 CicTypeChecker.type_of_aux'
275 metasenv context t CicUniv.oblivion_ugraph
277 (* retrieve_equations could also return flexible terms *)
278 if is_an_equality ty then Some(t,ty)
281 let ty' = unfold context ty in
282 if is_an_equality ty' then Some(t,ty') else None
283 with ProofEngineTypes.Fail _ -> None)
286 let bag = Equality.mk_equality_bag () in
287 let units, other_equalities, newmeta =
288 partition_unit_equalities context metasenv newmeta bag eqs_and_types
290 (* SIMPLIFICATION STEP
292 let env = (metasenv, context, CicUniv.oblivion_ugraph) in
293 let eq_uri = HExtlib.unopt (LibraryObjects.eq_URI()) in
294 Saturation.simplify_equalities bag eq_uri env units
297 let passive = Saturation.make_passive units in
298 let no = List.length units in
299 let active = Saturation.make_active [] in
300 let active,passive,newmeta =
301 if paramod then active,passive,newmeta
303 Saturation.pump_actives
304 context bag newmeta active passive (no+1) infinity
306 (active,passive,bag),cache,newmeta
308 let fill_hypothesis context metasenv oldnewmeta term tables (universe:Universe.universe) cache auto fast =
309 let head, metasenv, args, newmeta =
310 TermUtil.saturate_term oldnewmeta metasenv context term 0
312 let propositional_args =
316 let _,_,mt = CicUtil.lookup_meta i metasenv in
318 CicTypeChecker.type_of_aux' metasenv context mt
319 CicUniv.oblivion_ugraph
321 if is_propositional context sort then Some i else None
325 let results,cache,newmeta =
326 if propositional_args = [] then
327 let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
328 [args,metasenv,newmetas,head,newmeta],cache,newmeta
332 None,metasenv,term,term (* term non e' significativo *)
336 {AutoTypes.default_flags() with
337 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
338 maxwidth = 2;maxdepth = 2;
339 use_paramod=true;use_only_paramod=false}
341 {AutoTypes.default_flags() with
342 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
343 maxwidth = 2;maxdepth = 4;
344 use_paramod=true;use_only_paramod=false}
346 match auto newmeta tables universe cache context metasenv propositional_args flags with
347 | [],cache,newmeta -> raise (FillingFailure (cache,newmeta))
348 | substs,cache,newmeta ->
352 CicMetaSubst.apply_subst_metasenv subst metasenv
354 let head = CicMetaSubst.apply_subst subst head in
356 List.filter (fun (i,_,_) ->i >= oldnewmeta) metasenv
358 let args = List.map (CicMetaSubst.apply_subst subst) args in
359 let newm = CicMkImplicit.new_meta metasenv subst in
360 args,metasenv,newmetas,head,max newm newmeta)
361 substs, cache, newmeta
363 results,cache,newmeta
365 let build_equalities auto context metasenv tables universe cache newmeta equations =
367 (fun (facts,cache,newmeta) (t,ty) ->
368 (* in any case we add the equation to the cache *)
369 let cache = AutoCache.cache_add_list cache context [(t,ty)] in
371 let saturated,cache,newmeta =
372 fill_hypothesis context metasenv newmeta ty tables universe cache auto true
374 let (active,passive,bag) = tables in
375 let eqs,bag,newmeta =
377 (fun (acc,bag,newmeta) (args,metasenv,newmetas,head,newmeta') ->
378 let maxmeta,equality =
379 build_equality bag head args t newmetas newmeta'
381 equality::acc,bag,maxmeta)
382 ([],bag,newmeta) saturated
384 (eqs@facts, cache, newmeta)
385 with FillingFailure (cache,newmeta) ->
386 (* if filling hypothesis fails we add the equation to
388 (facts,cache,newmeta)
390 ([],cache,newmeta) equations
392 let close_more tables maxmeta context status auto universe cache =
393 let (active,passive,bag) = tables in
394 let proof, goalno = status in
395 let _, metasenv,_subst,_,_, _ = proof in
396 let signature = MetadataQuery.signature_of metasenv goalno in
398 retrieve_equations false signature universe cache context metasenv
404 CicTypeChecker.type_of_aux' metasenv context t
405 CicUniv.oblivion_ugraph in
406 (* retrieve_equations could also return flexible terms *)
407 if is_an_equality ty then Some(t,ty) else None)
409 let units, cache, maxm =
410 build_equalities auto context metasenv tables universe cache maxmeta eqs_and_types in
411 debug_print (lazy (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
412 string_of_int maxm));
414 (fun e -> debug_print (lazy (Equality.string_of_equality e)))
416 debug_print (lazy ">>>>>>>>>>>>>>>>>>>>>>");
417 let passive = Saturation.add_to_passive units passive in
418 let no = List.length units in
419 debug_print (lazy ("No = " ^ (string_of_int no)));
420 let active,passive,newmeta =
421 Saturation.pump_actives context bag maxm active passive (no+1) infinity
423 (active,passive,bag),cache,newmeta
425 let find_context_equalities
426 maxmeta bag context proof (universe:Universe.universe) cache
428 let module C = Cic in
429 let module S = CicSubstitution in
430 let module T = CicTypeChecker in
431 let _,metasenv,_subst,_,_, _ = proof in
432 let newmeta = max (ProofEngineHelpers.new_meta_of_proof ~proof) maxmeta in
433 (* if use_auto is true, we try to close the hypothesis of equational
434 statements using auto; a naif, and probably wrong approach *)
435 let rec aux cache index newmeta = function
436 | [] -> [], newmeta,cache
437 | (Some (_, C.Decl (term)))::tl ->
440 (Printf.sprintf "Examining: %d (%s)" index (CicPp.ppterm term)));
441 let do_find context term =
443 | C.Prod (name, s, t) when is_an_equality t ->
446 let term = S.lift index term in
447 let saturated,cache,newmeta =
448 fill_hypothesis context metasenv newmeta term
449 empty_tables universe cache default_auto false
453 (fun (acc,newmeta) (args,metasenv,newmetas,head,newmeta') ->
454 let newmeta, equality =
456 bag head args (Cic.Rel index) newmetas (max newmeta newmeta')
458 equality::acc, newmeta + 1)
459 ([],newmeta) saturated
462 with FillingFailure (cache,newmeta) ->
464 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
465 when LibraryObjects.is_eq_URI uri ->
466 let term = S.lift index term in
468 build_equality bag term [] (Cic.Rel index) [] newmeta
470 [e], (newmeta+1),cache
471 | _ -> [], newmeta, cache
473 let eqs, newmeta, cache = do_find context term in
474 let rest, newmeta,cache = aux cache (index+1) newmeta tl in
475 List.map (fun x -> index,x) eqs @ rest, newmeta, cache
477 aux cache (index+1) newmeta tl
479 let il, maxm, cache =
480 aux cache 1 newmeta context
482 let indexes, equalities = List.split il in
483 indexes, equalities, maxm, cache
486 (********** PARAMETERS PASSING ***************)
488 let bool params name default =
490 let s = List.assoc name params in
491 if s = "" || s = "1" || s = "true" || s = "yes" || s = "on" then true
492 else if s = "0" || s = "false" || s = "no" || s= "off" then false
494 let msg = "Unrecognized value for parameter "^name^"\n" in
495 let msg = msg^"Accepted values are 1,true,yes,on and 0,false,no,off" in
496 raise (ProofEngineTypes.Fail (lazy msg))
497 with Not_found -> default
500 let string params name default =
501 try List.assoc name params with
502 | Not_found -> default
505 let int params name default =
506 try int_of_string (List.assoc name params) with
507 | Not_found -> default
509 raise (ProofEngineTypes.Fail (lazy (name ^ " must be an integer")))
512 let flags_of_params params ?(for_applyS=false) () =
513 let int = int params in
514 let bool = bool params in
515 let close_more = bool "close_more" false in
516 let use_paramod = bool "use_paramod" true in
517 let use_only_paramod =
518 if for_applyS then true else bool "paramodulation" false in
519 let use_library = bool "library"
520 ((AutoTypes.default_flags()).AutoTypes.use_library) in
521 let depth = int "depth" ((AutoTypes.default_flags()).AutoTypes.maxdepth) in
522 let width = int "width" ((AutoTypes.default_flags()).AutoTypes.maxwidth) in
523 let size = int "size" ((AutoTypes.default_flags()).AutoTypes.maxsize) in
524 let gsize = int "gsize" ((AutoTypes.default_flags()).AutoTypes.maxgoalsizefactor) in
525 let do_type = bool "type" false in
526 let timeout = int "timeout" 0 in
527 { AutoTypes.maxdepth =
528 if use_only_paramod then 2 else depth;
529 AutoTypes.maxwidth = width;
530 AutoTypes.maxsize = size;
533 if for_applyS then Unix.gettimeofday () +. 30.0
537 Unix.gettimeofday() +. (float_of_int timeout);
538 AutoTypes.use_library = use_library;
539 AutoTypes.use_paramod = use_paramod;
540 AutoTypes.use_only_paramod = use_only_paramod;
541 AutoTypes.close_more = close_more;
542 AutoTypes.dont_cache_failures = false;
543 AutoTypes.maxgoalsizefactor = gsize;
544 AutoTypes.do_types = do_type;
547 let universe_of_params metasenv context universe tl =
548 if tl = [] then universe else
552 fst (CicTypeChecker.type_of_aux' metasenv context term
553 CicUniv.oblivion_ugraph))
556 Universe.index_list Universe.empty context (List.combine tl tys)
560 (***************** applyS *******************)
562 let new_metasenv_and_unify_and_t
563 dbd flags universe proof goal ?tables newmeta' metasenv'
564 context term' ty termty goal_arity
566 let (consthead,newmetasenv,arguments,_) =
567 TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in
569 match arguments with [] -> term' | _ -> Cic.Appl (term'::arguments)
571 let proof',oldmetasenv =
572 let (puri,metasenv,_subst,pbo,pty, attrs) = proof in
573 (puri,newmetasenv,_subst,pbo,pty, attrs),metasenv
575 let goal_for_paramod =
576 match LibraryObjects.eq_URI () with
578 Cic.Appl [Cic.MutInd (uri,0,[]); Cic.Sort Cic.Prop; consthead; ty]
579 | None -> raise (ProofEngineTypes.Fail (lazy "No equality defined"))
581 let newmeta = CicMkImplicit.new_meta newmetasenv (*subst*) [] in
582 let metasenv_for_paramod = (newmeta,context,goal_for_paramod)::newmetasenv in
584 let uri,_,_subst,p,ty, attrs = proof' in
585 uri,metasenv_for_paramod,_subst,p,ty, attrs
587 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
589 ProofEngineTypes.apply_tactic
590 (EqualityTactics.rewrite_tac ~direction:`RightToLeft
591 ~pattern:(ProofEngineTypes.conclusion_pattern None)
592 (Cic.Meta(newmeta,irl)) [])
595 let goal = match goals with [g] -> g | _ -> assert false in
597 ProofEngineTypes.apply_tactic
598 (PrimitiveTactics.apply_tac term'')
602 let (active, passive,bag), cache, maxmeta =
603 init_cache_and_tables ~dbd flags.use_library true true false universe
606 Saturation.given_clause bag maxmeta (proof'''',newmeta) active passive
607 max_int max_int flags.timeout
610 raise (ProofEngineTypes.Fail (lazy ("FIXME: propaga le tabelle")))
611 | Some (_,proof''''',_), active,passive,_ ->
613 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
614 ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m), active, passive
617 let rec count_prods context ty =
618 match CicReduction.whd context ty with
619 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
623 ~dbd ~term ~subst ~universe ?tables ~params:(univ,params) (proof, goal)
625 let module T = CicTypeChecker in
626 let module R = CicReduction in
627 let module C = Cic in
628 let (_,metasenv,_subst,_,_, _) = proof in
629 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
630 let flags = flags_of_params params ~for_applyS:true () in
631 let universe = universe_of_params metasenv context universe univ in
632 let newmeta = CicMkImplicit.new_meta metasenv subst in
633 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
635 C.Var (uri,exp_named_subst) ->
636 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
637 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
640 exp_named_subst_diff,newmeta',newmetasenvfragment,
641 C.Var (uri,exp_named_subst')
642 | C.Const (uri,exp_named_subst) ->
643 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
644 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
647 exp_named_subst_diff,newmeta',newmetasenvfragment,
648 C.Const (uri,exp_named_subst')
649 | C.MutInd (uri,tyno,exp_named_subst) ->
650 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
651 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
654 exp_named_subst_diff,newmeta',newmetasenvfragment,
655 C.MutInd (uri,tyno,exp_named_subst')
656 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
657 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
658 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
661 exp_named_subst_diff,newmeta',newmetasenvfragment,
662 C.MutConstruct (uri,tyno,consno,exp_named_subst')
663 | _ -> [],newmeta,[],term
665 let metasenv' = metasenv@newmetasenvfragment in
667 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.oblivion_ugraph
669 let termty = CicSubstitution.subst_vars exp_named_subst_diff termty in
670 let goal_arity = count_prods context ty in
671 let proof, gl, active, passive =
672 new_metasenv_and_unify_and_t dbd flags universe proof goal ?tables
673 newmeta' metasenv' context term' ty termty goal_arity
675 proof, gl, active, passive
678 (****************** AUTO ********************)
680 let mk_irl ctx = CicMkImplicit.identity_relocation_list_for_metavariable ctx;;
681 let ugraph = CicUniv.oblivion_ugraph;;
682 let typeof = CicTypeChecker.type_of_aux';;
684 let names = List.map (function None -> None | Some (x,_) -> Some x) ctx in
687 let is_in_prop context subst metasenv ty =
688 let sort,u = typeof ~subst metasenv context ty CicUniv.oblivion_ugraph in
689 is_propositional context sort
692 let assert_proof_is_valid proof metasenv context goalty =
695 let ty,u = typeof metasenv context proof CicUniv.oblivion_ugraph in
696 let b,_ = CicReduction.are_convertible context ty goalty u in
700 List.map (function None -> None | Some (x,_) -> Some x) context
702 debug_print (lazy ("PROOF:" ^ CicPp.pp proof names));
703 debug_print (lazy ("PROOFTY:" ^ CicPp.pp ty names));
704 debug_print (lazy ("GOAL:" ^ CicPp.pp goalty names));
705 debug_print (lazy ("MENV:" ^ CicMetaSubst.ppmetasenv [] metasenv));
712 let assert_subst_are_disjoint subst subst' =
715 (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst')
720 let split_goals_in_prop metasenv subst gl =
723 let _,context,ty = CicUtil.lookup_meta g metasenv in
725 let sort,u = typeof ~subst metasenv context ty ugraph in
726 is_propositional context sort
728 | CicTypeChecker.AssertFailure s
729 | CicTypeChecker.TypeCheckerFailure s ->
731 (lazy ("NON TIPA" ^ ppterm context (CicMetaSubst.apply_subst subst ty)));
734 (* FIXME... they should type! *)
738 let split_goals_with_metas metasenv subst gl =
741 let _,context,ty = CicUtil.lookup_meta g metasenv in
742 let ty = CicMetaSubst.apply_subst subst ty in
743 CicUtil.is_meta_closed ty)
747 let order_new_goals metasenv subst open_goals ppterm =
748 let prop,rest = split_goals_in_prop metasenv subst open_goals in
749 let closed_prop, open_prop = split_goals_with_metas metasenv subst prop in
750 let closed_type, open_type = split_goals_with_metas metasenv subst rest in
752 (List.map (fun x -> x,P) (open_prop @ closed_prop))
754 (List.map (fun x -> x,T) (open_type @ closed_type))
759 let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty,sort) open_goals
761 debug_print (lazy (" OPEN: "^
765 | (i,t,P) -> string_of_int i ^ ":"^ppterm t^ "Prop"
766 | (i,t,T) -> string_of_int i ^ ":"^ppterm t^ "Type")
771 let is_an_equational_goal = function
772 | Cic.Appl [Cic.MutInd(u,_,_);_;_;_] when LibraryObjects.is_eq_URI u -> true
777 let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
780 let calculate_timeout flags =
781 if flags.timeout = 0. then
782 (debug_print (lazy "AUTO WITH NO TIMEOUT");
783 {flags with timeout = infinity})
787 let is_equational_case goalty flags =
788 let ensure_equational t =
789 if is_an_equational_goal t then true
792 let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
793 raise (ProofEngineTypes.Fail (lazy msg))
796 (flags.use_paramod && is_an_equational_goal goalty) ||
797 (flags.use_only_paramod && ensure_equational goalty)
800 let cache_add_success sort cache k v =
801 if sort = P then cache_add_success cache k v else cache_remove_underinspection
806 type menv = Cic.metasenv
807 type subst = Cic.substitution
808 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
809 let candidate_no = ref 0;;
810 type candidate = int * Cic.term Lazy.t
811 type cache = AutoCache.cache
813 Saturation.active_table * Saturation.passive_table * Equality.equality_bag
816 (* the goal (mainly for depth) and key of the goal *)
817 goal * AutoCache.cache_key
819 (* goal has to be proved *)
821 (* goal has to be cached as a success obtained using candidate as the first
823 | S of goal * AutoCache.cache_key * candidate * int
825 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
826 menv * subst * int * op list * op list * fail list
828 (* list of computations that may lead to the solution: all op list will
829 * end with the same (S(g,_)) *)
832 (* menv, subst, alternatives, tables, cache, maxmeta *)
833 | Proved of menv * subst * elem list * tables * cache * int
834 | Gaveup of tables * cache * int
837 (* the status exported to the external observer *)
839 (* context, (goal,candidate) list, and_list, history *)
840 Cic.context * (int * Cic.term * bool * int * (int * Cic.term Lazy.t) list) list *
841 (int * Cic.term * int) list * Cic.term Lazy.t list
844 let rec aux acc = function
845 | (D g)::tl -> aux (acc@[g]) tl
851 List.filter (function (_,_,P) -> true | _ -> false) l
855 let rec aux acc = function
856 | (D g)::tl -> aux (acc@[g]) tl
857 | (S _)::tl -> aux acc tl
862 let calculate_goal_ty (goalno,_,_) s m =
864 let _,cc,goalty = CicUtil.lookup_meta goalno m in
865 (* XXX applicare la subst al contesto? *)
866 Some (cc, CicMetaSubst.apply_subst s goalty)
867 with CicUtil.Meta_not_found i when i = goalno -> None
869 let calculate_closed_goal_ty (goalno,_,_) s =
871 let cc,_,goalty = List.assoc goalno s in
872 (* XXX applicare la subst al contesto? *)
873 Some (cc, CicMetaSubst.apply_subst s goalty)
874 with Not_found -> None
876 let pp_status ctx status =
878 let names = Utils.names_of_context ctx in
881 ProofEngineReduction.replace
882 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
883 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
887 let string_of_do m s (gi,_,_ as g) d =
888 match calculate_goal_ty g s m with
889 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
890 | None -> Printf.sprintf "D(%d, _, %d)" gi d
892 let string_of_s m su k (ci,ct) gi =
893 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp (Lazy.force ct)) ci
895 let string_of_ol m su l =
899 | D (g,d,s) -> string_of_do m su (g,d,s) d
900 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
903 let string_of_fl m s fl =
905 (List.map (fun ((i,_,_),ty) ->
906 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
908 let rec aux = function
910 | (m,s,_,_,ol,fl)::tl ->
911 Printf.eprintf "< [%s] ;;; [%s]>\n"
912 (string_of_ol m s ol) (string_of_fl m s fl);
915 Printf.eprintf "-------------------------- status -------------------\n";
917 Printf.eprintf "-----------------------------------------------------\n";
920 let auto_status = ref [] ;;
921 let auto_context = ref [];;
922 let in_pause = ref false;;
923 let pause b = in_pause := b;;
924 let cond = Condition.create ();;
925 let mutex = Mutex.create ();;
926 let hint = ref None;;
927 let prune_hint = ref [];;
929 let step _ = Condition.signal cond;;
930 let give_hint n = hint := Some n;;
931 let give_prune_hint hint =
932 prune_hint := hint :: !prune_hint
939 Condition.wait cond mutex;
944 let get_auto_status _ =
945 let status = !auto_status in
946 let and_list,elems,last =
949 | (m,s,_,don,gl,fail)::tl ->
952 (fun (id,d,_ as g) ->
953 match calculate_goal_ty g s m with
954 | Some (_,x) -> Some (id,x,d) | None -> None)
958 (* these are the S goalsin the or list *)
961 (fun (m,s,_,don,gl,fail) ->
963 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
967 (* this function eats id from a list l::[id,x] returning x, l *)
968 let eat_tail_if_eq id l =
969 let rec aux (s, l) = function
971 | ((id1,_,_),k1,c)::tl when id = id1 ->
973 | None -> aux (Some c,l) tl
974 | Some _ -> assert false)
975 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
977 let c, l = aux (None, []) l in
980 let eat_in_parallel id l =
981 let rec aux (b,eaten, new_l as acc) l =
985 match eat_tail_if_eq id l with
986 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
987 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
991 let rec eat_all rows l =
995 match List.rev elem with
996 | ((to_eat,depth,_),k,_)::next_lunch ->
997 let b, eaten, l = eat_in_parallel to_eat l in
998 let eaten = HExtlib.list_uniq eaten in
999 let eaten = List.rev eaten in
1000 let b = true (* List.hd (List.rev b) *) in
1001 let rows = rows @ [to_eat,k,b,depth,eaten] in
1003 | [] -> eat_all rows or_list
1005 eat_all [] (List.rev orlist)
1009 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
1012 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
1013 and_list, rows, history
1015 !auto_context, elems, and_list, last
1018 (* Works if there is no dependency over proofs *)
1019 let is_a_green_cut goalty =
1020 CicUtil.is_meta_closed goalty
1022 let rec first_s = function
1023 | (D _)::tl -> first_s tl
1024 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
1027 let list_union l1 l2 =
1028 (* TODO ottimizzare compare *)
1029 HExtlib.list_uniq (List.sort compare (l1 @ l1))
1031 let rec eq_todo l1 l2 =
1033 | (D g1) :: tl1,(D g2) :: tl2 when g1=g2 -> eq_todo tl1 tl2
1034 | (S (g1,k1,(c1,lt1),i1)) :: tl1, (S (g2,k2,(c2,lt2),i2)) :: tl2
1035 when i1 = i2 && g1 = g2 && k1 = k2 && c1 = c2 ->
1036 if Lazy.force lt1 = Lazy.force lt2 then eq_todo tl1 tl2 else false
1040 let eat_head todo id fl orlist =
1041 let rec aux acc = function
1043 | (m, s, _, _, todo1, fl1)::tl as orlist ->
1044 let rec aux1 todo1 =
1045 match first_s todo1 with
1046 | None -> orlist, acc
1047 | Some (((gno,_,_),_,_,_), todo11) ->
1048 (* TODO confronto tra todo da ottimizzare *)
1049 if gno = id && eq_todo todo11 todo then
1050 aux (list_union fl1 acc) tl
1058 let close_proof p ty menv context =
1060 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
1062 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
1063 naif_closure p menv context
1065 (* XXX capire bene quando aggiungere alla cache *)
1066 let add_to_cache_and_del_from_orlist_if_green_cut
1067 g s m cache key todo orlist fl ctx size minsize
1069 let cache = cache_remove_underinspection cache key in
1070 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
1072 match calculate_closed_goal_ty g s with
1073 | None -> assert false
1074 | Some (canonical_ctx , gty) ->
1075 let goalno,depth,sort = g in
1076 let irl = mk_irl canonical_ctx in
1077 let goal = Cic.Meta(goalno, irl) in
1078 let proof = CicMetaSubst.apply_subst s goal in
1079 let green_proof, closed_proof =
1080 let b = is_a_green_cut proof in
1082 b, (* close_proof proof gty m ctx *) proof
1086 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
1087 if is_a_green_cut key then
1088 (* if the initia goal was closed, we cut alternatives *)
1089 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
1090 let orlist, fl = eat_head todo goalno fl orlist in
1092 if size < minsize then
1093 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
1095 (* if the proof is closed we cache it *)
1096 if green_proof then cache_add_success cache key proof
1097 else (* cache_add_success cache key closed_proof *)
1098 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
1100 cache, orlist, fl, true
1103 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
1104 if size < minsize then
1105 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
1106 (* if the substituted goal and the proof are closed we cache it *)
1107 if is_a_green_cut gty then
1108 if green_proof then cache_add_success cache gty proof
1109 else (* cache_add_success cache gty closed_proof *)
1110 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
1114 CicTypeChecker.type_of_aux' ~subst:s
1115 m ctx closed_proof CicUniv.oblivion_ugraph
1117 if is_a_green_cut ty then
1118 cache_add_success cache ty closed_proof
1121 | CicTypeChecker.TypeCheckerFailure _ ->*)
1122 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
1124 cache, orlist, fl, false
1126 let close_failures (fl : fail list) (cache : cache) =
1128 (fun cache ((gno,depth,_),gty) ->
1129 debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
1130 cache_add_failure cache gty depth)
1133 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
1134 let entry = goalno, (canonical_ctx, t,ty) in
1135 assert_subst_are_disjoint subst [entry];
1136 let subst = entry :: subst in
1137 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1140 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
1141 None,metasenv,subst ,(lazy (Cic.Meta(goalno,mk_irl context))),goalty, []
1144 tables maxm cache depth fake_proof goalno goalty subst context
1147 let active,passive,bag = tables in
1148 let ppterm = ppterm context in
1149 let status = (fake_proof,goalno) in
1150 if flags.use_only_paramod then
1152 debug_print (lazy ("PARAMODULATION SU: " ^
1153 string_of_int goalno ^ " " ^ ppterm goalty ));
1154 let goal_steps, saturation_steps, timeout =
1155 max_int,max_int,flags.timeout
1158 Saturation.given_clause bag maxm status active passive
1159 goal_steps saturation_steps timeout
1161 | None, active, passive, maxmeta ->
1162 [], (active,passive,bag), cache, maxmeta, flags
1163 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
1165 assert_subst_are_disjoint subst subst';
1166 let subst = subst@subst' in
1168 order_new_goals metasenv subst open_goals ppterm
1171 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1174 [(!candidate_no,proof),metasenv,subst,open_goals],
1175 (active,passive,bag),
1176 cache, maxmeta, flags
1182 ("SUBSUMPTION SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty));
1184 Saturation.all_subsumed bag maxm status active passive
1186 assert (maxmeta >= maxm);
1189 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
1190 assert_subst_are_disjoint subst subst';
1191 let subst = subst@subst' in
1193 order_new_goals metasenv subst open_goals ppterm
1196 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1199 (!candidate_no,proof),metasenv,subst,open_goals)
1202 res', (active,passive,bag), cache, maxmeta, flags
1207 goalty tables maxm subst fake_proof goalno depth context cand
1209 let ppterm = ppterm context in
1211 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1212 (PrimitiveTactics.apply_with_subst ~subst ~maxmeta:maxm ~term:cand)
1215 debug_print (lazy (" OK: " ^ ppterm cand));
1216 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
1217 let open_goals = order_new_goals metasenv subst open_goals ppterm in
1218 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
1220 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables , maxmeta
1222 | ProofEngineTypes.Fail s -> None,tables, maxm
1223 | CicUnification.Uncertain s -> None,tables, maxm
1226 let sort_new_elems =
1227 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
1228 List.length (prop_only l1) - List.length (prop_only l2))
1231 let applicative_case
1232 tables maxm depth subst fake_proof goalno goalty metasenv context universe
1235 let candidates = get_candidates universe cache goalty in
1236 let tables, elems, maxm =
1238 (fun (tables,elems,maxm) cand ->
1240 try_candidate goalty
1241 tables maxm subst fake_proof goalno depth context cand
1243 | None, tables,maxm -> tables,elems, maxm
1244 | Some x, tables, maxm -> tables,x::elems, maxm)
1245 (tables,[],maxm) candidates
1247 let elems = sort_new_elems elems in
1248 elems, tables, cache, maxm
1251 let equational_and_applicative_case
1252 universe flags m s g gty tables cache maxm context
1254 let goalno, depth, sort = g in
1255 let fake_proof = mk_fake_proof m s g gty context in
1256 if is_equational_case gty flags then
1257 let elems,tables,cache,maxm1, flags =
1258 equational_case tables maxm cache
1259 depth fake_proof goalno gty s context flags
1262 let more_elems, tables, cache, maxm1 =
1263 if flags.use_only_paramod then
1264 [],tables, cache, maxm
1267 tables maxm depth s fake_proof goalno
1268 gty m context universe cache
1271 elems@more_elems, tables, cache, maxm, flags
1273 let elems, tables, cache, maxm =
1274 applicative_case tables maxm depth s fake_proof goalno
1275 gty m context universe cache
1277 elems, tables, cache, maxm, flags
1279 let rec condition_for_hint i = function
1281 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
1282 | _::tl -> condition_for_hint i tl
1284 let remove_s_from_fl (id,_,_) (fl : fail list) =
1285 let rec aux = function
1287 | ((id1,_,_),_)::tl when id = id1 -> tl
1288 | hd::tl -> hd :: aux tl
1293 let prunable_for_size flags s m todo =
1294 let rec aux b = function
1295 | (S _)::tl -> aux b tl
1296 | (D (_,_,T))::tl -> aux b tl
1298 (match calculate_goal_ty g s m with
1300 | Some (canonical_ctx, gty) ->
1302 Utils.weight_of_term
1303 ~consider_metas:false ~count_metas_occurrences:true gty in
1304 let newb = b || gsize > flags.maxgoalsizefactor in
1311 let prunable ty todo =
1312 let rec aux b = function
1313 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
1314 | (D (_,_,T))::tl -> aux b tl
1322 let prunable menv subst ty todo =
1323 let rec aux = function
1324 | (S(_,k,_,_))::tl ->
1325 (match Equality.meta_convertibility_subst k ty menv with
1328 no_progress variant tl (* || aux tl*))
1329 | (D (_,_,T))::tl -> aux tl
1331 and no_progress variant = function
1332 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
1333 | D ((n,_,P) as g)::tl ->
1334 (match calculate_goal_ty g subst menv with
1335 | None -> no_progress variant tl
1337 (match calculate_goal_ty g variant menv with
1338 | None -> assert false
1341 no_progress variant tl
1343 | _::tl -> no_progress variant tl
1348 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
1350 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
1352 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
1354 let filter_prune_hint l =
1355 let prune = !prune_hint in
1356 prune_hint := []; (* possible race... *)
1357 if prune = [] then l
1358 else List.filter (condition_for_prune_hint prune) l
1360 let auto_main tables maxm context flags universe cache elems =
1361 auto_context := context;
1362 let rec aux tables maxm flags cache (elems : status) =
1363 (* pp_status context elems; *)
1364 (* DEBUGGING CODE: uncomment these two lines to stop execution at each iteration
1365 auto_status := elems;
1368 let elems = filter_prune_hint elems in
1370 | (m, s, size, don, todo, fl)::orlist when !hint <> None ->
1372 | Some i when condition_for_hint i todo ->
1373 aux tables maxm flags cache orlist
1376 aux tables maxm flags cache elems)
1378 (* complete failure *)
1379 Gaveup (tables, cache, maxm)
1380 | (m, s, _, _, [],_)::orlist ->
1381 (* complete success *)
1382 Proved (m, s, orlist, tables, cache, maxm)
1383 | (m, s, size, don, (D (_,_,T))::todo, fl)::orlist
1384 when not flags.AutoTypes.do_types ->
1385 (* skip since not Prop, don't even check if closed by side-effect *)
1386 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1387 | (m, s, size, don, (S(g, key, c,minsize) as op)::todo, fl)::orlist ->
1388 (* partial success, cache g and go on *)
1389 let cache, orlist, fl, sibling_pruned =
1390 add_to_cache_and_del_from_orlist_if_green_cut
1391 g s m cache key todo orlist fl context size minsize
1393 debug_print (lazy (AutoCache.cache_print context cache));
1394 let fl = remove_s_from_fl g fl in
1395 let don = if sibling_pruned then don else op::don in
1396 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1397 | (m, s, size, don, todo, fl)::orlist
1398 when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
1399 debug_print (lazy ("FAIL: WIDTH"));
1400 (* too many goals in and generated by last th *)
1401 let cache = close_failures fl cache in
1402 aux tables maxm flags cache orlist
1403 | (m, s, size, don, todo, fl)::orlist when size > flags.maxsize ->
1405 (lazy ("FAIL: SIZE: "^string_of_int size ^
1406 " > " ^ string_of_int flags.maxsize ));
1407 (* we already have a too large proof term *)
1408 let cache = close_failures fl cache in
1409 aux tables maxm flags cache orlist
1410 | _ when Unix.gettimeofday () > flags.timeout ->
1412 debug_print (lazy ("FAIL: TIMEOUT"));
1413 Gaveup (tables, cache, maxm)
1414 | (m, s, size, don, (D (gno,depth,_ as g))::todo, fl)::orlist as status ->
1416 match calculate_goal_ty g s m with
1418 (* closed by side effect *)
1419 debug_print (lazy ("SUCCESS: SIDE EFFECT: " ^ string_of_int gno));
1420 aux tables maxm flags cache ((m,s,size,don,todo, fl)::orlist)
1421 | Some (canonical_ctx, gty) ->
1423 Utils.weight_of_term ~consider_metas:false ~count_metas_occurrences:true gty
1425 if gsize > flags.maxgoalsizefactor then
1426 (debug_print (lazy ("FAIL: SIZE: goal: "^string_of_int gsize));
1427 aux tables maxm flags cache orlist)
1428 else if prunable_for_size flags s m todo then
1429 (debug_print (lazy ("POTO at depth: "^(string_of_int depth)));
1430 aux tables maxm flags cache orlist)
1432 (* still to be proved *)
1433 (debug_print (lazy ("EXAMINE: "^CicPp.ppterm gty));
1434 match cache_examine cache gty with
1435 | Failed_in d when d >= depth ->
1437 debug_print (lazy ("FAIL: DEPTH (cache): "^string_of_int gno));
1438 let cache = close_failures fl cache in
1439 aux tables maxm flags cache orlist
1440 | UnderInspection ->
1442 debug_print (lazy ("FAIL: LOOP: " ^ string_of_int gno));
1443 let cache = close_failures fl cache in
1444 aux tables maxm flags cache orlist
1446 debug_print (lazy ("SUCCESS: CACHE HIT: " ^ string_of_int gno));
1447 let s, m = put_in_subst s m g canonical_ctx t gty in
1448 aux tables maxm flags cache ((m, s, size, don,todo, fl)::orlist)
1450 | Failed_in _ when depth > 0 ->
1451 ( (* more depth or is the first time we see the goal *)
1452 if prunable m s gty todo then
1454 "FAIL: LOOP: one father is equal"));
1455 aux tables maxm flags cache orlist)
1457 let cache = cache_add_underinspection cache gty depth in
1458 auto_status := status;
1461 (lazy ("INSPECTING: " ^
1462 string_of_int gno ^ "("^ string_of_int size ^ "): "^
1464 (* elems are possible computations for proving gty *)
1465 let elems, tables, cache, maxm, flags =
1466 equational_and_applicative_case
1467 universe flags m s g gty tables cache maxm context
1470 (* this goal has failed *)
1471 let cache = close_failures ((g,gty)::fl) cache in
1472 aux tables maxm flags cache orlist
1474 (* elems = (cand,m,s,gl) *)
1475 let size_gl l = List.length
1476 (List.filter (function (_,_,P) -> true | _ -> false) l)
1479 let inj_gl gl = List.map (fun g -> D g) gl in
1480 let rec map = function
1481 | [] -> assert false
1482 | (cand,m,s,gl)::[] ->
1483 (* in the last one we add the failure *)
1485 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1487 (* we are the last in OR, we fail on g and
1488 * also on all failures implied by g *)
1489 (m,s, size + size_gl gl, don, todo, (g,gty)::fl)
1491 | (cand,m,s,gl)::tl ->
1492 (* we add the S step after gl and before todo *)
1494 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1496 (* since we are not the last in OR, we do not
1498 (m,s, size + size_gl gl, don, todo, []) :: map tl
1502 aux tables maxm flags cache elems)
1505 debug_print (lazy ("FAIL: DEPTH: " ^ string_of_int gno));
1506 let cache = close_failures fl cache in
1507 aux tables maxm flags cache orlist)
1509 (aux tables maxm flags cache elems : auto_result)
1514 auto_all_solutions maxm tables universe cache context metasenv gl flags
1516 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1519 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
1521 let elems = [metasenv,[],1,[],goals,[]] in
1522 let rec aux tables maxm solutions cache elems flags =
1523 match auto_main tables maxm context flags universe cache elems with
1524 | Gaveup (tables,cache,maxm) ->
1525 solutions,cache,maxm
1526 | Proved (metasenv,subst,others,tables,cache,maxm) ->
1527 if Unix.gettimeofday () > flags.timeout then
1528 ((subst,metasenv)::solutions), cache, maxm
1530 aux tables maxm ((subst,metasenv)::solutions) cache others flags
1532 let rc = aux tables maxm [] cache elems flags in
1534 | [],cache,maxm -> [],cache,maxm
1535 | solutions,cache,maxm ->
1538 (fun (subst,newmetasenv) ->
1540 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
1542 if opened = [] then Some subst else None)
1545 solutions,cache,maxm
1548 (* }}} ****************** AUTO ***************)
1550 let auto flags metasenv tables universe cache context metasenv gl =
1551 let initial_time = Unix.gettimeofday() in
1552 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1553 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
1554 let elems = [metasenv,[],1,[],goals,[]] in
1555 match auto_main tables 0 context flags universe cache elems with
1556 | Proved (metasenv,subst,_, tables,cache,_) ->
1558 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1559 Some (subst,metasenv), cache
1560 | Gaveup (tables,cache,maxm) ->
1562 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1566 let applyS_tac ~dbd ~term ~params ~universe =
1567 ProofEngineTypes.mk_tactic
1571 apply_smart ~dbd ~term ~subst:[] ~params ~universe status
1575 | CicUnification.UnificationFailure msg
1576 | CicTypeChecker.TypeCheckerFailure msg ->
1577 raise (ProofEngineTypes.Fail msg))
1579 let auto_tac ~(dbd:HSql.dbd) ~params:(univ,params) ~universe (proof, goal) =
1580 let _,metasenv,_subst,_,_, _ = proof in
1581 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
1582 let universe = universe_of_params metasenv context universe univ in
1583 let flags = flags_of_params params () in
1584 let use_library = flags.use_library in
1585 let tables,cache,newmeta =
1586 init_cache_and_tables ~dbd use_library flags.use_only_paramod true
1587 false universe (proof, goal) in
1588 let tables,cache,newmeta =
1589 if flags.close_more then
1591 tables newmeta context (proof, goal)
1592 auto_all_solutions universe cache
1593 else tables,cache,newmeta in
1594 let initial_time = Unix.gettimeofday() in
1595 let (_,oldmetasenv,_subst,_,_, _) = proof in
1598 metasenv,[],1,[],[D (goal,flags.maxdepth,P)],[]
1600 match auto_main tables newmeta context flags universe cache [elem] with
1601 | Proved (metasenv,subst,_, tables,cache,_) ->
1603 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time));*)
1604 let proof,metasenv =
1605 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1606 proof goal subst metasenv
1609 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
1610 ~newmetasenv:metasenv
1613 | Gaveup (tables,cache,maxm) ->
1616 string_of_float(Unix.gettimeofday()-.initial_time)));
1617 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
1620 let auto_tac ~dbd ~params ~universe =
1621 ProofEngineTypes.mk_tactic (auto_tac ~params ~dbd ~universe);;
1623 let eq_of_goal = function
1624 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1626 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1629 (* performs steps of rewrite with the universe, obtaining if possible
1631 let solve_rewrite_tac ~universe ~params:(univ,params) (proof,goal as status)=
1632 let _,metasenv,_subst,_,_,_ = proof in
1633 let _,context,ty = CicUtil.lookup_meta goal metasenv in
1634 let steps = int_of_string (string params "steps" "1") in
1635 let universe = universe_of_params metasenv context universe univ in
1636 let eq_uri = eq_of_goal ty in
1637 let (active,passive,bag), cache, maxm =
1638 (* we take the whole universe (no signature filtering) *)
1639 init_cache_and_tables false true false true universe (proof,goal)
1641 let initgoal = [], metasenv, ty in
1643 let equalities = (Saturation.list_of_passive passive) in
1644 (* we demodulate using both actives passives *)
1645 List.fold_left (fun tbl eq -> Indexing.index tbl eq) (snd active) equalities
1647 let env = metasenv,context,CicUniv.oblivion_ugraph in
1648 match Indexing.solve_demodulating bag env table initgoal steps with
1649 | Some (proof, metasenv, newty) ->
1652 | Cic.Appl[Cic.MutInd _;eq_ty;left;_] ->
1653 Equality.Exact (Equality.refl_proof eq_uri eq_ty left)
1657 Equality.build_goal_proof
1658 bag eq_uri proof refl newty [] context metasenv
1660 ProofEngineTypes.apply_tactic
1661 (PrimitiveTactics.apply_tac ~term:proofterm) status
1664 (ProofEngineTypes.Fail (lazy
1665 ("Unable to solve with " ^ string_of_int steps ^ " demodulations")))
1667 let solve_rewrite_tac ~params ~universe () =
1668 ProofEngineTypes.mk_tactic (solve_rewrite_tac ~universe ~params)
1672 let demodulate_tac ~dbd ~universe ~params:(univ, params) (proof,goal)=
1673 let curi,metasenv,_subst,pbo,pty, attrs = proof in
1674 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1675 let universe = universe_of_params metasenv context universe univ in
1676 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1677 let initgoal = [], metasenv, ty in
1678 let eq_uri = eq_of_goal ty in
1679 let (active,passive,bag), cache, maxm =
1680 init_cache_and_tables
1681 ~dbd false true true false universe (proof,goal)
1683 let equalities = (Saturation.list_of_passive passive) in
1684 (* we demodulate using both actives passives *)
1687 (fun tbl eq -> Indexing.index tbl eq)
1688 (snd active) equalities
1690 let changed,(newproof,newmetasenv, newty) =
1691 Indexing.demodulation_goal bag
1692 (metasenv,context,CicUniv.oblivion_ugraph) table initgoal
1696 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1698 Equality.build_goal_proof bag
1699 eq_uri newproof opengoal ty [] context metasenv
1701 let extended_metasenv = (maxm,context,newty)::metasenv in
1702 let extended_status =
1703 (curi,extended_metasenv,_subst,pbo,pty, attrs),goal in
1704 let (status,newgoals) =
1705 ProofEngineTypes.apply_tactic
1706 (PrimitiveTactics.apply_tac ~term:proofterm)
1708 (status,maxm::newgoals)
1710 else (* if newty = ty then *)
1711 raise (ProofEngineTypes.Fail (lazy "no progress"))
1712 (*else ProofEngineTypes.apply_tactic
1713 (ReductionTactics.simpl_tac
1714 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1717 let demodulate_tac ~dbd ~params ~universe =
1718 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~params ~universe);;
1720 let pp_proofterm = Equality.pp_proofterm;;
1722 let revision = "$Revision$";;
1723 let size_and_depth context metasenv t = 100, 100