git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Auto-import prelude now working properly
a1eaad1Gary Burgess
examples/passing/Applicative.purs+2
@@ -1,5 +1,7 @@
1module Main where
1module Main where
2
2
3+import Prelude ()
4+
3class Applicative f where
5class Applicative f where
4 pure :: forall a. a -> f a
6 pure :: forall a. a -> f a
5 (<*>) :: forall a b. f (a -> b) -> f a -> f b
7 (<*>) :: forall a b. f (a -> b) -> f a -> f b
src/Language/PureScript/Sugar/Names.hs+18−4
@@ -134,9 +134,10 @@ addExport exports name =
134--
134--
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'
140 where
141 where
141
142
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)
157
158
159+-- |
160+-- Add an import declaration for the Prelude to a module if it does not already explicitly import
161+-- it.
162+--
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
167+ where
168+ isPreludeImport (ImportDeclaration (ModuleName [ProperName "Prelude"]) _ _) = True
169+ isPreludeImport _ = False
170+ preludeImport = ImportDeclaration (ModuleName [ProperName "Prelude"]) Nothing Nothing
171+
158-- |
172-- |
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
160--
174--
@@ -329,7 +343,7 @@ type ExplicitImports = [DeclarationRef]
329-- explicitly imported declarations.
343-- explicitly imported declarations.
330--
344--
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
333 where
347 where
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