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_______________________________________________________________ *)
14 let print ?(depth=0) s =
15 prerr_endline (String.make depth '\t'^Lazy.force s)
16 let noprint ?(depth=0) _ = ()
17 let debug_print = noprint
19 open Continuationals.Stack
21 module Ast = CicNotationPt
23 (* ======================= statistics ========================= *)
25 let app_counter = ref 0
28 type t = NReference.reference
30 let compare = Pervasives.compare
31 let hash = Hashtbl.hash
34 module RefHash = Hashtbl.Make(RHT);;
37 nominations : int ref;
41 let statistics: info RefHash.t = RefHash.create 503
43 let incr_nominations tbl item =
45 let v = RefHash.find tbl item in incr v.nominations
47 RefHash.add tbl item {nominations = ref 1; uses = ref 0}
49 let incr_uses tbl item =
51 let v = RefHash.find tbl item in incr v.uses
52 with Not_found -> assert false
58 | Ast.NCic _ (* local candidate *)
61 let is_relevant tbl item =
63 let v = RefHash.find tbl item in
64 if !(v.nominations) < 60 then true (* not enough info *)
65 else if !(v.uses) = 0 then false
67 with Not_found -> true
70 let l = RefHash.fold (fun a v l -> (a,v)::l) tbl [] in
71 let relevance v = float !(v.uses) /. float !(v.nominations) in
72 let vcompare (_,v1) (_,v2) =
73 Pervasives.compare (relevance v1) (relevance v2) in
74 let l = List.sort vcompare l in
76 CicNotationPp.pp_term (Ast.NCic (NCic.Const a)) ^ ": rel = " ^
77 (string_of_float (relevance v)) ^
78 "; uses = " ^ (string_of_int !(v.uses)) ^
79 "; nom = " ^ (string_of_int !(v.nominations)) in
80 lazy ("\n\nSTATISTICS:\n" ^
81 String.concat "\n" (List.map vstring l))
83 (* ======================= utility functions ========================= *)
84 module IntSet = Set.Make(struct type t = int let compare = compare end)
86 let get_sgoalty status g =
87 let _,_,metasenv,subst,_ = status#obj in
89 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
90 let ty = NCicUntrusted.apply_subst subst ctx ty in
91 let ctx = NCicUntrusted.apply_subst_context
92 ~fix_projections:true subst ctx
94 NTacStatus.mk_cic_term ctx ty
95 with NCicUtils.Meta_not_found _ as exn -> fail ~exn (lazy "get_sgoalty")
99 let gty = get_sgoalty status g in
100 metas_of_term status gty
103 let menv_closure status gl =
104 let rec closure acc = function
106 | x::l when IntSet.mem x acc -> closure acc l
107 | x::l -> closure (IntSet.add x acc) (deps status x @ l)
108 in closure IntSet.empty gl
111 (* we call a "fact" an object whose hypothesis occur in the goal
112 or in types of goal-variables *)
113 let branch status ty =
114 let status, ty, metas = saturate ~delta:0 status ty in
115 noprint (lazy ("saturated ty :" ^ (ppterm status ty)));
116 let g_metas = metas_of_term status ty in
117 let clos = menv_closure status g_metas in
118 (* let _,_,metasenv,_,_ = status#obj in *)
122 let _, m = term_of_cic_term status m (ctx_of m) in
124 | NCic.Meta(i,_) -> IntSet.add i acc
128 (* IntSet.subset menv clos *)
129 IntSet.cardinal(IntSet.diff menv clos)
131 let is_a_fact status ty = branch status ty = 0
133 let is_a_fact_obj s uri =
134 let obj = NCicEnvironment.get_checked_obj uri in
136 | (_,_,[],[],NCic.Constant(_,_,_,ty,_)) ->
137 is_a_fact s (mk_cic_term [] ty)
138 (* aggiungere i costruttori *)
141 let is_a_fact_ast status subst metasenv ctx cand =
143 (lazy ("------- checking " ^ CicNotationPp.pp_term cand));
144 let status, t = disambiguate status ctx ("",0,cand) None in
145 let status,t = term_of_cic_term status t ctx in
146 let ty = NCicTypeChecker.typeof subst metasenv ctx t in
147 is_a_fact status (mk_cic_term ctx ty)
149 let current_goal status =
150 let open_goals = head_goals status#stack in
151 assert (List.length open_goals = 1);
152 let open_goal = List.hd open_goals in
153 let gty = get_goalty status open_goal in
154 let ctx = ctx_of gty in
157 let height_of_ref (NReference.Ref (uri, x)) =
162 | NReference.CoFix _ ->
163 let _,height,_,_,_ = NCicEnvironment.get_checked_obj uri in
165 | NReference.Def h -> h
166 | NReference.Fix (_,_,h) -> h
169 (*************************** height functions ********************************)
170 let fast_height_of_term t =
174 NCic.Meta (_,(_,NCic.Ctx l)) -> List.iter aux l
178 | NCic.Implicit _ -> assert false
181 prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[]
182 ~context:[] t ^ ":" ^ string_of_int (height_of_ref nref));
184 h := max !h (height_of_ref nref)
185 | NCic.Prod (_,t1,t2)
186 | NCic.Lambda (_,t1,t2) -> aux t1; aux t2
187 | NCic.LetIn (_,s,ty,t) -> aux s; aux ty; aux t
188 | NCic.Appl l -> List.iter aux l
189 | NCic.Match (_,outty,t,pl) -> aux outty; aux t; List.iter aux pl
194 let height_of_goal g status =
195 let ty = get_goalty status g in
196 let context = ctx_of ty in
197 let _, ty = term_of_cic_term status ty (ctx_of ty) in
198 let h = ref (fast_height_of_term ty) in
201 | _, NCic.Decl ty -> h := max !h (fast_height_of_term ty)
202 | _, NCic.Def (bo,ty) ->
203 h := max !h (fast_height_of_term ty);
204 h := max !h (fast_height_of_term bo);
210 let height_of_goals status =
211 let open_goals = head_goals status#stack in
212 assert (List.length open_goals > 0);
216 h := max !h (height_of_goal open_goal status))
218 debug_print (lazy ("altezza sequente: " ^ string_of_int !h));
222 (* =============================== paramod =========================== *)
223 let solve f status eq_cache goal =
226 if fast then NCicParamod.fast_eq_check
227 else NCicParamod.paramod in
229 let n,h,metasenv,subst,o = status#obj in
230 let gname, ctx, gty = List.assoc goal metasenv in
231 let gty = NCicUntrusted.apply_subst subst ctx gty in
232 let build_status (pt, _, metasenv, subst) =
234 debug_print (lazy ("refining: "^(NCicPp.ppterm ctx subst metasenv pt)));
235 let stamp = Unix.gettimeofday () in
236 let metasenv, subst, pt, pty =
237 (* NCicRefiner.typeof status
238 (* (status#set_coerc_db NCicCoercion.empty_db) *)
239 metasenv subst ctx pt None in
240 print (lazy ("refined: "^(NCicPp.ppterm ctx subst metasenv pt)));
241 debug_print (lazy ("synt: "^(NCicPp.ppterm ctx subst metasenv pty)));
242 let metasenv, subst =
243 NCicUnification.unify status metasenv subst ctx gty pty *)
245 (status#set_coerc_db NCicCoercion.empty_db)
246 metasenv subst ctx pt (Some gty)
248 debug_print (lazy (Printf.sprintf "Refined in %fs"
249 (Unix.gettimeofday() -. stamp)));
250 let status = status#set_obj (n,h,metasenv,subst,o) in
251 let metasenv = List.filter (fun j,_ -> j <> goal) metasenv in
252 let subst = (goal,(gname,ctx,pt,pty)) :: subst in
253 Some (status#set_obj (n,h,metasenv,subst,o))
255 NCicRefiner.RefineFailure msg
256 | NCicRefiner.Uncertain msg ->
257 debug_print (lazy ("WARNING: refining in fast_eq_check failed\n" ^
258 snd (Lazy.force msg) ^
259 "\n in the environment\n" ^
260 NCicPp.ppmetasenv subst metasenv)); None
261 | NCicRefiner.AssertFailure msg ->
262 debug_print (lazy ("WARNING: refining in fast_eq_check failed" ^
264 "\n in the environment\n" ^
265 NCicPp.ppmetasenv subst metasenv)); None
268 HExtlib.filter_map build_status
269 (f status metasenv subst ctx eq_cache (NCic.Rel ~-1,gty))
272 let fast_eq_check eq_cache status (goal:int) =
273 match solve NCicParamod.fast_eq_check status eq_cache goal with
274 | [] -> raise (Error (lazy "no proof found",None))
278 let dist_fast_eq_check eq_cache s =
279 NTactics.distribute_tac (fast_eq_check eq_cache) s
282 let auto_eq_check eq_cache status =
284 let s = dist_fast_eq_check eq_cache status in
287 | Error _ -> debug_print (lazy ("no paramod proof found"));[]
290 let index_local_equations eq_cache status =
291 debug_print (lazy "indexing equations");
292 let open_goals = head_goals status#stack in
293 let open_goal = List.hd open_goals in
294 let ngty = get_goalty status open_goal in
295 let ctx = apply_subst_context ~fix_projections:true status (ctx_of ngty) in
300 let t = NCic.Rel !c in
302 let ty = NCicTypeChecker.typeof [] [] ctx t in
303 if is_a_fact status (mk_cic_term ctx ty) then
304 (debug_print(lazy("eq indexing " ^ (NCicPp.ppterm ctx [] [] ty)));
305 NCicParamod.forward_infer_step eq_cache t ty)
307 (debug_print (lazy ("not a fact: " ^ (NCicPp.ppterm ctx [] [] ty)));
310 | NCicTypeChecker.TypeCheckerFailure _
311 | NCicTypeChecker.AssertFailure _ -> eq_cache)
315 let fast_eq_check_tac ~params s =
316 let unit_eq = index_local_equations s#eq_cache s in
317 dist_fast_eq_check unit_eq s
320 let paramod eq_cache status goal =
321 match solve NCicParamod.paramod status eq_cache goal with
322 | [] -> raise (Error (lazy "no proof found",None))
326 let paramod_tac ~params s =
327 let unit_eq = index_local_equations s#eq_cache s in
328 NTactics.distribute_tac (paramod unit_eq) s
331 let demod eq_cache status goal =
332 match solve NCicParamod.demod status eq_cache goal with
333 | [] -> raise (Error (lazy "no progress",None))
337 let demod_tac ~params s =
338 let unit_eq = index_local_equations s#eq_cache s in
339 NTactics.distribute_tac (demod unit_eq) s
343 let fast_eq_check_tac_all ~params eq_cache status =
344 let g,_,_ = current_goal status in
345 let allstates = fast_eq_check_all status eq_cache g in
346 let pseudo_low_tac s _ _ = s in
347 let pseudo_low_tactics =
348 List.map pseudo_low_tac allstates
350 List.map (fun f -> NTactics.distribute_tac f status) pseudo_low_tactics
355 let demod status eq_cache goal =
356 let n,h,metasenv,subst,o = status#obj in
357 let gname, ctx, gty = List.assoc goal metasenv in
358 let gty = NCicUntrusted.apply_subst subst ctx gty in
360 let demod_tac ~params s =
361 let unit_eq = index_local_equations s#eq_cache s in
362 dist_fast_eq_check unit_eq s
365 (*************** subsumption ****************)
367 let close_wrt_context =
371 | name, NCic.Decl t -> NCic.Prod(name,t,ty)
372 | name, NCic.Def(bo, _) -> NCicSubstitution.subst bo ty)
375 let args_for_context ?(k=1) ctx =
378 (fun (n,l) ctx_entry ->
380 | name, NCic.Decl t -> n+1,NCic.Rel(n)::l
381 | name, NCic.Def(bo, _) -> n+1,l)
385 let constant_for_meta ctx ty i =
386 let name = "cic:/foo"^(string_of_int i)^".con" in
387 let uri = NUri.uri_of_string name in
388 let ty = close_wrt_context ty ctx in
389 (* prerr_endline (NCicPp.ppterm [] [] [] ty); *)
390 let attr = (`Generated,`Definition,`Local) in
391 let obj = NCic.Constant([],name,None,ty,attr) in
392 (* Constant of relevance * string * term option * term * c_attr *)
396 let refresh metasenv =
398 (fun (metasenv,subst) (i,(iattr,ctx,ty)) ->
399 let ikind = NCicUntrusted.kind_of_meta iattr in
400 let metasenv,j,instance,ty =
401 NCicMetaSubst.mk_meta ~attrs:iattr
402 metasenv ctx ~with_type:ty ikind in
403 let s_entry = i,(iattr, ctx, instance, ty) in
404 let metasenv = List.filter (fun x,_ -> i <> x) metasenv in
405 metasenv,s_entry::subst)
406 (metasenv,[]) metasenv
408 (* close metasenv returns a ground instance of all the metas in the
409 metasenv, insantiatied with axioms, and the list of these axioms *)
410 let close_metasenv metasenv subst =
412 let metasenv = NCicUntrusted.apply_subst_metasenv subst metasenv in
414 let metasenv = NCicUntrusted.sort_metasenv subst metasenv in
416 (fun (subst,objs) (i,(iattr,ctx,ty)) ->
417 let ty = NCicUntrusted.apply_subst subst ctx ty in
419 NCicUntrusted.apply_subst_context ~fix_projections:true
421 let (uri,_,_,_,obj) as okind =
422 constant_for_meta ctx ty i in
424 NCicEnvironment.check_and_add_obj okind;
425 let iref = NReference.reference_of_spec uri NReference.Decl in
427 let args = args_for_context ctx in
428 if args = [] then NCic.Const iref
429 else NCic.Appl(NCic.Const iref::args)
431 (* prerr_endline (NCicPp.ppterm ctx [] [] iterm); *)
432 let s_entry = i, ([], ctx, iterm, ty)
433 in s_entry::subst,okind::objs
434 with _ -> assert false)
438 let ground_instances status gl =
439 let _,_,metasenv,subst,_ = status#obj in
440 let subset = menv_closure status gl in
441 let submenv = List.filter (fun (x,_) -> IntSet.mem x subset) metasenv in
443 let submenv = metasenv in
445 let subst, objs = close_metasenv submenv subst in
449 let (_, ctx, t, _) = List.assoc i subst in
450 debug_print (lazy (NCicPp.ppterm ctx [] [] t));
452 (fun (uri,_,_,_,_) as obj ->
453 NCicEnvironment.invalidate_item (`Obj (uri, obj)))
458 Not_found -> assert false
462 let replace_meta i args target =
463 let rec aux k = function
464 (* TODO: local context *)
465 | NCic.Meta (j,lc) when i = j ->
469 List.map (NCicSubstitution.subst_meta lc) args in
470 NCic.Appl(NCic.Rel k::args))
471 | NCic.Meta (j,lc) as m ->
478 aux k (NCicSubstitution.lift n t)) l))))
479 | t -> NCicUtils.map (fun _ k -> k+1) k aux t
484 let close_wrt_metasenv subst =
486 (fun ty (i,(iattr,ctx,mty)) ->
487 let mty = NCicUntrusted.apply_subst subst ctx mty in
489 NCicUntrusted.apply_subst_context ~fix_projections:true
491 let cty = close_wrt_context mty ctx in
492 let name = "foo"^(string_of_int i) in
493 let ty = NCicSubstitution.lift 1 ty in
494 let args = args_for_context ~k:1 ctx in
495 (* prerr_endline (NCicPp.ppterm ctx [] [] iterm); *)
496 let ty = replace_meta i args ty
498 NCic.Prod(name,cty,ty))
502 let _,_,metasenv,subst,_ = status#obj in
503 let subset = menv_closure status [g] in
504 let subset = IntSet.remove g subset in
505 let elems = IntSet.elements subset in
506 let _, ctx, ty = NCicUtils.lookup_meta g metasenv in
507 let ty = NCicUntrusted.apply_subst subst ctx ty in
508 debug_print (lazy ("metas in " ^ (NCicPp.ppterm ctx [] metasenv ty)));
509 debug_print (lazy (String.concat ", " (List.map string_of_int elems)));
510 let submenv = List.filter (fun (x,_) -> IntSet.mem x subset) metasenv in
511 let submenv = List.rev (NCicUntrusted.sort_metasenv subst submenv) in
513 let submenv = metasenv in
515 let ty = close_wrt_metasenv subst ty submenv in
516 debug_print (lazy (NCicPp.ppterm ctx [] [] ty));
520 (****************** smart application ********************)
522 let saturate_to_ref metasenv subst ctx nref ty =
523 let height = height_of_ref nref in
524 let rec aux metasenv ty args =
525 let ty,metasenv,moreargs =
526 NCicMetaSubst.saturate ~delta:height metasenv subst ctx ty 0 in
528 | NCic.Const(NReference.Ref (_,NReference.Def _) as nre)
530 let _, _, bo, _, _, _ = NCicEnvironment.get_checked_def nre in
531 aux metasenv bo (args@moreargs)
532 | NCic.Appl(NCic.Const(NReference.Ref (_,NReference.Def _) as nre)::tl)
534 let _, _, bo, _, _, _ = NCicEnvironment.get_checked_def nre in
535 aux metasenv (NCic.Appl(bo::tl)) (args@moreargs)
536 | _ -> ty,metasenv,(args@moreargs)
540 let smart_apply t unit_eq status g =
541 let n,h,metasenv,subst,o = status#obj in
542 let gname, ctx, gty = List.assoc g metasenv in
543 (* let ggty = mk_cic_term context gty in *)
544 let status, t = disambiguate status ctx t None in
545 let status,t = term_of_cic_term status t ctx in
546 let _,_,metasenv,subst,_ = status#obj in
547 let ty = NCicTypeChecker.typeof subst metasenv ctx t in
548 let ty,metasenv,args =
551 | NCic.Appl(NCic.Const(nref)::_) ->
552 saturate_to_ref metasenv subst ctx nref ty
554 NCicMetaSubst.saturate metasenv subst ctx ty 0 in
555 let metasenv,j,inst,_ = NCicMetaSubst.mk_meta metasenv ctx `IsTerm in
556 let status = status#set_obj (n,h,metasenv,subst,o) in
557 let pterm = if args=[] then t else
559 | NCic.Appl l -> NCic.Appl(l@args)
560 | _ -> NCic.Appl(t::args)
562 noprint(lazy("pterm " ^ (NCicPp.ppterm ctx [] [] pterm)));
563 noprint(lazy("pty " ^ (NCicPp.ppterm ctx [] [] ty)));
566 NUri.uri_of_string "cic:/matita/ng/Plogic/equality/eq_coerc.con" in
567 let ref = NReference.reference_of_spec uri (NReference.Def(2)) in
571 NCic.Appl[eq_coerc;ty;NCic.Implicit `Type;pterm;inst] in
572 let smart = mk_cic_term ctx smart in
574 let status = instantiate status g smart in
575 let _,_,metasenv,subst,_ = status#obj in
576 let _,ctx,jty = List.assoc j metasenv in
577 let jty = NCicUntrusted.apply_subst subst ctx jty in
578 debug_print(lazy("goal " ^ (NCicPp.ppterm ctx [] [] jty)));
579 fast_eq_check unit_eq status j
581 | Error _ as e -> debug_print (lazy "error"); raise e
583 let smart_apply_tac t s =
584 let unit_eq = index_local_equations s#eq_cache s in
585 NTactics.distribute_tac (smart_apply t unit_eq) s
587 let smart_apply_auto t eq_cache =
588 NTactics.distribute_tac (smart_apply t eq_cache)
591 (****************** types **************)
594 type th_cache = (NCic.context * InvRelDiscriminationTree.t) list
596 (* cartesian: term set list -> term list set *)
599 [] -> NDiscriminationTree.TermListSet.empty
601 NDiscriminationTree.TermSet.fold
602 (fun x acc -> NDiscriminationTree.TermListSet.add [x] acc) l NDiscriminationTree.TermListSet.empty
604 let rest = cartesian tl in
605 NDiscriminationTree.TermSet.fold
607 NDiscriminationTree.TermListSet.fold (fun l acc' -> NDiscriminationTree.TermListSet.add (x::l) acc') rest acc
608 ) he NDiscriminationTree.TermListSet.empty
611 (* all_keys_of_cic_type: term -> term set *)
612 let all_keys_of_cic_type metasenv subst context ty =
614 (* Here we are dropping the metasenv, but this should not raise any
615 exception (hopefully...) *)
617 NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0
623 NCic.Appl (he::tl) ->
626 let wty = NCicReduction.whd ~delta:0 ~subst context ty in
628 NDiscriminationTree.TermSet.add ty (aux ty)
630 NDiscriminationTree.TermSet.union
631 (NDiscriminationTree.TermSet.add ty (aux ty))
632 (NDiscriminationTree.TermSet.add wty (aux wty))
635 NDiscriminationTree.TermListSet.fold
636 (fun l acc -> NDiscriminationTree.TermSet.add (NCic.Appl l) acc)
637 (cartesian ((NDiscriminationTree.TermSet.singleton he)::tl'))
638 NDiscriminationTree.TermSet.empty
639 | _ -> NDiscriminationTree.TermSet.empty
641 let ty,ity = saturate ty in
642 let wty,iwty = saturate (NCicReduction.whd ~delta:0 ~subst context ty) in
644 [ity, NDiscriminationTree.TermSet.add ty (aux ty)]
646 [ity, NDiscriminationTree.TermSet.add ty (aux ty) ;
647 iwty, NDiscriminationTree.TermSet.add wty (aux wty) ]
650 let all_keys_of_type status t =
651 let _,_,metasenv,subst,_ = status#obj in
652 let context = ctx_of t in
653 let status, t = apply_subst status context t in
655 all_keys_of_cic_type metasenv subst context
656 (snd (term_of_cic_term status t context))
660 (fun (intros,keys) ->
662 NDiscriminationTree.TermSet.fold
663 (fun t acc -> Ncic_termSet.add (mk_cic_term context t) acc)
664 keys Ncic_termSet.empty
669 let keys_of_type status orig_ty =
670 (* Here we are dropping the metasenv (in the status), but this should not
671 raise any exception (hopefully...) *)
672 let _, ty, _ = saturate ~delta:max_int status orig_ty in
673 let _, ty = apply_subst status (ctx_of ty) ty in
676 let orig_ty' = NCicTacReduction.normalize ~subst context orig_ty in
677 if orig_ty' <> orig_ty then
678 let ty',_,_= NCicMetaSubst.saturate ~delta:0 metasenv subst context orig_ty' 0 in
684 (*CSC: strange: we keep ty, ty normalized and ty ~delta:(h-1) *)
686 let _, ty = term_of_cic_term status ty (ctx_of ty) in
688 | NCic.Const (NReference.Ref (_,(NReference.Def h | NReference.Fix (_,_,h))))
689 | NCic.Appl (NCic.Const(NReference.Ref(_,(NReference.Def h | NReference.Fix (_,_,h))))::_)
691 let _,ty,_= saturate status ~delta:(h-1) orig_ty in
698 let all_keys_of_term status t =
699 let status, orig_ty = typeof status (ctx_of t) t in
700 all_keys_of_type status orig_ty
703 let keys_of_term status t =
704 let status, orig_ty = typeof status (ctx_of t) t in
705 keys_of_type status orig_ty
708 let mk_th_cache status gl =
710 (fun (status, acc) g ->
711 let gty = get_goalty status g in
712 let ctx = ctx_of gty in
713 debug_print(lazy("th cache for: "^ppterm status gty));
714 debug_print(lazy("th cache in: "^ppcontext status ctx));
715 if List.mem_assq ctx acc then status, acc else
716 let idx = InvRelDiscriminationTree.empty in
719 (fun (status, i, idx) _ ->
720 let t = mk_cic_term ctx (NCic.Rel i) in
721 let status, keys = keys_of_term status t in
722 debug_print(lazy("indexing: "^ppterm status t ^ ": " ^ string_of_int (List.length keys)));
724 List.fold_left (fun idx k ->
725 InvRelDiscriminationTree.index idx k t) idx keys
730 status, (ctx, idx) :: acc)
734 let add_to_th t c ty =
735 let key_c = ctx_of t in
736 if not (List.mem_assq key_c c) then
737 (key_c ,InvRelDiscriminationTree.index
738 InvRelDiscriminationTree.empty ty t ) :: c
740 let rec replace = function
742 | (x, idx) :: tl when x == key_c ->
743 (x, InvRelDiscriminationTree.index idx ty t) :: tl
744 | x :: tl -> x :: replace tl
749 let rm_from_th t c ty =
750 let key_c = ctx_of t in
751 if not (List.mem_assq key_c c) then assert false
753 let rec replace = function
755 | (x, idx) :: tl when x == key_c ->
756 (x, InvRelDiscriminationTree.remove_index idx ty t) :: tl
757 | x :: tl -> x :: replace tl
762 let pp_idx status idx =
763 InvRelDiscriminationTree.iter idx
765 debug_print(lazy("K: " ^ NCicInverseRelIndexable.string_of_path k));
767 (fun t -> debug_print(lazy("\t"^ppterm status t)))
774 debug_print(lazy( "-----------------------------------------------"));
775 debug_print(lazy( (NCicPp.ppcontext ~metasenv:[] ~subst:[] ctx)));
776 debug_print(lazy( "||====> "));
780 let search_in_th gty th =
781 let c = ctx_of gty in
782 let rec aux acc = function
783 | [] -> (* Ncic_termSet.elements *) acc
786 let idx = List.assoc(*q*) k th in
787 let acc = Ncic_termSet.union acc
788 (InvRelDiscriminationTree.retrieve_unifiables idx gty)
791 with Not_found -> aux acc tl
793 aux Ncic_termSet.empty c
797 do_types : bool; (* solve goals in Type *)
798 last : bool; (* last goal: take first solution only *)
799 candidates: Ast.term list option;
807 {facts : th_cache; (* positive results *)
808 under_inspection : cic_term list * th_cache; (* to prune looping *)
809 unit_eq : NCicParamod.state;
813 let add_to_trace ~depth cache t =
816 debug_print ~depth (lazy ("Adding to trace: " ^ CicNotationPp.pp_term t));
817 {cache with trace = t::cache.trace}
818 | Ast.NCic _ (* local candidate *)
819 | _ -> (*not an application *) cache
822 (lazy ("Proof Trace: " ^ (String.concat ";"
823 (List.map CicNotationPp.pp_term tr))))
825 let remove_from_trace cache t =
828 (match cache.trace with
829 | _::tl -> {cache with trace = tl}
831 | Ast.NCic _ (* local candidate *)
832 | _ -> (*not an application *) cache *)
835 type goal = int * sort (* goal, depth, sort *)
836 type fail = goal * cic_term
837 type candidate = int * Ast.term (* unique candidate number, candidate *)
839 exception Gaveup of IntSet.t (* a sublist of unprovable conjunctive
840 atoms of the input goals *)
841 exception Proved of NTacStatus.tac_status * Ast.term list
843 (* let close_failures _ c = c;; *)
844 (* let prunable _ _ _ = false;; *)
845 (* let cache_examine cache gty = `Notfound;; *)
846 (* let put_in_subst s _ _ _ = s;; *)
847 (* let add_to_cache_and_del_from_orlist_if_green_cut _ _ c _ _ o f _ = c, o, f, false ;; *)
848 (* let cache_add_underinspection c _ _ = c;; *)
850 let init_cache ?(facts=[]) ?(under_inspection=[],[])
851 ?(unit_eq=NCicParamod.empty_state)
855 under_inspection = under_inspection;
859 let only signature _context candidate = true
861 (* TASSI: nel trie ci mettiamo solo il body, non il ty *)
863 NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] candidate
865 let height = fast_height_of_term candidate_ty in
866 let rc = signature >= height in
868 debug_print (lazy ("Filtro: " ^ NCicPp.ppterm ~context:[] ~subst:[]
869 ~metasenv:[] candidate ^ ": " ^ string_of_int height))
871 debug_print (lazy ("Tengo: " ^ NCicPp.ppterm ~context:[] ~subst:[]
872 ~metasenv:[] candidate ^ ": " ^ string_of_int height));
877 let candidate_no = ref 0;;
879 let openg_no status = List.length (head_goals status#stack)
881 let sort_candidates status ctx candidates =
882 let _,_,metasenv,subst,_ = status#obj in
884 let status,ct = disambiguate status ctx ("",0,cand) None in
885 let status,t = term_of_cic_term status ct ctx in
886 let ty = NCicTypeChecker.typeof subst metasenv ctx t in
887 let res = branch status (mk_cic_term ctx ty) in
888 debug_print (lazy ("branch factor for: " ^ (ppterm status ct) ^ " = "
889 ^ (string_of_int res)));
892 let candidates = List.map (fun t -> branch t,t) candidates in
894 List.sort (fun (a,_) (b,_) -> a - b) candidates in
895 let candidates = List.map snd candidates in
896 debug_print (lazy ("candidates =\n" ^ (String.concat "\n"
897 (List.map CicNotationPp.pp_term candidates))));
900 let sort_new_elems l =
901 List.sort (fun (_,s1) (_,s2) -> openg_no s1 - openg_no s2) l
903 let try_candidate ?(smart=0) flags depth status eq_cache ctx t =
905 debug_print ~depth (lazy ("try " ^ CicNotationPp.pp_term t));
907 if smart= 0 then NTactics.apply_tac ("",0,t) status
908 else if smart = 1 then smart_apply_auto ("",0,t) eq_cache status
909 else (* smart = 2: both *)
910 try NTactics.apply_tac ("",0,t) status
912 smart_apply_auto ("",0,t) eq_cache status
915 let og_no = openg_no status in
916 if (* og_no > flags.maxwidth || *)
917 ((depth + 1) = flags.maxdepth && og_no <> 0) then
918 (debug_print ~depth (lazy "pruned immediately"); None)
921 let status, cict = disambiguate status ctx ("",0,t) None in
922 let status,ct = term_of_cic_term status cict ctx in
923 let _,_,metasenv,subst,_ = status#obj in
924 let ty = NCicTypeChecker.typeof subst metasenv ctx ct in
925 let res = branch status (mk_cic_term ctx ty) in
926 if smart=1 && og_no > res then
927 (print (lazy ("branch factor for: " ^ (ppterm status cict) ^ " = "
928 ^ (string_of_int res) ^ " vs. " ^ (string_of_int og_no)));
929 print ~depth (lazy "strange application"); None)
932 Some ((!candidate_no,t),status))
933 with Error (msg,exn) -> debug_print ~depth (lazy "failed"); None
936 let sort_of subst metasenv ctx t =
937 let ty = NCicTypeChecker.typeof subst metasenv ctx t in
938 let metasenv',ty = NCicUnification.fix_sorts metasenv subst ty in
939 assert (metasenv = metasenv');
940 NCicTypeChecker.typeof subst metasenv ctx ty
943 let type0= NUri.uri_of_string ("cic:/matita/pts/Type0.univ")
946 let perforate_small subst metasenv context t =
947 let rec aux = function
948 | NCic.Appl (hd::tl) ->
950 let s = sort_of subst metasenv context t in
952 | NCic.Sort(NCic.Type [`Type,u])
953 when u=type0 -> NCic.Meta (0,(0,NCic.Irl 0))
956 NCic.Appl (hd::List.map map tl)
962 let get_cands retrieve_for diff empty gty weak_gty =
963 let cands = retrieve_for gty in
965 | None -> cands, empty
967 let more_cands = retrieve_for weak_gty in
968 cands, diff more_cands cands
971 let get_candidates ?(smart=true) depth flags status cache signature gty =
972 let maxd = ((depth + 1) = flags.maxdepth) in
973 let universe = status#auto_cache in
974 let _,_,metasenv,subst,_ = status#obj in
975 let context = ctx_of gty in
976 let _, raw_gty = term_of_cic_term status gty context in
977 let raw_weak_gty, weak_gty =
983 let weak = perforate_small subst metasenv context raw_gty in
984 Some weak, Some (mk_cic_term context weak)
988 let global_cands, smart_global_cands =
989 match flags.candidates with
990 | Some l when (not maxd) -> l,[]
994 let to_ast = function
995 | NCic.Const r when true (*is_relevant statistics r*) -> Some (Ast.NRef r)
996 | NCic.Const _ -> None
997 | _ -> assert false in
999 to_ast (NDiscriminationTree.TermSet.elements s) in
1002 (NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1004 NDiscriminationTree.TermSet.diff
1005 NDiscriminationTree.TermSet.empty
1006 raw_gty raw_weak_gty in
1008 let local_cands,smart_local_cands =
1011 let _status, t = term_of_cic_term status t context
1013 List.map to_ast (Ncic_termSet.elements s) in
1016 (fun ty -> search_in_th ty cache)
1017 Ncic_termSet.diff Ncic_termSet.empty gty weak_gty in
1019 sort_candidates status context (global_cands@local_cands),
1020 sort_candidates status context (smart_global_cands@smart_local_cands)
1024 let get_candidates ?(smart=true) status cache signature gty =
1025 let universe = status#auto_cache in
1026 let _,_,metasenv,subst,_ = status#obj in
1027 let context = ctx_of gty in
1029 let _status, t = term_of_cic_term status t context
1031 let c_ast = function
1032 | NCic.Const r -> Ast.NRef r | _ -> assert false in
1033 let _, raw_gty = term_of_cic_term status gty context in
1034 let keys = all_keys_of_cic_term metasenv subst context raw_gty in
1035 (* we only keep those keys that do not require any intros for now *)
1036 let no_intros_keys = snd (List.hd keys) in
1038 NDiscriminationTree.TermSet.fold
1040 NDiscriminationTree.TermSet.union acc
1041 (NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1043 ) no_intros_keys NDiscriminationTree.TermSet.empty in
1045 let cands = NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1049 NDiscriminationTree.TermSet.fold
1051 Ncic_termSet.union acc (search_in_th (mk_cic_term context ty) cache)
1052 ) no_intros_keys Ncic_termSet.empty in
1054 let local_cands = search_in_th gty cache in
1056 debug_print (lazy ("candidates for" ^ NTacStatus.ppterm status gty));
1057 debug_print (lazy ("local cands = " ^ (string_of_int (List.length (Ncic_termSet.elements local_cands)))));
1058 let together global local =
1060 (List.filter (only signature context)
1061 (NDiscriminationTree.TermSet.elements global)) @
1062 List.map t_ast (Ncic_termSet.elements local) in
1063 let candidates = together cands local_cands in
1064 let candidates = sort_candidates status context candidates in
1065 let smart_candidates =
1071 let weak_gty = perforate_small subst metasenv context raw_gty in
1073 NCic.Appl (hd:: HExtlib.mk_list(NCic.Meta (0,(0,NCic.Irl 0)))
1074 (List.length tl)) in *)
1076 NDiscriminationTree.DiscriminationTree.retrieve_unifiables
1080 NDiscriminationTree.TermSet.diff more_cands cands in
1081 let cic_weak_gty = mk_cic_term context weak_gty in
1082 let more_local_cands = search_in_th cic_weak_gty cache in
1083 let smart_local_cands =
1084 Ncic_termSet.diff more_local_cands local_cands in
1085 together smart_cands smart_local_cands
1086 (* together more_cands more_local_cands *)
1090 let smart_candidates = sort_candidates status context smart_candidates in
1091 (* if smart then smart_candidates, []
1092 else candidates, [] *)
1093 candidates, smart_candidates
1096 let get_candidates ?(smart=true) flags status cache signature gty =
1097 match flags.candidates with
1098 | None -> get_candidates ~smart status cache signature gty
1102 let applicative_case depth signature status flags gty cache =
1103 app_counter:= !app_counter+1;
1104 let _,_,metasenv,subst,_ = status#obj in
1105 let context = ctx_of gty in
1106 let tcache = cache.facts in
1107 let is_prod, is_eq =
1108 let status, t = term_of_cic_term status gty context in
1109 let t = NCicReduction.whd subst context t in
1111 | NCic.Prod _ -> true, false
1112 | _ -> false, NCicParamod.is_equation metasenv subst context t
1114 debug_print~depth (lazy (string_of_bool is_eq));
1116 let candidates, smart_candidates =
1117 get_candidates ~smart:(not is_eq) depth
1118 flags status tcache signature gty in
1119 (* if the goal is an equation we avoid to apply unit equalities,
1120 since superposition should take care of them; refl is an
1121 exception since it prompts for convertibility *)
1123 let test x = not (is_a_fact_ast status subst metasenv context x) in
1125 Ast.Ident("refl",None) ::List.filter test candidates
1126 else candidates in *)
1128 let candidates, smart_candidates =
1129 get_candidates ~smart:true depth
1130 flags status tcache signature gty in
1131 (* if the goal is an equation we avoid to apply unit equalities,
1132 since superposition should take care of them; refl is an
1133 exception since it prompts for convertibility *)
1134 let candidates,smart_candidates =
1135 let test x = not (is_a_fact_ast status subst metasenv context x) in
1137 Ast.Ident("refl",None) ::List.filter test candidates,
1138 List.filter test smart_candidates
1139 else candidates,smart_candidates in
1141 (lazy ("candidates: " ^ string_of_int (List.length candidates)));
1143 (lazy ("smart candidates: " ^
1144 string_of_int (List.length smart_candidates)));
1147 let smart_candidates = [] in *)
1148 let sm = if is_eq then 0 else 2 in
1149 let maxd = ((depth + 1) = flags.maxdepth) in
1150 let only_one = flags.last && maxd in
1151 debug_print (lazy ("only_one: " ^ (string_of_bool only_one)));
1152 debug_print (lazy ("maxd: " ^ (string_of_bool maxd)));
1156 if (only_one && (elems <> [])) then elems
1158 if (maxd && not(is_prod) &
1159 not(is_a_fact_ast status subst metasenv context cand))
1160 then (debug_print (lazy "pruned: not a fact"); elems)
1162 match try_candidate (~smart:sm)
1163 flags depth status cache.unit_eq context cand with
1165 | Some x -> x::elems)
1169 if only_one && elems <> [] then elems
1173 if (only_one && (elems <> [])) then elems
1175 if (maxd && not(is_prod) &&
1176 not(is_a_fact_ast status subst metasenv context cand))
1177 then (debug_print (lazy "pruned: not a fact"); elems)
1179 match try_candidate (~smart:1)
1180 flags depth status cache.unit_eq context cand with
1182 | Some x -> x::elems)
1191 (* gty is supposed to be meta-closed *)
1192 let is_subsumed depth status gty cache =
1193 if cache=[] then false else (
1194 debug_print ~depth (lazy("Subsuming " ^ (ppterm status gty)));
1195 let n,h,metasenv,subst,obj = status#obj in
1196 let ctx = ctx_of gty in
1197 let _ , target = term_of_cic_term status gty ctx in
1198 let target = NCicSubstitution.lift 1 target in
1199 (* candidates must only be searched w.r.t the given context *)
1202 let idx = List.assq ctx cache in
1203 Ncic_termSet.elements
1204 (InvRelDiscriminationTree.retrieve_generalizations idx gty)
1205 with Not_found -> []
1208 (lazy ("failure candidates: " ^ string_of_int (List.length candidates)));
1212 let _ , source = term_of_cic_term status t ctx in
1214 NCic.Prod("foo",source,target) in
1215 let metasenv,j,_,_ =
1216 NCicMetaSubst.mk_meta
1217 metasenv ctx ~with_type:implication `IsType in
1218 let status = status#set_obj (n,h,metasenv,subst,obj) in
1219 let status = status#set_stack [([1,Open j],[],[],`NoTag)] in
1221 let status = NTactics.intro_tac "foo" status in
1223 NTactics.apply_tac ("",0,Ast.NCic (NCic.Rel 1)) status
1225 if (head_goals status#stack = []) then raise Found
1230 with Found -> debug_print ~depth (lazy "success");true)
1233 let rec guess_name name ctx =
1234 if name = "_" then guess_name "auto" ctx else
1235 if not (List.mem_assoc name ctx) then name else
1236 guess_name (name^"'") ctx
1239 let is_prod status =
1240 let _, ctx, gty = current_goal status in
1241 let status, gty = apply_subst status ctx gty in
1242 let _, raw_gty = term_of_cic_term status gty ctx in
1244 | NCic.Prod (name,src,_) ->
1245 let status, src = whd status ~delta:0 ctx (mk_cic_term ctx src) in
1246 (match snd (term_of_cic_term status src ctx) with
1247 | NCic.Const(NReference.Ref (_,NReference.Ind _) as r)
1248 | NCic.Appl (NCic.Const(NReference.Ref (_,NReference.Ind _) as r)::_) ->
1249 let _,_,itys,_,_ = NCicEnvironment.get_checked_indtys r in
1251 (* | [_,_,_,[_;_]] con nat va, ovviamente, in loop *)
1253 | [_,_,_,[]] -> `Inductive (guess_name name ctx)
1254 | _ -> `Some (guess_name name ctx))
1255 | _ -> `Some (guess_name name ctx))
1258 let intro ~depth status facts name =
1259 let status = NTactics.intro_tac name status in
1260 let _, ctx, ngty = current_goal status in
1261 let t = mk_cic_term ctx (NCic.Rel 1) in
1262 let status, keys = keys_of_term status t in
1263 let facts = List.fold_left (add_to_th t) facts keys in
1264 debug_print ~depth (lazy ("intro: "^ name));
1265 (* unprovability is not stable w.r.t introduction *)
1269 let rec intros_facts ~depth status facts =
1270 if List.length (head_goals status#stack) <> 1 then status, facts else
1271 match is_prod status with
1275 intro ~depth status facts name
1276 in intros_facts ~depth status facts
1277 (* | `Inductive name ->
1278 let status = NTactics.case1_tac name status in
1279 intros_facts ~depth status facts *)
1280 | _ -> status, facts
1283 let intros ~depth status cache =
1284 match is_prod status with
1287 let trace = cache.trace in
1289 intros_facts ~depth status cache.facts
1291 if head_goals status#stack = [] then
1292 let status = NTactics.merge_tac status in
1293 [(0,Ast.Ident("__intros",None)),status], cache
1295 (* we reindex the equation from scratch *)
1296 let unit_eq = index_local_equations status#eq_cache status in
1297 let status = NTactics.merge_tac status in
1298 [(0,Ast.Ident("__intros",None)),status],
1299 init_cache ~facts ~unit_eq () ~trace
1303 let reduce ~whd ~depth status g =
1304 let n,h,metasenv,subst,o = status#obj in
1305 let attr, ctx, ty = NCicUtils.lookup_meta g metasenv in
1306 let ty = NCicUntrusted.apply_subst subst ctx ty in
1308 (if whd then NCicReduction.whd else NCicTacReduction.normalize) ~subst ctx ty
1313 (lazy ("reduced to: "^ NCicPp.ppterm ctx subst metasenv ty'));
1315 (g,(attr,ctx,ty'))::(List.filter (fun (i,_) -> i<>g) metasenv)
1317 let status = status#set_obj (n,h,metasenv,subst,o) in
1318 (* we merge to gain a depth level; the previous goal level should
1320 let status = NTactics.merge_tac status in
1322 [(!candidate_no,Ast.Ident("__whd",None)),status])
1325 let do_something signature flags status g depth gty cache =
1326 let l0, cache = intros ~depth status cache in
1327 if l0 <> [] then l0, cache
1330 let l = (*reduce ~whd:true ~depth status g @*) reduce ~whd:true ~depth status g in
1331 (* if l <> [] then l,cache else *)
1332 (* backward aplications *)
1337 ((!candidate_no,Ast.Ident("__paramod",None)),s))
1338 (auto_eq_check cache.unit_eq status)
1341 if ((l1 <> []) && flags.last) then [] else
1342 applicative_case depth signature status flags gty cache
1346 (fun ((_,t),_) -> toref incr_nominations statistics t) l2;
1347 (* states in l1 have have an empty set of subgoals: no point to sort them *)
1349 (lazy ("alternatives = " ^ (string_of_int (List.length (l1@l@l2)))));
1350 (* l1 @ (sort_new_elems (l @ l2)), cache *)
1351 l1 @ (List.rev l2) @ l, cache
1354 let pp_goal = function
1355 | (_,Continuationals.Stack.Open i)
1356 | (_,Continuationals.Stack.Closed i) -> string_of_int i
1359 let pp_goals status l =
1363 let gty = get_goalty status i in
1364 NTacStatus.ppterm status gty)
1371 let compare = Pervasives.compare
1375 module MS = HTopoSort.Make(M)
1378 let sort_tac status =
1380 match status#stack with
1381 | [] -> assert false
1382 | (goals, t, k, tag) :: s ->
1383 let g = head_goals status#stack in
1385 (List.rev (MS.topological_sort g (deps status))) in
1386 debug_print (lazy ("old g = " ^
1387 String.concat "," (List.map string_of_int g)));
1388 debug_print (lazy ("sorted goals = " ^
1389 String.concat "," (List.map string_of_int sortedg)));
1390 let is_it i = function
1391 | (_,Continuationals.Stack.Open j )
1392 | (_,Continuationals.Stack.Closed j ) -> i = j
1395 List.map (fun i -> List.find (is_it i) goals) sortedg
1397 (sorted_goals, t, k, tag) :: s
1399 status#set_stack gstatus
1402 let clean_up_tac status =
1404 match status#stack with
1405 | [] -> assert false
1406 | (g, t, k, tag) :: s ->
1407 let is_open = function
1408 | (_,Continuationals.Stack.Open _) -> true
1409 | (_,Continuationals.Stack.Closed _) -> false
1411 let g' = List.filter is_open g in
1412 (g', t, k, tag) :: s
1414 status#set_stack gstatus
1417 let focus_tac focus status =
1419 match status#stack with
1420 | [] -> assert false
1421 | (g, t, k, tag) :: s ->
1422 let in_focus = function
1423 | (_,Continuationals.Stack.Open i)
1424 | (_,Continuationals.Stack.Closed i) -> List.mem i focus
1426 let focus,others = List.partition in_focus g
1428 (* we need to mark it as a BranchTag, otherwise cannot merge later *)
1429 (focus,[],[],`BranchTag) :: (others, t, k, tag) :: s
1431 status#set_stack gstatus
1434 let deep_focus_tac level focus status =
1435 let in_focus = function
1436 | (_,Continuationals.Stack.Open i)
1437 | (_,Continuationals.Stack.Closed i) -> List.mem i focus
1439 let rec slice level gs =
1440 if level = 0 then [],[],gs else
1442 | [] -> assert false
1443 | (g, t, k, tag) :: s ->
1444 let f,o,gs = slice (level-1) s in
1445 let f1,o1 = List.partition in_focus g
1447 (f1,[],[],`BranchTag)::f, (o1, t, k, tag)::o, gs
1450 let f,o,s = slice level status#stack in f@o@s
1452 status#set_stack gstatus
1455 let rec stack_goals level gs =
1456 if level = 0 then []
1458 | [] -> assert false
1460 let is_open = function
1461 | (_,Continuationals.Stack.Open i) -> Some i
1462 | (_,Continuationals.Stack.Closed _) -> None
1464 HExtlib.filter_map is_open g @ stack_goals (level-1) s
1467 let open_goals level status = stack_goals level status#stack
1470 let move_to_side level status =
1471 match status#stack with
1472 | [] -> assert false
1474 let is_open = function
1475 | (_,Continuationals.Stack.Open i) -> Some i
1476 | (_,Continuationals.Stack.Closed _) -> None
1478 let others = menv_closure status (stack_goals (level-1) tl) in
1479 List.for_all (fun i -> IntSet.mem i others)
1480 (HExtlib.filter_map is_open g)
1482 let rec auto_clusters ?(top=false)
1483 flags signature cache depth status : unit =
1484 debug_print ~depth (lazy ("entering auto clusters at depth " ^
1485 (string_of_int depth)));
1486 debug_print ~depth (pptrace cache.trace);
1487 (* ignore(Unix.select [] [] [] 0.01); *)
1488 let status = clean_up_tac status in
1489 let goals = head_goals status#stack in
1491 if depth = 0 then raise (Proved (status, cache.trace))
1493 let status = NTactics.merge_tac status in
1495 let l,tree = cache.under_inspection in
1497 | [] -> cache (* possible because of intros that cleans the cache *)
1498 | a::tl -> let tree = rm_from_th a tree a in
1499 {cache with under_inspection = tl,tree}
1501 auto_clusters flags signature cache (depth-1) status
1502 else if List.length goals < 2 then
1503 auto_main flags signature cache depth status
1505 let all_goals = open_goals (depth+1) status in
1506 debug_print ~depth (lazy ("goals = " ^
1507 String.concat "," (List.map string_of_int all_goals)));
1508 let classes = HExtlib.clusters (deps status) all_goals in
1511 if List.length gl > flags.maxwidth then
1512 (debug_print ~depth (lazy "FAIL GLOBAL WIDTH");
1513 raise (Gaveup IntSet.empty))
1515 if List.length classes = 1 then
1517 {flags with last = (List.length all_goals = 1)} in
1518 (* no need to cluster *)
1519 auto_main flags signature cache depth status
1521 let classes = if top then List.rev classes else classes in
1527 ("cluster:" ^ String.concat "," (List.map string_of_int l)))
1529 let status,trace,b =
1531 (fun (status,trace,b) gl ->
1532 let cache = {cache with trace = trace} in
1534 {flags with last = (List.length gl = 1)} in
1535 let lold = List.length status#stack in
1536 debug_print ~depth (lazy ("stack length = " ^
1537 (string_of_int lold)));
1538 let fstatus = deep_focus_tac (depth+1) gl status in
1540 debug_print ~depth (lazy ("focusing on" ^
1541 String.concat "," (List.map string_of_int gl)));
1542 auto_main flags signature cache depth fstatus; assert false
1544 | Proved(status,trace) ->
1545 let status = NTactics.merge_tac status in
1546 let lnew = List.length status#stack in
1547 assert (lold = lnew);
1549 | Gaveup _ when top -> (status,trace,b)
1551 (status,cache.trace,false) classes
1553 let rec final_merge n s =
1554 if n = 0 then s else final_merge (n-1) (NTactics.merge_tac s)
1555 in let status = final_merge depth status
1556 in if b then raise (Proved(status,trace)) else raise (Gaveup IntSet.empty)
1560 (* BRAND NEW VERSION *)
1561 auto_main flags signature cache depth status: unit =
1562 debug_print ~depth (lazy "entering auto main");
1563 debug_print ~depth (pptrace cache.trace);
1564 debug_print ~depth (lazy ("stack length = " ^
1565 (string_of_int (List.length status#stack))));
1566 (* ignore(Unix.select [] [] [] 0.01); *)
1567 let status = sort_tac (clean_up_tac status) in
1568 let goals = head_goals status#stack in
1570 | [] when depth = 0 -> raise (Proved (status,cache.trace))
1572 let status = NTactics.merge_tac status in
1574 let l,tree = cache.under_inspection in
1576 | [] -> cache (* possible because of intros that cleans the cache *)
1577 | a::tl -> let tree = rm_from_th a tree a in
1578 {cache with under_inspection = tl,tree}
1580 auto_clusters flags signature cache (depth-1) status
1582 if depth > 0 && move_to_side depth status
1584 let status = NTactics.merge_tac status in
1586 let l,tree = cache.under_inspection in
1588 | [] -> cache (* possible because of intros that cleans the cache*)
1589 | a::tl -> let tree = rm_from_th a tree a in
1590 {cache with under_inspection = tl,tree}
1592 auto_clusters flags signature cache (depth-1) status
1594 let ng = List.length goals in
1595 (* moved inside auto_clusters *)
1596 if ng > flags.maxwidth then
1597 (print ~depth (lazy "FAIL LOCAL WIDTH"); raise (Gaveup IntSet.empty))
1598 else if depth = flags.maxdepth then
1599 raise (Gaveup IntSet.empty)
1601 let status = NTactics.branch_tac ~force:true status in
1602 let g,gctx, gty = current_goal status in
1603 let ctx,ty = close status g in
1604 let closegty = mk_cic_term ctx ty in
1605 let status, gty = apply_subst status gctx gty in
1606 debug_print ~depth (lazy("Attacking goal " ^ (string_of_int g) ^" : "^ppterm status gty));
1607 if is_subsumed depth status closegty (snd cache.under_inspection) then
1608 (debug_print ~depth (lazy "SUBSUMED");
1609 raise (Gaveup IntSet.add g IntSet.empty))
1611 let new_sig = height_of_goal g status in
1612 if new_sig < signature then
1613 (debug_print (lazy ("news = " ^ (string_of_int new_sig)));
1614 debug_print (lazy ("olds = " ^ (string_of_int signature))));
1615 let alternatives, cache =
1616 do_something signature flags status g depth gty cache in
1618 let l,tree = cache.under_inspection in
1619 let l,tree = closegty::l, add_to_th closegty tree closegty in
1620 {cache with under_inspection = l,tree} in
1622 (fun ((_,t),status) ->
1624 (lazy ("(re)considering goal " ^
1625 (string_of_int g) ^" : "^ppterm status gty));
1626 debug_print (~depth:depth)
1627 (lazy ("Case: " ^ CicNotationPp.pp_term t));
1629 if t=Ast.Ident("__whd",None) ||
1630 t=Ast.Ident("__intros",None)
1632 else depth+1,loop_cache in
1633 let cache = add_to_trace ~depth cache t in
1635 auto_clusters flags signature cache depth status
1637 debug_print ~depth (lazy "Failed");
1640 raise (debug_print(lazy "no more candidates"); Gaveup IntSet.empty)
1643 let int name l def =
1644 try int_of_string (List.assoc name l)
1645 with Failure _ | Not_found -> def
1648 module AstSet = Set.Make(struct type t = Ast.term let compare = compare end)
1650 let cleanup_trace s trace =
1651 (* removing duplicates *)
1654 (fun acc t -> AstSet.add t acc)
1655 AstSet.empty trace in
1656 let trace = AstSet.elements trace_set
1657 (* filtering facts *)
1661 | Ast.NRef (NReference.Ref (u,_)) -> not (is_a_fact_obj s u)
1665 let auto_tac ~params:(univ,flags) status =
1666 let oldstatus = status in
1667 let status = (status:> NTacStatus.tac_status) in
1668 let goals = head_goals status#stack in
1669 let status, facts = mk_th_cache status goals in
1670 let unit_eq = index_local_equations status#eq_cache status in
1671 let cache = init_cache ~facts ~unit_eq () in
1672 (* pp_th status facts; *)
1674 NDiscriminationTree.DiscriminationTree.iter status#auto_cache (fun p t ->
1676 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
1677 String.concat "\n " (List.map (
1678 NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[])
1679 (NDiscriminationTree.TermSet.elements t))
1687 let status, res = disambiguate status [] t None in
1688 let _,res = term_of_cic_term status res (ctx_of res)
1690 in Some (List.map to_Ast l)
1692 let depth = int "depth" flags 3 in
1693 let size = int "size" flags 10 in
1694 let width = int "width" flags 4 (* (3+List.length goals)*) in
1696 (* let goals = List.map (fun i -> (i,P)) goals in *)
1697 let signature = height_of_goals status in
1700 candidates = candidates;
1704 timeout = Unix.gettimeofday() +. 3000.;
1707 let initial_time = Unix.gettimeofday() in
1712 ("TIME ELAPSED:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1714 ("Applicative nodes:"^string_of_int !app_counter));
1715 raise (Error (lazy "auto gave up", None)))
1717 let _ = debug_print (lazy("\n\nRound "^string_of_int x^"\n")) in
1718 let flags = { flags with maxdepth = x }
1720 try auto_clusters (~top:true) flags signature cache 0 status;assert false
1722 try auto_main flags signature cache 0 status;assert false
1725 | Gaveup _ -> up_to (x+1) y
1726 | Proved (s,trace) ->
1727 debug_print (lazy ("proved at depth " ^ string_of_int x));
1728 List.iter (toref incr_uses statistics) trace;
1729 let trace = cleanup_trace s trace in
1730 let _ = debug_print (pptrace trace) in
1733 | (g,t,k,f) :: rest -> (filter_open g,t,k,f):: rest
1736 let s = s#set_stack stack in
1737 oldstatus#set_status s
1739 let s = up_to depth depth in
1740 print (print_stat statistics);
1742 ("TIME ELAPSED:"^string_of_float(Unix.gettimeofday()-.initial_time)));
1744 ("Applicative nodes:"^string_of_int !app_counter));