@@ -70,7 +70,7 @@ compile = compile' initEnvironment
7070
7171compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
7272compile' env opts ms = do
73− (sorted, _) <- sortModules $ if optionsNoPrelude opts then ms else (map importPrelude ms)
73+ (sorted, _) <- sortModules $ map importPrim $ if optionsNoPrelude opts then ms else (map importPrelude ms)
7474 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
7575 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
7676 regrouped <- stringifyErrorStack True $ createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
@@ -170,7 +170,7 @@ make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options
170170make outputDir opts ms = do
171171 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
172172
173− (sorted, graph) <- liftError $ sortModules $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
173+ (sorted, graph) <- liftError $ sortModules $ map importPrim $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
174174
175175 toRebuild <- foldM (\s (Module moduleName' _ _) -> do
176176 let filePath = runModuleName moduleName'
@@ -241,16 +241,19 @@ reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
241241 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
242242
243243-- |
244−-- Add an import declaration for the Prelude to a module if it does not already explicitly import
245−-- it.
244+-- Add an import declaration for a module if it does not already explicitly import it.
246245--
247−importPrelude :: Module -> Module
248−importPrelude m@(Module mn decls exps) =
249− if isPreludeImport `any` decls || mn == prelude then m
250− else Module mn (preludeImport : decls) exps
246+addDefaultImport :: ModuleName -> Module -> Module
247+addDefaultImport toImport m@(Module mn decls exps) =
248+ if isExistingImport `any` decls || mn == toImport then m
249+ else Module mn (ImportDeclaration toImport Nothing Nothing : decls) exps
251250 where
252− prelude = ModuleName [ProperName C.prelude]
253− isPreludeImport (ImportDeclaration (ModuleName [ProperName mn']) _ _) | mn' == C.prelude = True
254− isPreludeImport (PositionedDeclaration _ d) = isPreludeImport d
255− isPreludeImport _ = False
256− preludeImport = ImportDeclaration prelude Nothing Nothing
251+ isExistingImport (ImportDeclaration mn' _ _) | mn' == toImport = True
252+ isExistingImport (PositionedDeclaration _ d) = isExistingImport d
253+ isExistingImport _ = False
254+
255+importPrim :: Module -> Module
256+importPrim = addDefaultImport (ModuleName [ProperName C.prim])
257+
258+importPrelude :: Module -> Module
259+importPrelude = addDefaultImport (ModuleName [ProperName C.prelude])