+(*
+let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
+*)
+
+let calculate_timeout flags =
+ if flags.timeout = 0. then
+ (debug_print (lazy "AUTO WITH NO TIMEOUT");
+ {flags with timeout = infinity})
+ else
+ flags
+;;
+let is_equational_case goalty flags =
+ let ensure_equational t =
+ if is_an_equational_goal t then true
+ else false
+ (*
+ let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
+ raise (ProofEngineTypes.Fail (lazy msg))
+ *)
+ in
+ (flags.use_paramod && is_an_equational_goal goalty) ||
+ (flags.use_only_paramod && ensure_equational goalty)
+;;
+(*
+let cache_add_success sort cache k v =
+ if sort = P then cache_add_success cache k v else cache_remove_underinspection
+ cache k
+;;
+*)
+
+type menv = Cic.metasenv
+type subst = Cic.substitution
+type goal = ProofEngineTypes.goal * int * AutoTypes.sort
+let candidate_no = ref 0;;
+type candidate = int * Cic.term
+type cache = AutoCache.cache
+type tables =
+ Saturation.active_table * Saturation.passive_table * Equality.equality_bag
+
+type fail =
+ (* the goal (mainly for depth) and key of the goal *)
+ goal * AutoCache.cache_key
+type op =
+ (* goal has to be proved *)
+ | D of goal
+ (* goal has to be cached as a success obtained using candidate as the first
+ * step *)
+ | S of goal * AutoCache.cache_key * candidate * int
+type elem =
+ (* menv, subst, size, operations to do, failures to cache if any op fails *)
+ menv * subst * int * op list * fail list
+type status =
+ (* list of computations that may lead to the solution: all op list will
+ * end with the same (S(g,_)) *)
+ elem list
+type auto_result =
+ (* menv, subst, alternatives, tables, cache, maxmeta *)
+ | Proved of menv * subst * elem list * tables * cache * int
+ | Gaveup of tables * cache * int
+
+
+(* the status exported to the external observer *)
+type auto_status =
+ (* context, (goal,candidate) list, and_list, history *)
+ Cic.context * (Cic.term * (int * Cic.term) list) list *
+ Cic.term list * Cic.term list
+
+let d_prefix l =
+ let rec aux acc = function
+ | (D g)::tl -> aux (acc@[g]) tl
+ | _ -> acc
+ in
+ aux [] l
+;;
+let prop_only l =
+ List.filter (function (_,_,P) -> true | _ -> false) l
+;;
+
+let d_goals l =
+ let rec aux acc = function
+ | (D g)::tl -> aux (acc@[g]) tl
+ | (S _)::tl -> aux acc tl
+ | [] -> acc
+ in
+ aux [] l
+;;
+let calculate_goal_ty (goalno,_,_) s m =
+ try
+ let _,cc,goalty = CicUtil.lookup_meta goalno m in
+ (* XXX applicare la subst al contesto? *)
+ Some (cc, CicMetaSubst.apply_subst s goalty)
+ with CicUtil.Meta_not_found i when i = goalno -> None
+;;
+let calculate_closed_goal_ty (goalno,_,_) s =
+ try
+ let cc,_,goalty = List.assoc goalno s in
+ (* XXX applicare la subst al contesto? *)
+ Some (cc, CicMetaSubst.apply_subst s goalty)
+ with Not_found -> None
+;;
+let pp_status ctx status =
+ if debug then
+ let names = Utils.names_of_context ctx in
+ let pp x =
+ let x =
+ ProofEngineReduction.replace
+ ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false)
+ ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
+ in
+ CicPp.pp x names
+ in
+ let string_of_do m s (gi,_,_ as g) d =
+ match calculate_goal_ty g s m with
+ | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
+ | None -> Printf.sprintf "D(%d, _, %d)" gi d
+ in
+ let string_of_s m su k (ci,ct) gi =
+ Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp ct) ci
+ in
+ let string_of_ol m su l =
+ String.concat " | "
+ (List.map
+ (function
+ | D (g,d,s) -> string_of_do m su (g,d,s) d
+ | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi)
+ l)
+ in
+ let string_of_fl m s fl =
+ String.concat " | "
+ (List.map (fun ((i,_,_),ty) ->
+ Printf.sprintf "(%d, %s)" i (pp ty)) fl)
+ in
+ let rec aux = function
+ | [] -> ()
+ | (m,s,ol,fl)::tl ->
+ Printf.eprintf "< [%s] ;;; [%s]>\n"
+ (string_of_ol m s ol) (string_of_fl m s fl);
+ aux tl
+ in
+ Printf.eprintf "-------------------------- status -------------------\n";
+ aux status;
+ Printf.eprintf "-----------------------------------------------------\n";
+;;
+
+let auto_status = ref [] ;;
+let auto_context = ref [];;
+let in_pause = ref false;;
+let pause b = in_pause := b;;
+let cond = Condition.create ();;
+let mutex = Mutex.create ();;
+let hint = ref None;;
+
+let step _ = Condition.signal cond;;
+let give_hint n = hint := Some n;;
+
+let check_pause _ =
+ if !in_pause then
+ begin
+ Mutex.lock mutex;
+ Condition.wait cond mutex;
+ Mutex.unlock mutex
+ end
+;;
+
+let get_auto_status _ =
+ let status = !auto_status in
+(*
+ debug_print "status:";
+ List.iter (fun ((cand,ty),_,_,gl) ->
+ Printf.eprintf "cand: %s; ty: %s; gl: %d\n"
+ (CicPp.ppterm cand) (CicPp.ppterm ty) (List.length gl)) status;
+*)
+ let and_list,elems,last =
+ match status with
+ | [] -> [],[],[]
+ | (m,s,_,gl,fail)::tl ->
+ let and_list =
+ List.map snd
+ (HExtlib.filter_map
+ (fun g -> calculate_goal_ty g s m) (d_goals gl))
+ in
+ let rows =
+ (* these are the S goalsin the or list *)
+ let orlist =
+ List.map
+ (fun (m,s,_,gl,fail) ->
+ HExtlib.filter_map
+ (function S (g,k,c,_) -> Some (g,k,c) | _ -> None) gl)
+ status
+ in
+ (* this function eats id from a list l::[id,x] returning x, l *)
+ let eat_tail_if_eq id l =
+ match (List.rev l) with
+ | ((id1,_,_),k1,c)::tl when id = id1 -> Some c, List.rev tl
+ | _ -> None, l
+ in
+ let eat_in_parallel id l =
+ let rec aux (eaten, new_l as acc) l =
+ match l with
+ | [] -> acc
+ | l::tl ->
+ match eat_tail_if_eq id l with
+ | None, l -> aux (eaten, new_l@[l]) tl
+ | Some t,l -> aux (eaten@[t], new_l@[l]) tl
+ in
+ aux ([],[]) l
+ in
+ let rec eat_all rows l =
+ match l with
+ | [] -> rows
+ | elem::or_list ->
+ match List.rev elem with
+ | ((to_eat,_,_),k,_)::next_lunch ->
+ let eaten, l = eat_in_parallel to_eat l in
+ let eaten = HExtlib.list_uniq eaten in
+ let rows = rows @ [k,eaten] in
+ eat_all rows l
+ | [] -> eat_all rows or_list
+ in
+ eat_all [] orlist
+ in
+ let history =
+ HExtlib.filter_map
+ (function (S (_,_,(_,c),_)) -> Some c | _ -> None)
+ gl
+ in
+(* let rows = List.filter (fun (_,l) -> l <> []) rows in *)
+ and_list, rows, history
+ in
+ !auto_context, elems, and_list, last
+;;
+
+(* Works if there is no dependency over proofs *)
+let is_a_green_cut goalty =
+ CicUtil.is_meta_closed goalty
+;;
+let rec first_s = function
+ | (D _)::tl -> first_s tl
+ | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
+ | [] -> None
+;;
+let list_union l1 l2 =
+ (* TODO ottimizzare compare *)
+ HExtlib.list_uniq (List.sort compare (l1 @ l1))
+;;
+let eat_head todo id fl orlist =
+ let rec aux acc = function
+ | [] -> [], acc
+ | (m, s, _, todo1, fl1)::tl as orlist ->
+ let rec aux1 todo1 =
+ match first_s todo1 with
+ | None -> orlist, acc
+ | Some (((gno,_,_),_,_,_), todo11) ->
+ (* TODO confronto tra todo da ottimizzare *)
+ if gno = id && todo11 = todo then
+ aux (list_union fl1 acc) tl
+ else
+ aux1 todo11
+ in
+ aux1 todo1
+ in
+ aux fl orlist
+;;
+let close_proof p ty menv context =
+ let metas =
+ List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
+ in
+ let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
+ naif_closure p menv context
+;;
+(* XXX capire bene quando aggiungere alla cache *)
+let add_to_cache_and_del_from_orlist_if_green_cut
+ g s m cache key todo orlist fl ctx size minsize
+=
+ let cache = cache_remove_underinspection cache key in
+ (* prima per fare la irl usavamo il contesto vero e proprio e non quello
+ * canonico! XXX *)
+ match calculate_closed_goal_ty g s with
+ | None -> assert false
+ | Some (canonical_ctx , gty) ->
+ let goalno,depth,sort = g in
+ assert (sort = P);
+ let irl = mk_irl canonical_ctx in
+ let goal = Cic.Meta(goalno, irl) in
+ let proof = CicMetaSubst.apply_subst s goal in
+ let green_proof, closed_proof =
+ let b = is_a_green_cut proof in
+ if not b then
+ b, (* close_proof proof gty m ctx *) proof
+ else
+ b, proof
+ in
+ debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
+ if is_a_green_cut key then
+ (* if the initia goal was closed, we cut alternatives *)
+ let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
+ let orlist, fl = eat_head todo goalno fl orlist in
+ let cache =
+ if size < minsize then
+ (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
+ else
+ (* if the proof is closed we cache it *)
+ if green_proof then cache_add_success cache key proof
+ else (* cache_add_success cache key closed_proof *)
+ (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
+ in
+ cache, orlist, fl
+ else
+ let cache =
+ debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
+ if size < minsize then
+ (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
+ (* if the substituted goal and the proof are closed we cache it *)
+ if is_a_green_cut gty then
+ if green_proof then cache_add_success cache gty proof
+ else (* cache_add_success cache gty closed_proof *)
+ (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
+ else (*
+ try
+ let ty, _ =
+ CicTypeChecker.type_of_aux' ~subst:s
+ m ctx closed_proof CicUniv.oblivion_ugraph
+ in
+ if is_a_green_cut ty then
+ cache_add_success cache ty closed_proof
+ else cache
+ with
+ | CicTypeChecker.TypeCheckerFailure _ ->*)
+ (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
+ in
+ cache, orlist, fl
+;;
+let close_failures (fl : fail list) (cache : cache) =
+ List.fold_left
+ (fun cache ((gno,depth,_),gty) ->
+ debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
+ cache_add_failure cache gty depth)
+ cache fl
+;;
+let put_in_subst subst metasenv (goalno,_,_) canonical_ctx t ty =
+ let entry = goalno, (canonical_ctx, t,ty) in
+ assert_subst_are_disjoint subst [entry];
+ let subst = entry :: subst in
+ let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
+ subst, metasenv
+;;
+let mk_fake_proof metasenv (goalno,_,_) goalty context =
+ None,metasenv,Cic.Meta(goalno,mk_irl context),goalty, []
+;;