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);;
35 (* closing a term w.r.t. its metavariables
36 very naif version: it does not take dependencies properly into account *)
38 let naif_closure ?(prefix_name="xxx_") t metasenv context =
39 let metasenv = ProofEngineHelpers.sort_metasenv metasenv in
40 let n = List.length metasenv in
41 let what = List.map (fun (i,cc,ty) -> Cic.Meta(i,[])) metasenv in
44 (fun (i,acc) (_,cc,ty) -> (i-1,Cic.Rel i::acc))
47 let t = CicSubstitution.lift n t in
49 ProofEngineReduction.replace_lifting
50 ~equality:(fun c t1 t2 ->
52 | Cic.Meta(i,_),Cic.Meta(j,_) -> i = j
54 ~context ~what ~with_what ~where:t
58 (fun (n,t) (_,cc,ty) ->
59 n-1, Cic.Lambda(Cic.Name (prefix_name^string_of_int n),
60 CicSubstitution.lift n ty,t))
66 let lambda_close ?prefix_name t menv ctx =
67 let t = naif_closure ?prefix_name t menv ctx in
69 (fun (t,i) -> function
70 | None -> CicSubstitution.subst (Cic.Implicit None) t,i (* delift *)
71 | Some (name, Cic.Decl ty) -> Cic.Lambda (name, ty, t),i+1
72 | Some (name, Cic.Def (bo, _)) -> Cic.LetIn (name, bo, t),i+1)
73 (t,List.length menv) ctx
76 (* functions for retrieving theorems *)
78 exception FillingFailure of AutoCache.cache * int
80 let rec unfold context = function
81 | Cic.Prod(name,s,t) ->
82 let t' = unfold ((Some (name,Cic.Decl s))::context) t in
84 | t -> ProofEngineReduction.unfold context t
86 let find_library_theorems dbd proof goal =
87 let univ = MetadataQuery.universe_of_goal ~dbd false proof goal in
88 let terms = List.map CicUtil.term_of_uri univ in
91 (t,fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph)))
94 let find_context_theorems context metasenv =
97 (fun (res,i) ctxentry ->
99 | Some (_,Cic.Decl t) ->
100 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
101 | Some (_,Cic.Def (_,Some t)) ->
102 (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
103 | Some (_,Cic.Def (_,None)) ->
106 CicTypeChecker.type_of_aux'
107 metasenv context t CicUniv.empty_ugraph
114 let rec is_an_equality = function
115 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _]
116 when (LibraryObjects.is_eq_URI uri) -> true
117 | Cic.Prod (_, _, t) -> is_an_equality t
121 let partition_equalities =
122 List.partition (fun (_,ty) -> is_an_equality ty)
125 let default_auto maxm _ _ cache _ _ _ _ = [],cache,maxm ;;
128 let is_unit_equation context metasenv oldnewmeta term =
129 let head, metasenv, args, newmeta =
130 TermUtil.saturate_term oldnewmeta metasenv context term 0
132 let propositional_args =
136 let _,_,mt = CicUtil.lookup_meta i metasenv in
138 CicTypeChecker.type_of_aux' metasenv context mt
142 CicReduction.are_convertible ~metasenv context
143 sort (Cic.Sort Cic.Prop) u
145 if b then Some i else None
149 if propositional_args = [] then
151 List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv
153 Some (args,metasenv,newmetas,head,newmeta)
157 let get_candidates universe cache t =
159 (Universe.get_candidates universe t)@(AutoCache.get_candidates cache t)
162 (lazy ("candidates for " ^ (CicPp.ppterm t) ^ " = " ^
163 (String.concat "\n" (List.map CicPp.ppterm candidates)))) in
164 debug_print debug_msg;
168 let only signature context metasenv t =
171 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
173 let consts = MetadataConstraints.constants_of ty in
174 let b = MetadataConstraints.UriManagerSet.subset consts signature in
177 let ty' = unfold context ty in
178 let consts' = MetadataConstraints.constants_of ty' in
179 MetadataConstraints.UriManagerSet.subset consts' signature
181 | CicTypeChecker.TypeCheckerFailure _ -> assert false
182 | ProofEngineTypes.Fail _ -> false (* unfold may fail *)
185 let not_default_eq_term t =
187 let uri = CicUtil.uri_of_term t in
188 not (LibraryObjects.in_eq_URIs uri)
189 with Invalid_argument _ -> true
191 let retrieve_equations dont_filter signature universe cache context metasenv =
192 match LibraryObjects.eq_URI() with
195 let eq_uri = UriManager.strip_xpointer eq_uri in
196 let fake= Cic.Meta(-1,[]) in
197 let fake_eq = Cic.Appl [Cic.MutInd (eq_uri,0, []);fake;fake;fake] in
198 let candidates = get_candidates universe cache fake_eq in
199 if dont_filter then candidates
201 let candidates = List.filter not_default_eq_term candidates in
202 List.filter (only signature context metasenv) candidates
204 let build_equality bag head args proof newmetas maxmeta =
206 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] ->
208 if args = [] then proof else Cic.Appl (proof::args)
210 let o = !Utils.compare_terms t1 t2 in
211 let stat = (ty,t1,t2,o) in
212 (* let w = compute_equality_weight stat in *)
214 let proof = Equality.Exact p in
215 let e = Equality.mk_equality bag (w, proof, stat, newmetas) in
216 (* to clean the local context of metas *)
217 Equality.fix_metas bag maxmeta e
221 let partition_unit_equalities context metasenv newmeta bag equations =
223 (fun (units,other,maxmeta)(t,ty) ->
224 if not (CicUtil.is_meta_closed t && CicUtil.is_meta_closed ty) then
227 ("Skipping " ^ CicMetaSubst.ppterm_in_context ~metasenv [] t context
228 ^ " since it is not meta closed")
230 units,(t,ty)::other,maxmeta
232 match is_unit_equation context metasenv maxmeta ty with
233 | Some (args,metasenv,newmetas,head,newmeta') ->
234 let maxmeta,equality =
235 build_equality bag head args t newmetas newmeta' in
236 equality::units,other,maxmeta
238 units,(t,ty)::other,maxmeta)
239 ([],[],newmeta) equations
242 (Saturation.make_active [],
243 Saturation.make_passive [],
244 Equality.mk_equality_bag)
246 let init_cache_and_tables
247 ?dbd use_library paramod use_context dont_filter universe (proof, goal)
249 (* the local cache in initially empty *)
250 let cache = AutoCache.cache_empty in
251 let _, metasenv, _subst,_, _, _ = proof in
252 let signature = MetadataQuery.signature_of metasenv goal in
253 let newmeta = CicMkImplicit.new_meta metasenv [] in
254 let _,context,_ = CicUtil.lookup_meta goal metasenv in
255 let ct = if use_context then find_context_theorems context metasenv else [] in
257 (lazy ("ho trovato nel contesto " ^ (string_of_int (List.length ct))));
259 match use_library, dbd with
260 | true, Some dbd -> find_library_theorems dbd metasenv goal
264 (lazy ("ho trovato nella libreria " ^ (string_of_int (List.length lt))));
265 let cache = cache_add_list cache context (ct@lt) in
267 retrieve_equations dont_filter signature universe cache context metasenv
270 (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
275 CicTypeChecker.type_of_aux'
276 metasenv context t CicUniv.empty_ugraph
278 (* retrieve_equations could also return flexible terms *)
279 if is_an_equality ty then Some(t,ty)
282 let ty' = unfold context ty in
283 if is_an_equality ty' then Some(t,ty') else None
284 with ProofEngineTypes.Fail _ -> None)
287 let bag = Equality.mk_equality_bag () in
288 let units, other_equalities, newmeta =
289 partition_unit_equalities context metasenv newmeta bag eqs_and_types
291 (* SIMPLIFICATION STEP
293 let env = (metasenv, context, CicUniv.empty_ugraph) in
294 let eq_uri = HExtlib.unopt (LibraryObjects.eq_URI()) in
295 Saturation.simplify_equalities bag eq_uri env units
298 let passive = Saturation.make_passive units in
299 let no = List.length units in
300 let active = Saturation.make_active [] in
301 let active,passive,newmeta =
302 if paramod then active,passive,newmeta
304 Saturation.pump_actives
305 context bag newmeta active passive (no+1) infinity
307 (active,passive,bag),cache,newmeta
309 let fill_hypothesis context metasenv oldnewmeta term tables (universe:Universe.universe) cache auto fast =
310 let head, metasenv, args, newmeta =
311 TermUtil.saturate_term oldnewmeta metasenv context term 0
313 let propositional_args =
317 let _,_,mt = CicUtil.lookup_meta i metasenv in
319 CicTypeChecker.type_of_aux' metasenv context mt
323 CicReduction.are_convertible ~metasenv context
324 sort (Cic.Sort Cic.Prop) u
326 if b then Some i else None
330 let results,cache,newmeta =
331 if propositional_args = [] then
332 let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
333 [args,metasenv,newmetas,head,newmeta],cache,newmeta
337 None,metasenv,term,term (* term non e' significativo *)
341 {AutoTypes.default_flags() with
342 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
343 maxwidth = 2;maxdepth = 2;
344 use_paramod=true;use_only_paramod=false}
346 {AutoTypes.default_flags() with
347 AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
348 maxwidth = 2;maxdepth = 4;
349 use_paramod=true;use_only_paramod=false}
351 match auto newmeta tables universe cache context metasenv propositional_args flags with
352 | [],cache,newmeta -> raise (FillingFailure (cache,newmeta))
353 | substs,cache,newmeta ->
357 CicMetaSubst.apply_subst_metasenv subst metasenv
359 let head = CicMetaSubst.apply_subst subst head in
361 List.filter (fun (i,_,_) ->i >= oldnewmeta) metasenv
363 let args = List.map (CicMetaSubst.apply_subst subst) args in
364 let newm = CicMkImplicit.new_meta metasenv subst in
365 args,metasenv,newmetas,head,max newm newmeta)
366 substs, cache, newmeta
368 results,cache,newmeta
370 let build_equalities auto context metasenv tables universe cache newmeta equations =
372 (fun (facts,cache,newmeta) (t,ty) ->
373 (* in any case we add the equation to the cache *)
374 let cache = AutoCache.cache_add_list cache context [(t,ty)] in
376 let saturated,cache,newmeta =
377 fill_hypothesis context metasenv newmeta ty tables universe cache auto true
379 let (active,passive,bag) = tables in
380 let eqs,bag,newmeta =
382 (fun (acc,bag,newmeta) (args,metasenv,newmetas,head,newmeta') ->
383 let maxmeta,equality =
384 build_equality bag head args t newmetas newmeta'
386 equality::acc,bag,maxmeta)
387 ([],bag,newmeta) saturated
389 (eqs@facts, cache, newmeta)
390 with FillingFailure (cache,newmeta) ->
391 (* if filling hypothesis fails we add the equation to
393 (facts,cache,newmeta)
395 ([],cache,newmeta) equations
397 let close_more tables maxmeta context status auto universe cache =
398 let (active,passive,bag) = tables in
399 let proof, goalno = status in
400 let _, metasenv,_subst,_,_, _ = proof in
401 let signature = MetadataQuery.signature_of metasenv goalno in
403 retrieve_equations false signature universe cache context metasenv
409 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph in
410 (* retrieve_equations could also return flexible terms *)
411 if is_an_equality ty then Some(t,ty) else None)
413 let units, cache, maxm =
414 build_equalities auto context metasenv tables universe cache maxmeta eqs_and_types in
415 debug_print (lazy (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
416 string_of_int maxm));
418 (fun e -> debug_print (lazy (Equality.string_of_equality e)))
420 debug_print (lazy ">>>>>>>>>>>>>>>>>>>>>>");
421 let passive = Saturation.add_to_passive units passive in
422 let no = List.length units in
423 debug_print (lazy ("No = " ^ (string_of_int no)));
424 let active,passive,newmeta =
425 Saturation.pump_actives context bag maxm active passive (no+1) infinity
427 (active,passive,bag),cache,newmeta
429 let find_context_equalities
430 maxmeta bag context proof (universe:Universe.universe) cache
432 let module C = Cic in
433 let module S = CicSubstitution in
434 let module T = CicTypeChecker in
435 let _,metasenv,_subst,_,_, _ = proof in
436 let newmeta = max (ProofEngineHelpers.new_meta_of_proof ~proof) maxmeta in
437 (* if use_auto is true, we try to close the hypothesis of equational
438 statements using auto; a naif, and probably wrong approach *)
439 let rec aux cache index newmeta = function
440 | [] -> [], newmeta,cache
441 | (Some (_, C.Decl (term)))::tl ->
444 (Printf.sprintf "Examining: %d (%s)" index (CicPp.ppterm term)));
445 let do_find context term =
447 | C.Prod (name, s, t) when is_an_equality t ->
450 let term = S.lift index term in
451 let saturated,cache,newmeta =
452 fill_hypothesis context metasenv newmeta term
453 empty_tables universe cache default_auto false
457 (fun (acc,newmeta) (args,metasenv,newmetas,head,newmeta') ->
458 let newmeta, equality =
460 bag head args (Cic.Rel index) newmetas (max newmeta newmeta')
462 equality::acc, newmeta + 1)
463 ([],newmeta) saturated
466 with FillingFailure (cache,newmeta) ->
468 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
469 when LibraryObjects.is_eq_URI uri ->
470 let term = S.lift index term in
472 build_equality bag term [] (Cic.Rel index) [] newmeta
474 [e], (newmeta+1),cache
475 | _ -> [], newmeta, cache
477 let eqs, newmeta, cache = do_find context term in
478 let rest, newmeta,cache = aux cache (index+1) newmeta tl in
479 List.map (fun x -> index,x) eqs @ rest, newmeta, cache
481 aux cache (index+1) newmeta tl
483 let il, maxm, cache =
484 aux cache 1 newmeta context
486 let indexes, equalities = List.split il in
487 indexes, equalities, maxm, cache
490 (***************** applyS *******************)
492 let new_metasenv_and_unify_and_t
493 dbd flags universe proof goal ?tables newmeta' metasenv'
494 context term' ty termty goal_arity
496 let (consthead,newmetasenv,arguments,_) =
497 TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in
499 match arguments with [] -> term' | _ -> Cic.Appl (term'::arguments)
501 let proof',oldmetasenv =
502 let (puri,metasenv,_subst,pbo,pty, attrs) = proof in
503 (puri,newmetasenv,_subst,pbo,pty, attrs),metasenv
505 let goal_for_paramod =
506 match LibraryObjects.eq_URI () with
508 Cic.Appl [Cic.MutInd (uri,0,[]); Cic.Sort Cic.Prop; consthead; ty]
509 | None -> raise (ProofEngineTypes.Fail (lazy "No equality defined"))
511 let newmeta = CicMkImplicit.new_meta newmetasenv (*subst*) [] in
512 let metasenv_for_paramod = (newmeta,context,goal_for_paramod)::newmetasenv in
514 let uri,_,_subst,p,ty, attrs = proof' in
515 uri,metasenv_for_paramod,_subst,p,ty, attrs
517 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
519 ProofEngineTypes.apply_tactic
520 (EqualityTactics.rewrite_tac ~direction:`RightToLeft
521 ~pattern:(ProofEngineTypes.conclusion_pattern None)
522 (Cic.Meta(newmeta,irl)) [])
525 let goal = match goals with [g] -> g | _ -> assert false in
527 ProofEngineTypes.apply_tactic
528 (PrimitiveTactics.apply_tac term'')
532 let (active, passive,bag), cache, maxmeta =
533 init_cache_and_tables ~dbd flags.use_library true true false universe
536 Saturation.given_clause bag maxmeta (proof'''',newmeta) active passive
537 max_int max_int flags.timeout
540 raise (ProofEngineTypes.Fail (lazy ("FIXME: propaga le tabelle")))
541 | Some (_,proof''''',_), active,passive,_ ->
543 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
544 ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m), active, passive
547 let rec count_prods context ty =
548 match CicReduction.whd context ty with
549 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
552 let apply_smart ~dbd ~term ~subst ~universe ?tables flags (proof, goal) =
553 let module T = CicTypeChecker in
554 let module R = CicReduction in
555 let module C = Cic in
556 let (_,metasenv,_subst,_,_, _) = proof in
557 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
558 let newmeta = CicMkImplicit.new_meta metasenv subst in
559 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
561 C.Var (uri,exp_named_subst) ->
562 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
563 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
566 exp_named_subst_diff,newmeta',newmetasenvfragment,
567 C.Var (uri,exp_named_subst')
568 | C.Const (uri,exp_named_subst) ->
569 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
570 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
573 exp_named_subst_diff,newmeta',newmetasenvfragment,
574 C.Const (uri,exp_named_subst')
575 | C.MutInd (uri,tyno,exp_named_subst) ->
576 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
577 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
580 exp_named_subst_diff,newmeta',newmetasenvfragment,
581 C.MutInd (uri,tyno,exp_named_subst')
582 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
583 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
584 PrimitiveTactics.generalize_exp_named_subst_with_fresh_metas context newmeta uri
587 exp_named_subst_diff,newmeta',newmetasenvfragment,
588 C.MutConstruct (uri,tyno,consno,exp_named_subst')
589 | _ -> [],newmeta,[],term
591 let metasenv' = metasenv@newmetasenvfragment in
593 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.empty_ugraph
595 let termty = CicSubstitution.subst_vars exp_named_subst_diff termty in
596 let goal_arity = count_prods context ty in
597 let proof, gl, active, passive =
598 new_metasenv_and_unify_and_t dbd flags universe proof goal ?tables
599 newmeta' metasenv' context term' ty termty goal_arity
601 proof, gl, active, passive
604 (****************** AUTO ********************)
606 let mk_irl ctx = CicMkImplicit.identity_relocation_list_for_metavariable ctx;;
607 let ugraph = CicUniv.empty_ugraph;;
608 let typeof = CicTypeChecker.type_of_aux';;
610 let names = List.map (function None -> None | Some (x,_) -> Some x) ctx in
613 let is_in_prop context subst metasenv ty =
614 let sort,u = typeof ~subst metasenv context ty CicUniv.empty_ugraph in
615 fst (CicReduction.are_convertible context sort (Cic.Sort Cic.Prop) u)
618 let assert_proof_is_valid proof metasenv context goalty =
621 let ty,u = typeof metasenv context proof CicUniv.empty_ugraph in
622 let b,_ = CicReduction.are_convertible context ty goalty u in
626 List.map (function None -> None | Some (x,_) -> Some x) context
628 debug_print (lazy ("PROOF:" ^ CicPp.pp proof names));
629 debug_print (lazy ("PROOFTY:" ^ CicPp.pp ty names));
630 debug_print (lazy ("GOAL:" ^ CicPp.pp goalty names));
631 debug_print (lazy ("MENV:" ^ CicMetaSubst.ppmetasenv [] metasenv));
638 let assert_subst_are_disjoint subst subst' =
641 (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst')
646 let split_goals_in_prop metasenv subst gl =
649 let _,context,ty = CicUtil.lookup_meta g metasenv in
651 let sort,u = typeof ~subst metasenv context ty ugraph in
653 CicReduction.are_convertible
654 ~subst ~metasenv context sort (Cic.Sort Cic.Prop) u in
657 | CicTypeChecker.AssertFailure s
658 | CicTypeChecker.TypeCheckerFailure s ->
660 (lazy ("NON TIPA" ^ ppterm context (CicMetaSubst.apply_subst subst ty)));
663 (* FIXME... they should type! *)
667 let split_goals_with_metas metasenv subst gl =
670 let _,context,ty = CicUtil.lookup_meta g metasenv in
671 let ty = CicMetaSubst.apply_subst subst ty in
672 CicUtil.is_meta_closed ty)
676 let order_new_goals metasenv subst open_goals ppterm =
677 let prop,rest = split_goals_in_prop metasenv subst open_goals in
678 let closed_prop, open_prop = split_goals_with_metas metasenv subst prop in
680 (List.map (fun x -> x,P) (closed_prop @ open_prop))
682 (List.map (fun x -> x,T) rest)
687 let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty,sort) open_goals
689 debug_print (lazy (" OPEN: "^
693 | (i,t,P) -> string_of_int i ^ ":"^ppterm t^ "Prop"
694 | (i,t,T) -> string_of_int i ^ ":"^ppterm t^ "Type")
699 let is_an_equational_goal = function
700 | Cic.Appl [Cic.MutInd(u,_,_);_;_;_] when LibraryObjects.is_eq_URI u -> true
705 let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
708 let calculate_timeout flags =
709 if flags.timeout = 0. then
710 (debug_print (lazy "AUTO WITH NO TIMEOUT");
711 {flags with timeout = infinity})
715 let is_equational_case goalty flags =
716 let ensure_equational t =
717 if is_an_equational_goal t then true
720 let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
721 raise (ProofEngineTypes.Fail (lazy msg))
724 (flags.use_paramod && is_an_equational_goal goalty) ||
725 (flags.use_only_paramod && ensure_equational goalty)
728 let cache_add_success sort cache k v =
729 if sort = P then cache_add_success cache k v else cache_remove_underinspection
734 type menv = Cic.metasenv
735 type subst = Cic.substitution
736 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
737 let candidate_no = ref 0;;
738 type candidate = int * Cic.term
739 type cache = AutoCache.cache
741 Saturation.active_table * Saturation.passive_table * Equality.equality_bag
744 (* the goal (mainly for depth) and key of the goal *)
745 goal * AutoCache.cache_key
747 (* goal has to be proved *)
749 (* goal has to be cached as a success obtained using candidate as the first
751 | S of goal * AutoCache.cache_key * candidate * int
753 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
754 menv * subst * int * op list * op list * fail list
756 (* list of computations that may lead to the solution: all op list will
757 * end with the same (S(g,_)) *)
760 (* menv, subst, alternatives, tables, cache, maxmeta *)
761 | Proved of menv * subst * elem list * tables * cache * int
762 | Gaveup of tables * cache * int
765 (* the status exported to the external observer *)
767 (* context, (goal,candidate) list, and_list, history *)
768 Cic.context * (int * Cic.term * bool * int * (int * Cic.term) list) list *
769 (int * Cic.term * int) list * Cic.term list
772 let rec aux acc = function
773 | (D g)::tl -> aux (acc@[g]) tl
779 List.filter (function (_,_,P) -> true | _ -> false) l
783 let rec aux acc = function
784 | (D g)::tl -> aux (acc@[g]) tl
785 | (S _)::tl -> aux acc tl
790 let calculate_goal_ty (goalno,_,_) s m =
792 let _,cc,goalty = CicUtil.lookup_meta goalno m in
793 (* XXX applicare la subst al contesto? *)
794 Some (cc, CicMetaSubst.apply_subst s goalty)
795 with CicUtil.Meta_not_found i when i = goalno -> None
797 let calculate_closed_goal_ty (goalno,_,_) s =
799 let cc,_,goalty = List.assoc goalno s in
800 (* XXX applicare la subst al contesto? *)
801 Some (cc, CicMetaSubst.apply_subst s goalty)
802 with Not_found -> None
804 let pp_status ctx status =
806 let names = Utils.names_of_context ctx in
809 ProofEngineReduction.replace
810 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
811 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
815 let string_of_do m s (gi,_,_ as g) d =
816 match calculate_goal_ty g s m with
817 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
818 | None -> Printf.sprintf "D(%d, _, %d)" gi d
820 let string_of_s m su k (ci,ct) gi =
821 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp ct) ci
823 let string_of_ol m su l =
827 | D (g,d,s) -> string_of_do m su (g,d,s) d
828 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
831 let string_of_fl m s fl =
833 (List.map (fun ((i,_,_),ty) ->
834 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
836 let rec aux = function
838 | (m,s,_,_,ol,fl)::tl ->
839 Printf.eprintf "< [%s] ;;; [%s]>\n"
840 (string_of_ol m s ol) (string_of_fl m s fl);
843 Printf.eprintf "-------------------------- status -------------------\n";
845 Printf.eprintf "-----------------------------------------------------\n";
848 let auto_status = ref [] ;;
849 let auto_context = ref [];;
850 let in_pause = ref false;;
851 let pause b = in_pause := b;;
852 let cond = Condition.create ();;
853 let mutex = Mutex.create ();;
854 let hint = ref None;;
855 let prune_hint = ref [];;
857 let step _ = Condition.signal cond;;
858 let give_hint n = hint := Some n;;
859 let give_prune_hint hint =
860 prune_hint := hint :: !prune_hint
867 Condition.wait cond mutex;
872 let get_auto_status _ =
873 let status = !auto_status in
874 let and_list,elems,last =
877 | (m,s,_,don,gl,fail)::tl ->
880 (fun (id,d,_ as g) ->
881 match calculate_goal_ty g s m with
882 | Some (_,x) -> Some (id,x,d) | None -> None)
886 (* these are the S goalsin the or list *)
889 (fun (m,s,_,don,gl,fail) ->
891 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
895 (* this function eats id from a list l::[id,x] returning x, l *)
896 let eat_tail_if_eq id l =
897 let rec aux (s, l) = function
899 | ((id1,_,_),k1,c)::tl when id = id1 ->
901 | None -> aux (Some c,l) tl
902 | Some _ -> assert false)
903 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
905 let c, l = aux (None, []) l in
908 let eat_in_parallel id l =
909 let rec aux (b,eaten, new_l as acc) l =
913 match eat_tail_if_eq id l with
914 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
915 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
919 let rec eat_all rows l =
923 match List.rev elem with
924 | ((to_eat,depth,_),k,_)::next_lunch ->
925 let b, eaten, l = eat_in_parallel to_eat l in
926 let eaten = HExtlib.list_uniq eaten in
927 let eaten = List.rev eaten in
928 let b = true (* List.hd (List.rev b) *) in
929 let rows = rows @ [to_eat,k,b,depth,eaten] in
931 | [] -> eat_all rows or_list
933 eat_all [] (List.rev orlist)
937 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
940 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
941 and_list, rows, history
943 !auto_context, elems, and_list, last
946 (* Works if there is no dependency over proofs *)
947 let is_a_green_cut goalty =
948 CicUtil.is_meta_closed goalty
950 let rec first_s = function
951 | (D _)::tl -> first_s tl
952 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
955 let list_union l1 l2 =
956 (* TODO ottimizzare compare *)
957 HExtlib.list_uniq (List.sort compare (l1 @ l1))
959 let eat_head todo id fl orlist =
960 let rec aux acc = function
962 | (m, s, _, _, todo1, fl1)::tl as orlist ->
964 match first_s todo1 with
965 | None -> orlist, acc
966 | Some (((gno,_,_),_,_,_), todo11) ->
967 (* TODO confronto tra todo da ottimizzare *)
968 if gno = id && todo11 = todo then
969 aux (list_union fl1 acc) tl
977 let close_proof p ty menv context =
979 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
981 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
982 naif_closure p menv context
984 (* XXX capire bene quando aggiungere alla cache *)
985 let add_to_cache_and_del_from_orlist_if_green_cut
986 g s m cache key todo orlist fl ctx size minsize
988 let cache = cache_remove_underinspection cache key in
989 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
991 match calculate_closed_goal_ty g s with
992 | None -> assert false
993 | Some (canonical_ctx , gty) ->
994 let goalno,depth,sort = g in
995 let irl = mk_irl canonical_ctx in
996 let goal = Cic.Meta(goalno, irl) in
997 let proof = CicMetaSubst.apply_subst s goal in
998 let green_proof, closed_proof =
999 let b = is_a_green_cut proof in
1001 b, (* close_proof proof gty m ctx *) proof
1005 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
1006 if is_a_green_cut key then
1007 (* if the initia goal was closed, we cut alternatives *)
1008 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
1009 let orlist, fl = eat_head todo goalno fl orlist in
1011 if size < minsize then
1012 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
1014 (* if the proof is closed we cache it *)
1015 if green_proof then cache_add_success cache key proof
1016 else (* cache_add_success cache key closed_proof *)
1017 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
1019 cache, orlist, fl, true
1022 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
1023 if size < minsize then
1024 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
1025 (* if the substituted goal and the proof are closed we cache it *)
1026 if is_a_green_cut gty then
1027 if green_proof then cache_add_success cache gty proof
1028 else (* cache_add_success cache gty closed_proof *)
1029 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
1033 CicTypeChecker.type_of_aux' ~subst:s
1034 m ctx closed_proof CicUniv.oblivion_ugraph
1036 if is_a_green_cut ty then
1037 cache_add_success cache ty closed_proof
1040 | CicTypeChecker.TypeCheckerFailure _ ->*)
1041 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
1043 cache, orlist, fl, false
1045 let close_failures (fl : fail list) (cache : cache) =
1047 (fun cache ((gno,depth,_),gty) ->
1048 debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
1049 cache_add_failure cache gty depth)
1052 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
1053 let entry = goalno, (canonical_ctx, t,ty) in
1054 assert_subst_are_disjoint subst [entry];
1055 let subst = entry :: subst in
1056 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1059 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
1060 None,metasenv,subst ,Cic.Meta(goalno,mk_irl context),goalty, []
1063 tables maxm cache depth fake_proof goalno goalty subst context
1066 let active,passive,bag = tables in
1067 let ppterm = ppterm context in
1068 let status = (fake_proof,goalno) in
1069 if flags.use_only_paramod then
1071 debug_print (lazy ("PARAMODULATION SU: " ^
1072 string_of_int goalno ^ " " ^ ppterm goalty ));
1073 let goal_steps, saturation_steps, timeout =
1074 max_int,max_int,flags.timeout
1077 Saturation.given_clause bag maxm status active passive
1078 goal_steps saturation_steps timeout
1080 | None, active, passive, maxmeta ->
1081 [], (active,passive,bag), cache, maxmeta, flags
1082 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
1084 assert_subst_are_disjoint subst subst';
1085 let subst = subst@subst' in
1087 order_new_goals metasenv subst open_goals ppterm
1090 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1093 [(!candidate_no,proof),metasenv,subst,open_goals],
1094 (active,passive,bag),
1095 cache, maxmeta, flags
1101 ("SUBSUMPTION SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty));
1103 Saturation.all_subsumed bag maxm status active passive
1105 assert (maxmeta >= maxm);
1108 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
1109 assert_subst_are_disjoint subst subst';
1110 let subst = subst@subst' in
1112 order_new_goals metasenv subst open_goals ppterm
1115 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
1118 (!candidate_no,proof),metasenv,subst,open_goals)
1121 res', (active,passive,bag), cache, maxmeta, flags
1126 goalty tables maxm subst fake_proof goalno depth context cand
1128 let ppterm = ppterm context in
1130 let subst,((_,metasenv,_,_,_,_), open_goals),maxmeta =
1131 (PrimitiveTactics.apply_with_subst ~subst ~maxmeta:maxm ~term:cand)
1134 debug_print (lazy (" OK: " ^ ppterm cand));
1135 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
1136 let open_goals = order_new_goals metasenv subst open_goals ppterm in
1137 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
1139 Some ((!candidate_no,cand),metasenv,subst,open_goals), tables , maxmeta
1141 | ProofEngineTypes.Fail s -> None,tables, maxm
1142 | CicUnification.Uncertain s -> None,tables, maxm
1145 let sort_new_elems =
1146 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
1147 List.length (prop_only l1) - List.length (prop_only l2))
1150 let applicative_case
1151 tables maxm depth subst fake_proof goalno goalty metasenv context universe
1154 let candidates = get_candidates universe cache goalty in
1155 let tables, elems, maxm =
1157 (fun (tables,elems,maxm) cand ->
1159 try_candidate goalty
1160 tables maxm subst fake_proof goalno depth context cand
1162 | None, tables,maxm -> tables,elems, maxm
1163 | Some x, tables, maxm -> tables,x::elems, maxm)
1164 (tables,[],maxm) candidates
1166 let elems = sort_new_elems elems in
1167 elems, tables, cache, maxm
1170 let equational_and_applicative_case
1171 universe flags m s g gty tables cache maxm context
1173 let goalno, depth, sort = g in
1174 let fake_proof = mk_fake_proof m s g gty context in
1175 if is_equational_case gty flags then
1176 let elems,tables,cache,maxm1, flags =
1177 equational_case tables maxm cache
1178 depth fake_proof goalno gty s context flags
1181 let more_elems, tables, cache, maxm1 =
1182 if flags.use_only_paramod then
1183 [],tables, cache, maxm
1186 tables maxm depth s fake_proof goalno
1187 gty m context universe cache
1190 elems@more_elems, tables, cache, maxm, flags
1192 let elems, tables, cache, maxm =
1193 applicative_case tables maxm depth s fake_proof goalno
1194 gty m context universe cache
1196 elems, tables, cache, maxm, flags
1198 let rec condition_for_hint i = function
1200 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
1201 | _::tl -> condition_for_hint i tl
1203 let remove_s_from_fl (id,_,_) (fl : fail list) =
1204 let rec aux = function
1206 | ((id1,_,_),_)::tl when id = id1 -> tl
1207 | hd::tl -> hd :: aux tl
1212 let prunable_for_size flags s m todo =
1213 let rec aux b = function
1214 | (S _)::tl -> aux b tl
1215 | (D (_,_,T))::tl -> aux b tl
1217 (match calculate_goal_ty g s m with
1219 | Some (canonical_ctx, gty) ->
1221 Utils.weight_of_term
1222 ~consider_metas:false ~count_metas_occurrences:true gty in
1223 let newb = b || gsize > flags.maxgoalsizefactor in
1230 let prunable ty todo =
1231 let rec aux b = function
1232 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
1233 | (D (_,_,T))::tl -> aux b tl
1241 let prunable menv subst ty todo =
1242 let rec aux = function
1243 | (S(_,k,_,_))::tl ->
1244 (match Equality.meta_convertibility_subst k ty menv with
1247 no_progress variant tl (* || aux tl*))
1248 | (D (_,_,T))::tl -> aux tl
1250 and no_progress variant = function
1251 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
1252 | D ((n,_,P) as g)::tl ->
1253 (match calculate_goal_ty g subst menv with
1254 | None -> no_progress variant tl
1256 (match calculate_goal_ty g variant menv with
1257 | None -> assert false
1260 no_progress variant tl
1262 | _::tl -> no_progress variant tl
1267 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
1269 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
1271 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
1273 let filter_prune_hint l =
1274 let prune = !prune_hint in
1275 prune_hint := []; (* possible race... *)
1276 if prune = [] then l
1277 else List.filter (condition_for_prune_hint prune) l
1279 let auto_main tables maxm context flags universe cache elems =
1280 auto_context := context;
1281 let rec aux tables maxm flags cache (elems : status) =
1282 (* pp_status context elems; *)
1283 (* DEBUGGING CODE: uncomment these two lines to stop execution at each iteration
1284 auto_status := elems;
1287 let elems = filter_prune_hint elems in
1289 | (m, s, size, don, todo, fl)::orlist when !hint <> None ->
1291 | Some i when condition_for_hint i todo ->
1292 aux tables maxm flags cache orlist
1295 aux tables maxm flags cache elems)
1297 (* complete failure *)
1298 Gaveup (tables, cache, maxm)
1299 | (m, s, _, _, [],_)::orlist ->
1300 (* complete success *)
1301 Proved (m, s, orlist, tables, cache, maxm)
1302 | (m, s, size, don, (D (_,_,T))::todo, fl)::orlist
1303 when not flags.AutoTypes.do_types ->
1304 (* skip since not Prop, don't even check if closed by side-effect *)
1305 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1306 | (m, s, size, don, (S(g, key, c,minsize) as op)::todo, fl)::orlist ->
1307 (* partial success, cache g and go on *)
1308 let cache, orlist, fl, sibling_pruned =
1309 add_to_cache_and_del_from_orlist_if_green_cut
1310 g s m cache key todo orlist fl context size minsize
1312 debug_print (lazy (AutoCache.cache_print context cache));
1313 let fl = remove_s_from_fl g fl in
1314 let don = if sibling_pruned then don else op::don in
1315 aux tables maxm flags cache ((m, s, size, don, todo, fl)::orlist)
1316 | (m, s, size, don, todo, fl)::orlist
1317 when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
1318 debug_print (lazy ("FAIL: WIDTH"));
1319 (* too many goals in and generated by last th *)
1320 let cache = close_failures fl cache in
1321 aux tables maxm flags cache orlist
1322 | (m, s, size, don, todo, fl)::orlist when size > flags.maxsize ->
1324 (lazy ("FAIL: SIZE: "^string_of_int size ^
1325 " > " ^ string_of_int flags.maxsize ));
1326 (* we already have a too large proof term *)
1327 let cache = close_failures fl cache in
1328 aux tables maxm flags cache orlist
1329 | _ when Unix.gettimeofday () > flags.timeout ->
1331 debug_print (lazy ("FAIL: TIMEOUT"));
1332 Gaveup (tables, cache, maxm)
1333 | (m, s, size, don, (D (gno,depth,_ as g))::todo, fl)::orlist as status ->
1335 match calculate_goal_ty g s m with
1337 (* closed by side effect *)
1338 debug_print (lazy ("SUCCESS: SIDE EFFECT: " ^ string_of_int gno));
1339 aux tables maxm flags cache ((m,s,size,don,todo, fl)::orlist)
1340 | Some (canonical_ctx, gty) ->
1342 Utils.weight_of_term ~consider_metas:false ~count_metas_occurrences:true gty
1344 if gsize > flags.maxgoalsizefactor then
1345 (debug_print (lazy ("FAIL: SIZE: goal: "^string_of_int gsize));
1346 aux tables maxm flags cache orlist)
1347 else if prunable_for_size flags s m todo then
1348 (debug_print (lazy ("POTO at depth: "^(string_of_int depth)));
1349 aux tables maxm flags cache orlist)
1351 (* still to be proved *)
1352 (debug_print (lazy ("EXAMINE: "^CicPp.ppterm gty));
1353 match cache_examine cache gty with
1354 | Failed_in d when d >= depth ->
1356 debug_print (lazy ("FAIL: DEPTH (cache): "^string_of_int gno));
1357 let cache = close_failures fl cache in
1358 aux tables maxm flags cache orlist
1359 | UnderInspection ->
1361 debug_print (lazy ("FAIL: LOOP: " ^ string_of_int gno));
1362 let cache = close_failures fl cache in
1363 aux tables maxm flags cache orlist
1365 debug_print (lazy ("SUCCESS: CACHE HIT: " ^ string_of_int gno));
1366 let s, m = put_in_subst s m g canonical_ctx t gty in
1367 aux tables maxm flags cache ((m, s, size, don,todo, fl)::orlist)
1369 | Failed_in _ when depth > 0 ->
1370 ( (* more depth or is the first time we see the goal *)
1371 if prunable m s gty todo then
1373 "FAIL: LOOP: one father is equal"));
1374 aux tables maxm flags cache orlist)
1376 let cache = cache_add_underinspection cache gty depth in
1377 auto_status := status;
1380 (lazy ("INSPECTING: " ^
1381 string_of_int gno ^ "("^ string_of_int size ^ "): "^
1383 (* elems are possible computations for proving gty *)
1384 let elems, tables, cache, maxm, flags =
1385 equational_and_applicative_case
1386 universe flags m s g gty tables cache maxm context
1389 (* this goal has failed *)
1390 let cache = close_failures ((g,gty)::fl) cache in
1391 aux tables maxm flags cache orlist
1393 (* elems = (cand,m,s,gl) *)
1394 let size_gl l = List.length
1395 (List.filter (function (_,_,P) -> true | _ -> false) l)
1398 let inj_gl gl = List.map (fun g -> D g) gl in
1399 let rec map = function
1400 | [] -> assert false
1401 | (cand,m,s,gl)::[] ->
1402 (* in the last one we add the failure *)
1404 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1406 (* we are the last in OR, we fail on g and
1407 * also on all failures implied by g *)
1408 (m,s, size + size_gl gl, don, todo, (g,gty)::fl)
1410 | (cand,m,s,gl)::tl ->
1411 (* we add the S step after gl and before todo *)
1413 inj_gl gl @ (S(g,gty,cand,size+1))::todo
1415 (* since we are not the last in OR, we do not
1417 (m,s, size + size_gl gl, don, todo, []) :: map tl
1421 aux tables maxm flags cache elems)
1424 debug_print (lazy ("FAIL: DEPTH: " ^ string_of_int gno));
1425 let cache = close_failures fl cache in
1426 aux tables maxm flags cache orlist)
1428 (aux tables maxm flags cache elems : auto_result)
1433 auto_all_solutions maxm tables universe cache context metasenv gl flags
1435 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1438 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
1440 let elems = [metasenv,[],1,[],goals,[]] in
1441 let rec aux tables maxm solutions cache elems flags =
1442 match auto_main tables maxm context flags universe cache elems with
1443 | Gaveup (tables,cache,maxm) ->
1444 solutions,cache,maxm
1445 | Proved (metasenv,subst,others,tables,cache,maxm) ->
1446 if Unix.gettimeofday () > flags.timeout then
1447 ((subst,metasenv)::solutions), cache, maxm
1449 aux tables maxm ((subst,metasenv)::solutions) cache others flags
1451 let rc = aux tables maxm [] cache elems flags in
1453 | [],cache,maxm -> [],cache,maxm
1454 | solutions,cache,maxm ->
1457 (fun (subst,newmetasenv) ->
1459 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
1461 if opened = [] then Some subst else None)
1464 solutions,cache,maxm
1467 (* }}} ****************** AUTO ***************)
1469 let auto flags metasenv tables universe cache context metasenv gl =
1470 let initial_time = Unix.gettimeofday() in
1471 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1472 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
1473 let elems = [metasenv,[],1,[],goals,[]] in
1474 match auto_main tables 0 context flags universe cache elems with
1475 | Proved (metasenv,subst,_, tables,cache,_) ->
1477 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1478 Some (subst,metasenv), cache
1479 | Gaveup (tables,cache,maxm) ->
1481 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1485 let bool params name default =
1487 let s = List.assoc name params in
1488 if s = "" || s = "1" || s = "true" || s = "yes" || s = "on" then true
1489 else if s = "0" || s = "false" || s = "no" || s= "off" then false
1491 let msg = "Unrecognized value for parameter "^name^"\n" in
1492 let msg = msg^"Accepted values are 1,true,yes,on and 0,false,no,off" in
1493 raise (ProofEngineTypes.Fail (lazy msg))
1494 with Not_found -> default
1497 let string params name default =
1498 try List.assoc name params with
1499 | Not_found -> default
1502 let int params name default =
1503 try int_of_string (List.assoc name params) with
1504 | Not_found -> default
1506 raise (ProofEngineTypes.Fail (lazy (name ^ " must be an integer")))
1509 let flags_of_params params ?(for_applyS=false) () =
1510 let int = int params in
1511 let bool = bool params in
1512 let close_more = bool "close_more" false in
1513 let use_paramod = bool "use_paramod" true in
1514 let use_only_paramod =
1515 if for_applyS then true else bool "paramodulation" false in
1516 let use_library = bool "library"
1517 ((AutoTypes.default_flags()).AutoTypes.use_library) in
1518 let depth = int "depth" ((AutoTypes.default_flags()).AutoTypes.maxdepth) in
1519 let width = int "width" ((AutoTypes.default_flags()).AutoTypes.maxwidth) in
1520 let size = int "size" ((AutoTypes.default_flags()).AutoTypes.maxsize) in
1521 let gsize = int "gsize" ((AutoTypes.default_flags()).AutoTypes.maxgoalsizefactor) in
1522 let do_type = bool "type" false in
1523 let timeout = int "timeout" 0 in
1524 { AutoTypes.maxdepth =
1525 if use_only_paramod then 2 else depth;
1526 AutoTypes.maxwidth = width;
1527 AutoTypes.maxsize = size;
1530 if for_applyS then Unix.gettimeofday () +. 30.0
1534 Unix.gettimeofday() +. (float_of_int timeout);
1535 AutoTypes.use_library = use_library;
1536 AutoTypes.use_paramod = use_paramod;
1537 AutoTypes.use_only_paramod = use_only_paramod;
1538 AutoTypes.close_more = close_more;
1539 AutoTypes.dont_cache_failures = false;
1540 AutoTypes.maxgoalsizefactor = gsize;
1541 AutoTypes.do_types = do_type;
1544 let applyS_tac ~dbd ~term ~params ~universe =
1545 ProofEngineTypes.mk_tactic
1549 apply_smart ~dbd ~term ~subst:[] ~universe
1550 (flags_of_params params ~for_applyS:true ()) status
1554 | CicUnification.UnificationFailure msg
1555 | CicTypeChecker.TypeCheckerFailure msg ->
1556 raise (ProofEngineTypes.Fail msg))
1561 * auto superposition target = NAME
1562 * [table = NAME_LIST] [demod_table = NAME_LIST] [subterms_only]
1564 * - if table is omitted no superposition will be performed
1565 * - if demod_table is omitted no demodulation will be prformed
1566 * - subterms_only is passed to Indexing.superposition_right
1568 * lists are coded using _ (example: H_H1_H2)
1571 let eq_and_ty_of_goal = function
1572 | Cic.Appl [Cic.MutInd(uri,0,_);t;_;_] when LibraryObjects.is_eq_URI uri ->
1574 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1577 let rec find_in_ctx i name = function
1578 | [] -> raise (ProofEngineTypes.Fail (lazy ("Hypothesis not found: " ^ name)))
1579 | Some (Cic.Name name', _)::tl when name = name' -> i
1580 | _::tl -> find_in_ctx (i+1) name tl
1583 let rec position_of i x = function
1584 | [] -> assert false
1585 | j::tl when j <> x -> position_of (i+1) x tl
1590 let superposition_tac ~target ~table ~subterms_only ~demod_table status =
1591 Saturation.reset_refs();
1592 let proof,goalno = status in
1593 let curi,metasenv,_subst,pbo,pty, attrs = proof in
1594 let metano,context,ty = CicUtil.lookup_meta goalno metasenv in
1595 let eq_uri,tty = eq_and_ty_of_goal ty in
1596 let env = (metasenv, context, CicUniv.empty_ugraph) in
1597 let names = Utils.names_of_context context in
1598 let bag = Equality.mk_equality_bag () in
1599 let eq_index, equalities, maxm,cache =
1600 find_context_equalities 0 bag context proof Universe.empty AutoCache.cache_empty
1603 let what = find_in_ctx 1 target context in
1604 List.nth equalities (position_of 0 what eq_index)
1609 let others = Str.split (Str.regexp "_") table in
1610 List.map (fun other -> find_in_ctx 1 other context) others
1613 (fun other -> List.nth equalities (position_of 0 other eq_index))
1618 let index = List.fold_left Indexing.index Indexing.empty eq_other in
1620 if table = "" then maxm,[eq_what] else
1621 Indexing.superposition_right bag
1622 ~subterms_only eq_uri maxm env index eq_what
1624 debug_print (lazy ("Superposition right:"));
1625 debug_print (lazy ("\n eq: " ^ Equality.string_of_equality eq_what ~env));
1626 debug_print (lazy ("\n table: "));
1629 debug_print (lazy (" " ^ Equality.string_of_equality e ~env))) eq_other;
1630 debug_print (lazy ("\n result: "));
1631 List.iter (fun e -> debug_print (lazy (Equality.string_of_equality e ~env))) eql;
1632 debug_print (lazy ("\n result (cut&paste): "));
1635 let t = Equality.term_of_equality eq_uri e in
1636 debug_print (lazy (CicPp.pp t names)))
1638 debug_print (lazy ("\n result proofs: "));
1640 debug_print (lazy (let _,p,_,_,_ = Equality.open_equality e in
1641 let s = match p with Equality.Exact _ -> Subst.empty_subst | Equality.Step (s,_) -> s in
1642 Subst.ppsubst s ^ "\n" ^
1643 CicPp.pp (Equality.build_proof_term bag eq_uri [] 0 p) names))) eql;
1644 if demod_table <> "" then
1647 if eql = [] then [eq_what] else eql
1650 let demod = Str.split (Str.regexp "_") demod_table in
1651 List.map (fun other -> find_in_ctx 1 other context) demod
1655 (fun demod -> List.nth equalities (position_of 0 demod eq_index))
1658 let table = List.fold_left Indexing.index Indexing.empty eq_demod in
1661 (fun (maxm,acc) e ->
1663 Indexing.demodulation_equality bag eq_uri maxm env table e
1668 let eql = List.rev eql in
1669 debug_print (lazy ("\n result [demod]: "));
1671 (fun e -> debug_print (lazy (Equality.string_of_equality e ~env))) eql;
1672 debug_print (lazy ("\n result [demod] (cut&paste): "));
1675 let t = Equality.term_of_equality eq_uri e in
1676 debug_print (lazy (CicPp.pp t names)))
1682 let auto_tac ~(dbd:HSql.dbd) ~params ~universe (proof, goal) =
1683 (* argument parsing *)
1684 let string = string params in
1685 let bool = bool params in
1686 (* hacks to debug paramod *)
1687 let superposition = bool "superposition" false in
1688 let target = string "target" "" in
1689 let table = string "table" "" in
1690 let subterms_only = bool "subterms_only" false in
1691 let demod_table = string "demod_table" "" in
1692 match superposition with
1694 (* this is the ugly hack to debug paramod *)
1696 ~target ~table ~subterms_only ~demod_table (proof,goal)
1698 (* this is the real auto *)
1699 let _,metasenv,_subst,_,_, _ = proof in
1700 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
1701 let flags = flags_of_params params () in
1702 (* just for testing *)
1703 let use_library = flags.use_library in
1704 let tables,cache,newmeta =
1705 init_cache_and_tables ~dbd use_library flags.use_only_paramod true
1706 false universe (proof, goal) in
1707 let tables,cache,newmeta =
1708 if flags.close_more then
1710 tables newmeta context (proof, goal)
1711 auto_all_solutions universe cache
1712 else tables,cache,newmeta in
1713 let initial_time = Unix.gettimeofday() in
1714 let (_,oldmetasenv,_subst,_,_, _) = proof in
1717 metasenv,[],1,[],[D (goal,flags.maxdepth,P)],[]
1719 match auto_main tables newmeta context flags universe cache [elem] with
1720 | Proved (metasenv,subst,_, tables,cache,_) ->
1722 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time));*)
1723 let proof,metasenv =
1724 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1725 proof goal subst metasenv
1728 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
1729 ~newmetasenv:metasenv
1732 | Gaveup (tables,cache,maxm) ->
1735 string_of_float(Unix.gettimeofday()-.initial_time)));
1736 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
1739 let auto_tac ~dbd ~params ~universe =
1740 ProofEngineTypes.mk_tactic (auto_tac ~params ~dbd ~universe);;
1742 let eq_of_goal = function
1743 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1745 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1748 (* performs steps of rewrite with the universe, obtaining if possible
1750 let solve_rewrite_tac ~universe ?(steps=1) (proof,goal as status)=
1751 let _,metasenv,_subst,_,_,_ = proof in
1752 let _,context,ty = CicUtil.lookup_meta goal metasenv in
1753 let eq_uri = eq_of_goal ty in
1754 let (active,passive,bag), cache, maxm =
1755 (* we take the whole universe (no signature filtering) *)
1756 init_cache_and_tables false true false true universe (proof,goal)
1758 let initgoal = [], metasenv, ty in
1760 let equalities = (Saturation.list_of_passive passive) in
1761 (* we demodulate using both actives passives *)
1762 List.fold_left (fun tbl eq -> Indexing.index tbl eq) (snd active) equalities
1764 let env = metasenv,context,CicUniv.empty_ugraph in
1765 match Indexing.solve_demodulating bag env table initgoal steps with
1766 | Some (proof, metasenv, newty) ->
1769 | Cic.Appl[Cic.MutInd _;eq_ty;left;_] ->
1770 Equality.Exact (Equality.refl_proof eq_uri eq_ty left)
1774 Equality.build_goal_proof
1775 bag eq_uri proof refl newty [] context metasenv
1777 ProofEngineTypes.apply_tactic
1778 (PrimitiveTactics.apply_tac ~term:proofterm) status
1781 (ProofEngineTypes.Fail (lazy
1782 ("Unable to solve with " ^ string_of_int steps ^ " demodulations")))
1784 let solve_rewrite_tac ~universe ?steps () =
1785 ProofEngineTypes.mk_tactic (solve_rewrite_tac ~universe ?steps)
1789 let demodulate_tac ~dbd ~universe (proof,goal)=
1790 let curi,metasenv,_subst,pbo,pty, attrs = proof in
1791 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1792 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1793 let initgoal = [], metasenv, ty in
1794 let eq_uri = eq_of_goal ty in
1795 let (active,passive,bag), cache, maxm =
1796 init_cache_and_tables
1797 ~dbd false true true false universe (proof,goal)
1799 let equalities = (Saturation.list_of_passive passive) in
1800 (* we demodulate using both actives passives *)
1803 (fun tbl eq -> Indexing.index tbl eq)
1804 (snd active) equalities
1806 let changed,(newproof,newmetasenv, newty) =
1807 Indexing.demodulation_goal bag
1808 (metasenv,context,CicUniv.empty_ugraph) table initgoal
1812 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1814 Equality.build_goal_proof bag
1815 eq_uri newproof opengoal ty [] context metasenv
1817 let extended_metasenv = (maxm,context,newty)::metasenv in
1818 let extended_status =
1819 (curi,extended_metasenv,_subst,pbo,pty, attrs),goal in
1820 let (status,newgoals) =
1821 ProofEngineTypes.apply_tactic
1822 (PrimitiveTactics.apply_tac ~term:proofterm)
1824 (status,maxm::newgoals)
1826 else (* if newty = ty then *)
1827 raise (ProofEngineTypes.Fail (lazy "no progress"))
1828 (*else ProofEngineTypes.apply_tactic
1829 (ReductionTactics.simpl_tac
1830 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1833 let demodulate_tac ~dbd ~universe =
1834 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~universe);;
1836 let pp_proofterm = Equality.pp_proofterm;;
1838 let revision = "$Revision$";;
1839 let size_and_depth context metasenv t = 100, 100