-- | Replace the first variable with the given expression. The environment
-- shrinks.
--
inline :: Elt t
       => PreOpenExp acc (env, s) aenv t
       -> PreOpenExp acc env      aenv s
       -> PreOpenExp acc env      aenv t
inline f g = rebuildE (subTop g) f
  where
    subTop :: Elt t => PreOpenExp acc env aenv s -> Idx (env, s) t -> PreOpenExp acc env aenv t
    subTop s ZeroIdx      = s
    subTop _ (SuccIdx ix) = Var ix

-- | Replace an expression that uses the top environment variable with another.
-- The result of the first is let bound into the second.
--
inline :: Elt t
       => PreOpenExp acc (env, s) aenv t
       -> PreOpenExp acc env      aenv s
       -> PreOpenExp acc env      aenv t
inline f g = rebuildE (subTop g) f
  where
    subTop :: Elt t => PreOpenExp acc env aenv s -> Idx (env, s) t -> PreOpenExp acc env aenv t
    subTop s ZeroIdx      = s
    subTop _ (SuccIdx ix) = Var ix

-- | Replace an expression that uses the top environment variable with another.
-- The result of the first is let bound into the second.
--
inline f g = rebuildE (subTop g) f
  where
    subTop :: Elt t => PreOpenExp acc env aenv s -> Idx (env, s) t -> PreOpenExp acc env aenv t
    subTop s ZeroIdx      = s
    subTop _ (SuccIdx ix) = Var ix

-- | Replace an expression that uses the top environment variable with another.
-- The result of the first is let bound into the second.
--