(****************************)
(*  Substitution of redexes *)
(****************************)

(* Similar to lift_rec of Terms *)



Fixpoint lift_rec_r (L : redexes) : nat -> nat -> redexes :=
  fun k n : nat =>
  match L with
  | Var i => Var (relocate i k n)
  | Fun M => Fun (lift_rec_r M (S k) n)
  | Ap b M N => Ap b (lift_rec_r M k n) (lift_rec_r N k n)
  end.Definition lift_r (n : nat) (N : redexes) := lift_rec_r N 0 n.Definition insert_Var (N : redexes) (i k : nat) :=
  match compare k i with
  
      (* k<i *) | inleft (left _) => Var (pred i)
   (* k=i *) | inleft _ => lift_r k N
   (* k>i *) | _ => Var i
  end.(* Similar to subst_rec of Terms *)

Fixpoint subst_rec_r (L : redexes) : redexes -> nat -> redexes :=
  fun (N : redexes) (k : nat) =>
  match L with
  | Var i => insert_Var N i k
  | Fun M => Fun (subst_rec_r M N (S k))
  | Ap b M M' => Ap b (subst_rec_r M N k) (subst_rec_r M' N k)
  end.Definition subst_r (N M : redexes) := subst_rec_r M N 0.

Lemma subst_rec_preserve_comp : forall U1 V1 U2 V2 : redexes, comp U1 V1 -> comp U2 V2 -> forall n : nat, comp (subst_rec_r U1 U2 n) (subst_rec_r V1 V2 n).