swapAddImport :: E -> (E -> Bool) -> E -> ([E], [E]) -> ([E], [E])
swapAddImport imp cond to (imports, decls) = mapAccumR go imports decls
    where
        go :: [E] -> E -> ([E], E)
        go imps ex
            | cond ex    = (addImport imps, updatePos ex to)
            | L xs <- ex = L <$> mapAccumR go imps xs
            | otherwise  = (imps, ex)

        addImport :: [E] -> [E]
        addImport xs = ordNub $ imp : xs
swapAddImport :: E -> (E -> Bool) -> E -> ([E], [E]) -> ([E], [E])
swapAddImport imp cond to (imports, decls) = mapAccumR go imports decls
    where
        go :: [E] -> E -> ([E], E)
        go imps ex
            | cond ex    = (addImport imps, updatePos ex to)
            | L xs <- ex = L <$> mapAccumR go imps xs
            | otherwise  = (imps, ex)

        addImport :: [E] -> [E]
        addImport xs = ordNub $ imp : xs
swapAddImport imp cond to (imports, decls) = mapAccumR go imports decls
    where
        go :: [E] -> E -> ([E], E)
        go imps ex
            | cond ex    = (addImport imps, updatePos ex to)
            | L xs <- ex = L <$> mapAccumR go imps xs
            | otherwise  = (imps, ex)

        addImport :: [E] -> [E]
        addImport xs = ordNub $ imp : xs