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);;
34 let is_propositional context sort =
35 match CicReduction.whd context sort with
37 | Cic.Sort (Cic.CProp _) -> true
42 type auto_params = Cic.term list * (string * string) list
46 (* closing a term w.r.t. its metavariables
47 very naif version: it does not take dependencies properly into account *)
49 let naif_closure ?(prefix_name="xxx_") t metasenv context =
50 let metasenv = ProofEngineHelpers.sort_metasenv metasenv in
51 let n = List.length metasenv in
52 let what = List.map (fun (i,cc,ty) -> Cic.Meta(i,[])) metasenv in
55 (fun (i,acc) (_,cc,ty) -> (i-1,Cic.Rel i::acc))
58 let t = CicSubstitution.lift n t in
60 ProofEngineReduction.replace_lifting
61 ~equality:(fun c t1 t2 ->
63 | Cic.Meta(i,_),Cic.Meta(j,_) -> i = j
65 ~context ~what ~with_what ~where:t
69 (fun (n,t) (_,cc,ty) ->
70 n-1, Cic.Lambda(Cic.Name (prefix_name^string_of_int n),
71 CicSubstitution.lift n ty,t))
77 let lambda_close ?prefix_name t menv ctx =
78 let t = naif_closure ?prefix_name t menv ctx in
80 (fun (t,i) -> function
81 | None -> CicSubstitution.subst (Cic.Implicit None) t,i (* delift *)
82 | Some (name, Cic.Decl ty) -> Cic.Lambda (name, ty, t),i+1
83 | Some (name, Cic.Def (bo, ty)) -> Cic.LetIn (name, bo, ty, t),i+1)
84 (t,List.length menv) ctx
87 (* functions for retrieving theorems *)
89 exception FillingFailure of AutoCache.cache * int
91 let rec unfold context = function
92 | Cic.Prod(name,s,t) ->
93 let t' = unfold ((Some (name,Cic.Decl s))::context) t in
95 | t -> ProofEngineReduction.unfold context t
97 let find_library_theorems dbd proof goal =
98 let univ = MetadataQuery.universe_of_goal ~dbd false proof goal in
99 let terms = List.map CicUtil.term_of_uri univ in
102 (t,fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph)))
105 let find_context_theorems context metasenv =
108 (fun (res,i) ctxentry ->
110 | Some (_,Cic.Decl t) ->
111 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
112 | Some (_,Cic.Def (_,t)) ->
113 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
118 let rec is_an_equality = function
119 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
120 when (LibraryObjects.is_eq_URI uri) -> true
121 | Cic.Prod (_, _, t) -> is_an_equality t
125 let partition_equalities =
126 List.partition (fun (_,ty) -> is_an_equality ty)
129 let default_auto maxm _ _ cache _ _ _ _ = [],cache,maxm ;;
131 (* giusto per provare che succede
132 let is_unit_equation context metasenv oldnewmeta term =
133 let head, metasenv, args, newmeta =
134 TermUtil.saturate_term oldnewmeta metasenv context term 0
137 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
139 Some (args,metasenv,newmetas,head,newmeta) *)
141 let is_unit_equation context metasenv oldnewmeta term =
142 let head, metasenv, args, newmeta =
143 TermUtil.saturate_term oldnewmeta metasenv context term 0
145 let propositional_args =
149 let _,_,mt = CicUtil.lookup_meta i metasenv in
151 CicTypeChecker.type_of_aux' metasenv context mt
152 CicUniv.oblivion_ugraph
154 if is_propositional context sort then Some i else None
158 if propositional_args = [] then
160 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
162 Some (args,metasenv,newmetas,head,newmeta)
166 let get_candidates skip_trie_filtering universe cache t =
167 let t = if skip_trie_filtering then Cic.Meta(0,[]) else t in
169 (Universe.get_candidates universe t)@(AutoCache.get_candidates cache t)
172 (lazy ("candidates for " ^ (CicPp.ppterm t) ^ " = " ^
173 (String.concat "\n" (List.map CicPp.ppterm candidates)))) in
174 debug_print debug_msg;
178 let only signature context metasenv t =
181 CicTypeChecker.type_of_aux' metasenv context t CicUniv.oblivion_ugraph
183 let consts = MetadataConstraints.constants_of ty in
184 let b = MetadataConstraints.UriManagerSet.subset consts signature in
185 if b then (prerr_endline ("keeping " ^ (CicPp.ppterm t)); b)
187 let ty' = unfold context ty in
188 let consts' = MetadataConstraints.constants_of ty' in
189 let b = MetadataConstraints.UriManagerSet.subset consts' signature in
190 if not b then prerr_endline ("filtering " ^ (CicPp.ppterm t))
191 else prerr_endline ("keeping " ^ (CicPp.ppterm t)); b
193 | CicTypeChecker.TypeCheckerFailure _ -> assert false
194 | ProofEngineTypes.Fail _ -> false (* unfold may fail *)
197 let not_default_eq_term t =
199 let uri = CicUtil.uri_of_term t in
200 not (LibraryObjects.in_eq_URIs uri)
201 with Invalid_argument _ -> true
203 let retrieve_equations dont_filter signature universe cache context metasenv =
204 match LibraryObjects.eq_URI() with
207 let eq_uri = UriManager.strip_xpointer eq_uri in
208 let fake= Cic.Meta(-1,[]) in
209 let fake_eq = Cic.Appl [Cic.MutInd (eq_uri,0, []);fake;fake;fake] in
210 let candidates = get_candidates false universe cache fake_eq in
211 if dont_filter then candidates
212 else let eq_uri = UriManager.uri_of_uriref eq_uri 0 None in
213 (* let candidates = List.filter not_default_eq_term candidates in *)
215 (only (MetadataConstraints.UriManagerSet.add eq_uri signature)
216 context metasenv) candidates
218 let build_equality bag head args proof newmetas maxmeta =
220 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] ->
222 if args = [] then proof else Cic.Appl (proof::args)
224 let o = !Utils.compare_terms t1 t2 in
225 let stat = (ty,t1,t2,o) in
226 (* let w = compute_equality_weight stat in *)
228 let proof = Equality.Exact p in
229 let e = Equality.mk_equality bag (w, proof, stat, newmetas) in
230 (* to clean the local context of metas *)
231 Equality.fix_metas bag maxmeta e
235 let partition_unit_equalities context metasenv newmeta bag equations =
237 (fun (units,other,maxmeta)(t,ty) ->
238 if not (CicUtil.is_meta_closed t && CicUtil.is_meta_closed ty) then
241 ("Skipping " ^ CicMetaSubst.ppterm_in_context ~metasenv [] t context
242 ^ " since it is not meta closed")
244 units,(t,ty)::other,maxmeta
246 match is_unit_equation context metasenv maxmeta ty with
247 | Some (args,metasenv,newmetas,head,newmeta') ->
248 let maxmeta,equality =
249 build_equality bag head args t newmetas newmeta' in
250 equality::units,other,maxmeta
252 units,(t,ty)::other,maxmeta)
253 ([],[],newmeta) equations
256 (Saturation.make_active [],
257 Saturation.make_passive [],
258 Equality.mk_equality_bag)
261 let init_cache_and_tables
262 ?dbd use_library paramod use_context dont_filter universe (proof, goal)
264 (* the local cache in initially empty *)
265 let cache = AutoCache.cache_empty in
266 let _, metasenv, _subst,_, _, _ = proof in
267 let signature = MetadataQuery.signature_of metasenv goal in
268 let newmeta = CicMkImplicit.new_meta metasenv [] in
269 let _,context,_ = CicUtil.lookup_meta goal metasenv in
270 let ct = if use_context then find_context_theorems context metasenv else [] in
272 (lazy ("ho trovato nel contesto " ^ (string_of_int (List.length ct))));
274 match use_library, dbd with
275 | true, Some dbd -> find_library_theorems dbd metasenv goal
279 (lazy ("ho trovato nella libreria " ^ (string_of_int (List.length lt))));
280 let cache = cache_add_list cache context (ct@lt) in
282 retrieve_equations dont_filter (* true *) signature universe cache context metasenv
285 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
290 CicTypeChecker.type_of_aux'
291 metasenv context t CicUniv.oblivion_ugraph
293 (* retrieve_equations could also return flexible terms *)
294 if is_an_equality ty then Some(t,ty)
297 let ty' = unfold context ty in
298 if is_an_equality ty' then Some(t,ty') else None
299 with ProofEngineTypes.Fail _ -> None)
302 let bag = Equality.mk_equality_bag () in
303 let units, other_equalities, newmeta =
304 partition_unit_equalities context metasenv newmeta bag eqs_and_types
306 (* SIMPLIFICATION STEP
308 let env = (metasenv, context, CicUniv.oblivion_ugraph) in
309 let eq_uri = HExtlib.unopt (LibraryObjects.eq_URI()) in
310 Saturation.simplify_equalities bag eq_uri env units
313 let passive = Saturation.make_passive units in
314 let no = List.length units in
315 let active = Saturation.make_active [] in
316 let active,passive,newmeta =
317 if paramod then active,passive,newmeta
319 Saturation.pump_actives
320 context bag newmeta active passive (no+1) infinity
322 (active,passive,bag),cache,newmeta
324 let fill_hypothesis context metasenv oldnewmeta term tables (universe:Universe.universe) cache auto fast =
325 let head, metasenv, args, newmeta =
326 TermUtil.saturate_term oldnewmeta metasenv context term 0
328 let propositional_args =
332 let _,_,mt = CicUtil.lookup_meta i metasenv in
334 CicTypeChecker.type_of_aux' metasenv context mt
335 CicUniv.oblivion_ugraph
337 if is_propositional context sort then Some i else None
341 let results,cache,newmeta =
342 if propositional_args = [] then
343 let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
344 [args,metasenv,newmetas,head,newmeta],cache,newmeta
348 None,metasenv,term,term (* term non e' significativo *)
352 {AutoTypes.default_flags() with
353 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
354 maxwidth = 2;maxdepth = 2;
355 use_paramod=true;use_only_paramod=false}
357 {AutoTypes.default_flags() with
358 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
359 maxwidth = 2;maxdepth = 4;
360 use_paramod=true;use_only_paramod=false}
362 match auto newmeta tables universe cache context metasenv propositional_args flags with
363 | [],cache,newmeta -> raise (FillingFailure (cache,newmeta))
364 | substs,cache,newmeta ->
368 CicMetaSubst.apply_subst_metasenv subst metasenv
370 let head = CicMetaSubst.apply_subst subst head in
372 List.filter (fun (i,_,_) ->i >= oldnewmeta) metasenv
374 let args = List.map (CicMetaSubst.apply_subst subst) args in
375 let newm = CicMkImplicit.new_meta metasenv subst in
376 args,metasenv,newmetas,head,max newm newmeta)
377 substs, cache, newmeta
379 results,cache,newmeta
381 let build_equalities auto context metasenv tables universe cache newmeta equations =
383 (fun (facts,cache,newmeta) (t,ty) ->
384 (* in any case we add the equation to the cache *)
385 let cache = AutoCache.cache_add_list cache context [(t,ty)] in
387 let saturated,cache,newmeta =
388 fill_hypothesis context metasenv newmeta ty tables universe cache auto true
390 let (active,passive,bag) = tables in
391 let eqs,bag,newmeta =
393 (fun (acc,bag,newmeta) (args,metasenv,newmetas,head,newmeta') ->
394 let maxmeta,equality =
395 build_equality bag head args t newmetas newmeta'
397 equality::acc,bag,maxmeta)
398 ([],bag,newmeta) saturated
400 (eqs@facts, cache, newmeta)
401 with FillingFailure (cache,newmeta) ->
402 (* if filling hypothesis fails we add the equation to
404 (facts,cache,newmeta)
406 ([],cache,newmeta) equations
408 let close_more tables maxmeta context status auto universe cache =
409 let (active,passive,bag) = tables in
410 let proof, goalno = status in
411 let _, metasenv,_subst,_,_, _ = proof in
412 let signature = MetadataQuery.signature_of metasenv goalno in
414 retrieve_equations false signature universe cache context metasenv
420 CicTypeChecker.type_of_aux' metasenv context t
421 CicUniv.oblivion_ugraph in
422 (* retrieve_equations could also return flexible terms *)
423 if is_an_equality ty then Some(t,ty) else None)
425 let units, cache, maxm =
426 build_equalities auto context metasenv tables universe cache maxmeta eqs_and_types in
427 debug_print (lazy (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
428 string_of_int maxm));
430 (fun e -> debug_print (lazy (Equality.string_of_equality e)))
432 debug_print (lazy ">>>>>>>>>>>>>>>>>>>>>>");
433 let passive = Saturation.add_to_passive units passive in
434 let no = List.length units in
435 debug_print (lazy ("No = " ^ (string_of_int no)));
436 let active,passive,newmeta =
437 Saturation.pump_actives context bag maxm active passive (no+1) infinity
439 (active,passive,bag),cache,newmeta
441 let find_context_equalities
442 maxmeta bag context proof (universe:Universe.universe) cache
444 let module C = Cic in
445 let module S = CicSubstitution in
446 let module T = CicTypeChecker in
447 let _,metasenv,_subst,_,_, _ = proof in
448 let newmeta = max (ProofEngineHelpers.new_meta_of_proof ~proof) maxmeta in
449 (* if use_auto is true, we try to close the hypothesis of equational
450 statements using auto; a naif, and probably wrong approach *)
451 let rec aux cache index newmeta = function
452 | [] -> [], newmeta,cache
453 | (Some (_, C.Decl (term)))::tl ->
456 (Printf.sprintf "Examining: %d (%s)" index (CicPp.ppterm term)));
457 let do_find context term =
459 | C.Prod (name, s, t) when is_an_equality t ->
462 let term = S.lift index term in
463 let saturated,cache,newmeta =
464 fill_hypothesis context metasenv newmeta term
465 empty_tables universe cache default_auto false
469 (fun (acc,newmeta) (args,metasenv,newmetas,head,newmeta') ->
470 let newmeta, equality =
472 bag head args (Cic.Rel index) newmetas (max newmeta newmeta')
474 equality::acc, newmeta + 1)
475 ([],newmeta) saturated
478 with FillingFailure (cache,newmeta) ->
480 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
481 when LibraryObjects.is_eq_URI uri ->
482 let term = S.lift index term in
484 build_equality bag term [] (Cic.Rel index) [] newmeta
486 [e], (newmeta+1),cache
487 | _ -> [], newmeta, cache
489 let eqs, newmeta, cache = do_find context term in
490 let rest, newmeta,cache = aux cache (index+1) newmeta tl in
491 List.map (fun x -> index,x) eqs @ rest, newmeta, cache
493 aux cache (index+1) newmeta tl
495 let il, maxm, cache =
496 aux cache 1 newmeta context
498 let indexes, equalities = List.split il in
499 indexes, equalities, maxm, cache
502 (********** PARAMETERS PASSING ***************)
504 let bool params name default =
506 let s = List.assoc name params in
507 if s = "" || s = "1" || s = "true" || s = "yes" || s = "on" then true
508 else if s = "0" || s = "false" || s = "no" || s= "off" then false
510 let msg = "Unrecognized value for parameter "^name^"\n" in
511 let msg = msg^"Accepted values are 1,true,yes,on and 0,false,no,off" in
512 raise (ProofEngineTypes.Fail (lazy msg))
513 with Not_found -> default
516 let string params name default =
517 try List.assoc name params with
518 | Not_found -> default
521 let int params name default =
522 try int_of_string (List.assoc name params) with
523 | Not_found -> default
525 raise (ProofEngineTypes.Fail (lazy (name ^ " must be an integer")))
528 let flags_of_params params ?(for_applyS=false) () =
529 let int = int params in
530 let bool = bool params in
531 let close_more = bool "close_more" false in
532 let use_paramod = bool "use_paramod" true in
533 let skip_trie_filtering = bool "skip_trie_filtering" false in
534 let skip_context = bool "skip_context" false in
535 let use_only_paramod =
536 if for_applyS then true else bool "paramodulation" false in
537 let use_library = bool "library"
538 ((AutoTypes.default_flags()).AutoTypes.use_library) in
539 let depth = int "depth" ((AutoTypes.default_flags()).AutoTypes.maxdepth) in
540 let width = int "width" ((AutoTypes.default_flags()).AutoTypes.maxwidth) in
541 let size = int "size" ((AutoTypes.default_flags()).AutoTypes.maxsize) in
542 let gsize = int "gsize" ((AutoTypes.default_flags()).AutoTypes.maxgoalsizefactor) in
543 let do_type = bool "type" false in
544 let timeout = int "timeout" 0 in
545 { AutoTypes.maxdepth =
546 if use_only_paramod then 2 else depth;
547 AutoTypes.maxwidth = width;
548 AutoTypes.maxsize = size;
551 if for_applyS then Unix.gettimeofday () +. 30.0
555 Unix.gettimeofday() +. (float_of_int timeout);
556 AutoTypes.use_library = use_library;
557 AutoTypes.use_paramod = use_paramod;
558 AutoTypes.use_only_paramod = use_only_paramod;
559 AutoTypes.close_more = close_more;
560 AutoTypes.dont_cache_failures = false;
561 AutoTypes.maxgoalsizefactor = gsize;
562 AutoTypes.do_types = do_type;
563 AutoTypes.skip_trie_filtering = skip_trie_filtering;
564 AutoTypes.skip_context = skip_context;
567 let universe_of_params metasenv context universe tl =
568 if tl = [] then universe else
572 fst (CicTypeChecker.type_of_aux' metasenv context term
573 CicUniv.oblivion_ugraph))
576 Universe.index_list Universe.empty context (List.combine tl tys)
580 (***************** applyS *******************)
582 let new_metasenv_and_unify_and_t
583 dbd flags universe proof goal ?tables newmeta' metasenv'
584 context term' ty termty goal_arity
586 let (consthead,newmetasenv,arguments,_) =
587 TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in
589 match arguments with [] -> term' | _ -> Cic.Appl (term'::arguments)
591 let proof',oldmetasenv =
592 let (puri,metasenv,_subst,pbo,pty, attrs) = proof in
593 (puri,newmetasenv,_subst,pbo,pty, attrs),metasenv
595 let goal_for_paramod =
596 match LibraryObjects.eq_URI () with
598 Cic.Appl [Cic.MutInd (uri,0,[]); Cic.Sort Cic.Prop; consthead; ty]
599 | None -> raise (ProofEngineTypes.Fail (lazy "No equality defined"))
601 let newmeta = CicMkImplicit.new_meta newmetasenv (*subst*) [] in
602 let metasenv_for_paramod = (newmeta,context,goal_for_paramod)::newmetasenv in
604 let uri,_,_subst,p,ty, attrs = proof' in
605 uri,metasenv_for_paramod,_subst,p,ty, attrs
607 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
609 ProofEngineTypes.apply_tactic
610 (EqualityTactics.rewrite_tac ~direction:`RightToLeft
611 ~pattern:(ProofEngineTypes.conclusion_pattern None)
612 (Cic.Meta(newmeta,irl)) [])
615 let goal = match goals with [g] -> g | _ -> assert false in
617 ProofEngineTypes.apply_tactic
618 (PrimitiveTactics.apply_tac term'')
622 let (active, passive,bag), cache, maxmeta =
623 init_cache_and_tables ~dbd flags.use_library true true false universe
626 Saturation.given_clause bag maxmeta (proof'''',newmeta) active passive
627 max_int max_int flags.timeout
630 raise (ProofEngineTypes.Fail (lazy ("FIXME: propaga le tabelle")))
631 | Some (_,proof''''',_), active,passive,_ ->
633 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
634 ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m), active, passive
637 let rec count_prods context ty =
638 match CicReduction.whd context ty with
639 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
643 ~dbd ~term ~subst ~universe ?tables ~params:(univ,params) (proof, goal)
645 let module T = CicTypeChecker in
646 let module R = CicReduction in
647 let module C = Cic in
648 let (_,metasenv,_subst,_,_, _) = proof in
649 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
650 let flags = flags_of_params params ~for_applyS:true () in
651 let universe = universe_of_params metasenv context universe univ in
652 let newmeta = CicMkImplicit.new_meta metasenv subst in
653 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
655 C.Var (uri,exp_named_subst) ->
656 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
657 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
660 exp_named_subst_diff,newmeta',newmetasenvfragment,
661 C.Var (uri,exp_named_subst')
662 | C.Const (uri,exp_named_subst) ->
663 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
664 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
667 exp_named_subst_diff,newmeta',newmetasenvfragment,
668 C.Const (uri,exp_named_subst')
669 | C.MutInd (uri,tyno,exp_named_subst) ->
670 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
671 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
674 exp_named_subst_diff,newmeta',newmetasenvfragment,
675 C.MutInd (uri,tyno,exp_named_subst')
676 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
677 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
678 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
681 exp_named_subst_diff,newmeta',newmetasenvfragment,
682 C.MutConstruct (uri,tyno,consno,exp_named_subst')
683 | _ -> [],newmeta,[],term
685 let metasenv' = metasenv@newmetasenvfragment in
687 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.oblivion_ugraph
689 let termty = CicSubstitution.subst_vars exp_named_subst_diff termty in
690 let goal_arity = count_prods context ty in
691 let proof, gl, active, passive =
692 new_metasenv_and_unify_and_t dbd flags universe proof goal ?tables
693 newmeta' metasenv' context term' ty termty goal_arity
695 proof, gl, active, passive
698 (****************** AUTO ********************)
700 let mk_irl ctx = CicMkImplicit.identity_relocation_list_for_metavariable ctx;;
701 let ugraph = CicUniv.oblivion_ugraph;;
702 let typeof = CicTypeChecker.type_of_aux';;
704 let names = List.map (function None -> None | Some (x,_) -> Some x) ctx in
707 let is_in_prop context subst metasenv ty =
708 let sort,u = typeof ~subst metasenv context ty CicUniv.oblivion_ugraph in
709 is_propositional context sort
712 exception NotConvertible;;
714 let check_proof_is_valid proof metasenv context goalty =
718 let ty,u = typeof metasenv context proof CicUniv.oblivion_ugraph in
719 let b,_ = CicReduction.are_convertible context ty goalty u in
720 if not b then raise NotConvertible else b
723 List.map (function None -> None | Some (x,_) -> Some x) context
725 debug_print (lazy ("PROOF:" ^ CicPp.pp proof names));
726 (* debug_print (lazy ("PROOFTY:" ^ CicPp.pp ty names)); *)
727 debug_print (lazy ("GOAL:" ^ CicPp.pp goalty names));
728 debug_print (lazy ("MENV:" ^ CicMetaSubst.ppmetasenv [] metasenv));
734 let assert_proof_is_valid proof metasenv context goalty =
735 assert (check_proof_is_valid proof metasenv context goalty)
738 let assert_subst_are_disjoint subst subst' =
741 (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst')
746 let split_goals_in_prop metasenv subst gl =
749 let _,context,ty = CicUtil.lookup_meta g metasenv in
751 let sort,u = typeof ~subst metasenv context ty ugraph in
752 is_propositional context sort
754 | CicTypeChecker.AssertFailure s
755 | CicTypeChecker.TypeCheckerFailure s ->
757 (lazy ("NON TIPA" ^ ppterm context (CicMetaSubst.apply_subst subst ty)));
760 (* FIXME... they should type! *)
764 let split_goals_with_metas metasenv subst gl =
767 let _,context,ty = CicUtil.lookup_meta g metasenv in
768 let ty = CicMetaSubst.apply_subst subst ty in
769 CicUtil.is_meta_closed ty)
773 let order_new_goals metasenv subst open_goals ppterm =
774 let prop,rest = split_goals_in_prop metasenv subst open_goals in
775 let closed_prop, open_prop = split_goals_with_metas metasenv subst prop in
776 let closed_type, open_type = split_goals_with_metas metasenv subst rest in
778 (List.map (fun x -> x,P) (open_prop @ closed_prop))
780 (List.map (fun x -> x,T) (open_type @ closed_type))
785 let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty,sort) open_goals
787 debug_print (lazy (" OPEN: "^
791 | (i,t,P) -> string_of_int i ^ ":"^ppterm t^ "Prop"
792 | (i,t,T) -> string_of_int i ^ ":"^ppterm t^ "Type")
797 let is_an_equational_goal = function
798 | Cic.Appl [Cic.MutInd(u,_,_);_;_;_] when LibraryObjects.is_eq_URI u -> true
803 let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
806 let calculate_timeout flags =
807 if flags.timeout = 0. then
808 (debug_print (lazy "AUTO WITH NO TIMEOUT");
809 {flags with timeout = infinity})
813 let is_equational_case goalty flags =
814 let ensure_equational t =
815 if is_an_equational_goal t then true
818 let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
819 raise (ProofEngineTypes.Fail (lazy msg))
822 (flags.use_paramod && is_an_equational_goal goalty) ||
823 (flags.use_only_paramod && ensure_equational goalty)
826 let cache_add_success sort cache k v =
827 if sort = P then cache_add_success cache k v else cache_remove_underinspection
832 type menv = Cic.metasenv
833 type subst = Cic.substitution
834 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
835 let candidate_no = ref 0;;
836 type candidate = int * Cic.term Lazy.t
837 type cache = AutoCache.cache
839 Saturation.active_table * Saturation.passive_table * Equality.equality_bag
842 (* the goal (mainly for depth) and key of the goal *)
843 goal * AutoCache.cache_key
845 (* goal has to be proved *)
847 (* goal has to be cached as a success obtained using candidate as the first
849 | S of goal * AutoCache.cache_key * candidate * int
851 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
852 menv * subst * int * op list * op list * fail list
854 (* list of computations that may lead to the solution: all op list will
855 * end with the same (S(g,_)) *)
858 (* menv, subst, alternatives, tables, cache, maxmeta *)
859 | Proved of menv * subst * elem list * tables * cache * int
860 | Gaveup of tables * cache * int
863 (* the status exported to the external observer *)
865 (* context, (goal,candidate) list, and_list, history *)
866 Cic.context * (int * Cic.term * bool * int * (int * Cic.term Lazy.t) list) list *
867 (int * Cic.term * int) list * Cic.term Lazy.t list
870 let rec aux acc = function
871 | (D g)::tl -> aux (acc@[g]) tl
877 List.filter (function (_,_,P) -> true | _ -> false) l
881 let rec aux acc = function
882 | (D g)::tl -> aux (acc@[g]) tl
883 | (S _)::tl -> aux acc tl
889 let calculate_goal_ty (goalno,_,_) s m =
891 let _,cc,goalty = CicUtil.lookup_meta goalno m in
892 (* XXX applicare la subst al contesto? *)
893 Some (cc, CicMetaSubst.apply_subst s goalty)
894 with CicUtil.Meta_not_found i when i = goalno -> None
897 let calculate_closed_goal_ty (goalno,_,_) s =
899 let cc,_,goalty = List.assoc goalno s in
900 (* XXX applicare la subst al contesto? *)
901 Some (cc, CicMetaSubst.apply_subst s goalty)
902 with Not_found -> None
905 let pp_status ctx status =
907 let names = Utils.names_of_context ctx in
910 ProofEngineReduction.replace
911 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
912 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
916 let string_of_do m s (gi,_,_ as g) d =
917 match calculate_goal_ty g s m with
918 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
919 | None -> Printf.sprintf "D(%d, _, %d)" gi d
921 let string_of_s m su k (ci,ct) gi =
922 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp (Lazy.force ct)) ci
924 let string_of_ol m su l =
928 | D (g,d,s) -> string_of_do m su (g,d,s) d
929 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
932 let string_of_fl m s fl =
934 (List.map (fun ((i,_,_),ty) ->
935 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
937 let rec aux = function
939 | (m,s,_,_,ol,fl)::tl ->
940 Printf.eprintf "< [%s] ;;; [%s]>\n"
941 (string_of_ol m s ol) (string_of_fl m s fl);
944 Printf.eprintf "-------------------------- status -------------------\n";
946 Printf.eprintf "-----------------------------------------------------\n";
949 let auto_status = ref [] ;;
950 let auto_context = ref [];;
951 let in_pause = ref false;;
952 let pause b = in_pause := b;;
953 let cond = Condition.create ();;
954 let mutex = Mutex.create ();;
955 let hint = ref None;;
956 let prune_hint = ref [];;
958 let step _ = Condition.signal cond;;
959 let give_hint n = hint := Some n;;
960 let give_prune_hint hint =
961 prune_hint := hint :: !prune_hint
968 Condition.wait cond mutex;
973 let get_auto_status _ =
974 let status = !auto_status in
975 let and_list,elems,last =
978 | (m,s,_,don,gl,fail)::tl ->
981 (fun (id,d,_ as g) ->
982 match calculate_goal_ty g s m with
983 | Some (_,x) -> Some (id,x,d) | None -> None)
987 (* these are the S goalsin the or list *)
990 (fun (m,s,_,don,gl,fail) ->
992 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
996 (* this function eats id from a list l::[id,x] returning x, l *)
997 let eat_tail_if_eq id l =
998 let rec aux (s, l) = function
1000 | ((id1,_,_),k1,c)::tl when id = id1 ->
1002 | None -> aux (Some c,l) tl
1003 | Some _ -> assert false)
1004 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
1006 let c, l = aux (None, []) l in
1009 let eat_in_parallel id l =
1010 let rec aux (b,eaten, new_l as acc) l =
1014 match eat_tail_if_eq id l with
1015 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
1016 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
1020 let rec eat_all rows l =
1024 match List.rev elem with
1025 | ((to_eat,depth,_),k,_)::next_lunch ->
1026 let b, eaten, l = eat_in_parallel to_eat l in
1027 let eaten = HExtlib.list_uniq eaten in
1028 let eaten = List.rev eaten in
1029 let b = true (* List.hd (List.rev b) *) in
1030 let rows = rows @ [to_eat,k,b,depth,eaten] in
1032 | [] -> eat_all rows or_list
1034 eat_all [] (List.rev orlist)
1038 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
1041 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
1042 and_list, rows, history
1044 !auto_context, elems, and_list, last
1047 (* Works if there is no dependency over proofs *)
1048 let is_a_green_cut goalty =
1049 CicUtil.is_meta_closed goalty
1051 let rec first_s = function
1052 | (D _)::tl -> first_s tl
1053 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
1056 let list_union l1 l2 =
1057 (* TODO ottimizzare compare *)
1058 HExtlib.list_uniq (List.sort compare (l1 @ l1))
1060 let rec eq_todo l1 l2 =
1062 | (D g1) :: tl1,(D g2) :: tl2 when g1=g2 -> eq_todo tl1 tl2
1063 | (S (g1,k1,(c1,lt1),i1)) :: tl1, (S (g2,k2,(c2,lt2),i2)) :: tl2
1064 when i1 = i2 && g1 = g2 && k1 = k2 && c1 = c2 ->
1065 if Lazy.force lt1 = Lazy.force lt2 then eq_todo tl1 tl2 else false
1069 let eat_head todo id fl orlist =
1070 let rec aux acc = function
1072 | (m, s, _, _, todo1, fl1)::tl as orlist ->
1073 let rec aux1 todo1 =
1074 match first_s todo1 with
1075 | None -> orlist, acc
1076 | Some (((gno,_,_),_,_,_), todo11) ->
1077 (* TODO confronto tra todo da ottimizzare *)
1078 if gno = id && eq_todo todo11 todo then
1079 aux (list_union fl1 acc) tl
1087 let close_proof p ty menv context =
1089 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
1091 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
1092 naif_closure p menv context
1094 (* XXX capire bene quando aggiungere alla cache *)
1095 let add_to_cache_and_del_from_orlist_if_green_cut
1096 g s m cache key todo orlist fl ctx size minsize
1098 let cache = cache_remove_underinspection cache key in
1099 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
1101 match calculate_closed_goal_ty g s with
1102 | None -> assert false
1103 | Some (canonical_ctx , gty) ->
1104 let goalno,depth,sort = g in
1105 let irl = mk_irl canonical_ctx in
1106 let goal = Cic.Meta(goalno, irl) in
1107 let proof = CicMetaSubst.apply_subst s goal in
1108 let green_proof, closed_proof =
1109 let b = is_a_green_cut proof in
1111 b, (* close_proof proof gty m ctx *) proof
1115 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
1116 if is_a_green_cut key then
1117 (* if the initia goal was closed, we cut alternatives *)
1118 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
1119 let orlist, fl = eat_head todo goalno fl orlist in
1121 if size < minsize then
1122 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
1124 (* if the proof is closed we cache it *)
1125 if green_proof then cache_add_success cache key proof
1126 else (* cache_add_success cache key closed_proof *)
1127 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
1129 cache, orlist, fl, true
1132 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
1133 if size < minsize then
1134 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
1135 (* if the substituted goal and the proof are closed we cache it *)
1136 if is_a_green_cut gty then
1137 if green_proof then cache_add_success cache gty proof
1138 else (* cache_add_success cache gty closed_proof *)
1139 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
1143 CicTypeChecker.type_of_aux' ~subst:s
1144 m ctx closed_proof CicUniv.oblivion_ugraph
1146 if is_a_green_cut ty then
1147 cache_add_success cache ty closed_proof
1150 | CicTypeChecker.TypeCheckerFailure _ ->*)
1151 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
1153 cache, orlist, fl, false
1155 let close_failures (fl : fail list) (cache : cache) =
1157 (fun cache ((gno,depth,_),gty) ->
1158 debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
1159 cache_add_failure cache gty depth)
1162 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
1163 let entry = goalno, (canonical_ctx, t,ty) in
1164 assert_subst_are_disjoint subst [entry];
1165 let subst = entry :: subst in
1166 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1170 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
1171 None,metasenv,subst ,(lazy (Cic.Meta(goalno,mk_irl context))),goalty, []
1174 tables maxm cache depth fake_proof goalno goalty subst context
1177 let active,passive,bag = tables in
1178 let ppterm = ppterm context in
1179 let status = (fake_proof,goalno) in
1180 if flags.use_only_paramod then
1182 debug_print (lazy ("PARAMODULATION SU: " ^
1183 string_of_int goalno ^ " " ^ ppterm goalty ));
1184 let goal_steps, saturation_steps, timeout =
1185 max_int,max_int,flags.timeout
1189 Saturation.given_clause bag maxm status active passive
1190 goal_steps saturation_steps timeout
1192 | None, active, passive, maxmeta ->
1193 [], (active,passive,bag), cache, maxmeta, flags
1194 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
1196 assert_subst_are_disjoint subst subst';
1197 let subst = subst@subst' in
1199 order_new_goals metasenv subst open_goals ppterm
1202 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1205 [(!candidate_no,proof),metasenv,subst,open_goals],
1206 (active,passive,bag),
1207 cache, maxmeta, flags
1213 ("SUBSUMPTION SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty));
1215 Saturation.all_subsumed bag maxm status active passive
1217 assert (maxmeta >= maxm);
1220 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
1221 assert_subst_are_disjoint subst subst';
1222 let subst = subst@subst' in
1224 order_new_goals metasenv subst open_goals ppterm
1227 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1230 (!candidate_no,proof),metasenv,subst,open_goals)
1233 res', (active,passive,bag), cache, maxmeta, flags
1238 goalty tables maxm subst fake_proof goalno depth context cand
1240 let ppterm = ppterm context in
1242 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1243 (PrimitiveTactics.apply_with_subst ~subst ~maxmeta:maxm ~term:cand)
1246 debug_print (lazy (" OK: " ^ ppterm cand));
1247 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
1248 let open_goals = order_new_goals metasenv subst open_goals ppterm in
1249 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
1251 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables , maxmeta
1253 | ProofEngineTypes.Fail s -> None,tables, maxm
1254 | CicUnification.Uncertain s -> None,tables, maxm
1257 let sort_new_elems =
1258 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
1259 List.length (prop_only l1) - List.length (prop_only l2))
1262 let applicative_case
1263 tables maxm depth subst fake_proof goalno goalty metasenv context universe
1266 let candidates = get_candidates flags.skip_trie_filtering universe cache goalty in
1267 let tables, elems, maxm =
1269 (fun (tables,elems,maxm) cand ->
1271 try_candidate goalty
1272 tables maxm subst fake_proof goalno depth context cand
1274 | None, tables,maxm -> tables,elems, maxm
1275 | Some x, tables, maxm -> tables,x::elems, maxm)
1276 (tables,[],maxm) candidates
1278 let elems = sort_new_elems elems in
1279 elems, tables, cache, maxm
1282 let equational_and_applicative_case
1283 universe flags m s g gty tables cache maxm context
1285 let goalno, depth, sort = g in
1286 let fake_proof = mk_fake_proof m s g gty context in
1287 if is_equational_case gty flags then
1288 let elems,tables,cache,maxm1, flags =
1289 equational_case tables maxm cache
1290 depth fake_proof goalno gty s context flags
1293 let more_elems, tables, cache, maxm1 =
1294 if flags.use_only_paramod then
1295 [],tables, cache, maxm
1298 tables maxm depth s fake_proof goalno
1299 gty m context universe cache flags
1302 elems@more_elems, tables, cache, maxm, flags
1304 let elems, tables, cache, maxm =
1305 applicative_case tables maxm depth s fake_proof goalno
1306 gty m context universe cache flags
1308 elems, tables, cache, maxm, flags
1310 let rec condition_for_hint i = function
1312 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
1313 | _::tl -> condition_for_hint i tl
1315 let remove_s_from_fl (id,_,_) (fl : fail list) =
1316 let rec aux = function
1318 | ((id1,_,_),_)::tl when id = id1 -> tl
1319 | hd::tl -> hd :: aux tl
1324 let prunable_for_size flags s m todo =
1325 let rec aux b = function
1326 | (S _)::tl -> aux b tl
1327 | (D (_,_,T))::tl -> aux b tl
1329 (match calculate_goal_ty g s m with
1331 | Some (canonical_ctx, gty) ->
1333 Utils.weight_of_term
1334 ~consider_metas:false ~count_metas_occurrences:true gty in
1335 let newb = b || gsize > flags.maxgoalsizefactor in
1342 let prunable ty todo =
1343 let rec aux b = function
1344 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
1345 | (D (_,_,T))::tl -> aux b tl
1353 let prunable menv subst ty todo =
1354 let rec aux = function
1355 | (S(_,k,_,_))::tl ->
1356 (match Equality.meta_convertibility_subst k ty menv with
1359 no_progress variant tl (* || aux tl*))
1360 | (D (_,_,T))::tl -> aux tl
1362 and no_progress variant = function
1363 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
1364 | D ((n,_,P) as g)::tl ->
1365 (match calculate_goal_ty g subst menv with
1366 | None -> no_progress variant tl
1368 (match calculate_goal_ty g variant menv with
1369 | None -> assert false
1372 no_progress variant tl
1374 | _::tl -> no_progress variant tl
1379 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
1381 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
1383 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
1385 let filter_prune_hint l =
1386 let prune = !prune_hint in
1387 prune_hint := []; (* possible race... *)
1388 if prune = [] then l
1389 else List.filter (condition_for_prune_hint prune) l
1391 let auto_main tables maxm context flags universe cache elems =
1392 auto_context := context;
1393 let rec aux tables maxm flags cache (elems : status) =
1394 (* pp_status context elems; *)
1395 (* DEBUGGING CODE: uncomment these two lines to stop execution at each iteration
1396 auto_status := elems;
1399 let elems = filter_prune_hint elems in
1401 | (m, s, size, don, todo, fl)::orlist when !hint <> None ->
1402 debug_print (lazy "skip");
1404 | Some i when condition_for_hint i todo ->
1405 aux tables maxm flags cache orlist
1408 aux tables maxm flags cache elems)
1410 (* complete failure *)
1411 debug_print (lazy "give up");
1412 Gaveup (tables, cache, maxm)
1413 | (m, s, _, _, [],_)::orlist ->
1414 (* complete success *)
1415 debug_print (lazy "success");
1416 Proved (m, s, orlist, tables, cache, maxm)
1417 | (m, s, size, don, (D (_,_,T))::todo, fl)::orlist
1418 when not flags.AutoTypes.do_types ->
1419 (* skip since not Prop, don't even check if closed by side-effect *)
1420 debug_print (lazy "skip existential goal");
1421 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1422 | (m, s, size, don, (S(g, key, c,minsize) as op)::todo, fl)::orlist ->
1423 (* partial success, cache g and go on *)
1424 let cache, orlist, fl, sibling_pruned =
1425 add_to_cache_and_del_from_orlist_if_green_cut
1426 g s m cache key todo orlist fl context size minsize
1428 debug_print (lazy (AutoCache.cache_print context cache));
1429 let fl = remove_s_from_fl g fl in
1430 let don = if sibling_pruned then don else op::don in
1431 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1432 | (m, s, size, don, todo, fl)::orlist
1433 when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
1434 debug_print (lazy ("FAIL: WIDTH"));
1435 (* too many goals in and generated by last th *)
1436 let cache = close_failures fl cache in
1437 aux tables maxm flags cache orlist
1438 | (m, s, size, don, todo, fl)::orlist when size > flags.maxsize ->
1440 (lazy ("FAIL: SIZE: "^string_of_int size ^
1441 " > " ^ string_of_int flags.maxsize ));
1442 (* we already have a too large proof term *)
1443 let cache = close_failures fl cache in
1444 aux tables maxm flags cache orlist
1445 | _ when Unix.gettimeofday () > flags.timeout ->
1447 debug_print (lazy ("FAIL: TIMEOUT"));
1448 Gaveup (tables, cache, maxm)
1449 | (m, s, size, don, (D (gno,depth,_ as g))::todo, fl)::orlist as status ->
1451 debug_print (lazy "attack goal");
1452 match calculate_goal_ty g s m with
1454 (* closed by side effect *)
1455 debug_print (lazy ("SUCCESS: SIDE EFFECT: " ^ string_of_int gno));
1456 aux tables maxm flags cache ((m,s,size,don,todo, fl)::orlist)
1457 | Some (canonical_ctx, gty) ->
1459 Utils.weight_of_term ~consider_metas:false ~count_metas_occurrences:true gty
1461 if gsize > flags.maxgoalsizefactor then
1462 (debug_print (lazy ("FAIL: SIZE: goal: "^string_of_int gsize));
1463 aux tables maxm flags cache orlist)
1464 else if prunable_for_size flags s m todo then
1465 (debug_print (lazy ("POTO at depth: "^(string_of_int depth)));
1466 aux tables maxm flags cache orlist)
1468 (* still to be proved *)
1469 (debug_print (lazy ("EXAMINE: "^CicPp.ppterm gty));
1470 match cache_examine cache gty with
1471 | Failed_in d when d >= depth ->
1473 debug_print (lazy ("FAIL: DEPTH (cache): "^string_of_int gno));
1474 let cache = close_failures fl cache in
1475 aux tables maxm flags cache orlist
1476 | UnderInspection ->
1478 debug_print (lazy ("FAIL: LOOP: " ^ string_of_int gno));
1479 let cache = close_failures fl cache in
1480 aux tables maxm flags cache orlist
1482 debug_print (lazy ("SUCCESS: CACHE HIT: " ^ string_of_int gno));
1483 let s, m = put_in_subst s m g canonical_ctx t gty in
1484 aux tables maxm flags cache ((m, s, size, don,todo, fl)::orlist)
1486 | Failed_in _ when depth > 0 ->
1487 ( (* more depth or is the first time we see the goal *)
1488 if prunable m s gty todo then
1490 "FAIL: LOOP: one father is equal"));
1491 aux tables maxm flags cache orlist)
1493 let cache = cache_add_underinspection cache gty depth in
1494 auto_status := status;
1497 (lazy ("INSPECTING: " ^
1498 string_of_int gno ^ "("^ string_of_int size ^ "): "^
1500 (* elems are possible computations for proving gty *)
1501 let elems, tables, cache, maxm, flags =
1502 equational_and_applicative_case
1503 universe flags m s g gty tables cache maxm context
1506 (* this goal has failed *)
1507 let cache = close_failures ((g,gty)::fl) cache in
1508 aux tables maxm flags cache orlist
1510 (* elems = (cand,m,s,gl) *)
1511 let size_gl l = List.length
1512 (List.filter (function (_,_,P) -> true | _ -> false) l)
1515 let inj_gl gl = List.map (fun g -> D g) gl in
1516 let rec map = function
1517 | [] -> assert false
1518 | (cand,m,s,gl)::[] ->
1519 (* in the last one we add the failure *)
1521 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1523 (* we are the last in OR, we fail on g and
1524 * also on all failures implied by g *)
1525 (m,s, size + size_gl gl, don, todo, (g,gty)::fl)
1527 | (cand,m,s,gl)::tl ->
1528 (* we add the S step after gl and before todo *)
1530 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1532 (* since we are not the last in OR, we do not
1534 (m,s, size + size_gl gl, don, todo, []) :: map tl
1538 aux tables maxm flags cache elems)
1541 debug_print (lazy ("FAIL: DEPTH: " ^ string_of_int gno));
1542 let cache = close_failures fl cache in
1543 aux tables maxm flags cache orlist)
1545 (aux tables maxm flags cache elems : auto_result)
1550 auto_all_solutions maxm tables universe cache context metasenv gl flags
1552 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1555 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
1557 let elems = [metasenv,[],1,[],goals,[]] in
1558 let rec aux tables maxm solutions cache elems flags =
1559 match auto_main tables maxm context flags universe cache elems with
1560 | Gaveup (tables,cache,maxm) ->
1561 solutions,cache,maxm
1562 | Proved (metasenv,subst,others,tables,cache,maxm) ->
1563 if Unix.gettimeofday () > flags.timeout then
1564 ((subst,metasenv)::solutions), cache, maxm
1566 aux tables maxm ((subst,metasenv)::solutions) cache others flags
1568 let rc = aux tables maxm [] cache elems flags in
1570 | [],cache,maxm -> [],cache,maxm
1571 | solutions,cache,maxm ->
1574 (fun (subst,newmetasenv) ->
1576 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
1578 if opened = [] then Some subst else None)
1581 solutions,cache,maxm
1584 (* }}} ****************** AUTO ***************)
1586 let auto flags metasenv tables universe cache context metasenv gl =
1587 let initial_time = Unix.gettimeofday() in
1588 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1589 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
1590 let elems = [metasenv,[],1,[],goals,[]] in
1591 match auto_main tables 0 context flags universe cache elems with
1592 | Proved (metasenv,subst,_, tables,cache,_) ->
1594 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1595 Some (subst,metasenv), cache
1596 | Gaveup (tables,cache,maxm) ->
1598 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1602 let applyS_tac ~dbd ~term ~params ~universe =
1603 ProofEngineTypes.mk_tactic
1607 apply_smart ~dbd ~term ~subst:[] ~params ~universe status
1611 | CicUnification.UnificationFailure msg
1612 | CicTypeChecker.TypeCheckerFailure msg ->
1613 raise (ProofEngineTypes.Fail msg))
1615 let auto_tac ~(dbd:HSql.dbd) ~params:(univ,params) ~universe (proof, goal) =
1616 let _,metasenv,_subst,_,_, _ = proof in
1617 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
1618 let flags = flags_of_params params () in
1619 let universe = universe_of_params metasenv context universe univ in
1620 let use_library = flags.use_library in
1621 let tables,cache,newmeta =
1622 init_cache_and_tables ~dbd use_library flags.use_only_paramod (not flags.skip_context)
1623 false universe (proof, goal) in
1624 let tables,cache,newmeta =
1625 if flags.close_more then
1627 tables newmeta context (proof, goal)
1628 auto_all_solutions universe cache
1629 else tables,cache,newmeta in
1630 let initial_time = Unix.gettimeofday() in
1631 let (_,oldmetasenv,_subst,_,_, _) = proof in
1634 metasenv,[],1,[],[D (goal,flags.maxdepth,P)],[]
1636 match auto_main tables newmeta context flags universe cache [elem] with
1637 | Proved (metasenv,subst,_, tables,cache,_) ->
1639 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1640 let proof,metasenv =
1641 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1642 proof goal subst metasenv
1645 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
1646 ~newmetasenv:metasenv
1649 | Gaveup (tables,cache,maxm) ->
1652 string_of_float(Unix.gettimeofday()-.initial_time)));
1653 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
1656 let auto_tac ~dbd ~params ~universe =
1657 ProofEngineTypes.mk_tactic (auto_tac ~params ~dbd ~universe);;
1659 let eq_of_goal = function
1660 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1662 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1665 (* performs steps of rewrite with the universe, obtaining if possible
1667 let solve_rewrite_tac ~universe ~params:(univ,params) (proof,goal as status)=
1668 let _,metasenv,_subst,_,_,_ = proof in
1669 let _,context,ty = CicUtil.lookup_meta goal metasenv in
1670 let steps = int_of_string (string params "steps" "1") in
1671 let universe = universe_of_params metasenv context universe univ in
1672 let eq_uri = eq_of_goal ty in
1673 let (active,passive,bag), cache, maxm =
1674 (* we take the whole universe (no signature filtering) *)
1675 init_cache_and_tables false true false true universe (proof,goal)
1677 let initgoal = [], metasenv, ty in
1679 let equalities = (Saturation.list_of_passive passive) in
1680 (* we demodulate using both actives passives *)
1681 List.fold_left (fun tbl eq -> Indexing.index tbl eq) (snd active) equalities
1683 let env = metasenv,context,CicUniv.oblivion_ugraph in
1684 match Indexing.solve_demodulating bag env table initgoal steps with
1685 | Some (proof, metasenv, newty) ->
1688 | Cic.Appl[Cic.MutInd _;eq_ty;left;_] ->
1689 Equality.Exact (Equality.refl_proof eq_uri eq_ty left)
1693 Equality.build_goal_proof
1694 bag eq_uri proof refl newty [] context metasenv
1696 ProofEngineTypes.apply_tactic
1697 (PrimitiveTactics.apply_tac ~term:proofterm) status
1700 (ProofEngineTypes.Fail (lazy
1701 ("Unable to solve with " ^ string_of_int steps ^ " demodulations")))
1703 let solve_rewrite_tac ~params ~universe () =
1704 ProofEngineTypes.mk_tactic (solve_rewrite_tac ~universe ~params)
1707 (* Demodulate thorem *)
1708 let open_type ty bo =
1709 let rec open_type_aux context ty k args =
1711 | Cic.Prod (n,s,t) ->
1713 FreshNamesGenerator.mk_fresh_name [] context n ~typ:s ~subst:[] in
1714 let entry = match n' with
1715 | Cic.Name _ -> Some (n',(Cic.Decl s))
1716 | Cic.Anonymous -> None
1718 open_type_aux (entry::context) t (k+1) ((Cic.Rel k)::args)
1719 | Cic.LetIn (n,s,sty,t) ->
1720 let entry = Some (n,(Cic.Def (s,sty)))
1722 open_type_aux (entry::context) t (k+1) args
1723 | _ -> context, ty, args
1725 let context, ty, args = open_type_aux [] ty 1 [] in
1727 | [] -> context, ty, bo
1728 | _ -> context, ty, Cic.Appl (bo::args)
1731 let rec close_type bo ty context =
1733 | [] -> assert_proof_is_valid bo [] [] ty; (bo,ty)
1734 | Some (n,(Cic.Decl s))::tl ->
1735 close_type (Cic.Lambda (n,s,bo)) (Cic.Prod (n,s,ty)) tl
1736 | Some (n,(Cic.Def (s,sty)))::tl ->
1737 close_type (Cic.LetIn (n,s,sty,bo)) (Cic.LetIn (n,s,sty,ty)) tl
1741 let is_subsumed univ context ty =
1742 let candidates = Universe.get_candidates univ ty in
1746 | Some found -> Some found
1749 let mk_irl = CicMkImplicit.identity_relocation_list_for_metavariable in
1750 let metasenv = [(0,context,ty)] in
1751 let fake_proof = None,metasenv,[] , (lazy (Cic.Meta(0,mk_irl context))),ty,[] in
1752 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1753 (PrimitiveTactics.apply_with_subst ~subst:[] ~maxmeta:0 ~term:cand) (fake_proof,0)
1755 let prop_goals, other = split_goals_in_prop metasenv subst open_goals in
1756 if prop_goals = [] then Some cand else None
1758 | ProofEngineTypes.Fail s -> None
1759 | CicUnification.Uncertain s -> None
1763 let demodulate_theorem ~universe uri =
1765 match LibraryObjects.eq_URI () with
1767 | None -> raise (ProofEngineTypes.Fail (lazy "equality not declared")) in
1768 let obj,_ = CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
1772 | Cic.Constant(n, _, ty ,_, _) -> open_type ty (Cic.Const(uri,[]))
1773 | _ -> raise (ProofEngineTypes.Fail (lazy "not a theorem"))
1775 if CicUtil.is_closed ty then
1776 raise (ProofEngineTypes.Fail (lazy ("closed term: dangerous reduction")));
1777 let initgoal = [], [], ty in
1778 (* compute the signature *)
1780 let ty_set = MetadataConstraints.constants_of ty in
1781 let hyp_set = MetadataQuery.signature_of_hypothesis context [] in
1782 let set = MetadataConstraints.UriManagerSet.union ty_set hyp_set in
1783 MetadataQuery.close_with_types set [] context
1785 (* retrieve equations from the universe universe *)
1787 retrieve_equations true signature universe AutoCache.cache_empty context []
1790 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
1795 CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph
1797 (* retrieve_equations could also return flexible terms *)
1798 if is_an_equality ty then Some(t,ty)
1801 let ty' = unfold context ty in
1802 if is_an_equality ty' then Some(t,ty') else None
1803 with ProofEngineTypes.Fail _ -> None)
1806 let bag = Equality.mk_equality_bag () in
1808 let units, _, newmeta =
1809 partition_unit_equalities context [] (CicMkImplicit.new_meta [] []) bag eqs_and_types
1813 (fun tbl eq -> Indexing.index tbl eq)
1814 Indexing.empty units
1816 let changed,(newproof,newmetasenv, newty) =
1818 ([],context,CicUniv.oblivion_ugraph) table initgoal in
1821 let oldproof = Equality.Exact bo in
1823 Equality.build_goal_proof (~contextualize:false) (~forward:true) bag
1824 eq_uri newproof oldproof ty [] context newmetasenv
1826 if newmetasenv <> [] then
1827 raise (ProofEngineTypes.Fail (lazy ("metasenv not empty")))
1830 assert_proof_is_valid proofterm newmetasenv context newty;
1831 match is_subsumed universe context newty with
1833 (ProofEngineTypes.Fail (lazy ("subsumed by " ^ CicPp.ppterm t)))
1834 | None -> close_type proofterm newty context
1837 else (* if newty = ty then *)
1838 raise (ProofEngineTypes.Fail (lazy "no progress"))
1839 (*else ProofEngineTypes.apply_tactic
1840 (ReductionTactics.simpl_tac
1841 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1845 (* NEW DEMODULATE *)
1846 let demodulate_tac ~dbd ~universe ~params:(univ, params) (proof,goal)=
1847 let curi,metasenv,_subst,pbo,pty, attrs = proof in
1848 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1849 let universe = universe_of_params metasenv context universe univ in
1850 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1851 let initgoal = [], metasenv, ty in
1853 match LibraryObjects.eq_URI () with
1855 | None -> raise (ProofEngineTypes.Fail (lazy "equality not declared")) in
1856 (* let eq_uri = eq_of_goal ty in *)
1857 let (active,passive,bag), cache, maxm =
1858 init_cache_and_tables
1859 ~dbd false false true true universe (proof,goal)
1861 let equalities = (Saturation.list_of_passive passive) in
1862 (* we demodulate using both actives passives *)
1865 (fun tbl eq -> Indexing.index tbl eq)
1866 (snd active) equalities
1868 let changed,(newproof,newmetasenv, newty) =
1869 (* Indexing.demodulation_goal bag *)
1871 (metasenv,context,CicUniv.oblivion_ugraph) table initgoal
1875 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1877 Equality.build_goal_proof (~contextualize:false) bag
1878 eq_uri newproof opengoal ty [] context metasenv
1880 let extended_metasenv = (maxm,context,newty)::metasenv in
1881 let extended_status =
1882 (curi,extended_metasenv,_subst,pbo,pty, attrs),goal in
1883 let (status,newgoals) =
1884 ProofEngineTypes.apply_tactic
1885 (PrimitiveTactics.apply_tac ~term:proofterm)
1887 (status,maxm::newgoals)
1889 else (* if newty = ty then *)
1890 raise (ProofEngineTypes.Fail (lazy "no progress"))
1891 (*else ProofEngineTypes.apply_tactic
1892 (ReductionTactics.simpl_tac
1893 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1896 let demodulate_tac ~dbd ~params ~universe =
1897 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~params ~universe);;
1899 let pp_proofterm = Equality.pp_proofterm;;
1901 let revision = "$Revision$";;
1902 let size_and_depth context metasenv t = 100, 100