@@ -89,8 +89,11 @@ updateExportedModule env mn update = do
90-- Adds an empty module to an ExportEnvironment.
90-- Adds an empty module to an ExportEnvironment.
92−addEmptyModule :: ExportEnvironment -> ModuleName -> ExportEnvironment
92+addEmptyModule :: ExportEnvironment -> ModuleName -> Either String ExportEnvironment
93−addEmptyModule env name = M.insert name (Exports [] [] []) env
93+addEmptyModule env name =
94+ if name `M.member` env
95+ then throwError $ "Module '" ++ show name ++ "' has been defined more than once"
96+ else return $ M.insert name (Exports [] [] []) env
96-- Adds a type belonging to a module to the export environment.
99-- Adds a type belonging to a module to the export environment.
@@ -255,7 +258,9 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName "Prim"]) pri
256 -- Add all of the exported declarations from a module to the global export environment
259 -- Add all of the exported declarations from a module to the global export environment
257 addModule :: ExportEnvironment -> Module -> Either String ExportEnvironment
260 addModule :: ExportEnvironment -> Module -> Either String ExportEnvironment
261+ addModule env m@(Module mn ds _) = do
262+ env' <- addEmptyModule env mn
258− addModule env m@(Module mn ds _) = rethrowForModule m $ foldM (addDecl mn) (addEmptyModule env mn) ds
263+ rethrowForModule m $ foldM (addDecl mn) env' ds
260 -- Add a declaration from a module to the global export environment
265 -- Add a declaration from a module to the global export environment
261 addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either String ExportEnvironment
266 addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either String ExportEnvironment