(****************************)
(*  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 lift_rec_subst_rec : forall (U V : redexes) (k p n : nat), lift_rec_r (subst_rec_r V U p) (p + n) k = subst_rec_r (lift_rec_r V (S (p + n)) k) (lift_rec_r U n k) p.