@@ -19,6 +19,7 @@ module Language.PureScript.Sugar.Names (
19import Control.Applicative ((<$>))
19import Control.Applicative ((<$>))
20import Control.Monad (foldM, liftM)
20import Control.Monad (foldM, liftM)
21import Data.Generics (extM, mkM, everywhereM)
21import Data.Generics (extM, mkM, everywhereM)
22+import Data.List (intersect)
22import qualified Data.Map as M
23import qualified Data.Map as M
23import qualified Data.Set as S
24import qualified Data.Set as S
24import Language.PureScript.Declarations
25import Language.PureScript.Declarations
@@ -159,7 +160,7 @@ findExports = foldM addModule (ExportEnvironment M.empty M.empty M.empty)
160-- Type representing a set of declarations being explicitly imported from a module
161-- Type representing a set of declarations being explicitly imported from a module
162−type ExplicitImports = [Either Ident ProperName]
163+type ExplicitImports = [ImportType]
165-- Finds the imports within a module, mapping the imported module name to an optional set of
166-- 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
176-- Constructs a local environment for a module.
177-- Constructs a local environment for a module.
178+-- TODO: check explicit imports actually exist
178resolveImports :: ExportEnvironment -> Module -> Either String ImportEnvironment
180resolveImports :: ExportEnvironment -> Module -> Either String ImportEnvironment
179resolveImports env (Module currentModule decls) = do
181resolveImports env (Module currentModule decls) = do
180− types <- resolve exportedTypes (resolveDefs filterTypes)
182+ types <- resolve exportedTypes (resolveDefs typeImports)
181 dataConstructors <- resolve exportedDataConstructors resolveDcons
183 dataConstructors <- resolve exportedDataConstructors resolveDcons
182− typeClasses <- resolve exportedTypeClasses (resolveDefs filterClasses)
184+ typeClasses <- resolve exportedTypeClasses (resolveDefs classImports)
183 return $ ImportEnvironment types dataConstructors typeClasses
185 return $ ImportEnvironment types dataConstructors typeClasses
185 scope = M.insert currentModule Nothing (findImports decls)
187 scope = M.insert currentModule Nothing (findImports decls)
186 resolve get f = foldM f M.empty (M.toList $ get env)
188 resolve get f = foldM f M.empty (M.toList $ get env)
187 resolveDefs filt result (mn, names) = case M.lookup mn scope of
189 resolveDefs filt result (mn, names) = case M.lookup mn scope of
188 Just Nothing -> foldM (resolveDef mn) result (S.toList names)
190 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)
190 Nothing -> Right result
192 Nothing -> Right result
191 resolveDef mn result name = case M.lookup name result of
193 resolveDef mn result name = case M.lookup name result of
192 Nothing -> return $ M.insert name (Qualified (Just mn) name) result
194 Nothing -> return $ M.insert name (Qualified (Just mn) name) result
@@ -195,10 +197,9 @@ resolveImports env (Module currentModule decls) = do
195 then "' defines '" ++ show name ++ "' which conflicts with imported definition '" ++ show (Qualified (Just mn) name) ++ "'"
197 then "' defines '" ++ show name ++ "' which conflicts with imported definition '" ++ show (Qualified (Just mn) name) ++ "'"
196 else "' has conflicting imports for '" ++ show name ++ "': '" ++ show x ++ "', '" ++ show (Qualified (Just mn) name) ++ "'"
198 else "' has conflicting imports for '" ++ show name ++ "': '" ++ show x ++ "', '" ++ show (Qualified (Just mn) name) ++ "'"
197 resolveDcons result (mn, tcons) = case M.lookup mn scope of
199 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)
200+ Just Nothing -> foldM (foldM $ resolveDef mn) result (snd `map` S.toList tcons)
199− Just (Just expl) ->
201+ Just (Just expl) -> foldM (foldM $ resolveDef mn) result (snd `map` selectedTcons)
200− let selectedTcons = filter (\(tcon, _) -> any (either (const False) (tcon ==)) expl) (S.toList tcons) in
203+ selectedTcons = filter ((`elem` explTypeImports) . fst) (S.toList tcons)
201− foldM (\result (tcon, dcons) -> foldM (resolveDef mn) result dcons) result selectedTcons
204+ explTypeImports = typeImports expl
202 Nothing -> Right result
205 Nothing -> Right result
203− filterTypes result = either (const result) (`S.insert` result)
204− filterClasses result _ = result