git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Auto-import prelude now working properly
commita1eaad18c827439ea88a04c5933a25ef34975e2d authorGary Burgess <gary.burgess@gmail.com> dateTue 18 Mar 2014, 23:51 (13 years ago) parentc34a9cf
2 files changed+20−4
examples/passing/Applicative.purs+2 src/Language/PureScript/Sugar/Names.hs+18−4
examples/passing/Applicative.purs+2
@@ -1,5 +1,7 @@
11module Main where
22
3+import Prelude ()
4+
35class Applicative f where
46 pure :: forall a. a -> f a
57 (<*>) :: 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 =
134134--
135135desugarImports :: [Module] -> Either String [Module]
136136desugarImports modules = do
137− unfilteredExports <- findExports modules
138− exports <- foldM filterModuleExports unfilteredExports modules
139− mapM (renameInModule' unfilteredExports exports) modules
137+ let modules' = importPrelude `map` modules
138+ unfilteredExports <- findExports modules'
139+ exports <- foldM filterModuleExports unfilteredExports modules'
140+ mapM (renameInModule' unfilteredExports exports) modules'
140141 where
141142
142143 -- Filters the exports for a module in the global exports environment so that only explicitly
@@ -155,6 +156,19 @@ desugarImports modules = do
155156 imports <- resolveImports env m
156157 renameInModule imports env (elaborateExports exps m)
157158
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+
158172-- |
159173-- Rethrow an error with the name of the current module in the case of a failure
160174--
@@ -329,7 +343,7 @@ type ExplicitImports = [DeclarationRef]
329343-- explicitly imported declarations.
330344--
331345findImports :: [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
333347 where
334348 findImports' result (ImportDeclaration mn expl qual) = M.insert mn (expl, qual) result
335349 findImports' result _ = result