expandSplit m (first, rest) result =
    ML.foldrWithKey' expandOne result m
    where
      expandOne :: [String] -> [[String]] -> S.Set [String] -> S.Set [String]
      expandOne k vs a
        | k `isPrefixOf` rest = foldr (S.insert . splice) a vs
        | otherwise = a
        where
          splice :: [String] -> [String]
          splice v = first ++ v ++ drop (length k) rest

-- | Expansions of a given set of molecules.
expandSplit m (first, rest) result =
    ML.foldrWithKey' expandOne result m
    where
      expandOne :: [String] -> [[String]] -> S.Set [String] -> S.Set [String]
      expandOne k vs a
        | k `isPrefixOf` rest = foldr (S.insert . splice) a vs
        | otherwise = a
        where
          splice :: [String] -> [String]
          splice v = first ++ v ++ drop (length k) rest

-- | Expansions of a given set of molecules.
expandSplit m (first, rest) result =
    ML.foldrWithKey' expandOne result m
    where
      expandOne :: [String] -> [[String]] -> S.Set [String] -> S.Set [String]
      expandOne k vs a
        | k `isPrefixOf` rest = foldr (S.insert . splice) a vs
        | otherwise = a
        where
          splice :: [String] -> [String]
          splice v = first ++ v ++ drop (length k) rest

-- | Expansions of a given set of molecules.