@@ -134,9 +134,10 @@ addExport exports name =
135desugarImports :: [Module] -> Either String [Module]
135desugarImports :: [Module] -> Either String [Module]
136desugarImports modules = do
136desugarImports modules = do
137+ let modules' = importPrelude `map` modules
137− unfilteredExports <- findExports modules
138+ unfilteredExports <- findExports modules'
138− exports <- foldM filterModuleExports unfilteredExports modules
139+ exports <- foldM filterModuleExports unfilteredExports modules'
139− mapM (renameInModule' unfilteredExports exports) modules
140+ mapM (renameInModule' unfilteredExports exports) modules'
142 -- Filters the exports for a module in the global exports environment so that only explicitly
143 -- Filters the exports for a module in the global exports environment so that only explicitly
@@ -155,6 +156,19 @@ desugarImports modules = do
155 imports <- resolveImports env m
156 imports <- resolveImports env m
156 renameInModule imports env (elaborateExports exps m)
157 renameInModule imports env (elaborateExports exps m)
160+-- Add an import declaration for the Prelude to a module if it does not already explicitly import
163+importPrelude :: Module -> Module
164+importPrelude m@(Module mn decls exps) =
165+ if isPreludeImport `any` decls then m
166+ else Module mn (preludeImport : decls) exps
168+ isPreludeImport (ImportDeclaration (ModuleName [ProperName "Prelude"]) _ _) = True
169+ isPreludeImport _ = False
170+ preludeImport = ImportDeclaration (ModuleName [ProperName "Prelude"]) Nothing Nothing
159-- Rethrow an error with the name of the current module in the case of a failure
173-- Rethrow an error with the name of the current module in the case of a failure
@@ -329,7 +343,7 @@ type ExplicitImports = [DeclarationRef]
329-- explicitly imported declarations.
343-- explicitly imported declarations.
331findImports :: [Declaration] -> M.Map ModuleName (Maybe ExplicitImports, Maybe ModuleName)
345findImports :: [Declaration] -> M.Map ModuleName (Maybe ExplicitImports, Maybe ModuleName)
332−findImports = M.insertWith (flip const) (ModuleName [ProperName "Prelude"]) (Nothing, Nothing) . foldl findImports' M.empty
346+findImports = foldl findImports' M.empty
334 findImports' result (ImportDeclaration mn expl qual) = M.insert mn (expl, qual) result
348 findImports' result (ImportDeclaration mn expl qual) = M.insert mn (expl, qual) result
335 findImports' result _ = result
349 findImports' result _ = result