@@ -89,8 +89,11 @@ updateExportedModule env mn update = do
8989-- |
9090-- Adds an empty module to an ExportEnvironment.
9191--
92−addEmptyModule :: ExportEnvironment -> ModuleName -> ExportEnvironment
93−addEmptyModule env name = M.insert name (Exports [] [] []) env
92+addEmptyModule :: ExportEnvironment -> ModuleName -> Either String ExportEnvironment
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
9497
9598-- |
9699-- Adds a type belonging to a module to the export environment.
@@ -255,7 +258,9 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName "Prim"]) pri
255258
256259 -- Add all of the exported declarations from a module to the global export environment
257260 addModule :: ExportEnvironment -> Module -> Either String ExportEnvironment
258− addModule env m@(Module mn ds _) = rethrowForModule m $ foldM (addDecl mn) (addEmptyModule env mn) ds
261+ addModule env m@(Module mn ds _) = do
262+ env' <- addEmptyModule env mn
263+ rethrowForModule m $ foldM (addDecl mn) env' ds
259264
260265 -- Add a declaration from a module to the global export environment
261266 addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either String ExportEnvironment