removeAliases :: Rule -> Rule
removeAliases (Rule lhs rhs) = Rule lhs' (substitute subst rhs)
  where (lhs', subst) = collectAliases (renameUnderscores lhs)

        collectAliases t = runWriter (collect t)

        collect :: Term -> Writer Substitution Term
        collect (Appl f ts) = Appl f <$> (mapM collect ts)
        collect (Var x) = return (Var x)
        collect (Alias x t) = do
          t' <- collect t
          tell (M.singleton x t')
          return t'
removeAliases :: Rule -> Rule
removeAliases (Rule lhs rhs) = Rule lhs' (substitute subst rhs)
  where (lhs', subst) = collectAliases (renameUnderscores lhs)

        collectAliases t = runWriter (collect t)

        collect :: Term -> Writer Substitution Term
        collect (Appl f ts) = Appl f <$> (mapM collect ts)
        collect (Var x) = return (Var x)
        collect (Alias x t) = do
          t' <- collect t
          tell (M.singleton x t')
          return t'
removeAliases (Rule lhs rhs) = Rule lhs' (substitute subst rhs)
  where (lhs', subst) = collectAliases (renameUnderscores lhs)

        collectAliases t = runWriter (collect t)

        collect :: Term -> Writer Substitution Term
        collect (Appl f ts) = Appl f <$> (mapM collect ts)
        collect (Var x) = return (Var x)
        collect (Alias x t) = do
          t' <- collect t
          tell (M.singleton x t')
          return t'