let rec term_replace alpha x exp t =
let frec = term_replace alpha x exp in
match t with
| Tconst _ -> t
| Tvar x0 -> if Var.equal x0 x then exp else t
| Tdata _ -> Tlet(x,exp,t)
| Tapp (f, ts) -> e_app f (List.map (frec) ts)
| Tif (a, b, c) -> e_if (frec a) (frec b) (frec c)
| Tlet (x0, a, b) ->
if bad_term_has_var [x0] exp then
match alpha x with
| None -> Tlet(x,exp,t)
| Some y ->
let by = term_replace alpha x0 (Tvar y) b in
Tlet(y,frec a,frec by)
else
let b' = if Var.equal x0 x then b else frec b in
Tlet(x0,frec a,b')