2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
15 let debug_print ?(depth=0) s =
16 if !debug then prerr_endline (String.make depth '\t'^Lazy.force s) else ()
17 (* let print = debug_print *)
18 let print ?(depth=0) s =
19 prerr_endline (String.make depth '\t'^Lazy.force s)
21 let debug_do f = if !debug then f () else ()
23 open Continuationals.Stack
25 module Ast = CicNotationPt
26 let app_counter = ref 0
28 (* ======================= utility functions ========================= *)
29 module IntSet = Set.Make(struct type t = int let compare = compare end)
31 let get_sgoalty status g =
32 let _,_,metasenv,subst,_ = status#obj in
34 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
35 let ty = NCicUntrusted.apply_subst subst ctx ty in
36 let ctx = NCicUntrusted.apply_subst_context
37 ~fix_projections:true subst ctx
39 NTacStatus.mk_cic_term ctx ty
40 with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_sgoalty")
44 let gty = get_sgoalty status g in
45 metas_of_term status gty
48 let menv_closure status gl =
49 let rec closure acc = function
51 | x::l when IntSet.mem x acc -> closure acc l
52 | x::l -> closure (IntSet.add x acc) (deps status x @ l)
53 in closure IntSet.empty gl
56 (* we call a "fact" an object whose hypothesis occur in the goal
57 or in types of goal-variables *)
58 let is_a_fact status ty =
59 let status, ty, _ = saturate ~delta:0 status ty in
60 debug_print (lazy (ppterm status ty));
61 let g_metas = metas_of_term status ty in
62 let clos = menv_closure status g_metas in
63 let _,_,metasenv,_,_ = status#obj in
66 (fun acc (i,_) -> IntSet.add i acc)
68 in IntSet.equal clos menv;;
70 let is_a_fact_obj s uri =
71 let obj = NCicEnvironment.get_checked_obj uri in
73 | (_,_,[],[],NCic.Constant(_,_,Some(t),ty,_)) ->
74 is_a_fact s (mk_cic_term [] ty)
75 (* aggiungere i costruttori *)
78 let current_goal status =
79 let open_goals = head_goals status#stack in
80 assert (List.length open_goals = 1);
81 let open_goal = List.hd open_goals in
82 let gty = get_goalty status open_goal in
83 let ctx = ctx_of gty in
87 (* =============================== paramod =========================== *)
88 let solve fast status eq_cache goal =
90 if fast then NCicParamod.fast_eq_check
91 else NCicParamod.paramod in
92 let n,h,metasenv,subst,o = status#obj in
93 let gname, ctx, gty = List.assoc goal metasenv in
94 let gty = NCicUntrusted.apply_subst subst ctx gty in
95 let build_status (pt, _, metasenv, subst) =
97 print (lazy ("refining: "^(NCicPp.ppterm ctx subst metasenv pt)));
98 let stamp = Unix.gettimeofday () in
99 let metasenv, subst, pt, pty =
100 NCicRefiner.typeof status
101 (* (status#set_coerc_db NCicCoercion.empty_db) *)
102 metasenv subst ctx pt None in
103 print (lazy ("synt: "^(NCicPp.ppterm ctx subst metasenv pty)));
104 let metasenv, subst =
105 NCicUnification.unify status metasenv subst ctx gty pty
106 (* the previous code is much less expensive than directly refining
107 pt with expected type pty
109 prerr_endline ("exp: "^(NCicPp.ppterm ctx subst metasenv gty));
111 (status#set_coerc_db NCicCoercion.empty_db)
112 metasenv subst ctx pt (Some gty) *)
114 print (lazy (Printf.sprintf "Refined in %fs"
115 (Unix.gettimeofday() -. stamp)));
116 let status = status#set_obj (n,h,metasenv,subst,o) in
117 let metasenv = List.filter (fun j,_ -> j <> goal) metasenv in
118 let subst = (goal,(gname,ctx,pt,pty)) :: subst in
119 Some (status#set_obj (n,h,metasenv,subst,o))
121 NCicRefiner.RefineFailure msg
122 | NCicRefiner.Uncertain msg ->
123 debug_print (lazy ("WARNING: refining in fast_eq_check failed" ^
124 snd (Lazy.force msg))); None
125 | NCicRefiner.AssertFailure msg ->
126 debug_print (lazy ("WARNING: refining in fast_eq_check failed" ^
127 Lazy.force msg)); None
130 HExtlib.filter_map build_status
131 (f status metasenv subst ctx eq_cache (NCic.Rel ~-1,gty))
134 let fast_eq_check eq_cache status goal =
135 match solve true status eq_cache goal with
136 | [] -> raise (Error (lazy "no proof found",None))
140 let dist_fast_eq_check eq_cache s =
141 NTactics.distribute_tac (fast_eq_check eq_cache) s
144 let auto_eq_check eq_cache status =
146 let s = dist_fast_eq_check eq_cache status in
152 (* warning: ctx is supposed to be already instantiated w.r.t subst *)
153 let index_local_equations eq_cache status =
154 let open_goals = head_goals status#stack in
155 let open_goal = List.hd open_goals in
156 let ngty = get_goalty status open_goal in
157 let ctx = ctx_of ngty in
162 let t = NCic.Rel !c in
164 let ty = NCicTypeChecker.typeof [] [] ctx t in
165 debug_print(lazy("eq indexing " ^ (NCicPp.ppterm ctx [] [] ty)));
166 NCicParamod.forward_infer_step eq_cache t ty
168 | NCicTypeChecker.TypeCheckerFailure _
169 | NCicTypeChecker.AssertFailure _ -> eq_cache)
173 let fast_eq_check_tac ~params s =
174 let unit_eq = index_local_equations s#eq_cache s in
175 dist_fast_eq_check unit_eq s
178 let paramod eq_cache status goal =
179 match solve false status eq_cache goal with
180 | [] -> raise (Error (lazy "no proof found",None))
184 let paramod_tac ~params s =
185 let unit_eq = index_local_equations s#eq_cache s in
186 NTactics.distribute_tac (paramod unit_eq) s
190 let fast_eq_check_tac_all ~params eq_cache status =
191 let g,_,_ = current_goal status in
192 let allstates = fast_eq_check_all status eq_cache g in
193 let pseudo_low_tac s _ _ = s in
194 let pseudo_low_tactics =
195 List.map pseudo_low_tac allstates
197 List.map (fun f -> NTactics.distribute_tac f status) pseudo_low_tactics
202 let demod status eq_cache goal =
203 let n,h,metasenv,subst,o = status#obj in
204 let gname, ctx, gty = List.assoc goal metasenv in
205 let gty = NCicUntrusted.apply_subst subst ctx gty in
207 let demod_tac ~params s =
208 let unit_eq = index_local_equations s#eq_cache s in
209 dist_fast_eq_check unit_eq s
212 (*************** subsumption ****************)
214 let close_wrt_context =
218 | name, NCic.Decl t -> NCic.Prod(name,t,ty)
219 | name, NCic.Def(bo, _) -> NCicSubstitution.subst bo ty)
222 let args_for_context ?(k=1) ctx =
225 (fun (n,l) ctx_entry ->
227 | name, NCic.Decl t -> n+1,NCic.Rel(n)::l
228 | name, NCic.Def(bo, _) -> n+1,l)
232 let constant_for_meta ctx ty i =
233 let name = "cic:/foo"^(string_of_int i)^".con" in
234 let uri = NUri.uri_of_string name in
235 let ty = close_wrt_context ty ctx in
236 (* prerr_endline (NCicPp.ppterm [] [] [] ty); *)
237 let attr = (`Generated,`Definition,`Local) in
238 let obj = NCic.Constant([],name,None,ty,attr) in
239 (* Constant of relevance * string * term option * term * c_attr *)
243 let refresh metasenv =
245 (fun (metasenv,subst) (i,(iattr,ctx,ty)) ->
246 let ikind = NCicUntrusted.kind_of_meta iattr in
247 let metasenv,j,instance,ty =
248 NCicMetaSubst.mk_meta ~attrs:iattr
249 metasenv ctx ~with_type:ty ikind in
250 let s_entry = i,(iattr, ctx, instance, ty) in
251 let metasenv = List.filter (fun x,_ -> i <> x) metasenv in
252 metasenv,s_entry::subst)
253 (metasenv,[]) metasenv
255 (* close metasenv returns a ground instance of all the metas in the
256 metasenv, insantiatied with axioms, and the list of these axioms *)
257 let close_metasenv metasenv subst =
259 let metasenv = NCicUntrusted.apply_subst_metasenv subst metasenv in
261 let metasenv = NCicUntrusted.sort_metasenv subst metasenv in
263 (fun (subst,objs) (i,(iattr,ctx,ty)) ->
264 let ty = NCicUntrusted.apply_subst subst ctx ty in
266 NCicUntrusted.apply_subst_context ~fix_projections:true
268 let (uri,_,_,_,obj) as okind =
269 constant_for_meta ctx ty i in
271 NCicEnvironment.check_and_add_obj okind;
272 let iref = NReference.reference_of_spec uri NReference.Decl in
274 let args = args_for_context ctx in
275 if args = [] then NCic.Const iref
276 else NCic.Appl(NCic.Const iref::args)
278 (* prerr_endline (NCicPp.ppterm ctx [] [] iterm); *)
279 let s_entry = i, ([], ctx, iterm, ty)
280 in s_entry::subst,okind::objs
281 with _ -> assert false)
285 let ground_instances status gl =
286 let _,_,metasenv,subst,_ = status#obj in
287 let subset = menv_closure status gl in
288 let submenv = List.filter (fun (x,_) -> IntSet.mem x subset) metasenv in
290 let submenv = metasenv in
292 let subst, objs = close_metasenv submenv subst in
296 let (_, ctx, t, _) = List.assoc i subst in
297 debug_print (lazy (NCicPp.ppterm ctx [] [] t));
299 (fun (uri,_,_,_,_) as obj ->
300 NCicEnvironment.invalidate_item (`Obj (uri, obj)))
305 Not_found -> assert false
309 let replace_meta i args target =
310 let rec aux k = function
311 (* TODO: local context *)
312 | NCic.Meta (j,lc) when i = j ->
316 List.map (NCicSubstitution.subst_meta lc) args in
317 NCic.Appl(NCic.Rel k::args))
318 | NCic.Meta (j,lc) as m ->
325 aux k (NCicSubstitution.lift n t)) l))))
326 | t -> NCicUtils.map (fun _ k -> k+1) k aux t
331 let close_wrt_metasenv subst =
333 (fun ty (i,(iattr,ctx,mty)) ->
334 let mty = NCicUntrusted.apply_subst subst ctx mty in
336 NCicUntrusted.apply_subst_context ~fix_projections:true
338 let cty = close_wrt_context mty ctx in
339 let name = "foo"^(string_of_int i) in
340 let ty = NCicSubstitution.lift 1 ty in
341 let args = args_for_context ~k:1 ctx in
342 (* prerr_endline (NCicPp.ppterm ctx [] [] iterm); *)
343 let ty = replace_meta i args ty
345 NCic.Prod(name,cty,ty))
349 let _,_,metasenv,subst,_ = status#obj in
350 let subset = menv_closure status [g] in
351 let subset = IntSet.remove g subset in
352 let elems = IntSet.elements subset in
353 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
354 let ty = NCicUntrusted.apply_subst subst ctx ty in
355 debug_print (lazy ("metas in " ^ (NCicPp.ppterm ctx [] metasenv ty)));
356 debug_print (lazy (String.concat ", " (List.map string_of_int elems)));
357 let submenv = List.filter (fun (x,_) -> IntSet.mem x subset) metasenv in
358 let submenv = List.rev (NCicUntrusted.sort_metasenv subst submenv) in
360 let submenv = metasenv in
362 let ty = close_wrt_metasenv subst ty submenv in
363 debug_print (lazy (NCicPp.ppterm ctx [] [] ty));
369 (* =================================== auto =========================== *)
370 (****************** AUTO ********************
372 let calculate_timeout flags =
373 if flags.timeout = 0. then
374 (debug_print (lazy "AUTO WITH NO TIMEOUT");
375 {flags with timeout = infinity})
379 let is_equational_case goalty flags =
380 let ensure_equational t =
381 if is_an_equational_goal t then true
384 (flags.use_paramod && is_an_equational_goal goalty) ||
385 (flags.use_only_paramod && ensure_equational goalty)
388 type menv = Cic.metasenv
389 type subst = Cic.substitution
390 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
391 let candidate_no = ref 0;;
392 type candidate = int * Cic.term Lazy.t
393 type cache = AutoCache.cache
396 (* the goal (mainly for depth) and key of the goal *)
397 goal * AutoCache.cache_key
399 (* goal has to be proved *)
401 (* goal has to be cached as a success obtained using candidate as the first
403 | S of goal * AutoCache.cache_key * candidate * int
405 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
406 menv * subst * int * op list * op list * fail list
408 (* list of computations that may lead to the solution: all op list will
409 * end with the same (S(g,_)) *)
412 (* menv, subst, alternatives, tables, cache *)
413 | Proved of menv * subst * elem list * AutomationCache.tables * cache
414 | Gaveup of AutomationCache.tables * cache
417 (* the status exported to the external observer *)
419 (* context, (goal,candidate) list, and_list, history *)
420 Cic.context * (int * Cic.term * bool * int * (int * Cic.term Lazy.t) list) list *
421 (int * Cic.term * int) list * Cic.term Lazy.t list
424 let rec aux acc = function
425 | (D g)::tl -> aux (acc@[g]) tl
431 let calculate_goal_ty (goalno,_,_) s m =
433 let _,cc,goalty = CicUtil.lookup_meta goalno m in
434 (* XXX applicare la subst al contesto? *)
435 Some (cc, CicMetaSubst.apply_subst s goalty)
436 with CicUtil.Meta_not_found i when i = goalno -> None
439 let calculate_closed_goal_ty (goalno,_,_) s =
441 let cc,_,goalty = List.assoc goalno s in
442 (* XXX applicare la subst al contesto? *)
443 Some (cc, CicMetaSubst.apply_subst s goalty)
448 let pp_status ctx status =
450 let names = Utils.names_of_context ctx in
453 ProofEngineReduction.replace
454 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
455 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
459 let string_of_do m s (gi,_,_ as g) d =
460 match calculate_goal_ty g s m with
461 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
462 | None -> Printf.sprintf "D(%d, _, %d)" gi d
464 let string_of_s m su k (ci,ct) gi =
465 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp (Lazy.force ct)) ci
467 let string_of_ol m su l =
471 | D (g,d,s) -> string_of_do m su (g,d,s) d
472 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
475 let string_of_fl m s fl =
477 (List.map (fun ((i,_,_),ty) ->
478 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
480 let rec aux = function
482 | (m,s,_,_,ol,fl)::tl ->
483 Printf.eprintf "< [%s] ;;; [%s]>\n"
484 (string_of_ol m s ol) (string_of_fl m s fl);
487 Printf.eprintf "-------------------------- status -------------------\n";
489 Printf.eprintf "-----------------------------------------------------\n";
492 let auto_status = ref [] ;;
493 let auto_context = ref [];;
494 let in_pause = ref false;;
495 let pause b = in_pause := b;;
496 let cond = Condition.create ();;
497 let mutex = Mutex.create ();;
498 let hint = ref None;;
499 let prune_hint = ref [];;
501 let step _ = Condition.signal cond;;
502 let give_hint n = hint := Some n;;
503 let give_prune_hint hint =
504 prune_hint := hint :: !prune_hint
511 Condition.wait cond mutex;
516 let get_auto_status _ =
517 let status = !auto_status in
518 let and_list,elems,last =
521 | (m,s,_,don,gl,fail)::tl ->
524 (fun (id,d,_ as g) ->
525 match calculate_goal_ty g s m with
526 | Some (_,x) -> Some (id,x,d) | None -> None)
530 (* these are the S goalsin the or list *)
533 (fun (m,s,_,don,gl,fail) ->
535 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
539 (* this function eats id from a list l::[id,x] returning x, l *)
540 let eat_tail_if_eq id l =
541 let rec aux (s, l) = function
543 | ((id1,_,_),k1,c)::tl when id = id1 ->
545 | None -> aux (Some c,l) tl
546 | Some _ -> assert false)
547 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
549 let c, l = aux (None, []) l in
552 let eat_in_parallel id l =
553 let rec aux (b,eaten, new_l as acc) l =
557 match eat_tail_if_eq id l with
558 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
559 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
563 let rec eat_all rows l =
567 match List.rev elem with
568 | ((to_eat,depth,_),k,_)::next_lunch ->
569 let b, eaten, l = eat_in_parallel to_eat l in
570 let eaten = HExtlib.list_uniq eaten in
571 let eaten = List.rev eaten in
572 let b = true (* List.hd (List.rev b) *) in
573 let rows = rows @ [to_eat,k,b,depth,eaten] in
575 | [] -> eat_all rows or_list
577 eat_all [] (List.rev orlist)
581 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
584 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
585 and_list, rows, history
587 !auto_context, elems, and_list, last
590 (* Works if there is no dependency over proofs *)
591 let is_a_green_cut goalty =
592 CicUtil.is_meta_closed goalty
594 let rec first_s = function
595 | (D _)::tl -> first_s tl
596 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
599 let list_union l1 l2 =
600 (* TODO ottimizzare compare *)
601 HExtlib.list_uniq (List.sort compare (l1 @ l1))
603 let rec eq_todo l1 l2 =
605 | (D g1) :: tl1,(D g2) :: tl2 when g1=g2 -> eq_todo tl1 tl2
606 | (S (g1,k1,(c1,lt1),i1)) :: tl1, (S (g2,k2,(c2,lt2),i2)) :: tl2
607 when i1 = i2 && g1 = g2 && k1 = k2 && c1 = c2 ->
608 if Lazy.force lt1 = Lazy.force lt2 then eq_todo tl1 tl2 else false
612 let eat_head todo id fl orlist =
613 let rec aux acc = function
615 | (m, s, _, _, todo1, fl1)::tl as orlist ->
617 match first_s todo1 with
618 | None -> orlist, acc
619 | Some (((gno,_,_),_,_,_), todo11) ->
620 (* TODO confronto tra todo da ottimizzare *)
621 if gno = id && eq_todo todo11 todo then
622 aux (list_union fl1 acc) tl
630 let close_proof p ty menv context =
632 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
634 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
635 naif_closure p menv context
637 (* XXX capire bene quando aggiungere alla cache *)
638 let add_to_cache_and_del_from_orlist_if_green_cut
639 g s m cache key todo orlist fl ctx size minsize
641 let cache = cache_remove_underinspection cache key in
642 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
644 match calculate_closed_goal_ty g s with
645 | None -> assert false
646 | Some (canonical_ctx , gty) ->
647 let goalno,depth,sort = g in
648 let irl = mk_irl canonical_ctx in
649 let goal = Cic.Meta(goalno, irl) in
650 let proof = CicMetaSubst.apply_subst s goal in
651 let green_proof, closed_proof =
652 let b = is_a_green_cut proof in
654 b, (* close_proof proof gty m ctx *) proof
658 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
659 if is_a_green_cut key then
660 (* if the initia goal was closed, we cut alternatives *)
661 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
662 let orlist, fl = eat_head todo goalno fl orlist in
664 if size < minsize then
665 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
667 (* if the proof is closed we cache it *)
668 if green_proof then cache_add_success cache key proof
669 else (* cache_add_success cache key closed_proof *)
670 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
672 cache, orlist, fl, true
675 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
676 if size < minsize then
677 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
678 (* if the substituted goal and the proof are closed we cache it *)
679 if is_a_green_cut gty then
680 if green_proof then cache_add_success cache gty proof
681 else (* cache_add_success cache gty closed_proof *)
682 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
686 CicTypeChecker.type_of_aux' ~subst:s
687 m ctx closed_proof CicUniv.oblivion_ugraph
689 if is_a_green_cut ty then
690 cache_add_success cache ty closed_proof
693 | CicTypeChecker.TypeCheckerFailure _ ->*)
694 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
696 cache, orlist, fl, false
698 let close_failures (fl : fail list) (cache : cache) =
700 (fun cache ((gno,depth,_),gty) ->
701 if CicUtil.is_meta_closed gty then
702 ( debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
703 cache_add_failure cache gty depth)
708 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
709 let entry = goalno, (canonical_ctx, t,ty) in
710 assert_subst_are_disjoint subst [entry];
711 let subst = entry :: subst in
713 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
718 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
719 None,metasenv,subst ,(lazy (Cic.Meta(goalno,mk_irl context))),goalty, []
723 tables cache depth fake_proof goalno goalty subst context
726 let active,passive,bag = tables in
727 let ppterm = ppterm context in
728 let status = (fake_proof,goalno) in
729 if flags.use_only_paramod then
731 debug_print (lazy ("PARAMODULATION SU: " ^
732 string_of_int goalno ^ " " ^ ppterm goalty ));
733 let goal_steps, saturation_steps, timeout =
734 max_int,max_int,flags.timeout
737 Saturation.given_clause bag status active passive
738 goal_steps saturation_steps timeout
740 | None, active, passive, bag ->
741 [], (active,passive,bag), cache, flags
742 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
744 assert_subst_are_disjoint subst subst';
745 let subst = subst@subst' in
747 order_new_goals metasenv subst open_goals ppterm
750 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
753 [(!candidate_no,proof),metasenv,subst,open_goals],
754 (active,passive,bag), cache, flags
758 debug_print (lazy ("NARROWING DEL GOAL: " ^
759 string_of_int goalno ^ " " ^ ppterm goalty ));
760 let goal_steps, saturation_steps, timeout =
764 Saturation.solve_narrowing bag status active passive goal_steps
766 | None, active, passive, bag ->
767 [], (active,passive,bag), cache, flags
768 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
770 assert_subst_are_disjoint subst subst';
771 let subst = subst@subst' in
773 order_new_goals metasenv subst open_goals ppterm
776 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
779 [(!candidate_no,proof),metasenv,subst,open_goals],
780 (active,passive,bag), cache, flags
784 let params = ([],["use_context","false"]) in
785 let automation_cache = {
786 AutomationCache.tables = tables ;
787 AutomationCache.univ = Universe.empty; }
790 let ((_,metasenv,subst,_,_,_),open_goals) =
792 solve_rewrite ~params ~automation_cache
795 let proof = lazy (Cic.Meta (-1,[])) in
796 [(!candidate_no,proof),metasenv,subst,[]],tables, cache, flags
797 with ProofEngineTypes.Fail _ -> [], tables, cache, flags
799 let res = Saturation.all_subsumed bag status active passive in
802 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
803 assert_subst_are_disjoint subst subst';
804 let subst = subst@subst' in
806 order_new_goals metasenv subst open_goals ppterm
809 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
812 (!candidate_no,proof),metasenv,subst,open_goals)
815 res', (active,passive,bag), cache, flags
822 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
823 let p1 = List.length (prop_only l1) in
824 let p2 = List.length (prop_only l2) in
825 if p1 = p2 then List.length l1 - List.length l2 else p1-p2)
829 let try_candidate dbd
830 goalty tables subst fake_proof goalno depth context cand
832 let ppterm = ppterm context in
834 let actives, passives, bag = tables in
835 let (_,metasenv,subst,_,_,_), open_goals =
836 ProofEngineTypes.apply_tactic
837 (PrimitiveTactics.apply_tac ~term:cand)
840 let tables = actives, passives,
841 Equality.push_maxmeta bag
842 (max (Equality.maxmeta bag) (CicMkImplicit.new_meta metasenv subst))
844 debug_print (lazy (" OK: " ^ ppterm cand));
845 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
846 let open_goals = order_new_goals metasenv subst open_goals ppterm in
847 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
849 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables
851 | ProofEngineTypes.Fail s -> None,tables
852 | CicUnification.Uncertain s -> None,tables
855 let applicative_case dbd
856 tables depth subst fake_proof goalno goalty metasenv context
857 signature universe cache flags
861 | Cic.Appl (hd::tl) ->
862 Cic.Appl (hd :: HExtlib.mk_list (Cic.Meta (0,[])) (List.length tl))
865 let goalty_aux = goalty in
867 get_candidates flags.skip_trie_filtering universe cache goalty_aux
869 (* if the goal is an equality we skip the congruence theorems
871 if is_equational_case goalty flags
872 then List.filter not_default_eq_term candidates
875 let candidates = List.filter (only signature context metasenv) candidates
879 (fun (tables,elems) cand ->
881 try_candidate dbd goalty
882 tables subst fake_proof goalno depth context cand
884 | None, tables -> tables, elems
885 | Some x, tables -> tables, x::elems)
886 (tables,[]) candidates
888 let elems = sort_new_elems elems in
892 let try_smart_candidate dbd
893 goalty tables subst fake_proof goalno depth context cand
895 let ppterm = ppterm context in
897 let params = ([],[]) in
898 let automation_cache = {
899 AutomationCache.tables = tables ;
900 AutomationCache.univ = Universe.empty; }
902 debug_print (lazy ("candidato per " ^ string_of_int goalno
903 ^ ": " ^ CicPp.ppterm cand));
905 let (_,metasenv,subst,_,_,_) = fake_proof in
906 prerr_endline ("metasenv:\n" ^ CicMetaSubst.ppmetasenv [] metasenv);
907 prerr_endline ("subst:\n" ^ CicMetaSubst.ppsubst ~metasenv subst);
909 let ((_,metasenv,subst,_,_,_),open_goals) =
910 apply_smart ~dbd ~term:cand ~params ~automation_cache
913 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
914 let open_goals = order_new_goals metasenv subst open_goals ppterm in
915 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
917 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables
919 | ProofEngineTypes.Fail s -> None,tables
920 | CicUnification.Uncertain s -> None,tables
923 let smart_applicative_case dbd
924 tables depth subst fake_proof goalno goalty metasenv context signature
929 | Cic.Appl (hd::tl) ->
930 Cic.Appl (hd :: HExtlib.mk_list (Cic.Meta (0,[])) (List.length tl))
933 let smart_candidates =
934 get_candidates flags.skip_trie_filtering universe cache goalty_aux
937 get_candidates flags.skip_trie_filtering universe cache goalty
939 let smart_candidates =
941 (fun x -> not(List.mem x candidates)) smart_candidates
944 (lazy ("smart_candidates" ^ " = " ^
945 (String.concat "\n" (List.map CicPp.ppterm smart_candidates)))) in
946 debug_print debug_msg;
947 let candidates = List.filter (only signature context metasenv) candidates in
948 let smart_candidates =
949 List.filter (only signature context metasenv) smart_candidates
952 let penalty cand depth =
953 if only signature context metasenv cand then depth else ((prerr_endline (
954 "penalizzo " ^ CicPp.ppterm cand));depth -1)
959 (fun (tables,elems) cand ->
961 try_candidate dbd goalty
962 tables subst fake_proof goalno depth context cand
965 (* if normal application fails we try to be smart *)
966 (match try_smart_candidate dbd goalty
967 tables subst fake_proof goalno depth context cand
969 | None, tables -> tables, elems
970 | Some x, tables -> tables, x::elems)
971 | Some x, tables -> tables, x::elems)
972 (tables,[]) candidates
974 let tables, smart_elems =
976 (fun (tables,elems) cand ->
978 try_smart_candidate dbd goalty
979 tables subst fake_proof goalno depth context cand
981 | None, tables -> tables, elems
982 | Some x, tables -> tables, x::elems)
983 (tables,[]) smart_candidates
985 let elems = sort_new_elems (elems @ smart_elems) in
989 let equational_and_applicative_case dbd
990 signature universe flags m s g gty tables cache context
992 let goalno, depth, sort = g in
993 let fake_proof = mk_fake_proof m s g gty context in
994 if is_equational_case gty flags then
995 let elems,tables,cache, flags =
996 equational_case tables cache
997 depth fake_proof goalno gty s context flags
999 let more_elems, tables, cache =
1000 if flags.use_only_paramod then
1003 applicative_case dbd
1004 tables depth s fake_proof goalno
1005 gty m context signature universe cache flags
1007 elems@more_elems, tables, cache, flags
1009 let elems, tables, cache =
1010 match LibraryObjects.eq_URI () with
1012 smart_applicative_case dbd tables depth s fake_proof goalno
1013 gty m context signature universe cache flags
1015 applicative_case dbd tables depth s fake_proof goalno
1016 gty m context signature universe cache flags
1018 elems, tables, cache, flags
1020 let rec condition_for_hint i = function
1022 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
1023 | _::tl -> condition_for_hint i tl
1025 let prunable_for_size flags s m todo =
1026 let rec aux b = function
1027 | (S _)::tl -> aux b tl
1028 | (D (_,_,T))::tl -> aux b tl
1030 (match calculate_goal_ty g s m with
1032 | Some (canonical_ctx, gty) ->
1034 Utils.weight_of_term
1035 ~consider_metas:false ~count_metas_occurrences:true gty in
1036 let newb = b || gsize > flags.maxgoalsizefactor in
1043 let prunable ty todo =
1044 let rec aux b = function
1045 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
1046 | (D (_,_,T))::tl -> aux b tl
1054 let prunable menv subst ty todo =
1055 let rec aux = function
1056 | (S(_,k,_,_))::tl ->
1057 (match Equality.meta_convertibility_subst k ty menv with
1060 no_progress variant tl (* || aux tl*))
1061 | (D (_,_,T))::tl -> aux tl
1063 and no_progress variant = function
1064 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
1065 | D ((n,_,P) as g)::tl ->
1066 (match calculate_goal_ty g subst menv with
1067 | None -> no_progress variant tl
1069 (match calculate_goal_ty g variant menv with
1070 | None -> assert false
1072 if gty = gty' then no_progress variant tl
1074 (prerr_endline (string_of_int n);
1075 prerr_endline (CicPp.ppterm gty);
1076 prerr_endline (CicPp.ppterm gty');
1077 prerr_endline "---------- subst";
1078 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv subst);
1079 prerr_endline "---------- variant";
1080 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv variant);
1081 prerr_endline "---------- menv";
1082 prerr_endline (CicMetaSubst.ppmetasenv [] menv);
1083 no_progress variant tl) *)
1085 | _::tl -> no_progress variant tl
1090 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
1092 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
1094 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
1096 let filter_prune_hint c l =
1097 let prune = !prune_hint in
1098 prune_hint := []; (* possible race... *)
1099 if prune = [] then c,l
1101 cache_reset_underinspection c,
1102 List.filter (condition_for_prune_hint prune) l
1108 auto_all_solutions dbd tables universe cache context metasenv gl flags
1113 MetadataConstraints.UriManagerSet.union set
1114 (MetadataQuery.signature_of metasenv g)
1116 MetadataConstraints.UriManagerSet.empty gl
1118 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1121 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
1123 let elems = [metasenv,[],1,[],goals,[]] in
1124 let rec aux tables solutions cache elems flags =
1125 match auto_main dbd tables context flags signature universe cache elems with
1126 | Gaveup (tables,cache) ->
1127 solutions,cache, tables
1128 | Proved (metasenv,subst,others,tables,cache) ->
1129 if Unix.gettimeofday () > flags.timeout then
1130 ((subst,metasenv)::solutions), cache, tables
1132 aux tables ((subst,metasenv)::solutions) cache others flags
1134 let rc = aux tables [] cache elems flags in
1136 | [],cache,tables -> [],cache,tables
1137 | solutions, cache,tables ->
1140 (fun (subst,newmetasenv) ->
1142 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
1144 if opened = [] then Some subst else None)
1147 solutions,cache,tables
1150 (******************* AUTO ***************)
1153 let auto dbd flags metasenv tables universe cache context metasenv gl =
1154 let initial_time = Unix.gettimeofday() in
1158 MetadataConstraints.UriManagerSet.union set
1159 (MetadataQuery.signature_of metasenv g)
1161 MetadataConstraints.UriManagerSet.empty gl
1163 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
1164 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
1165 let elems = [metasenv,[],1,[],goals,[]] in
1166 match auto_main dbd tables context flags signature universe cache elems with
1167 | Proved (metasenv,subst,_, tables,cache) ->
1169 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1170 Some (subst,metasenv), cache
1171 | Gaveup (tables,cache) ->
1173 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1177 let auto_tac ~(dbd:HSql.dbd) ~params:(univ,params) ~automation_cache (proof, goal) =
1178 let flags = flags_of_params params () in
1179 let use_library = flags.use_library in
1180 let universe, tables, cache =
1181 init_cache_and_tables
1182 ~dbd ~use_library ~use_context:(not flags.skip_context)
1183 automation_cache univ (proof, goal)
1185 let _,metasenv,subst,_,_, _ = proof in
1186 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
1187 let signature = MetadataQuery.signature_of metasenv goal in
1192 CicTypeChecker.type_of_aux' metasenv context t
1193 CicUniv.oblivion_ugraph
1195 MetadataConstraints.UriManagerSet.union set
1196 (MetadataConstraints.constants_of ty)
1201 if flags.close_more then
1203 tables context (proof, goal)
1204 (auto_all_solutions dbd) signature universe cache
1205 else tables,cache in
1206 let initial_time = Unix.gettimeofday() in
1207 let (_,oldmetasenv,_,_,_, _) = proof in
1210 metasenv,subst,1,[],[D (goal,flags.maxdepth,P)],[]
1212 match auto_main dbd tables context flags signature universe cache [elem] with
1213 | Proved (metasenv,subst,_, tables,cache) ->
1215 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1216 let proof,metasenv =
1217 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1218 proof goal subst metasenv
1221 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
1222 ~newmetasenv:metasenv
1225 | Gaveup (tables,cache) ->
1228 string_of_float(Unix.gettimeofday()-.initial_time)));
1229 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
1233 (****************** smart application ********************)
1236 let smart_apply t unit_eq status g =
1237 let n,h,metasenv,subst,o = status#obj in
1238 let gname, ctx, gty = List.assoc g metasenv in
1239 (* let ggty = mk_cic_term context gty in *)
1240 let status, t = disambiguate status ctx t None in
1241 let status,t = term_of_cic_term status t ctx in
1242 let ty = NCicTypeChecker.typeof subst metasenv ctx t in
1243 let ty,metasenv,args = NCicMetaSubst.saturate metasenv subst ctx ty 0 in
1244 let metasenv,j,inst,_ = NCicMetaSubst.mk_meta metasenv ctx `IsTerm in
1245 let status = status#set_obj (n,h,metasenv,subst,o) in
1246 let pterm = if args=[] then t else NCic.Appl(t::args) in
1249 NUri.uri_of_string "cic:/matita/ng/Plogic/equality/eq_coerc.con" in
1250 let ref = NReference.reference_of_spec uri (NReference.Def(2)) in
1254 NCic.Appl[eq_coerc;ty;NCic.Implicit `Type;pterm;inst] in
1255 let smart = mk_cic_term ctx smart in
1257 let status = instantiate status g smart in
1258 let _,_,metasenv,subst,_ = status#obj in
1259 let _,ctx,jty = List.assoc j metasenv in
1260 let jty = NCicUntrusted.apply_subst subst ctx jty in
1261 debug_print(lazy("goal " ^ (NCicPp.ppterm ctx [] [] jty)));
1262 fast_eq_check unit_eq status j
1264 | Error _ as e -> debug_print (lazy "error"); raise e
1266 let smart_apply_tac t s =
1267 let unit_eq = index_local_equations s#eq_cache s in
1268 NTactics.distribute_tac (smart_apply t unit_eq) s
1270 let smart_apply_auto t eq_cache =
1271 NTactics.distribute_tac (smart_apply t eq_cache)
1274 (****************** types **************)
1277 type th_cache = (NCic.context * InvRelDiscriminationTree.t) list
1279 let keys_of_term status t =
1280 let status, orig_ty = typeof status (ctx_of t) t in
1281 let _, ty, _ = saturate ~delta:max_int status orig_ty in
1284 let _, ty = term_of_cic_term status ty (ctx_of ty) in
1286 | NCic.Const (NReference.Ref (_,NReference.Def h))
1287 | NCic.Appl (NCic.Const(NReference.Ref(_,NReference.Def h))::_)
1289 let _,ty,_= saturate status ~delta:(h-1) orig_ty in
1296 let mk_th_cache status gl =
1298 (fun (status, acc) g ->
1299 let gty = get_goalty status g in
1300 let ctx = ctx_of gty in
1301 debug_print(lazy("th cache for: "^ppterm status gty));
1302 debug_print(lazy("th cache in: "^ppcontext status ctx));
1303 if List.mem_assq ctx acc then status, acc else
1304 let idx = InvRelDiscriminationTree.empty in
1307 (fun (status, i, idx) _ ->
1308 let t = mk_cic_term ctx (NCic.Rel i) in
1309 debug_print(lazy("indexing: "^ppterm status t));
1310 let status, keys = keys_of_term status t in
1312 List.fold_left (fun idx k ->
1313 InvRelDiscriminationTree.index idx k t) idx keys
1316 (status, 1, idx) ctx
1318 status, (ctx, idx) :: acc)
1322 let add_to_th t c ty =
1323 let key_c = ctx_of t in
1324 if not (List.mem_assq key_c c) then
1325 (key_c ,InvRelDiscriminationTree.index
1326 InvRelDiscriminationTree.empty ty t ) :: c
1328 let rec replace = function
1330 | (x, idx) :: tl when x == key_c ->
1331 (x, InvRelDiscriminationTree.index idx ty t) :: tl
1332 | x :: tl -> x :: replace tl
1337 let pp_idx status idx =
1338 InvRelDiscriminationTree.iter idx
1340 debug_print(lazy("K: " ^ NCicInverseRelIndexable.string_of_path k));
1342 (fun t -> debug_print(lazy("\t"^ppterm status t)))
1349 debug_print(lazy( "-----------------------------------------------"));
1350 debug_print(lazy( (NCicPp.ppcontext ~metasenv:[] ~subst:[] ctx)));
1351 debug_print(lazy( "||====> "));
1355 let search_in_th gty th =
1356 let c = ctx_of gty in
1357 let rec aux acc = function
1358 | [] -> (* Ncic_termSet.elements *) acc
1361 let idx = List.assq k th in
1362 let acc = Ncic_termSet.union acc
1363 (InvRelDiscriminationTree.retrieve_unifiables idx gty)
1366 with Not_found -> aux acc tl
1368 aux Ncic_termSet.empty c
1372 do_types : bool; (* solve goals in Type *)
1380 {facts : th_cache; (* positive results *)
1381 under_inspection : th_cache; (* to prune looping *)
1382 unit_eq : NCicParamod.state
1386 type goal = int * sort (* goal, depth, sort *)
1387 type fail = goal * cic_term
1388 type candidate = int * Ast.term (* unique candidate number, candidate *)
1390 exception Gaveup of IntSet.t (* a sublist of unprovable conjunctive
1391 atoms of the input goals *)
1392 exception Proved of NTacStatus.tac_status
1394 (* let close_failures _ c = c;; *)
1395 (* let prunable _ _ _ = false;; *)
1396 (* let cache_examine cache gty = `Notfound;; *)
1397 (* let put_in_subst s _ _ _ = s;; *)
1398 (* let add_to_cache_and_del_from_orlist_if_green_cut _ _ c _ _ o f _ = c, o, f, false ;; *)
1399 (* let cache_add_underinspection c _ _ = c;; *)
1401 let init_cache ?(facts=[]) ?(under_inspection=[])
1402 ?(unit_eq=NCicParamod.empty_state) _ =
1404 under_inspection = under_inspection;
1408 let only _ _ _ = true;;
1410 let candidate_no = ref 0;;
1412 let openg_no status = List.length (head_goals status#stack)
1414 let sort_new_elems l =
1415 List.sort (fun (_,s1) (_,s2) -> openg_no s1 - openg_no s2) l
1417 let try_candidate ?(smart=0) flags depth status eq_cache t =
1419 print ~depth (lazy ("------------ try " ^ CicNotationPp.pp_term t));
1421 if smart= 0 then NTactics.apply_tac ("",0,t) status
1422 else if smart = 1 then smart_apply_auto ("",0,t) eq_cache status
1423 else (* smart = 2: both *)
1424 try NTactics.apply_tac ("",0,t) status
1426 smart_apply_auto ("",0,t) eq_cache status in
1427 let og_no = openg_no status in
1428 if og_no > flags.maxwidth ||
1429 (depth = flags.maxdepth && og_no <> 0) then
1430 (debug_print ~depth (lazy "pruned immediately"); None)
1433 Some ((!candidate_no,t),status))
1434 with Error (msg,exn) -> debug_print ~depth (lazy "failed"); None
1437 let get_candidates ?(smart=true) status cache signature gty =
1438 let universe = status#auto_cache in
1439 let context = ctx_of gty in
1441 let _status, t = term_of_cic_term status t context
1443 let c_ast = function
1444 | NCic.Const r -> Ast.NRef r | _ -> assert false in
1445 let _, raw_gty = term_of_cic_term status gty context in
1446 let cands = NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1448 let local_cands = search_in_th gty cache in
1449 let together global local =
1451 (List.filter (only signature context)
1452 (NDiscriminationTree.TermSet.elements global)) @
1453 List.map t_ast (Ncic_termSet.elements local) in
1454 let candidates = together cands local_cands in
1455 let smart_candidates =
1458 | NCic.Appl (hd::tl) ->
1460 NCic.Appl (hd:: HExtlib.mk_list(NCic.Meta (0,(0,NCic.Irl 0)))
1461 (List.length tl)) in
1463 NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1464 universe weak_gty in
1466 NDiscriminationTree.TermSet.diff more_cands cands in
1467 let cic_weak_gty = mk_cic_term context weak_gty in
1468 let more_local_cands = search_in_th cic_weak_gty cache in
1469 let smart_local_cands =
1470 Ncic_termSet.diff more_local_cands local_cands in
1471 together smart_cands smart_local_cands
1475 candidates, smart_candidates
1478 let applicative_case depth signature status flags gty (cache:cache) =
1479 app_counter:= !app_counter+1;
1480 let _,_,metasenv,subst,_ = status#obj in
1481 let context = ctx_of gty in
1482 let tcache = cache.facts in
1484 let status, t = term_of_cic_term status gty context in
1485 NCicParamod.is_equation metasenv subst context t
1487 debug_print(lazy (string_of_bool is_eq));
1488 let candidates, smart_candidates =
1489 get_candidates ~smart:(not is_eq) status tcache signature gty in
1491 (lazy ("candidates: " ^ string_of_int (List.length candidates)));
1493 (lazy ("smart candidates: " ^
1494 string_of_int (List.length smart_candidates)));
1495 let sm = if is_eq then 0 else 2 in
1499 match try_candidate (~smart:sm)
1500 flags depth status cache.unit_eq cand with
1502 | Some x -> x::elems)
1508 match try_candidate (~smart:1)
1509 flags depth status cache.unit_eq cand with
1511 | Some x -> x::elems)
1520 (* gty is supposed to be meta-closed *)
1521 let is_subsumed depth status gty cache =
1522 if cache=[] then false else (
1523 debug_print ~depth (lazy("Subsuming " ^ (ppterm status gty)));
1524 let n,h,metasenv,subst,obj = status#obj in
1525 let ctx = ctx_of gty in
1526 let _ , target = term_of_cic_term status gty ctx in
1527 let target = NCicSubstitution.lift 1 target in
1528 (* candidates must only be searched w.r.t the given context *)
1531 let idx = List.assq ctx cache in
1532 Ncic_termSet.elements
1533 (InvRelDiscriminationTree.retrieve_generalizations idx gty)
1534 with Not_found -> []
1537 (lazy ("failure candidates: " ^ string_of_int (List.length candidates)));
1541 let _ , source = term_of_cic_term status t ctx in
1543 NCic.Prod("foo",source,target) in
1544 let metasenv,j,_,_ =
1545 NCicMetaSubst.mk_meta
1546 metasenv ctx ~with_type:implication `IsType in
1547 let status = status#set_obj (n,h,metasenv,subst,obj) in
1548 let status = status#set_stack [([1,Open j],[],[],`NoTag)] in
1550 let status = NTactics.intro_tac "foo" status in
1552 NTactics.apply_tac ("",0,Ast.NCic (NCic.Rel 1)) status
1554 if (head_goals status#stack = []) then raise Found
1559 with Found -> debug_print ~depth (lazy "success");true)
1562 let rec guess_name name ctx =
1563 if name = "_" then guess_name "auto" ctx else
1564 if not (List.mem_assoc name ctx) then name else
1565 guess_name (name^"'") ctx
1568 let is_prod status =
1569 let _, ctx, gty = current_goal status in
1570 let _, raw_gty = term_of_cic_term status gty ctx in
1572 | NCic.Prod (name,_,_) -> Some (guess_name name ctx)
1575 let intro ~depth status facts name =
1576 let status = NTactics.intro_tac name status in
1577 let _, ctx, ngty = current_goal status in
1578 let t = mk_cic_term ctx (NCic.Rel 1) in
1579 let status, keys = keys_of_term status t in
1580 let facts = List.fold_left (add_to_th t) facts keys in
1581 debug_print ~depth (lazy ("intro: "^ name));
1582 (* unprovability is not stable w.r.t introduction *)
1586 let rec intros_facts ~depth status facts =
1587 match is_prod status with
1590 intro ~depth status facts name
1591 in intros_facts ~depth status facts
1592 | _ -> status, facts
1595 let rec intros ~depth status (cache:cache) =
1596 match is_prod status with
1599 intros_facts ~depth status cache.facts
1601 (* we reindex the equation from scratch *)
1603 index_local_equations status#eq_cache status in
1604 (* under_inspection must be set to empty *)
1605 status, init_cache ~facts ~unit_eq ()
1606 | _ -> status, cache
1609 let reduce ~depth status g =
1610 let n,h,metasenv,subst,o = status#obj in
1611 let attr, ctx, ty = NCicUtils.lookup_meta g metasenv in
1612 let ty = NCicUntrusted.apply_subst subst ctx ty in
1613 let ty' = NCicReduction.whd ~subst ctx ty in
1617 (lazy ("reduced to: "^ NCicPp.ppterm ctx subst metasenv ty'));
1619 (g,(attr,ctx,ty'))::(List.filter (fun (i,_) -> i<>g) metasenv)
1621 let status = status#set_obj (n,h,metasenv,subst,o) in
1623 [(!candidate_no,Ast.Ident("__whd",None)),status])
1626 let do_something signature flags status g depth gty cache =
1628 let l = reduce ~depth status g in
1629 (* backward aplications *)
1634 ((!candidate_no,Ast.Ident("__paramod",None)),s))
1635 (auto_eq_check cache.unit_eq status) in
1637 if (l1 <> []) then []
1638 else applicative_case depth signature status flags gty cache
1639 (* fast paramodulation *)
1641 (* states in l1 have have an empty set of subgoals: no point to sort them *)
1642 l1 @ (sort_new_elems (l@l2)), cache
1645 let pp_goal = function
1646 | (_,Continuationals.Stack.Open i)
1647 | (_,Continuationals.Stack.Closed i) -> string_of_int i
1650 let pp_goals status l =
1654 let gty = get_goalty status i in
1655 NTacStatus.ppterm status gty)
1662 let compare = Pervasives.compare
1666 module MS = HTopoSort.Make(M)
1669 let sort_tac status =
1671 match status#stack with
1672 | [] -> assert false
1673 | (goals, t, k, tag) :: s ->
1674 let g = head_goals status#stack in
1676 (List.rev (MS.topological_sort g (deps status))) in
1677 debug_print (lazy ("old g = " ^
1678 String.concat "," (List.map string_of_int g)));
1679 debug_print (lazy ("sorted goals = " ^
1680 String.concat "," (List.map string_of_int sortedg)));
1681 let is_it i = function
1682 | (_,Continuationals.Stack.Open j )
1683 | (_,Continuationals.Stack.Closed j ) -> i = j
1686 List.map (fun i -> List.find (is_it i) goals) sortedg
1688 (sorted_goals, t, k, tag) :: s
1690 status#set_stack gstatus
1693 let clean_up_tac status =
1695 match status#stack with
1696 | [] -> assert false
1697 | (g, t, k, tag) :: s ->
1698 let is_open = function
1699 | (_,Continuationals.Stack.Open _) -> true
1700 | (_,Continuationals.Stack.Closed _) -> false
1702 let g' = List.filter is_open g in
1703 (g', t, k, tag) :: s
1705 status#set_stack gstatus
1708 let focus_tac focus status =
1710 match status#stack with
1711 | [] -> assert false
1712 | (g, t, k, tag) :: s ->
1713 let in_focus = function
1714 | (_,Continuationals.Stack.Open i)
1715 | (_,Continuationals.Stack.Closed i) -> List.mem i focus
1717 let focus,others = List.partition in_focus g
1719 (* we need to mark it as a BranchTag, otherwise cannot merge later *)
1720 (focus,[],[],`BranchTag) :: (others, t, k, tag) :: s
1722 status#set_stack gstatus
1725 let rec auto_clusters ?(top=false)
1726 flags signature cache depth status : unit =
1727 debug_print ~depth (lazy "entering auto clusters");
1728 (* ignore(Unix.select [] [] [] 0.01); *)
1729 let status = clean_up_tac status in
1730 let goals = head_goals status#stack in
1731 if goals = [] then raise (Proved status)
1732 else if depth = flags.maxdepth then raise (Gaveup IntSet.empty)
1733 else if List.length goals < 2 then
1734 auto_main flags signature cache depth status
1736 debug_print ~depth (lazy ("goals = " ^
1737 String.concat "," (List.map string_of_int goals)));
1738 let classes = HExtlib.clusters (deps status) goals in
1739 let classes = if top then List.rev classes else classes in
1745 ("cluster:" ^ String.concat "," (List.map string_of_int l)))
1749 (fun (status,b) gl ->
1750 let status = focus_tac gl status in
1752 debug_print ~depth (lazy ("focusing on" ^
1753 String.concat "," (List.map string_of_int gl)));
1754 auto_main flags signature cache depth status; assert false
1756 | Proved(status) -> (NTactics.merge_tac status,true)
1757 | Gaveup _ when top -> (NTactics.merge_tac status,b)
1759 (status,false) classes
1760 in if b then raise (Proved status) else raise (Gaveup IntSet.empty)
1764 (* the goals returned upon failure are an unsatisfiable subset
1765 of the initial head goals in the stack *)
1767 auto_main flags signature (cache:cache) depth status: unit =
1768 debug_print ~depth (lazy "entering auto main");
1769 (* ignore(Unix.select [] [] [] 0.01); *)
1770 let status = sort_tac (clean_up_tac status) in
1771 let goals = head_goals status#stack in
1773 | [] -> raise (Proved status)
1775 let branch = List.length(goals)>1 in
1776 if depth = flags.maxdepth then raise (Gaveup IntSet.empty)
1779 if branch then NTactics.branch_tac status
1781 let status, cache = intros ~depth status cache in
1782 let g,gctx, gty = current_goal status in
1783 let ctx,ty = close status g in
1784 let closegty = mk_cic_term ctx ty in
1785 let status, gty = apply_subst status gctx gty in
1786 debug_print ~depth (lazy("Attacking goal " ^ (string_of_int g) ^" : "^ppterm status gty));
1787 if is_subsumed depth status closegty cache.under_inspection then
1788 (debug_print (lazy "SUBSUMED");
1789 raise (Gaveup IntSet.add g IntSet.empty))
1791 let alternatives, cache =
1792 do_something signature flags status g depth gty cache in
1794 let under_inspection =
1795 add_to_th closegty cache.under_inspection closegty in
1796 {cache with under_inspection = under_inspection} in
1799 (* the underscore information does not need to be returned
1801 (fun unsat ((_,t),status) ->
1802 let depth',looping_cache =
1803 if t=Ast.Ident("__whd",None) then depth,cache
1804 else depth+1, loop_cache in
1805 debug_print (~depth:depth')
1806 (lazy ("Case: " ^ CicNotationPp.pp_term t));
1807 try auto_clusters flags signature loop_cache
1808 depth' status; unsat
1811 debug_print (~depth:depth') (lazy "proved");
1813 let status = NTactics.merge_tac status
1815 (* old cache, here *)
1816 try auto_clusters flags signature cache
1817 depth status; assert false
1820 (lazy ("Unsat1 at depth " ^ (string_of_int depth)
1822 (pp_goals status (IntSet.elements f))));
1823 (* TODO: cache failures *)
1824 IntSet.union f unsat
1825 else raise (Proved status)
1827 debug_print (~depth:depth')
1828 (lazy ("Unsat2 at depth " ^ (string_of_int depth')
1830 (pp_goals status (IntSet.elements f))));
1831 (* TODO: cache local failures *)
1833 IntSet.empty alternatives
1835 raise (Gaveup IntSet.add orig unsat)
1838 let int name l def =
1839 try int_of_string (List.assoc name l)
1840 with Failure _ | Not_found -> def
1843 let auto_tac ~params:(_univ,flags) status =
1844 let oldstatus = status in
1845 let status = (status:> NTacStatus.tac_status) in
1846 let goals = head_goals status#stack in
1847 let status, facts = mk_th_cache status goals in
1848 let unit_eq = index_local_equations status#eq_cache status in
1849 let cache = init_cache ~facts ~unit_eq () in
1850 (* pp_th status facts; *)
1852 NDiscriminationTree.DiscriminationTree.iter status#auto_cache (fun p t ->
1854 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
1855 String.concat "\n " (List.map (
1856 NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[])
1857 (NDiscriminationTree.TermSet.elements t))
1860 let depth = int "depth" flags 3 in
1861 let size = int "size" flags 10 in
1862 let width = int "width" flags (3+List.length goals) in
1864 let goals = List.map (fun i -> (i,P)) goals in
1865 let signature = () in
1870 timeout = Unix.gettimeofday() +. 3000.;
1873 let initial_time = Unix.gettimeofday() in
1878 ("TIME ELAPSED:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1880 ("Applicative nodes:"^string_of_int !app_counter));
1881 raise (Error (lazy "auto gave up", None)))
1883 let _ = debug_print (lazy("\n\nRound "^string_of_int x^"\n")) in
1884 let flags = { flags with maxdepth = x }
1886 try auto_clusters (~top:true) flags signature cache 0 status;assert false
1888 | Gaveup _ -> up_to (x+1) y
1890 print (lazy ("proved at depth " ^ string_of_int x));
1893 | (g,t,k,f) :: rest -> (filter_open g,t,k,f):: rest
1896 let s = s#set_stack stack in
1897 oldstatus#set_status s
1899 let s = up_to depth depth in
1901 ("TIME ELAPSED:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1903 ("Applicative nodes:"^string_of_int !app_counter));