@@ -19,6 +19,7 @@ module Language.PureScript.Sugar.Names (
1919import Control.Applicative ((<$>))
2020import Control.Monad (foldM, liftM)
2121import Data.Generics (extM, mkM, everywhereM)
22+import Data.List (intersect)
2223import qualified Data.Map as M
2324import qualified Data.Set as S
2425import Language.PureScript.Declarations
@@ -159,7 +160,7 @@ findExports = foldM addModule (ExportEnvironment M.empty M.empty M.empty)
159160-- |
160161-- Type representing a set of declarations being explicitly imported from a module
161162--
162−type ExplicitImports = [Either Ident ProperName]
163+type ExplicitImports = [ImportType]
163164
164165-- |
165166-- Finds the imports within a module, mapping the imported module name to an optional set of
@@ -174,19 +175,20 @@ findImports = foldl findImports' M.empty
174175
175176-- |
176177-- Constructs a local environment for a module.
178+-- TODO: check explicit imports actually exist
177179--
178180resolveImports :: ExportEnvironment -> Module -> Either String ImportEnvironment
179181resolveImports env (Module currentModule decls) = do
180− types <- resolve exportedTypes (resolveDefs filterTypes)
182+ types <- resolve exportedTypes (resolveDefs typeImports)
181183 dataConstructors <- resolve exportedDataConstructors resolveDcons
182− typeClasses <- resolve exportedTypeClasses (resolveDefs filterClasses)
184+ typeClasses <- resolve exportedTypeClasses (resolveDefs classImports)
183185 return $ ImportEnvironment types dataConstructors typeClasses
184186 where
185187 scope = M.insert currentModule Nothing (findImports decls)
186188 resolve get f = foldM f M.empty (M.toList $ get env)
187189 resolveDefs filt result (mn, names) = case M.lookup mn scope of
188190 Just Nothing -> foldM (resolveDef mn) result (S.toList names)
189− Just (Just expl) -> foldM (resolveDef mn) result (S.toList (S.intersection names (foldl filt S.empty expl)))
191+ Just (Just expl) -> foldM (resolveDef mn) result (S.toList names `intersect` filt expl)
190192 Nothing -> Right result
191193 resolveDef mn result name = case M.lookup name result of
192194 Nothing -> return $ M.insert name (Qualified (Just mn) name) result
@@ -195,10 +197,9 @@ resolveImports env (Module currentModule decls) = do
195197 then "' defines '" ++ show name ++ "' which conflicts with imported definition '" ++ show (Qualified (Just mn) name) ++ "'"
196198 else "' has conflicting imports for '" ++ show name ++ "': '" ++ show x ++ "', '" ++ show (Qualified (Just mn) name) ++ "'"
197199 resolveDcons result (mn, tcons) = case M.lookup mn scope of
198− Just Nothing -> foldM (\result (tcon, dcons) -> foldM (resolveDef mn) result dcons) result (S.toList tcons)
199− Just (Just expl) ->
200− let selectedTcons = filter (\(tcon, _) -> any (either (const False) (tcon ==)) expl) (S.toList tcons) in
201− foldM (\result (tcon, dcons) -> foldM (resolveDef mn) result dcons) result selectedTcons
200+ Just Nothing -> foldM (foldM $ resolveDef mn) result (snd `map` S.toList tcons)
201+ Just (Just expl) -> foldM (foldM $ resolveDef mn) result (snd `map` selectedTcons)
202+ where
203+ selectedTcons = filter ((`elem` explTypeImports) . fst) (S.toList tcons)
204+ explTypeImports = typeImports expl
202205 Nothing -> Right result
203− filterTypes result = either (const result) (`S.insert` result)
204− filterClasses result _ = result