+ let rec orphan_murder bag acc i =
+ match Terms.get_from_bag i bag with
+ | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
+ | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
+ | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
+ if (List.mem i acc) then (false,acc)
+ else match orphan_murder bag acc i1 with
+ | (true,acc) -> (true,acc)
+ | (false,acc) ->
+ let (res,acc) = orphan_murder bag acc i2 in
+ if res then res,acc else res,i::acc
+ ;;
+
+ let orphan_murder bag actives cl =
+ let (id,_,_,_,_) = cl in
+ let actives = List.map (fun (i,_,_,_,_) -> i) actives in
+ let (res,_) = orphan_murder bag actives id in
+ if res then debug "Orphan murdered"; res
+ ;;
+ let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
+ let orphan_murder bag actives x =
+ prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
+ ;;
+
+ (* demodulate and check for subsumption *)
+ let simplify table maxvar bag clause =
+ if is_identity_clause ~unify:false clause then bag,None
+ (* else if orphan_murder bag actives clause then bag,None *)
+ else let bag, clause = demodulate bag clause table in
+ if is_identity_clause ~unify:false clause then bag,None
+ else
+ match is_subsumed ~unify:false bag maxvar clause table with
+ | None -> bag, Some clause
+ | Some _ -> bag, None
+ ;;
+
+ let simplify table maxvar bag clause =
+ match simplify table maxvar bag clause with
+ | bag, None ->
+ let (id,_,_,_,_) = clause in
+ let (_,_,iter) = Terms.get_from_bag id bag in
+ Terms.replace_in_bag (clause,true,iter) bag, None
+ | bag, Some clause -> bag, Some clause
+ (*let (id,_,_,_) = clause in
+ if orphan_murder bag clause then
+ Terms.M.add id (clause,true) bag, Some clause
+ else bag, Some clause*)
+ ;;
+ let prof_simplify = HExtlib.profile ~enable "simplify";;
+ let simplify table maxvar bag x =
+ prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
+ ;;
+
+ let one_pass_simplification new_clause (alist,atable) bag maxvar =
+ match simplify atable maxvar bag new_clause with
+ | bag,None -> bag,None (* new_clause has been discarded *)
+ | bag,(Some clause) ->
+ let ctable = IDX.index_clause IDX.DT.empty clause in
+ let bag, alist, atable =
+ List.fold_left
+ (fun (bag, alist, atable) c ->
+ match simplify ctable maxvar bag c with
+ |bag,None -> (bag,alist,atable)
+ (* an active clause as been discarded *)
+ |bag,Some c1 ->
+ bag, c :: alist, IDX.index_clause atable c)
+ (bag,[],IDX.DT.empty) alist
+ in
+ bag, Some (clause, (alist,atable))
+ ;;
+ let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
+ let one_pass_simplification new_clause t bag x =
+ prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
+ ;;
+
+ let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
+ let atable1 =
+ if new_cl then atable else
+ IDX.index_clause atable cl
+ in
+ (* Simplification of new_clause with : *
+ * - actives and cl if new_clause is not cl *
+ * - only actives otherwise *)
+ match
+ simplify atable1 maxvar bag new_clause with
+ | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
+ | bag,Some clause ->
+ (* Simplification of each active clause with clause *
+ * which is the simplified form of new_clause *)
+ let ctable = IDX.index_clause IDX.DT.empty clause in
+ let bag, newa, alist, atable =
+ List.fold_left
+ (fun (bag, newa, alist, atable) c ->
+ match simplify ctable maxvar bag c with
+ |bag,None -> (bag, newa, alist, atable)
+ (* an active clause as been discarded *)
+ |bag,Some c1 ->
+ if (c1 == c) then
+ bag, newa, c :: alist,
+ IDX.index_clause atable c
+ else
+ bag, c1 :: newa, alist, atable)
+ (bag,[],[],IDX.DT.empty) alist
+ in
+ if new_cl then
+ bag, (Some cl, Some (clause, (alist,atable), newa))
+ else
+ (* if new_clause is not cl, we simplify cl with clause *)
+ match simplify ctable maxvar bag cl with
+ | bag,None ->
+ (* cl has been discarded *)
+ bag,(None, Some (clause, (alist,atable), newa))
+ | bag,Some cl1 ->
+ bag,(Some cl1, Some (clause, (alist,atable), newa))
+ ;;
+ let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
+ let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
+ prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
+ ;;
+
+ let keep_simplified cl (alist,atable) bag maxvar =
+ let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
+ if new_cl then
+ match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
+ | _,(None, _) -> assert false
+ | bag,(Some _, None) -> bag,None
+ | bag,(Some _, Some (clause, (alist,atable), newa)) ->
+ keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
+ bag (newa@newc)
+ else
+ match newc with
+ | [] -> bag, Some (cl, (alist,atable))
+ | hd::tl ->
+ match simplification_step ~new_cl cl
+ (alist,atable) bag maxvar hd with
+ | _,(None,None) -> assert false
+ | bag,(Some _,None) ->
+ keep_simplified_aux ~new_cl cl (alist,atable) bag tl
+ | bag,(None, Some _) -> bag,None
+ | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
+ let alist,atable =
+ (clause::alist, IDX.index_clause atable clause)
+ in
+ keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
+ bag (newa@tl)
+ in
+ keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
+ ;;
+ let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
+ let keep_simplified cl t bag x =
+ prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
+ ;;
+
+ (* this is like simplify but raises Success *)
+ let simplify_goal ~no_demod maxvar table bag g_actives clause =
+ let bag, clause =
+ if no_demod then bag, clause else demodulate bag clause table
+ in
+ if List.exists (are_alpha_eq clause) g_actives then None else
+ if (is_identity_clause ~unify:true clause)
+ then raise (Success (bag, maxvar, clause))
+ else
+ let (id,nlit,plit,vl,_) = clause in
+ if vl = [] then Some (bag,clause)
+ else
+ let l,r,ty =
+ match nlit,plit with
+ | [],[Terms.Equation(l,r,ty,_),_] -> l,r,ty
+ | _ -> assert false
+ in
+ match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
+ table (Some(bag,maxvar,clause,Subst.id_subst)) with
+ | None -> Some (bag,clause)
+ | Some (bag,maxvar,cl,subst) ->
+ prerr_endline "Goal subsumed";
+ raise (Success (bag,maxvar,cl))
+(*
+ else match is_subsumed ~unify:true bag maxvar clause table with
+ | None -> Some (bag, clause)
+ | Some ((bag,maxvar),c) ->
+ prerr_endline "Goal subsumed";
+ raise (Success (bag,maxvar,c))
+*)
+ ;;
+
+ let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
+ let simplify_goal ~no_demod maxvar table bag g_actives x =
+ prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
+ ;;
+
+ (* =================== inference ===================== *)
+
+ (* this is OK for both the sup_left and sup_right inference steps *)
+ let superposition table varlist subterm pos context =
+ let cands = IDX.DT.retrieve_unifiables table subterm in
+ HExtlib.filter_map
+ (fun (dir, _, _, (id,nlit,plit,vl,_ (*as uc*))) ->
+ match nlit,plit with
+ | [],[Terms.Equation (l,r,_,o),_] ->
+ (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
+ try
+ let subst =
+ Unif.unification (* (varlist@vl)*) [] subterm side
+ in
+ if o = Terms.Incomparable || o = Terms.Invertible then
+ let side = Subst.apply_subst subst side in
+ let newside = Subst.apply_subst subst newside in
+ let o = Order.compare_terms side newside in
+ (* XXX: check Riazanov p. 33 (iii) *)
+ if o <> Terms.Lt && o <> Terms.Eq then
+ Some (context newside, subst, id, pos, dir)
+ else
+ ((*prerr_endline ("Filtering: " ^
+ Pp.pp_foterm side ^ " =(< || =)" ^
+ Pp.pp_foterm newside);*)None)
+ else
+ Some (context newside, subst, id, pos, dir)
+ with FoUnif.UnificationFailure _ -> None)
+ | _ -> assert false)
+ (IDX.ClauseSet.elements cands)
+ ;;
+
+ (* Superposes selected equation with equalities in table *)
+ let superposition_with_table bag maxvar (id,nlit,plit,vl,_) table =
+ match nlit,plit with
+ | [],[Terms.Equation (l,r,ty,Terms.Lt),_] ->
+ fold_build_new_clause bag maxvar id Terms.Superposition
+ (fun _ -> true)
+ (all_positions [3]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
+ r (superposition table vl))
+ | [],[Terms.Equation (l,r,ty,Terms.Invertible),_]
+ | [],[Terms.Equation (l,r,ty,Terms.Gt),_] ->
+ fold_build_new_clause bag maxvar id Terms.Superposition
+ (fun _ -> true)
+ (all_positions [2]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
+ l (superposition table vl))
+ | [],[Terms.Equation (l,r,ty,Terms.Incomparable),_] ->
+ let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
+ let l = Subst.apply_subst subst l in
+ let r = Subst.apply_subst subst r in
+ let o = Order.compare_terms l r in
+ o <> avoid && o <> Terms.Eq
+ in
+ let bag, maxvar,r_terms =
+ fold_build_new_clause bag maxvar id Terms.Superposition
+ (filtering Terms.Gt)
+ (all_positions [3]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
+ r (superposition table vl))
+ in
+ let bag, maxvar, l_terms =
+ fold_build_new_clause bag maxvar id Terms.Superposition
+ (filtering Terms.Lt)
+ (all_positions [2]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
+ l (superposition table vl))
+ in
+ bag, maxvar, r_terms @ l_terms
+ | _ -> assert false
+ ;;
+
+ (* the current equation is normal w.r.t. demodulation with atable
+ * (and is not the identity) *)
+ let infer_right bag maxvar current (alist,atable) =
+ (* We demodulate actives clause with current until all *
+ * active clauses are reduced w.r.t each other *)
+ (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
+ let ctable = IDX.index_clause IDX.DT.empty current in
+ (* let bag, (alist, atable) =
+ let bag, alist =
+ HExtlib.filter_map_acc (simplify ctable) bag alist
+ in
+ bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
+ in*)
+ debug "Simplified active clauses with fact";
+ (* We superpose active clauses with current *)
+ let bag, maxvar, new_clauses =
+ List.fold_left
+ (fun (bag, maxvar, acc) active ->
+ let bag, maxvar, newc =
+ superposition_with_table bag maxvar active ctable
+ in
+ bag, maxvar, newc @ acc)
+ (bag, maxvar, []) alist
+ in
+ debug "First superpositions";
+ (* We add current to active clauses so that it can be *
+ * superposed with itself *)
+ let alist, atable =
+ current :: alist, IDX.index_clause atable current
+ in
+ debug "Indexed";
+ let fresh_current, maxvar = Utils.fresh_clause maxvar current in
+ (* We need to put fresh_current into the bag so that all *
+ * variables clauses refer to are known. *)
+ let bag, fresh_current = Terms.add_to_bag fresh_current bag in
+ (* We superpose current with active clauses *)
+ let bag, maxvar, additional_new_clauses =
+ superposition_with_table bag maxvar fresh_current atable
+ in
+ debug "Another superposition";
+ let new_clauses = new_clauses @ additional_new_clauses in
+ debug (lazy (Printf.sprintf "Demodulating %d clauses"
+ (List.length new_clauses)));
+ let bag, new_clauses =
+ HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
+ in
+ debug "Demodulated new clauses";
+ bag, maxvar, (alist, atable), new_clauses
+ ;;
+
+ let prof_ir = HExtlib.profile ~enable "infer_right";;
+ let infer_right bag maxvar current t =
+ prof_ir.HExtlib.profile (infer_right bag maxvar current) t
+ ;;
+
+ let infer_left bag maxvar goal (_alist, atable) =
+ (* We superpose the goal with active clauses *)
+ if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
+ else
+ let bag, maxvar, new_goals =
+ superposition_with_table bag maxvar goal atable
+ in
+ debug "Superposed goal with active clauses";
+ (* We simplify the new goals with active clauses *)
+ let bag, new_goals =
+ List.fold_left
+ (fun (bag, acc) g ->
+ match simplify_goal ~no_demod:false maxvar atable bag [] g with
+ | None -> assert false
+ | Some (bag,g) -> bag,g::acc)
+ (bag, []) new_goals
+ in
+ debug "Simplified new goals with active clauses";
+ bag, maxvar, List.rev new_goals
+ ;;
+
+ let prof_il = HExtlib.profile ~enable "infer_left";;
+ let infer_left bag maxvar goal t =
+ prof_il.HExtlib.profile (infer_left bag maxvar goal) t
+ ;;
+
+ end