@@ -12,6 +12,9 @@
13-----------------------------------------------------------------------------
13-----------------------------------------------------------------------------
15+{-# LANGUAGE FlexibleContexts #-}
16+{-# LANGUAGE ScopedTypeVariables #-}
15module Language.PureScript.Sugar.Names (
18module Language.PureScript.Sugar.Names (
16 desugarImports
19 desugarImports
@@ -84,7 +87,7 @@ data ImportEnvironment = ImportEnvironment
84-- Updates the exports for a module from the global environment. If the module was not previously
87-- Updates the exports for a module from the global environment. If the module was not previously
85-- present in the global environment, it is created.
88-- present in the global environment, it is created.
87−updateExportedModule :: ExportEnvironment -> ModuleName -> (Exports -> Either ErrorStack Exports) -> Either ErrorStack ExportEnvironment
90+updateExportedModule :: (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> ModuleName -> (Exports -> m Exports) -> m ExportEnvironment
88updateExportedModule env mn update = do
91updateExportedModule env mn update = do
89 let exports = fromMaybe (error "Module was undefined in updateExportedModule") $ mn `M.lookup` env
92 let exports = fromMaybe (error "Module was undefined in updateExportedModule") $ mn `M.lookup` env
90 exports' <- update exports
93 exports' <- update exports
@@ -93,7 +96,7 @@ updateExportedModule env mn update = do
94-- Adds an empty module to an ExportEnvironment.
97-- Adds an empty module to an ExportEnvironment.
96−addEmptyModule :: ExportEnvironment -> ModuleName -> Either ErrorStack ExportEnvironment
99+addEmptyModule :: (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> ModuleName -> m ExportEnvironment
97addEmptyModule env name =
100addEmptyModule env name =
98 if name `M.member` env
101 if name `M.member` env
99 then throwError $ mkErrorStack ("Module '" ++ show name ++ "' has been defined more than once") Nothing
102 then throwError $ mkErrorStack ("Module '" ++ show name ++ "' has been defined more than once") Nothing
@@ -102,7 +105,7 @@ addEmptyModule env name =
103-- Adds a type belonging to a module to the export environment.
106-- Adds a type belonging to a module to the export environment.
105−addType :: ExportEnvironment -> ModuleName -> ProperName -> [ProperName] -> Either ErrorStack ExportEnvironment
108+addType :: (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> ModuleName -> ProperName -> [ProperName] -> m ExportEnvironment
106addType env mn name dctors = updateExportedModule env mn $ \m -> do
109addType env mn name dctors = updateExportedModule env mn $ \m -> do
107 let exTypes = exportedTypes m
110 let exTypes = exportedTypes m
108 let exDctors = snd `concatMap` exTypes
111 let exDctors = snd `concatMap` exTypes
@@ -117,7 +120,7 @@ addType env mn name dctors = updateExportedModule env mn $ \m -> do
118-- Adds a class to the export environment.
121-- Adds a class to the export environment.
120−addTypeClass :: ExportEnvironment -> ModuleName -> ProperName -> Either ErrorStack ExportEnvironment
123+addTypeClass :: (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> ModuleName -> ProperName -> m ExportEnvironment
121addTypeClass env mn name = updateExportedModule env mn $ \m -> do
124addTypeClass env mn name = updateExportedModule env mn $ \m -> do
122 let exTypes = exportedTypes m
125 let exTypes = exportedTypes m
123 let exDctors = snd `concatMap` exTypes
126 let exDctors = snd `concatMap` exTypes
@@ -129,7 +132,7 @@ addTypeClass env mn name = updateExportedModule env mn $ \m -> do
130-- Adds a class to the export environment.
133-- Adds a class to the export environment.
132−addValue :: ExportEnvironment -> ModuleName -> Ident -> Either ErrorStack ExportEnvironment
135+addValue :: (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> ModuleName -> Ident -> m ExportEnvironment
133addValue env mn name = updateExportedModule env mn $ \m -> do
136addValue env mn name = updateExportedModule env mn $ \m -> do
134 values <- addExport "value" (exportedValues m) name
137 values <- addExport "value" (exportedValues m) name
135 return $ m { exportedValues = values }
138 return $ m { exportedValues = values }
@@ -138,7 +141,7 @@ addValue env mn name = updateExportedModule env mn $ \m -> do
138-- Adds an entry to a list of exports unless it is already present, in which case an error is
141-- Adds an entry to a list of exports unless it is already present, in which case an error is
139-- returned.
142-- returned.
141−addExport :: (Eq a, Show a) => String -> [a] -> a -> Either ErrorStack [a]
144+addExport :: (Applicative m, MonadError ErrorStack m, Eq a, Show a) => String -> [a] -> a -> m [a]
142addExport what exports name =
145addExport what exports name =
143 if name `elem` exports
146 if name `elem` exports
144 then throwMultipleDefError what name
147 then throwMultipleDefError what name
@@ -147,7 +150,7 @@ addExport what exports name =
148-- Replaces all local names with qualified names within a set of modules.
151-- Replaces all local names with qualified names within a set of modules.
150−desugarImports :: [Module] -> Either ErrorStack [Module]
153+desugarImports :: forall m. (Applicative m, MonadError ErrorStack m) => [Module] -> m [Module]
151desugarImports modules = do
154desugarImports modules = do
152 unfilteredExports <- findExports modules
155 unfilteredExports <- findExports modules
153 exports <- foldM filterModuleExports unfilteredExports modules
156 exports <- foldM filterModuleExports unfilteredExports modules
@@ -157,13 +160,13 @@ desugarImports modules = do
157 -- Filters the exports for a module in the global exports environment so that only explicitly
160 -- Filters the exports for a module in the global exports environment so that only explicitly
158 -- exported members remain. If the module does not explicitly export anything, everything is
161 -- exported members remain. If the module does not explicitly export anything, everything is
159 -- exported.
162 -- exported.
160− filterModuleExports :: ExportEnvironment -> Module -> Either ErrorStack ExportEnvironment
163+ filterModuleExports :: ExportEnvironment -> Module -> m ExportEnvironment
161 filterModuleExports env (Module mn _ (Just exps)) = filterExports mn exps env
164 filterModuleExports env (Module mn _ (Just exps)) = filterExports mn exps env
162 filterModuleExports env _ = return env
165 filterModuleExports env _ = return env
164 -- Rename and check all the names within a module. We tweak the global exports environment so
167 -- Rename and check all the names within a module. We tweak the global exports environment so
165 -- the module has access to an unfiltered list of its own members.
168 -- the module has access to an unfiltered list of its own members.
166− renameInModule' :: ExportEnvironment -> ExportEnvironment -> Module -> Either ErrorStack Module
169+ renameInModule' :: ExportEnvironment -> ExportEnvironment -> Module -> m Module
167 renameInModule' unfilteredExports exports m@(Module mn _ _) =
170 renameInModule' unfilteredExports exports m@(Module mn _ _) =
168 rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ do
171 rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ do
169 let env = M.update (\_ -> M.lookup mn unfilteredExports) mn exports
172 let env = M.update (\_ -> M.lookup mn unfilteredExports) mn exports
@@ -202,13 +205,13 @@ elaborateImports (Module mn decls exps) = Module mn decls' exps
202-- Replaces all local names with qualified names within a module and checks that all existing
205-- Replaces all local names with qualified names within a module and checks that all existing
203-- qualified names are valid.
206-- qualified names are valid.
205−renameInModule :: ImportEnvironment -> ExportEnvironment -> Module -> Either ErrorStack Module
208+renameInModule :: forall m. (Applicative m, MonadError ErrorStack m) => ImportEnvironment -> ExportEnvironment -> Module -> m Module
206renameInModule imports exports (Module mn decls exps) =
209renameInModule imports exports (Module mn decls exps) =
207 Module mn <$> parU decls go <*> pure exps
210 Module mn <$> parU decls go <*> pure exps
209 (go, _, _, _, _) = everywhereWithContextOnValuesM (Nothing, []) updateDecl updateValue updateBinder updateCase defS
212 (go, _, _, _, _) = everywhereWithContextOnValuesM (Nothing, []) updateDecl updateValue updateBinder updateCase defS
211− updateDecl :: (Maybe SourceSpan, [Ident]) -> Declaration -> Either ErrorStack ((Maybe SourceSpan, [Ident]), Declaration)
214+ updateDecl :: (Maybe SourceSpan, [Ident]) -> Declaration -> m ((Maybe SourceSpan, [Ident]), Declaration)
212 updateDecl (_, bound) d@(PositionedDeclaration pos _ _) = return ((Just pos, bound), d)
215 updateDecl (_, bound) d@(PositionedDeclaration pos _ _) = return ((Just pos, bound), d)
213 updateDecl (pos, bound) (DataDeclaration dtype name args dctors) =
216 updateDecl (pos, bound) (DataDeclaration dtype name args dctors) =
214 (,) (pos, bound) <$> (DataDeclaration dtype name args <$> mapM (sndM (mapM (updateTypesEverywhere pos))) dctors)
217 (,) (pos, bound) <$> (DataDeclaration dtype name args <$> mapM (sndM (mapM (updateTypesEverywhere pos))) dctors)
@@ -226,7 +229,7 @@ renameInModule imports exports (Module mn decls exps) =
226 (,) (pos, name : bound) <$> (ExternDeclaration fit name js <$> updateTypesEverywhere pos ty)
229 (,) (pos, name : bound) <$> (ExternDeclaration fit name js <$> updateTypesEverywhere pos ty)
227 updateDecl s d = return (s, d)
230 updateDecl s d = return (s, d)
229− updateValue :: (Maybe SourceSpan, [Ident]) -> Expr -> Either ErrorStack ((Maybe SourceSpan, [Ident]), Expr)
232+ updateValue :: (Maybe SourceSpan, [Ident]) -> Expr -> m ((Maybe SourceSpan, [Ident]), Expr)
230 updateValue (_, bound) v@(PositionedValue pos' _ _) = return ((Just pos', bound), v)
233 updateValue (_, bound) v@(PositionedValue pos' _ _) = return ((Just pos', bound), v)
231 updateValue (pos, bound) (Abs (Left arg) val') = return ((pos, arg : bound), Abs (Left arg) val')
234 updateValue (pos, bound) (Abs (Left arg) val') = return ((pos, arg : bound), Abs (Left arg) val')
232 updateValue (pos, bound) (Let ds val') = do
235 updateValue (pos, bound) (Let ds val') = do
@@ -244,12 +247,12 @@ renameInModule imports exports (Module mn decls exps) =
244 updateValue s@(pos, _) (TypedValue check val ty) = (,) s <$> (TypedValue check val <$> updateTypesEverywhere pos ty)
247 updateValue s@(pos, _) (TypedValue check val ty) = (,) s <$> (TypedValue check val <$> updateTypesEverywhere pos ty)
245 updateValue s v = return (s, v)
248 updateValue s v = return (s, v)
247− updateBinder :: (Maybe SourceSpan, [Ident]) -> Binder -> Either ErrorStack ((Maybe SourceSpan, [Ident]), Binder)
250+ updateBinder :: (Maybe SourceSpan, [Ident]) -> Binder -> m ((Maybe SourceSpan, [Ident]), Binder)
248 updateBinder (_, bound) v@(PositionedBinder pos _ _) = return ((Just pos, bound), v)
251 updateBinder (_, bound) v@(PositionedBinder pos _ _) = return ((Just pos, bound), v)
249 updateBinder s@(pos, _) (ConstructorBinder name b) = (,) s <$> (ConstructorBinder <$> updateDataConstructorName name pos <*> pure b)
252 updateBinder s@(pos, _) (ConstructorBinder name b) = (,) s <$> (ConstructorBinder <$> updateDataConstructorName name pos <*> pure b)
250 updateBinder s v = return (s, v)
253 updateBinder s v = return (s, v)
252− updateCase :: (Maybe SourceSpan, [Ident]) -> CaseAlternative -> Either ErrorStack ((Maybe SourceSpan, [Ident]), CaseAlternative)
255+ updateCase :: (Maybe SourceSpan, [Ident]) -> CaseAlternative -> m ((Maybe SourceSpan, [Ident]), CaseAlternative)
253 updateCase (pos, bound) c@(CaseAlternative bs _) = return ((pos, concatMap binderNames bs ++ bound), c)
256 updateCase (pos, bound) c@(CaseAlternative bs _) = return ((pos, concatMap binderNames bs ++ bound), c)
255 letBoundVariable :: Declaration -> Maybe Ident
258 letBoundVariable :: Declaration -> Maybe Ident
@@ -257,10 +260,10 @@ renameInModule imports exports (Module mn decls exps) =
257 letBoundVariable (PositionedDeclaration _ _ d) = letBoundVariable d
260 letBoundVariable (PositionedDeclaration _ _ d) = letBoundVariable d
258 letBoundVariable _ = Nothing
261 letBoundVariable _ = Nothing
260− updateTypesEverywhere :: Maybe SourceSpan -> Type -> Either ErrorStack Type
263+ updateTypesEverywhere :: Maybe SourceSpan -> Type -> m Type
261 updateTypesEverywhere pos0 = everywhereOnTypesM (updateType pos0)
264 updateTypesEverywhere pos0 = everywhereOnTypesM (updateType pos0)
263− updateType :: Maybe SourceSpan -> Type -> Either ErrorStack Type
266+ updateType :: Maybe SourceSpan -> Type -> m Type
264 updateType pos (TypeConstructor name) = TypeConstructor <$> updateTypeName name pos
267 updateType pos (TypeConstructor name) = TypeConstructor <$> updateTypeName name pos
265 updateType pos (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym <$> updateTypeName name pos <*> pure tys
268 updateType pos (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym <$> updateTypeName name pos <*> pure tys
266 updateType pos (ConstrainedType cs t) = ConstrainedType <$> updateConstraints pos cs <*> pure t
269 updateType pos (ConstrainedType cs t) = ConstrainedType <$> updateConstraints pos cs <*> pure t
@@ -280,7 +283,7 @@ renameInModule imports exports (Module mn decls exps) =
280 -> (Exports -> a -> Bool)
283 -> (Exports -> a -> Bool)
281 -> Qualified a
284 -> Qualified a
282 -> Maybe SourceSpan
285 -> Maybe SourceSpan
283− -> Either ErrorStack (Qualified a)
286+ -> m (Qualified a)
284 update t getI checkE qname@(Qualified mn' name) pos = positioned $ case (M.lookup qname imports', mn') of
287 update t getI checkE qname@(Qualified mn' name) pos = positioned $ case (M.lookup qname imports', mn') of
285 (Just qname', _) -> return qname'
288 (Just qname', _) -> return qname'
286 (Nothing, Just mn'') -> do
289 (Nothing, Just mn'') -> do
@@ -299,13 +302,13 @@ renameInModule imports exports (Module mn decls exps) =
299 Just pos' -> rethrowWithPosition pos' err
302 Just pos' -> rethrowWithPosition pos' err
301 -- Gets the exports for a module, or an error message if the module doesn't exist
304 -- Gets the exports for a module, or an error message if the module doesn't exist
302− getExports :: ModuleName -> Either ErrorStack Exports
305+ getExports :: ModuleName -> m Exports
303 getExports mn' = maybe (throwError $ mkErrorStack ("Unknown module '" ++ show mn' ++ "'") Nothing) return $ M.lookup mn' exports
306 getExports mn' = maybe (throwError $ mkErrorStack ("Unknown module '" ++ show mn' ++ "'") Nothing) return $ M.lookup mn' exports
306-- Finds all exported declarations in a set of modules.
309-- Finds all exported declarations in a set of modules.
308−findExports :: [Module] -> Either ErrorStack ExportEnvironment
311+findExports :: forall m. (Applicative m, MonadError ErrorStack m) => [Module] -> m ExportEnvironment
309findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) primExports
312findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) primExports
@@ -315,13 +318,13 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) pri
315 mkTypeEntry (Qualified _ name) = (name, [])
318 mkTypeEntry (Qualified _ name) = (name, [])
317 -- Add all of the exported declarations from a module to the global export environment
320 -- Add all of the exported declarations from a module to the global export environment
318− addModule :: ExportEnvironment -> Module -> Either ErrorStack ExportEnvironment
321+ addModule :: ExportEnvironment -> Module -> m ExportEnvironment
319 addModule env (Module mn ds _) = do
322 addModule env (Module mn ds _) = do
320 env' <- addEmptyModule env mn
323 env' <- addEmptyModule env mn
321 rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ foldM (addDecl mn) env' ds
324 rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ foldM (addDecl mn) env' ds
323 -- Add a declaration from a module to the global export environment
326 -- Add a declaration from a module to the global export environment
324− addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either ErrorStack ExportEnvironment
327+ addDecl :: ModuleName -> ExportEnvironment -> Declaration -> m ExportEnvironment
325 addDecl mn env (TypeClassDeclaration tcn _ _ ds) = do
328 addDecl mn env (TypeClassDeclaration tcn _ _ ds) = do
326 env' <- addTypeClass env mn tcn
329 env' <- addTypeClass env mn tcn
327 foldM go env' ds
330 foldM go env' ds
@@ -341,7 +344,7 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) pri
341-- Filters the exports for a module to ensure only explicit exports are kept in the global exports
344-- Filters the exports for a module to ensure only explicit exports are kept in the global exports
342-- environment.
345-- environment.
344−filterExports :: ModuleName -> [DeclarationRef] -> ExportEnvironment -> Either ErrorStack ExportEnvironment
347+filterExports :: forall m. (Applicative m, MonadError ErrorStack m) => ModuleName -> [DeclarationRef] -> ExportEnvironment -> m ExportEnvironment
345filterExports mn exps env = do
348filterExports mn exps env = do
346 let moduleExports = fromMaybe (error "Module is missing") (mn `M.lookup` env)
349 let moduleExports = fromMaybe (error "Module is missing") (mn `M.lookup` env)
347 moduleExports' <- rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ filterModule moduleExports
350 moduleExports' <- rethrow (mkCompileError ("Error in module " ++ show mn) Nothing `combineErrors`) $ filterModule moduleExports
@@ -349,7 +352,7 @@ filterExports mn exps env = do
351 -- Filter the exports for the specific module
354 -- Filter the exports for the specific module
352− filterModule :: Exports -> Either ErrorStack Exports
355+ filterModule :: Exports -> m Exports
353 filterModule exported = do
356 filterModule exported = do
354 types' <- foldM (filterTypes $ exportedTypes exported) [] exps
357 types' <- foldM (filterTypes $ exportedTypes exported) [] exps
355 values <- foldM (filterValues $ exportedValues exported) [] exps
358 values <- foldM (filterValues $ exportedValues exported) [] exps
@@ -358,7 +361,7 @@ filterExports mn exps env = do
359 -- Ensure the exported types and data constructors exist in the module and add them to the set of
362 -- Ensure the exported types and data constructors exist in the module and add them to the set of
360 -- exports
363 -- exports
361− filterTypes :: [(ProperName, [ProperName])] -> [(ProperName, [ProperName])] -> DeclarationRef -> Either ErrorStack [(ProperName, [ProperName])]
364+ filterTypes :: [(ProperName, [ProperName])] -> [(ProperName, [ProperName])] -> DeclarationRef -> m [(ProperName, [ProperName])]
362 filterTypes expTys result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterTypes expTys result r
365 filterTypes expTys result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterTypes expTys result r
363 filterTypes expTys result (TypeRef name expDcons) = do
366 filterTypes expTys result (TypeRef name expDcons) = do
364 dcons <- maybe (throwError $ mkErrorStack ("Cannot export undefined type '" ++ show name ++ "'") Nothing) return $ name `lookup` expTys
367 dcons <- maybe (throwError $ mkErrorStack ("Cannot export undefined type '" ++ show name ++ "'") Nothing) return $ name `lookup` expTys
@@ -367,14 +370,14 @@ filterExports mn exps env = do
367 filterTypes _ result _ = return result
370 filterTypes _ result _ = return result
369 -- Ensure the exported data constructors exists for a type and add them to the list of exports
372 -- Ensure the exported data constructors exists for a type and add them to the list of exports
370− filterDcons :: ProperName -> [ProperName] -> [ProperName] -> ProperName -> Either ErrorStack [ProperName]
373+ filterDcons :: ProperName -> [ProperName] -> [ProperName] -> ProperName -> m [ProperName]
371 filterDcons tcon exps' result name =
374 filterDcons tcon exps' result name =
372 if name `elem` exps'
375 if name `elem` exps'
373 then return $ name : result
376 then return $ name : result
374 else throwError $ mkErrorStack ("Cannot export undefined data constructor '" ++ show name ++ "' for type '" ++ show tcon ++ "'") Nothing
377 else throwError $ mkErrorStack ("Cannot export undefined data constructor '" ++ show name ++ "' for type '" ++ show tcon ++ "'") Nothing
376 -- Ensure the exported classes exist in the module and add them to the set of exports
379 -- Ensure the exported classes exist in the module and add them to the set of exports
377− filterClasses :: [ProperName] -> [ProperName] -> DeclarationRef -> Either ErrorStack [ProperName]
380+ filterClasses :: [ProperName] -> [ProperName] -> DeclarationRef -> m [ProperName]
378 filterClasses exps' result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterClasses exps' result r
381 filterClasses exps' result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterClasses exps' result r
379 filterClasses exps' result (TypeClassRef name) =
382 filterClasses exps' result (TypeClassRef name) =
380 if name `elem` exps'
383 if name `elem` exps'
@@ -383,7 +386,7 @@ filterExports mn exps env = do
383 filterClasses _ result _ = return result
386 filterClasses _ result _ = return result
385 -- Ensure the exported values exist in the module and add them to the set of exports
388 -- Ensure the exported values exist in the module and add them to the set of exports
386− filterValues :: [Ident] -> [Ident] -> DeclarationRef -> Either ErrorStack [Ident]
389+ filterValues :: [Ident] -> [Ident] -> DeclarationRef -> m [Ident]
387 filterValues exps' result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterValues exps' result r
390 filterValues exps' result (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ filterValues exps' result r
388 filterValues exps' result (ValueRef name) =
391 filterValues exps' result (ValueRef name) =
389 if name `elem` exps'
392 if name `elem` exps'
@@ -405,7 +408,7 @@ findImports = foldl (findImports' Nothing) M.empty
406-- Constructs a local environment for a module.
409-- Constructs a local environment for a module.
408−resolveImports :: ExportEnvironment -> Module -> Either ErrorStack ImportEnvironment
411+resolveImports :: forall m. (Applicative m, MonadError ErrorStack m) => ExportEnvironment -> Module -> m ImportEnvironment
409resolveImports env (Module currentModule decls _) =
412resolveImports env (Module currentModule decls _) =
410 foldM resolveImport' (ImportEnvironment M.empty M.empty M.empty M.empty) (M.toList scope)
413 foldM resolveImport' (ImportEnvironment M.empty M.empty M.empty M.empty) (M.toList scope)
@@ -416,7 +419,7 @@ resolveImports env (Module currentModule decls _) =
416 scope :: M.Map ModuleName (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)
419 scope :: M.Map ModuleName (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)
417 scope = M.insert currentModule (Nothing, Unqualified, Nothing) (findImports decls)
420 scope = M.insert currentModule (Nothing, Unqualified, Nothing) (findImports decls)
419− resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
422+ resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)) -> m ImportEnvironment
420 resolveImport' imp (mn, (pos, typ, impQual)) = do
423 resolveImport' imp (mn, (pos, typ, impQual)) = do
421 modExports <- positioned $ maybe (throwError $ mkErrorStack ("Cannot import unknown module '" ++ show mn ++ "'") Nothing) return $ mn `M.lookup` env
424 modExports <- positioned $ maybe (throwError $ mkErrorStack ("Cannot import unknown module '" ++ show mn ++ "'") Nothing) return $ mn `M.lookup` env
422 positioned $ resolveImport currentModule mn modExports imp impQual typ
425 positioned $ resolveImport currentModule mn modExports imp impQual typ
@@ -428,19 +431,19 @@ resolveImports env (Module currentModule decls _) =
429-- Extends the local environment for a module by resolving an import of another module.
432-- Extends the local environment for a module by resolving an import of another module.
431−resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> ImportDeclarationType -> Either ErrorStack ImportEnvironment
434+resolveImport :: forall m. (Applicative m, MonadError ErrorStack m) => ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> ImportDeclarationType -> m ImportEnvironment
432resolveImport currentModule importModule exps imps impQual =
435resolveImport currentModule importModule exps imps impQual =
433 resolveByType
436 resolveByType
436− resolveByType :: ImportDeclarationType -> Either ErrorStack ImportEnvironment
439+ resolveByType :: ImportDeclarationType -> m ImportEnvironment
437 resolveByType Unqualified = importAll importExplicit
440 resolveByType Unqualified = importAll importExplicit
438 resolveByType (Qualifying explImports) = (checkedRefs >=> foldM importExplicit imps) explImports
441 resolveByType (Qualifying explImports) = (checkedRefs >=> foldM importExplicit imps) explImports
439 resolveByType (Hiding hiddenImports) = do
442 resolveByType (Hiding hiddenImports) = do
440 hiddenImports' <- checkedRefs hiddenImports
443 hiddenImports' <- checkedRefs hiddenImports
441 importAll (importNonHidden hiddenImports')
444 importAll (importNonHidden hiddenImports')
443− importNonHidden :: [DeclarationRef] -> ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
446+ importNonHidden :: [DeclarationRef] -> ImportEnvironment -> DeclarationRef -> m ImportEnvironment
444 importNonHidden hidden m ref =
447 importNonHidden hidden m ref =
445 if isHidden hidden ref
448 if isHidden hidden ref
446 then return m
449 then return m
@@ -459,14 +462,14 @@ resolveImport currentModule importModule exps imps impQual =
459 isHidden hidden ref = ref `elem` hidden
462 isHidden hidden ref = ref `elem` hidden
461 -- Import all symbols
464 -- Import all symbols
462− importAll :: (ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment) -> Either ErrorStack ImportEnvironment
465+ importAll :: (ImportEnvironment -> DeclarationRef -> m ImportEnvironment) -> m ImportEnvironment
463 importAll importer = do
466 importAll importer = do
464 imp' <- foldM (\m (name, dctors) -> importer m (TypeRef name (Just dctors))) imps (exportedTypes exps)
467 imp' <- foldM (\m (name, dctors) -> importer m (TypeRef name (Just dctors))) imps (exportedTypes exps)
465 imp'' <- foldM (\m name -> importer m (ValueRef name)) imp' (exportedValues exps)
468 imp'' <- foldM (\m name -> importer m (ValueRef name)) imp' (exportedValues exps)
466 foldM (\m name -> importer m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
469 foldM (\m name -> importer m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
468 -- Import something explicitly
471 -- Import something explicitly
469− importExplicit :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
472+ importExplicit :: ImportEnvironment -> DeclarationRef -> m ImportEnvironment
470 importExplicit imp (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ importExplicit imp r
473 importExplicit imp (PositionedDeclarationRef pos _ r) = rethrowWithPosition pos $ importExplicit imp r
471 importExplicit imp (ValueRef name) = do
474 importExplicit imp (ValueRef name) = do
472 values' <- updateImports (importedValues imp) name
475 values' <- updateImports (importedValues imp) name
@@ -483,7 +486,7 @@ resolveImport currentModule importModule exps imps impQual =
483 importExplicit _ _ = error "Invalid argument to importExplicit"
486 importExplicit _ _ = error "Invalid argument to importExplicit"
485 -- Check if DeclarationRef points to an existent symbol
488 -- Check if DeclarationRef points to an existent symbol
486− checkedRefs :: [DeclarationRef] -> Either ErrorStack [DeclarationRef]
489+ checkedRefs :: [DeclarationRef] -> m [DeclarationRef]
487 checkedRefs = mapM check
490 checkedRefs = mapM check
489 check (PositionedDeclarationRef pos _ r) =
492 check (PositionedDeclarationRef pos _ r) =
@@ -504,7 +507,7 @@ resolveImport currentModule importModule exps imps impQual =
504 allExportedDataConstructors name = fromMaybe [] $ name `lookup` exportedTypes exps
507 allExportedDataConstructors name = fromMaybe [] $ name `lookup` exportedTypes exps
506 -- Add something to the ImportEnvironment if it does not already exist there
509 -- Add something to the ImportEnvironment if it does not already exist there
507− updateImports :: (Ord a, Show a) => M.Map (Qualified a) (Qualified a) -> a -> Either ErrorStack (M.Map (Qualified a) (Qualified a))
510+ updateImports :: (Ord a, Show a) => M.Map (Qualified a) (Qualified a) -> a -> m (M.Map (Qualified a) (Qualified a))
508 updateImports m name = case M.lookup (Qualified impQual name) m of
511 updateImports m name = case M.lookup (Qualified impQual name) m of
509 Nothing -> return $ M.insert (Qualified impQual name) (Qualified (Just importModule) name) m
512 Nothing -> return $ M.insert (Qualified impQual name) (Qualified (Just importModule) name) m
510 Just (Qualified Nothing _) -> error "Invalid state in updateImports"
513 Just (Qualified Nothing _) -> error "Invalid state in updateImports"
@@ -521,11 +524,11 @@ resolveImport currentModule importModule exps imps impQual =
522 -- Ensure that an explicitly imported data constructor exists for the type it is being imported
525 -- Ensure that an explicitly imported data constructor exists for the type it is being imported
524− checkDctorExists :: [ProperName] -> ProperName -> Either ErrorStack ProperName
527+ checkDctorExists :: [ProperName] -> ProperName -> m ProperName
525 checkDctorExists = checkImportExists "data constructor"
528 checkDctorExists = checkImportExists "data constructor"
527 -- Check that an explicitly imported item exists in the module it is being imported from
530 -- Check that an explicitly imported item exists in the module it is being imported from
528− checkImportExists :: (Eq a, Show a) => String -> [a] -> a -> Either ErrorStack a
531+ checkImportExists :: (Eq a, Show a) => String -> [a] -> a -> m a
529 checkImportExists t exports item =
532 checkImportExists t exports item =
530 if item `elem` exports
533 if item `elem` exports
531 then return item
534 then return item
@@ -534,7 +537,7 @@ resolveImport currentModule importModule exps imps impQual =
535-- Raises an error for when there is more than one definition for something.
538-- Raises an error for when there is more than one definition for something.
537−throwMultipleDefError :: (Show a) => String -> a -> Either ErrorStack b
540+throwMultipleDefError :: (Applicative m, MonadError ErrorStack m, Show a) => String -> a -> m b
538throwMultipleDefError what name = throwError $
541throwMultipleDefError what name = throwError $
539 mkErrorStack ("Multiple definitions for " ++ what ++ " '" ++ show name ++ "'") Nothing
542 mkErrorStack ("Multiple definitions for " ++ what ++ " '" ++ show name ++ "'") Nothing
@@ -542,7 +545,7 @@ throwMultipleDefError what name = throwError $
542-- Raises an error for when there is a conflicting definition for something, for example, a type
545-- Raises an error for when there is a conflicting definition for something, for example, a type
543-- class and data constructor of the same name.
546-- class and data constructor of the same name.
545−throwConflictingDefError :: (Show a) => String -> String -> a -> Either ErrorStack b
548+throwConflictingDefError :: (Applicative m, MonadError ErrorStack m, Show a) => String -> String -> a -> m b
546throwConflictingDefError what1 what2 name = throwError $
549throwConflictingDefError what1 what2 name = throwError $
547 mkErrorStack (what1 ++ " '" ++ show name ++ "' cannot be defined in the same module as a " ++ what2 ++ " of the same name") Nothing
550 mkErrorStack (what1 ++ " '" ++ show name ++ "' cannot be defined in the same module as a " ++ what2 ++ " of the same name") Nothing