+ match left_position with
+ | Some (newt, subst, id2, dir, pos) ->
+ begin
+ match build_clause bag (fun _ -> true) Terms.Demodulation
+ newt subst id id2 pos dir
+ with
+ | None -> assert false
+ | Some x -> Some (x,false)
+ end
+ | None ->
+ match first_position
+ [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
+ (demod table vl)
+ with
+ | None -> None
+ | Some (newt, subst, id2, dir, pos) ->
+ match build_clause bag (fun _ -> true)
+ Terms.Demodulation newt subst id id2 pos dir
+ with
+ | None -> assert false
+ | Some x -> Some (x,true)
+ ;;
+
+ let parallel_demod table vl bag t pos ctx id =
+ match demod table vl t with
+ | None -> (bag,t,id)
+ | Some (newside, subst, id2, dir) ->
+ match build_clause bag (fun _ -> true)
+ Terms.Demodulation (ctx newside) subst id id2 pos dir
+ with
+ | None -> assert false
+ | Some (bag,(id,_,_,_)) ->
+ (bag,newside,id)
+ ;;
+
+ let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
+ match literal with
+ | Terms.Predicate t -> assert false
+ | Terms.Equation (l,r,ty,_) ->
+ let bag,l,id1 = if jump_to_right then (bag,l,id) else
+ parallel_positions bag [2]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
+ (parallel_demod table vl)
+ in
+ let jump_to_right = id1 = id in
+ let bag,r,id2 =
+ parallel_positions bag [3]
+ (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
+ (parallel_demod table vl)
+ in
+ if id = id2 then None
+ else
+ let cl,_,_ = Terms.get_from_bag id2 bag in
+ Some ((bag,cl),jump_to_right)
+ ;;
+
+ let rec demodulate_old ~jump_to_right bag clause table =
+ match demodulate_once ~jump_to_right bag clause table with
+ | None -> bag, clause
+ | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
+ bag clause table
+ ;;
+(*
+ let rec demodulate_old ~jump_to_right bag clause table =
+ match demodulate_once_old ~jump_to_right bag clause table with
+ | None -> bag, clause
+ | Some ((bag, clause),r) -> demodulate_old ~jump_to_right:r
+ bag clause table
+ ;;
+*)
+
+ let are_alpha_eq cl1 cl2 =
+ let get_term (_,lit,_,_) =
+ match lit with
+ | Terms.Predicate _ -> assert false
+ | Terms.Equation (l,r,ty,_) ->
+ Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
+ in
+ try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
+ with FoUnif.UnificationFailure _ -> false
+ ;;
+
+ let demodulate bag clause table =
+ demodulate ~jump_to_right:false bag clause table
+;;
+(*
+ let (bag1,c1), (bag2,c2) =
+ demodulate ~jump_to_right:false bag clause table,
+ demodulate_old ~jump_to_right:false bag clause table
+ in
+ if are_alpha_eq c1 c2 then bag1,c1
+ else
+ begin
+ prerr_endline (Pp.pp_unit_clause clause);
+ prerr_endline (Pp.pp_unit_clause c1);
+ prerr_endline (Pp.pp_unit_clause c2);
+ prerr_endline "Bag1 :";
+ prerr_endline (Pp.pp_bag bag1);
+ prerr_endline "Bag2 :";
+ prerr_endline (Pp.pp_bag bag2);
+ assert false
+ end
+ ;; *)
+
+ let prof_demodulate = HExtlib.profile ~enable "demodulate";;
+ let demodulate bag clause x =
+ prof_demodulate.HExtlib.profile (demodulate bag clause) x
+ ;;
+
+ (* move away *)
+ let is_identity_clause ~unify = function
+ | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
+ | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
+ (try ignore(Unif.unification (* vl *) [] l r); true
+ with FoUnif.UnificationFailure _ -> false)
+ | _, Terms.Equation (_,_,_,_), _, _ -> false
+ | _, Terms.Predicate _, _, _ -> assert false
+ ;;
+
+ let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
+ let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
+ (Subst.apply_subst subst t)) subst in
+ match build_clause bag filter rule t subst id id2 pos dir with
+ | Some (bag, c) -> Some ((bag, maxvar), c)
+ | None -> None
+ ;;
+ let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
+ let build_new_clause bag maxvar filter rule t subst id id2 pos x =
+ prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
+ rule t subst id id2 pos) x
+ ;;
+
+ let fold_build_new_clause bag maxvar id rule filter res =
+ let (bag, maxvar), res =
+ HExtlib.filter_map_acc
+ (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
+ build_new_clause bag maxvar filter rule t subst id id2 pos dir)
+ (bag, maxvar) res
+ in
+ bag, maxvar, res
+ ;;
+
+
+ let rewrite_eq ~unify l r ty vl table =
+ let retrieve = if unify then IDX.DT.retrieve_unifiables
+ else IDX.DT.retrieve_generalizations in
+ let lcands = retrieve table l in
+ let rcands = retrieve table r in
+ let f b c =
+ let id, dir, l, r, vl =
+ match c with
+ | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
+ |_ -> assert false
+ in
+ let reverse = (dir = Terms.Left2Right) = b in
+ let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
+ else r,l, Terms.Right2Left in
+ (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
+ in
+ let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
+ let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
+ let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
+ let locked_vars = if unify then [] else vl in
+ let rec aux = function
+ | [] -> None
+ | (id2,dir,c,vl1)::tl ->
+ try
+ let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
+ Some (id2, dir, subst)
+ with FoUnif.UnificationFailure _ -> aux tl
+ in
+ aux (cands1 @ cands2)
+ ;;
+
+ let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
+ match lit with
+ | Terms.Predicate _ -> assert false
+ | Terms.Equation (l,r,ty,_) ->
+ match rewrite_eq ~unify l r ty vl table with
+ | None -> None
+ | Some (id2, dir, subst) ->
+ let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
+ build_new_clause bag maxvar (fun _ -> true)
+ Terms.Superposition id_t subst id id2 [2] dir
+ ;;
+ let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
+ let is_subsumed ~unify bag maxvar c x =
+ prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
+ ;;
+ (* id refers to a clause proving contextl l = contextr r *)
+
+ let rec deep_eq ~unify l r ty pos contextl contextr table acc =
+ match acc with
+ | None -> None
+ | Some(bag,maxvar,(id,lit,vl,p),subst) ->
+ let l = Subst.apply_subst subst l in
+ let r = Subst.apply_subst subst r in
+ try
+ let subst1 = Unif.unification (* vl *) [] l r in
+ let lit =
+ match lit with Terms.Predicate _ -> assert false
+ | Terms.Equation (l,r,ty,o) ->
+ Terms.Equation (FoSubst.apply_subst subst1 l,
+ FoSubst.apply_subst subst1 r, ty, o)
+ in
+ Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
+ with FoUnif.UnificationFailure _ ->
+ match rewrite_eq ~unify l r ty vl table with
+ | Some (id2, dir, subst1) ->
+ let newsubst = Subst.concat subst1 subst in
+ let id_t =
+ FoSubst.apply_subst newsubst
+ (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
+ in
+ (match
+ build_new_clause bag maxvar (fun _ -> true)
+ Terms.Superposition id_t
+ subst1 id id2 (pos@[2]) dir
+ with
+ | Some ((bag, maxvar), c) ->
+ Some(bag,maxvar,c,newsubst)
+ | None -> assert false)
+ | None ->
+ match l,r with
+ | Terms.Node (a::la), Terms.Node (b::lb) when
+ a = b && List.length la = List.length lb ->
+ let acc,_,_,_ =
+ List.fold_left2
+ (fun (acc,pre,postl,postr) a b ->
+ let newcl =
+ fun x -> contextl(Terms.Node (pre@(x::postl))) in
+ let newcr =
+ fun x -> contextr(Terms.Node (pre@(x::postr))) in
+ let newpos = List.length pre::pos in
+ let footail l =
+ if l = [] then [] else List.tl l in
+ (deep_eq ~unify a b ty
+ newpos newcl newcr table acc,pre@[b],
+ footail postl, footail postr))
+ (acc,[a],List.tl la,List.tl lb) la lb
+ in acc
+ | _,_ -> None
+ ;;
+ let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
+ let deep_eq ~unify l r ty pos contextl contextr table x =
+ prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
+ ;;
+
+ 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