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_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
17 let debug_print ?(depth=0) s =
18 if !debug then prerr_endline (String.make depth '\t'^Lazy.force s) else ()
19 let debug_do f = if !debug then f () else ()
21 open Continuationals.Stack
23 module Ast = CicNotationPt
25 (* =================================== paramod =========================== *)
26 let auto_paramod ~params:(l,_) status goal =
28 | None -> raise (Error (lazy "no proof found",None))
30 let gty = get_goalty status goal in
31 let n,h,metasenv,subst,o = status#obj in
32 let status,t = term_of_cic_term status gty (ctx_of gty) in
36 let status, t = disambiguate status (ctx_of gty) t None in
37 let status, ty = typeof status (ctx_of t) t in
38 let status, t = term_of_cic_term status t (ctx_of gty) in
39 let status, ty = term_of_cic_term status ty (ctx_of ty) in
40 (status, (t,ty) :: l))
44 NCicParamod.nparamod status metasenv subst (ctx_of gty) (NCic.Rel ~-1,t) l
46 | [] -> raise (Error (lazy "no proof found",None))
47 | (pt, _, metasenv, subst)::_ ->
48 let status = status#set_obj (n,h,metasenv,subst,o) in
49 instantiate status goal (mk_cic_term (ctx_of gty) pt)
52 let auto_paramod_tac ~params status =
53 NTactics.distribute_tac (auto_paramod ~params) status
56 (* =================================== auto =========================== *)
57 (****************** AUTO ********************
59 let calculate_timeout flags =
60 if flags.timeout = 0. then
61 (debug_print (lazy "AUTO WITH NO TIMEOUT");
62 {flags with timeout = infinity})
66 let is_equational_case goalty flags =
67 let ensure_equational t =
68 if is_an_equational_goal t then true
71 (flags.use_paramod && is_an_equational_goal goalty) ||
72 (flags.use_only_paramod && ensure_equational goalty)
75 type menv = Cic.metasenv
76 type subst = Cic.substitution
77 type goal = ProofEngineTypes.goal * int * AutoTypes.sort
78 let candidate_no = ref 0;;
79 type candidate = int * Cic.term Lazy.t
80 type cache = AutoCache.cache
83 (* the goal (mainly for depth) and key of the goal *)
84 goal * AutoCache.cache_key
86 (* goal has to be proved *)
88 (* goal has to be cached as a success obtained using candidate as the first
90 | S of goal * AutoCache.cache_key * candidate * int
92 (* menv, subst, size, operations done (only S), operations to do, failures to cache if any op fails *)
93 menv * subst * int * op list * op list * fail list
95 (* list of computations that may lead to the solution: all op list will
96 * end with the same (S(g,_)) *)
99 (* menv, subst, alternatives, tables, cache *)
100 | Proved of menv * subst * elem list * AutomationCache.tables * cache
101 | Gaveup of AutomationCache.tables * cache
104 (* the status exported to the external observer *)
106 (* context, (goal,candidate) list, and_list, history *)
107 Cic.context * (int * Cic.term * bool * int * (int * Cic.term Lazy.t) list) list *
108 (int * Cic.term * int) list * Cic.term Lazy.t list
111 let rec aux acc = function
112 | (D g)::tl -> aux (acc@[g]) tl
118 let calculate_goal_ty (goalno,_,_) s m =
120 let _,cc,goalty = CicUtil.lookup_meta goalno m in
121 (* XXX applicare la subst al contesto? *)
122 Some (cc, CicMetaSubst.apply_subst s goalty)
123 with CicUtil.Meta_not_found i when i = goalno -> None
126 let calculate_closed_goal_ty (goalno,_,_) s =
128 let cc,_,goalty = List.assoc goalno s in
129 (* XXX applicare la subst al contesto? *)
130 Some (cc, CicMetaSubst.apply_subst s goalty)
135 let pp_status ctx status =
137 let names = Utils.names_of_context ctx in
140 ProofEngineReduction.replace
141 ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
142 ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
146 let string_of_do m s (gi,_,_ as g) d =
147 match calculate_goal_ty g s m with
148 | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
149 | None -> Printf.sprintf "D(%d, _, %d)" gi d
151 let string_of_s m su k (ci,ct) gi =
152 Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp (Lazy.force ct)) ci
154 let string_of_ol m su l =
158 | D (g,d,s) -> string_of_do m su (g,d,s) d
159 | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
162 let string_of_fl m s fl =
164 (List.map (fun ((i,_,_),ty) ->
165 Printf.sprintf "(%d, %s)" i (pp ty)) fl)
167 let rec aux = function
169 | (m,s,_,_,ol,fl)::tl ->
170 Printf.eprintf "< [%s] ;;; [%s]>\n"
171 (string_of_ol m s ol) (string_of_fl m s fl);
174 Printf.eprintf "-------------------------- status -------------------\n";
176 Printf.eprintf "-----------------------------------------------------\n";
179 let auto_status = ref [] ;;
180 let auto_context = ref [];;
181 let in_pause = ref false;;
182 let pause b = in_pause := b;;
183 let cond = Condition.create ();;
184 let mutex = Mutex.create ();;
185 let hint = ref None;;
186 let prune_hint = ref [];;
188 let step _ = Condition.signal cond;;
189 let give_hint n = hint := Some n;;
190 let give_prune_hint hint =
191 prune_hint := hint :: !prune_hint
198 Condition.wait cond mutex;
203 let get_auto_status _ =
204 let status = !auto_status in
205 let and_list,elems,last =
208 | (m,s,_,don,gl,fail)::tl ->
211 (fun (id,d,_ as g) ->
212 match calculate_goal_ty g s m with
213 | Some (_,x) -> Some (id,x,d) | None -> None)
217 (* these are the S goalsin the or list *)
220 (fun (m,s,_,don,gl,fail) ->
222 (function S (g,k,c,_) -> Some (g,k,c) | _ -> None)
226 (* this function eats id from a list l::[id,x] returning x, l *)
227 let eat_tail_if_eq id l =
228 let rec aux (s, l) = function
230 | ((id1,_,_),k1,c)::tl when id = id1 ->
232 | None -> aux (Some c,l) tl
233 | Some _ -> assert false)
234 | ((id1,_,_),k1,c as e)::tl -> aux (s, e::l) tl
236 let c, l = aux (None, []) l in
239 let eat_in_parallel id l =
240 let rec aux (b,eaten, new_l as acc) l =
244 match eat_tail_if_eq id l with
245 | None, l -> aux (b@[false], eaten, new_l@[l]) tl
246 | Some t,l -> aux (b@[true],eaten@[t], new_l@[l]) tl
250 let rec eat_all rows l =
254 match List.rev elem with
255 | ((to_eat,depth,_),k,_)::next_lunch ->
256 let b, eaten, l = eat_in_parallel to_eat l in
257 let eaten = HExtlib.list_uniq eaten in
258 let eaten = List.rev eaten in
259 let b = true (* List.hd (List.rev b) *) in
260 let rows = rows @ [to_eat,k,b,depth,eaten] in
262 | [] -> eat_all rows or_list
264 eat_all [] (List.rev orlist)
268 (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
271 (* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
272 and_list, rows, history
274 !auto_context, elems, and_list, last
277 (* Works if there is no dependency over proofs *)
278 let is_a_green_cut goalty =
279 CicUtil.is_meta_closed goalty
281 let rec first_s = function
282 | (D _)::tl -> first_s tl
283 | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
286 let list_union l1 l2 =
287 (* TODO ottimizzare compare *)
288 HExtlib.list_uniq (List.sort compare (l1 @ l1))
290 let rec eq_todo l1 l2 =
292 | (D g1) :: tl1,(D g2) :: tl2 when g1=g2 -> eq_todo tl1 tl2
293 | (S (g1,k1,(c1,lt1),i1)) :: tl1, (S (g2,k2,(c2,lt2),i2)) :: tl2
294 when i1 = i2 && g1 = g2 && k1 = k2 && c1 = c2 ->
295 if Lazy.force lt1 = Lazy.force lt2 then eq_todo tl1 tl2 else false
299 let eat_head todo id fl orlist =
300 let rec aux acc = function
302 | (m, s, _, _, todo1, fl1)::tl as orlist ->
304 match first_s todo1 with
305 | None -> orlist, acc
306 | Some (((gno,_,_),_,_,_), todo11) ->
307 (* TODO confronto tra todo da ottimizzare *)
308 if gno = id && eq_todo todo11 todo then
309 aux (list_union fl1 acc) tl
317 let close_proof p ty menv context =
319 List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
321 let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
322 naif_closure p menv context
324 (* XXX capire bene quando aggiungere alla cache *)
325 let add_to_cache_and_del_from_orlist_if_green_cut
326 g s m cache key todo orlist fl ctx size minsize
328 let cache = cache_remove_underinspection cache key in
329 (* prima per fare la irl usavamo il contesto vero e proprio e non quello
331 match calculate_closed_goal_ty g s with
332 | None -> assert false
333 | Some (canonical_ctx , gty) ->
334 let goalno,depth,sort = g in
335 let irl = mk_irl canonical_ctx in
336 let goal = Cic.Meta(goalno, irl) in
337 let proof = CicMetaSubst.apply_subst s goal in
338 let green_proof, closed_proof =
339 let b = is_a_green_cut proof in
341 b, (* close_proof proof gty m ctx *) proof
345 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
346 if is_a_green_cut key then
347 (* if the initia goal was closed, we cut alternatives *)
348 let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
349 let orlist, fl = eat_head todo goalno fl orlist in
351 if size < minsize then
352 (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
354 (* if the proof is closed we cache it *)
355 if green_proof then cache_add_success cache key proof
356 else (* cache_add_success cache key closed_proof *)
357 (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
359 cache, orlist, fl, true
362 debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
363 if size < minsize then
364 (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
365 (* if the substituted goal and the proof are closed we cache it *)
366 if is_a_green_cut gty then
367 if green_proof then cache_add_success cache gty proof
368 else (* cache_add_success cache gty closed_proof *)
369 (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
373 CicTypeChecker.type_of_aux' ~subst:s
374 m ctx closed_proof CicUniv.oblivion_ugraph
376 if is_a_green_cut ty then
377 cache_add_success cache ty closed_proof
380 | CicTypeChecker.TypeCheckerFailure _ ->*)
381 (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
383 cache, orlist, fl, false
385 let close_failures (fl : fail list) (cache : cache) =
387 (fun cache ((gno,depth,_),gty) ->
388 if CicUtil.is_meta_closed gty then
389 ( debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
390 cache_add_failure cache gty depth)
395 let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
396 let entry = goalno, (canonical_ctx, t,ty) in
397 assert_subst_are_disjoint subst [entry];
398 let subst = entry :: subst in
400 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
405 let mk_fake_proof metasenv subst (goalno,_,_) goalty context =
406 None,metasenv,subst ,(lazy (Cic.Meta(goalno,mk_irl context))),goalty, []
410 tables cache depth fake_proof goalno goalty subst context
413 let active,passive,bag = tables in
414 let ppterm = ppterm context in
415 let status = (fake_proof,goalno) in
416 if flags.use_only_paramod then
418 debug_print (lazy ("PARAMODULATION SU: " ^
419 string_of_int goalno ^ " " ^ ppterm goalty ));
420 let goal_steps, saturation_steps, timeout =
421 max_int,max_int,flags.timeout
424 Saturation.given_clause bag status active passive
425 goal_steps saturation_steps timeout
427 | None, active, passive, bag ->
428 [], (active,passive,bag), cache, flags
429 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
431 assert_subst_are_disjoint subst subst';
432 let subst = subst@subst' in
434 order_new_goals metasenv subst open_goals ppterm
437 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
440 [(!candidate_no,proof),metasenv,subst,open_goals],
441 (active,passive,bag), cache, flags
445 debug_print (lazy ("NARROWING DEL GOAL: " ^
446 string_of_int goalno ^ " " ^ ppterm goalty ));
447 let goal_steps, saturation_steps, timeout =
451 Saturation.solve_narrowing bag status active passive goal_steps
453 | None, active, passive, bag ->
454 [], (active,passive,bag), cache, flags
455 | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
457 assert_subst_are_disjoint subst subst';
458 let subst = subst@subst' in
460 order_new_goals metasenv subst open_goals ppterm
463 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
466 [(!candidate_no,proof),metasenv,subst,open_goals],
467 (active,passive,bag), cache, flags
471 let params = ([],["use_context","false"]) in
472 let automation_cache = {
473 AutomationCache.tables = tables ;
474 AutomationCache.univ = Universe.empty; }
477 let ((_,metasenv,subst,_,_,_),open_goals) =
479 solve_rewrite ~params ~automation_cache
482 let proof = lazy (Cic.Meta (-1,[])) in
483 [(!candidate_no,proof),metasenv,subst,[]],tables, cache, flags
484 with ProofEngineTypes.Fail _ -> [], tables, cache, flags
486 let res = Saturation.all_subsumed bag status active passive in
489 (fun (subst',(_,metasenv,_subst,proof,_, _),open_goals) ->
490 assert_subst_are_disjoint subst subst';
491 let subst = subst@subst' in
493 order_new_goals metasenv subst open_goals ppterm
496 List.map (fun (x,sort) -> x,depth-1,sort) open_goals
499 (!candidate_no,proof),metasenv,subst,open_goals)
502 res', (active,passive,bag), cache, flags
509 List.sort (fun (_,_,_,l1) (_,_,_,l2) ->
510 let p1 = List.length (prop_only l1) in
511 let p2 = List.length (prop_only l2) in
512 if p1 = p2 then List.length l1 - List.length l2 else p1-p2)
516 let try_candidate dbd
517 goalty tables subst fake_proof goalno depth context cand
519 let ppterm = ppterm context in
521 let actives, passives, bag = tables in
522 let (_,metasenv,subst,_,_,_), open_goals =
523 ProofEngineTypes.apply_tactic
524 (PrimitiveTactics.apply_tac ~term:cand)
527 let tables = actives, passives,
528 Equality.push_maxmeta bag
529 (max (Equality.maxmeta bag) (CicMkImplicit.new_meta metasenv subst))
531 debug_print (lazy (" OK: " ^ ppterm cand));
532 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
533 let open_goals = order_new_goals metasenv subst open_goals ppterm in
534 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
536 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables
538 | ProofEngineTypes.Fail s -> None,tables
539 | CicUnification.Uncertain s -> None,tables
542 let applicative_case dbd
543 tables depth subst fake_proof goalno goalty metasenv context
544 signature universe cache flags
548 | Cic.Appl (hd::tl) ->
549 Cic.Appl (hd :: HExtlib.mk_list (Cic.Meta (0,[])) (List.length tl))
552 let goalty_aux = goalty in
554 get_candidates flags.skip_trie_filtering universe cache goalty_aux
556 (* if the goal is an equality we skip the congruence theorems
558 if is_equational_case goalty flags
559 then List.filter not_default_eq_term candidates
562 let candidates = List.filter (only signature context metasenv) candidates
566 (fun (tables,elems) cand ->
568 try_candidate dbd goalty
569 tables subst fake_proof goalno depth context cand
571 | None, tables -> tables, elems
572 | Some x, tables -> tables, x::elems)
573 (tables,[]) candidates
575 let elems = sort_new_elems elems in
579 let try_smart_candidate dbd
580 goalty tables subst fake_proof goalno depth context cand
582 let ppterm = ppterm context in
584 let params = ([],[]) in
585 let automation_cache = {
586 AutomationCache.tables = tables ;
587 AutomationCache.univ = Universe.empty; }
589 debug_print (lazy ("candidato per " ^ string_of_int goalno
590 ^ ": " ^ CicPp.ppterm cand));
592 let (_,metasenv,subst,_,_,_) = fake_proof in
593 prerr_endline ("metasenv:\n" ^ CicMetaSubst.ppmetasenv [] metasenv);
594 prerr_endline ("subst:\n" ^ CicMetaSubst.ppsubst ~metasenv subst);
596 let ((_,metasenv,subst,_,_,_),open_goals) =
597 apply_smart ~dbd ~term:cand ~params ~automation_cache
600 let metasenv = CicRefine.pack_coercion_metasenv metasenv in
601 let open_goals = order_new_goals metasenv subst open_goals ppterm in
602 let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
604 Some ((!candidate_no,lazy cand),metasenv,subst,open_goals), tables
606 | ProofEngineTypes.Fail s -> None,tables
607 | CicUnification.Uncertain s -> None,tables
610 let smart_applicative_case dbd
611 tables depth subst fake_proof goalno goalty metasenv context signature
616 | Cic.Appl (hd::tl) ->
617 Cic.Appl (hd :: HExtlib.mk_list (Cic.Meta (0,[])) (List.length tl))
620 let smart_candidates =
621 get_candidates flags.skip_trie_filtering universe cache goalty_aux
624 get_candidates flags.skip_trie_filtering universe cache goalty
626 let smart_candidates =
628 (fun x -> not(List.mem x candidates)) smart_candidates
631 (lazy ("smart_candidates" ^ " = " ^
632 (String.concat "\n" (List.map CicPp.ppterm smart_candidates)))) in
633 debug_print debug_msg;
634 let candidates = List.filter (only signature context metasenv) candidates in
635 let smart_candidates =
636 List.filter (only signature context metasenv) smart_candidates
639 let penalty cand depth =
640 if only signature context metasenv cand then depth else ((prerr_endline (
641 "penalizzo " ^ CicPp.ppterm cand));depth -1)
646 (fun (tables,elems) cand ->
648 try_candidate dbd goalty
649 tables subst fake_proof goalno depth context cand
652 (* if normal application fails we try to be smart *)
653 (match try_smart_candidate dbd goalty
654 tables subst fake_proof goalno depth context cand
656 | None, tables -> tables, elems
657 | Some x, tables -> tables, x::elems)
658 | Some x, tables -> tables, x::elems)
659 (tables,[]) candidates
661 let tables, smart_elems =
663 (fun (tables,elems) cand ->
665 try_smart_candidate dbd goalty
666 tables subst fake_proof goalno depth context cand
668 | None, tables -> tables, elems
669 | Some x, tables -> tables, x::elems)
670 (tables,[]) smart_candidates
672 let elems = sort_new_elems (elems @ smart_elems) in
676 let equational_and_applicative_case dbd
677 signature universe flags m s g gty tables cache context
679 let goalno, depth, sort = g in
680 let fake_proof = mk_fake_proof m s g gty context in
681 if is_equational_case gty flags then
682 let elems,tables,cache, flags =
683 equational_case tables cache
684 depth fake_proof goalno gty s context flags
686 let more_elems, tables, cache =
687 if flags.use_only_paramod then
691 tables depth s fake_proof goalno
692 gty m context signature universe cache flags
694 elems@more_elems, tables, cache, flags
696 let elems, tables, cache =
697 match LibraryObjects.eq_URI () with
699 smart_applicative_case dbd tables depth s fake_proof goalno
700 gty m context signature universe cache flags
702 applicative_case dbd tables depth s fake_proof goalno
703 gty m context signature universe cache flags
705 elems, tables, cache, flags
707 let rec condition_for_hint i = function
709 | S (_,_,(j,_),_):: tl -> j <> i (* && condition_for_hint i tl *)
710 | _::tl -> condition_for_hint i tl
712 let prunable_for_size flags s m todo =
713 let rec aux b = function
714 | (S _)::tl -> aux b tl
715 | (D (_,_,T))::tl -> aux b tl
717 (match calculate_goal_ty g s m with
719 | Some (canonical_ctx, gty) ->
722 ~consider_metas:false ~count_metas_occurrences:true gty in
723 let newb = b || gsize > flags.maxgoalsizefactor in
730 let prunable ty todo =
731 let rec aux b = function
732 | (S(_,k,_,_))::tl -> aux (b || Equality.meta_convertibility k ty) tl
733 | (D (_,_,T))::tl -> aux b tl
741 let prunable menv subst ty todo =
742 let rec aux = function
743 | (S(_,k,_,_))::tl ->
744 (match Equality.meta_convertibility_subst k ty menv with
747 no_progress variant tl (* || aux tl*))
748 | (D (_,_,T))::tl -> aux tl
750 and no_progress variant = function
751 | [] -> (*prerr_endline "++++++++++++++++++++++++ no_progress";*) true
752 | D ((n,_,P) as g)::tl ->
753 (match calculate_goal_ty g subst menv with
754 | None -> no_progress variant tl
756 (match calculate_goal_ty g variant menv with
757 | None -> assert false
759 if gty = gty' then no_progress variant tl
761 (prerr_endline (string_of_int n);
762 prerr_endline (CicPp.ppterm gty);
763 prerr_endline (CicPp.ppterm gty');
764 prerr_endline "---------- subst";
765 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv subst);
766 prerr_endline "---------- variant";
767 prerr_endline (CicMetaSubst.ppsubst ~metasenv:menv variant);
768 prerr_endline "---------- menv";
769 prerr_endline (CicMetaSubst.ppmetasenv [] menv);
770 no_progress variant tl) *)
772 | _::tl -> no_progress variant tl
777 let condition_for_prune_hint prune (m, s, size, don, todo, fl) =
779 HExtlib.filter_map (function S (_,_,(c,_),_) -> Some c | _ -> None) todo
781 List.for_all (fun i -> List.for_all (fun j -> i<>j) prune) s
783 let filter_prune_hint c l =
784 let prune = !prune_hint in
785 prune_hint := []; (* possible race... *)
786 if prune = [] then c,l
788 cache_reset_underinspection c,
789 List.filter (condition_for_prune_hint prune) l
795 auto_all_solutions dbd tables universe cache context metasenv gl flags
800 MetadataConstraints.UriManagerSet.union set
801 (MetadataQuery.signature_of metasenv g)
803 MetadataConstraints.UriManagerSet.empty gl
805 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
808 (fun (x,s) -> D (x,flags.maxdepth,s)) goals
810 let elems = [metasenv,[],1,[],goals,[]] in
811 let rec aux tables solutions cache elems flags =
812 match auto_main dbd tables context flags signature universe cache elems with
813 | Gaveup (tables,cache) ->
814 solutions,cache, tables
815 | Proved (metasenv,subst,others,tables,cache) ->
816 if Unix.gettimeofday () > flags.timeout then
817 ((subst,metasenv)::solutions), cache, tables
819 aux tables ((subst,metasenv)::solutions) cache others flags
821 let rc = aux tables [] cache elems flags in
823 | [],cache,tables -> [],cache,tables
824 | solutions, cache,tables ->
827 (fun (subst,newmetasenv) ->
829 ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
831 if opened = [] then Some subst else None)
834 solutions,cache,tables
837 (******************* AUTO ***************)
840 let auto dbd flags metasenv tables universe cache context metasenv gl =
841 let initial_time = Unix.gettimeofday() in
845 MetadataConstraints.UriManagerSet.union set
846 (MetadataQuery.signature_of metasenv g)
848 MetadataConstraints.UriManagerSet.empty gl
850 let goals = order_new_goals metasenv [] gl CicPp.ppterm in
851 let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
852 let elems = [metasenv,[],1,[],goals,[]] in
853 match auto_main dbd tables context flags signature universe cache elems with
854 | Proved (metasenv,subst,_, tables,cache) ->
856 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
857 Some (subst,metasenv), cache
858 | Gaveup (tables,cache) ->
860 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
864 let auto_tac ~(dbd:HSql.dbd) ~params:(univ,params) ~automation_cache (proof, goal) =
865 let flags = flags_of_params params () in
866 let use_library = flags.use_library in
867 let universe, tables, cache =
868 init_cache_and_tables
869 ~dbd ~use_library ~use_context:(not flags.skip_context)
870 automation_cache univ (proof, goal)
872 let _,metasenv,subst,_,_, _ = proof in
873 let _,context,goalty = CicUtil.lookup_meta goal metasenv in
874 let signature = MetadataQuery.signature_of metasenv goal in
879 CicTypeChecker.type_of_aux' metasenv context t
880 CicUniv.oblivion_ugraph
882 MetadataConstraints.UriManagerSet.union set
883 (MetadataConstraints.constants_of ty)
888 if flags.close_more then
890 tables context (proof, goal)
891 (auto_all_solutions dbd) signature universe cache
893 let initial_time = Unix.gettimeofday() in
894 let (_,oldmetasenv,_,_,_, _) = proof in
897 metasenv,subst,1,[],[D (goal,flags.maxdepth,P)],[]
899 match auto_main dbd tables context flags signature universe cache [elem] with
900 | Proved (metasenv,subst,_, tables,cache) ->
902 ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
904 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
905 proof goal subst metasenv
908 ProofEngineHelpers.compare_metasenvs ~oldmetasenv
909 ~newmetasenv:metasenv
912 | Gaveup (tables,cache) ->
915 string_of_float(Unix.gettimeofday()-.initial_time)));
916 raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
921 type th_cache = (NCic.context * InvRelDiscriminationTree.t) list
923 let keys_of_term status t =
924 let status, orig_ty = typeof status (ctx_of t) t in
925 let _, ty, _ = saturate ~delta:max_int status orig_ty in
928 let _, ty = term_of_cic_term status ty (ctx_of ty) in
930 | NCic.Const (NReference.Ref (_,NReference.Def h))
931 | NCic.Appl (NCic.Const(NReference.Ref(_,NReference.Def h))::_)
933 let _,ty,_= saturate status ~delta:(h-1) orig_ty in
940 let mk_th_cache status gl =
942 (fun (status, acc) g ->
943 let gty = get_goalty status g in
944 let ctx = ctx_of gty in
945 debug_print(lazy("th cache for: "^ppterm status gty));
946 debug_print(lazy("th cache in: "^ppcontext status ctx));
947 if List.mem_assq ctx acc then status, acc else
948 let idx = InvRelDiscriminationTree.empty in
951 (fun (status, i, idx) _ ->
952 let t = mk_cic_term ctx (NCic.Rel i) in
953 debug_print(lazy("indexing: "^ppterm status t));
954 let status, keys = keys_of_term status t in
956 List.fold_left (fun idx k ->
957 InvRelDiscriminationTree.index idx k t) idx keys
962 status, (ctx, idx) :: acc)
966 let add_to_th t c ty =
967 let key_c = ctx_of t in
968 if not (List.mem_assq key_c c) then
969 (key_c ,InvRelDiscriminationTree.index
970 InvRelDiscriminationTree.empty ty t ) :: c
972 let rec replace = function
974 | (x, idx) :: tl when x == key_c ->
975 (x, InvRelDiscriminationTree.index idx ty t) :: tl
976 | x :: tl -> x :: replace tl
981 let pp_idx status idx =
982 InvRelDiscriminationTree.iter idx
984 debug_print(lazy("K: " ^ NCicInverseRelIndexable.string_of_path k));
986 (fun t -> debug_print(lazy("\t"^ppterm status t)))
993 debug_print(lazy( "-----------------------------------------------"));
994 debug_print(lazy( (NCicPp.ppcontext ~metasenv:[] ~subst:[] ctx)));
995 debug_print(lazy( "||====> "));
1000 let search_in_th gty th =
1001 let c = ctx_of gty in
1002 let rec aux acc = function
1003 | [] -> Ncic_termSet.elements acc
1006 let idx = List.assq k th in
1007 let acc = Ncic_termSet.union acc
1008 (InvRelDiscriminationTree.retrieve_unifiables idx gty)
1011 with Not_found -> aux acc tl
1013 aux Ncic_termSet.empty c
1015 type cache_examination_result =
1018 | `Succeded of NCic.term
1023 type goal = int * sort (* goal, depth, sort *)
1024 type fail = goal * cic_term
1025 type candidate = int * Ast.term (* unique candidate number, candidate *)
1028 (* goal has to be proved *)
1030 (* goal has to be cached as a success obtained using candidate as the first
1032 | S of goal * (#tac_status as 'a)
1033 (* * cic_term * candidate (* int was minsize *) *)
1034 | L of goal * (#tac_status as 'a)
1036 let pp_goal (g,_) = string_of_int g
1037 let pp_item = function
1038 | D g -> "D" ^ pp_goal g
1039 | S (g,_) -> "S" ^ pp_goal g
1040 | L (g,_) -> "L" ^ pp_goal g
1043 do_types : bool; (* solve goals in Type *)
1050 type 'a tree_status = #tac_status as 'a * int * int
1051 type 'a tree_item = 'a op
1054 (AndOrTree.andT, 'a tree_status, 'a tree_item) AndOrTree.position
1056 (AndOrTree.orT, 'a tree_status, 'a tree_item) AndOrTree.position
1058 type 'a auto_status = 'a and_pos * th_cache
1060 type 'a auto_result =
1062 | Proved of (#tac_status as 'a) * 'a auto_status option (* alt. proofs *)
1064 let close_failures _ c = c;;
1065 let prunable _ _ _ = false;;
1066 let cache_examine cache gty = `Notfound;;
1067 let put_in_subst s _ _ _ = s;;
1068 let add_to_cache_and_del_from_orlist_if_green_cut _ _ c _ _ o f _ = c, o, f, false ;;
1069 let cache_add_underinspection c _ _ = c;;
1070 let equational_case _ _ _ _ _ _ = [];;
1071 let only _ _ _ = true;;
1073 let candidate_no = ref 0;;
1075 let sort_new_elems l =
1076 List.sort (fun (_,_,_,_,l1) (_,_,_,_,l2) -> List.length l1 - List.length l2) l
1079 let try_candidate flags depth status t g =
1081 debug_print ~depth (lazy ("try " ^ CicNotationPp.pp_term t));
1082 let status = NTactics.focus_tac [g] status in
1083 let status = NTactics.apply_tac ("",0,t) status in
1084 let open_goals = head_goals status#stack in
1086 (lazy ("success: "^String.concat " "(List.map string_of_int open_goals)));
1087 if List.length open_goals > flags.maxwidth ||
1088 (depth = flags.maxdepth && open_goals <> []) then
1089 (debug_print ~depth (lazy "pruned immediately"); None)
1092 Some ((!candidate_no,t),status,open_goals))
1093 with Error (msg,exn) -> debug_print ~depth (lazy "failed"); None
1096 let rec mk_irl n = function
1098 | _ :: tl -> NCic.Rel n :: mk_irl (n+1) tl
1101 let get_candidates status cache_th signature gty =
1102 let universe = status#auto_cache in
1103 let context = ctx_of gty in
1104 let _, raw_gty = term_of_cic_term status gty context in
1106 NDiscriminationTree.DiscriminationTree.retrieve_unifiables universe raw_gty
1109 List.filter (only signature context)
1110 (NDiscriminationTree.TermSet.elements cands)
1113 let _status, t = term_of_cic_term status t context in Ast.NCic t)
1114 (search_in_th gty cache_th)
1116 List.map (function NCic.Const r -> Ast.NRef r | _ -> assert false) cands
1119 let applicative_case depth signature status flags g gty cache =
1120 let candidates = get_candidates status cache signature gty in
1122 (lazy ("candidates: " ^ string_of_int (List.length candidates)));
1126 match try_candidate flags depth status cand g with
1128 | Some x -> x::elems)
1133 let calculate_goal_ty (goalno,_) status =
1134 try Some (get_goalty status goalno)
1135 with Error _ -> None
1138 let equational_and_applicative_case
1139 signature flags status g depth gty cache
1142 if false (*is_equational_case gty flags*) then
1145 signature status flags g gty cache
1148 applicative_case depth
1149 signature status flags g gty cache
1154 (*match LibraryObjects.eq_URI () with
1156 smart_applicative_case dbd tables depth s fake_proof goalno
1157 gty m context signature universe cache flags
1159 applicative_case depth
1160 signature status flags g gty cache
1165 List.map (fun c,s,gl ->
1166 c,1,1,s,List.map (fun i ->
1168 match calculate_goal_ty (i,()) s with
1169 | None -> assert false
1171 let _, sort = typeof s (ctx_of gty) gty in
1172 match term_of_cic_term s sort (ctx_of sort) with
1173 | _, NCic.Sort NCic.Prop -> P
1178 let elems = sort_new_elems elems in
1184 let rec aux acc = function
1185 | (D g)::tl -> aux (acc@[g]) tl
1186 | (S _|L _)::tl -> aux acc tl
1192 List.filter (function (_,P) -> true | _ -> false) l
1195 let rec guess_name name ctx =
1196 if name = "_" then guess_name "auto" ctx else
1197 if not (List.mem_assoc name ctx) then name else
1198 guess_name (name^"'") ctx
1201 let intro_case status gno gty depth cache name =
1202 let status = NTactics.focus_tac [gno] status in
1203 let status = NTactics.intro_tac (guess_name name (ctx_of gty)) status in
1204 let open_goals = head_goals status#stack in
1205 assert (List.length open_goals = 1);
1206 let open_goal = List.hd open_goals in
1207 let ngty = get_goalty status open_goal in
1208 let ctx = ctx_of ngty in
1209 let t = mk_cic_term ctx (NCic.Rel 1) in
1210 let status, keys = keys_of_term status t in
1211 let cache = List.fold_left (add_to_th t) cache keys in
1212 debug_print ~depth (lazy ("intro: "^ string_of_int open_goal));
1214 (* XXX calculate the sort *)
1215 [(!candidate_no,Ast.Implicit `JustOne),0,0,status,[open_goal,P]],
1219 let do_something signature flags s gno depth gty cache =
1220 let _s, raw_gty = term_of_cic_term s gty (ctx_of gty) in
1222 | NCic.Prod (name,_,_) -> intro_case s gno gty depth cache name
1224 equational_and_applicative_case signature flags s gno depth gty cache
1228 module Z = AndOrTree
1230 let img_counter = ref 0 ;;
1233 let file = ("/tmp/a"^string_of_int !img_counter^".dot") in
1234 debug_print (lazy("generating " ^ file));
1236 let oc = open_out file in
1237 let fmt = Format.formatter_of_out_channel oc in
1238 GraphvizPp.Dot.header fmt;
1239 Z.dump pp_item pos fmt;
1240 GraphvizPp.Dot.trailer fmt;
1241 Format.fprintf fmt "@?";
1243 ignore(Sys.command ("dot -Tpng "^file^" > "^file^".png"));
1244 (*ignore(Sys.command ("eog "^file^".png"))*))
1247 let rightmost_bro pred pos =
1248 let rec last acc pos =
1249 let acc = if pred pos then Some pos else acc in
1250 match Z.right pos with
1252 | Some pos -> last acc pos
1257 let leftmost_bro pred pos =
1259 if pred pos then Some pos else
1260 match Z.right pos with
1262 | Some pos -> fst pos
1267 let rec first_left_mark_L_as_D pred pos =
1272 match Z.getA pos with
1274 Z.inject T.Nil (Z.setA s (D g) pos)
1277 match Z.left pos with
1280 first_left_mark_L_as_D pred pos
1284 match Z.getO pos with
1286 | D _ | L _ -> false
1291 match Z.getA pos with
1293 | _,D _ | _,L _ -> false
1296 let is_not_oS x = not (is_oS x);;
1297 let is_not_aS x = not (is_aS x);;
1299 let is_oL pos = match Z.getO pos with L _ -> true | _ -> false ;;
1300 let is_aL pos = match Z.getA pos with _,L _ -> true | _ -> false ;;
1302 let is_not_oL x = not (is_oL x) ;;
1303 let is_not_aL x = not (is_aL x) ;;
1305 let rec forall_left pred pos =
1306 match Z.left pos with
1308 | Some pos -> if pred pos then forall_left pred pos else false
1312 let rec product = function
1314 | ((g,s) :: tl) as l -> (s,List.map fst l) :: product tl
1317 let has_no_alternatives (pos : 'a and_pos) =
1318 match Z.getA pos with
1323 let rec collect_left_up (pos : 'a and_pos) =
1324 match Z.left pos with
1326 (match Z.getA pos with
1327 | _, L (g,s) -> (g,s) :: collect_left_up pos
1330 match Z.upA pos with
1331 | None -> [] (* root *)
1332 | Some pos -> collect_left_up (Z.upO pos)
1335 let compute_failed_goals (pos : 'a and_pos) =
1336 let curr = match Z.getA pos with (s,_,_),D g -> (g,s) | _ -> assert false in
1337 product (List.rev (curr :: collect_left_up pos) )
1341 debug_print (lazy("CACHE FAILURES/UNDERINSPECTION"));
1342 List.iter (fun (s,gl) ->
1343 debug_print (lazy("FAIL: " ^
1344 String.concat " , " (List.map (fun g ->
1345 match calculate_goal_ty g s with
1349 let _,_,_,subst,_ = s#obj in
1350 let _,cc,_,ty = NCicUtils.lookup_subst i subst in
1351 let ty = mk_cic_term cc ty in
1352 string_of_int i^":"^ppterm s ty
1353 with NCicUtils.Subst_not_found _ -> "XXXX")
1355 let s, gty = apply_subst s (ctx_of gty) gty in
1356 string_of_int (fst g)^":"^ppterm s gty) gl))))
1361 match Z.getA pos with
1364 (match calculate_goal_ty g s with
1366 | Some gty -> metas_of_term s gty = [])
1367 | _, L _ -> assert false
1370 let auto_main flags signature (pos : 'a and_pos) cache =
1371 let solved g depth size s (pos : 'a and_pos) =
1372 Z.inject (T.Node(`Or,[D g,T.Node(`And(s,depth,size),[])])) pos
1374 let failed (pos : 'a and_pos) =
1375 pp_failures (compute_failed_goals pos);
1376 Z.inject (T.Node(`Or,[])) pos
1379 let rec next ~unfocus (pos : 'a and_pos) cache =
1380 (* TODO: process USER HINT is any *)
1381 match Z.downA pos with
1382 | Z.Unexplored -> attack pos cache (Z.getA pos)
1383 | Z.Alternatives pos -> nextO ~unfocus pos cache
1385 and nextO ~unfocus (pos : 'a or_pos) cache =
1386 match Z.getO pos with
1387 | S _ | L _ -> assert false (* XXX set to Nil when backtrack *)
1389 match Z.downO pos with
1390 | Z.Solution (s,_,_) -> move_solution_up ~unfocus true s pos cache
1391 | Z.Todo pos -> next ~unfocus:true pos cache
1393 and next_choice_point (pos : 'a and_pos) cache =
1395 let rec global_choice_point (pos : 'a and_pos) : 'a auto_result =
1396 (* prerr_endline "global"; show pos; *)
1397 match Z.upA pos with
1400 let alts = Z.inject T.Nil alts in
1402 match Z.getO alts with
1403 | S (s,g) -> Z.setO (L (s,g)) alts
1404 | D (g) -> Z.setO (L (g,Obj.magic g)) alts
1405 (* L (and other marks) for OR should have no arguments *)
1406 | L _ -> assert false
1408 match Z.right alts with
1410 let upalts = Z.upO alts in
1411 let upalts = Z.inject T.Nil upalts in
1413 match Z.getA upalts with
1414 | s,S (a,b) -> Z.setA s (L (a,b)) upalts
1415 | _,L _ -> assert false
1416 | s,D (a) -> Z.setA s (L (a,Obj.magic a)) upalts
1420 match Z.downO pos with
1421 | Z.Solution (s,_,_) ->
1422 move_solution_up ~unfocus:false true s pos cache
1423 | Z.Todo pos -> next ~unfocus:true pos cache
1425 and backtrack (pos : 'a and_pos) : 'a auto_result =
1426 (* prerr_endline "backtrack"; show pos; *)
1427 let pos = Z.inject T.Nil pos in
1429 match Z.getA pos with
1430 | s,D g | s, S (g,_) | s,L(g,_) -> Z.setA s (D g) pos
1432 match first_left_mark_L_as_D is_aS pos with
1433 | None -> global_choice_point pos
1435 let rec local_choice_point pos =
1436 (* prerr_endline "local"; show pos; *)
1437 match Z.downA pos with
1438 | Z.Unexplored -> attack pos cache (Z.getA pos)
1439 | Z.Alternatives alts ->
1440 match leftmost_bro is_not_oL alts with
1441 | None -> assert false (* is not L, thus has alternatives *)
1443 let is_D = is_not_oS pos in
1444 match if is_D then Z.downO pos else Z.downOr pos with
1445 | Z.Solution (s,_,_) -> assert(is_D);
1446 move_solution_up ~unfocus:false true s pos cache
1447 | Z.Todo pos when is_D -> attack pos cache (Z.getA pos)
1449 match first_left_mark_L_as_D is_aS pos with
1450 | Some pos -> local_choice_point pos
1451 | None -> assert false
1453 local_choice_point pos
1457 and next_choice (pos : 'a and_pos) cache =
1458 next_choice_point pos cache
1460 and move_solution_up
1462 (status : #tac_status as 'a) (pos : 'a or_pos) cache
1464 let pos = (* mark as solved *)
1465 match Z.getO pos with
1466 | L _ -> assert false (* XXX *)
1470 Z.inject T.Nil (Z.setO (L (g,status)) pos)
1472 Z.setO (S (g,status)) pos
1474 let has_alternative_or = match Z.right pos with None -> false | _ -> true in
1475 let pos = Z.upO pos in
1476 let are_all_lbro_L = forall_left is_aL pos in
1477 let has_no_alternative =
1478 ((not has_alternative_or) && are_sons_L) ||
1481 match Z.getA pos with
1482 | _, L _ -> assert false
1483 | (_, size, depth), S (g,_)
1484 (* S if already solved and then solved again because of a backtrack *)
1485 | (_, size, depth), D g ->
1487 if has_no_alternative then (L (g,status)) else (S (g,status))in
1488 (* TODO: cache success g *)
1489 let pos = if has_no_alternative then Z.inject T.Nil pos else pos in
1490 let status = if unfocus then NTactics.unfocus_tac status else status
1492 let news = status,size,depth in
1493 let pos = Z.setA news newg pos in
1494 match Z.right pos with
1495 | Some pos -> next ~unfocus:true pos cache
1497 match Z.upA pos with
1498 | None -> Proved (status, Some (pos,cache))
1501 ~unfocus:true (has_no_alternative && are_all_lbro_L)
1504 and attack pos cache and_item =
1505 (* show pos; uncomment to show the tree *)
1507 | _, S _ -> assert false (* next would close the proof or give a D *)
1508 | _, L _ -> assert false (* L is a final solution *)
1509 | (_, depth, _),_ when Unix.gettimeofday () > flags.timeout ->
1510 debug_print ~depth (lazy ("fail timeout"));
1512 | (s, depth, width), D (_, T as g) when not flags.do_types ->
1513 debug_print ~depth (lazy "skip goal in Type");
1514 next ~unfocus:false (solved g depth width s pos) cache
1515 | (_,depth,_), D _ when depth > flags.maxdepth ->
1516 debug_print ~depth (lazy "fail depth");
1517 next_choice (failed pos) cache
1518 | (_,depth,size), D _ when size > flags.maxsize ->
1519 debug_print ~depth (lazy "fail size");
1520 next_choice (failed pos) cache
1521 | (s,depth,size), D (gno,_ as g) ->
1522 assert (Z.eject pos = T.Nil); (*subtree must be unexplored *)
1523 match calculate_goal_ty g s with
1525 debug_print ~depth (lazy("success side effect: "^string_of_int gno));
1526 next ~unfocus:false (solved g depth size s pos) cache
1528 let s, gty = apply_subst s (ctx_of gty) gty in
1529 debug_print ~depth (lazy ("EXAMINE: "^ ppterm s gty));
1530 match cache_examine cache gty with
1531 | `Failed_in d when d <= depth ->
1532 debug_print ~depth(lazy("fail depth (c): "^string_of_int gno));
1533 next_choice (failed pos) cache
1534 | `UnderInspection ->
1535 debug_print ~depth (lazy("fail loop: "^string_of_int gno));
1536 next_choice (failed pos) cache
1538 debug_print ~depth (lazy("success (c): "^string_of_int gno));
1539 let s = put_in_subst s g t gty in
1540 next ~unfocus:true (solved g depth size s pos) cache
1543 (* more depth than before or first time we see the goal *)
1544 if prunable s gty () then
1545 (debug_print ~depth (lazy( "fail one father is equal"));
1546 next_choice (failed pos) cache)
1548 let cache = cache_add_underinspection cache gty depth in
1549 debug_print ~depth (lazy ("INSPECTING: " ^
1550 string_of_int gno ^ "("^ string_of_int size ^ ") "));
1551 let subgoals, cache =
1552 do_something signature flags s gno depth gty cache
1554 if subgoals = [] then (* this goal has failed *)
1555 next_choice (failed pos) cache
1557 let size_gl l = List.length (prop_only l) in
1560 (fun (_cand,depth_incr,size_mult,s,gl) ->
1563 (s,depth+depth_incr,size+size_mult*(size_gl gl)),
1564 List.map (fun g -> D g,T.Nil) gl))
1568 (Z.inject (T.Node (`Or,subtrees)) pos) cache
1570 (next ~unfocus:true pos cache : 'a auto_result)
1573 let int name l def =
1574 try int_of_string (List.assoc name l)
1575 with Failure _ | Not_found -> def
1578 let auto_tac ~params:(_univ,flags) status =
1579 let goals = head_goals status#stack in
1580 let status, cache = mk_th_cache status goals in
1581 (* pp_th status cache; *)
1583 NDiscriminationTree.DiscriminationTree.iter status#auto_cache (fun p t ->
1585 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
1586 String.concat "\n " (List.map (
1587 NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[])
1588 (NDiscriminationTree.TermSet.elements t))
1591 let depth = int "depth" flags 3 in
1592 let size = int "size" flags 10 in
1593 let width = int "width" flags (3+List.length goals) in
1595 let goals = List.map (fun i -> D(i,P), T.Nil) goals in
1596 let elems = Z.start (T.Node (`And(status,0,0),goals)) in
1597 let signature = () in
1602 timeout = Unix.gettimeofday() +. 3000.;
1606 if x > y then raise (Error (lazy "auto gave up", None))
1608 let _ = debug_print (lazy("\n\nRound "^string_of_int x^"\n")) in
1609 let flags = { flags with maxdepth = x } in
1610 match auto_main flags signature elems cache with
1611 | Gaveup -> up_to (x+1) y
1613 HLog.debug ("proved at depth " ^ string_of_int x);
1616 | (g,t,k,f) :: rest -> (filter_open g,t,k,f):: rest
1624 let rec rm_assoc n = function
1625 | [] -> assert false
1626 | (x,i)::tl when n=x -> i,tl
1627 | p::tl -> let i,tl = rm_assoc n tl in i,p::tl
1630 let merge canonicals elements n m =
1631 let cn,canonicals = rm_assoc n canonicals in
1632 let cm,canonicals = rm_assoc m canonicals in
1633 let ln,elements = rm_assoc cn elements in
1634 let lm,elements = rm_assoc cm elements in
1636 (n,cm)::(m,cm)::List.map
1638 if xc = cn then (x,cm) else p) canonicals
1640 let elements = (cn,ln@lm)::elements
1646 let canonicals = List.map (fun x -> (x,x)) l in
1647 let elements = List.map (fun x -> (x,[x])) l in
1649 (fun (canonicals,elements) x ->
1652 (fun (canonicals,elements) d ->
1653 merge canonicals elements d x)
1654 (canonicals,elements) dep)
1655 (canonicals,elements) l
1658 let group_by_tac ~eq_predicate ~action:tactic status =
1659 let goals = head_goals status#stack in
1660 if List.length goals < 2 then tactic status
1662 let eq_predicate = eq_predicate status in
1663 let rec aux classes = function
1667 let c = List.find (fun c -> eq_predicate c g) classes in
1668 let classes = List.filter ((<>) c) classes in
1669 aux ((g::c) :: classes) tl
1670 with Not_found -> aux ([g] :: classes) tl
1672 let classes = aux [] goals in
1675 HLog.debug ("cluster:" ^ String.concat "," (List.map string_of_int l)))
1678 let l2 = HExtlib.list_mapi (fun x i -> x,i+1) l2 in
1679 List.map (fun x -> List.assoc x l2) l1
1681 NTactics.block_tac ([ NTactics.branch_tac ~force:false]
1683 HExtlib.list_concat ~sep:[NTactics.shift_tac]
1684 (List.map (fun gl-> [NTactics.pos_tac (pos_of gl goals); tactic]) classes)
1686 [ NTactics.merge_tac ]) status
1689 module IntSet = Set.Make(struct type t = int let compare = compare end)
1691 let type_dependency status gl g =
1692 let rec closure acc = function
1694 | x::l when IntSet.mem x acc -> closure acc l
1696 let acc = IntSet.add x acc in
1697 let gty = get_goalty status x in
1698 let deps = metas_of_term status gty in
1699 closure acc (deps @ l)
1701 not (IntSet.is_empty
1703 (closure IntSet.empty gl)
1704 (closure IntSet.empty [g])))
1707 let auto_tac ~params =
1708 group_by_tac ~eq_predicate:type_dependency ~action:(auto_tac ~params)
1711 (* ========================= dispatching of auto/auto_paramod ============ *)
1712 let auto_tac ~params:(_,flags as params) =
1713 if List.mem_assoc "paramodulation" flags then
1714 auto_paramod_tac ~params
1715 else if List.mem_assoc "demod" flags then
1716 NnAuto.demod_tac ~params
1717 else if List.mem_assoc "paramod" flags then
1718 NnAuto.paramod_tac ~params
1719 else if List.mem_assoc "fast_paramod" flags then
1720 NnAuto.fast_eq_check_tac ~params
1721 else if List.mem_assoc "slir" flags then
1722 NnAuto.auto_tac ~params