let rec pred_replace alpha x exp p =
let frec = pred_replace alpha x exp in
match p with
| Papp(f,ts) -> p_app f (List.map (term_replace alpha x exp) ts)
| (Ptrue | Pfalse) as p -> p
| Pimplies(p,q) -> p_implies (frec p) (frec q)
| Pif(a,p,q) -> p_if (term_replace alpha x exp a) (frec p) (frec q)
| Pand(p,q) -> p_and (frec p) (frec q)
| Por(p,q) -> p_or (frec p) (frec q)
| Piff(p,q) -> p_iff (frec p) (frec q)
| Pnot(p) -> p_not (frec p)
| Pforall(x0,p) as p0 ->
begin
if Var.equal x x0 then p0
else if e_has_var [x0] exp then
match alpha x with
| None -> Plet(x,exp,p0)
| Some y ->
let py = pred_replace alpha x0 (Tvar y) p in
let py' = frec py in
Pforall( y , py' )
else Pforall(x0, frec p)
end
| Pexists(x0,p) as p0 ->
begin
if Var.equal x x0 then p0
else if e_has_var [x0] exp then
match alpha x with
| None -> Plet(x,exp,p0)
| Some y ->
let py = pred_replace alpha x0 (Tvar y) p in
let py' = frec py in
Pexists( y , py' )
else Pexists(x0,frec p)
end
| Plet(x0,t,p) as p0 ->
begin
if e_has_var [x0] exp then
match alpha x with
| None -> Plet(x,exp,p0)
| Some y ->
let t' = term_replace alpha x exp t in
let py = pred_replace alpha x0 (Tvar y) p in
let py' = frec py in
Plet( y , t' , py' )
else
let t' = term_replace alpha x exp t in
let p' = if Var.equal x0 x then p else frec p in
Plet(x0,t',p')
end
| Pnamed(a,p) -> Pnamed(a,frec p)