git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #303 from garyb/auto-prelude

Auto import prelude

commit7d378e0d700dd420f6fdb1fbb396a1913c210d6f authorPhil Freeman <paf31@cantab.net> dateTue 18 Mar 2014, 17:00 (13 years ago) parentbdaf5c9a1eaad1
11 files changed+54−3
examples/manual/passing/Import.purs+3 examples/manual/passing/RedefinedFixity.purs+6 examples/passing/Applicative.purs+2 examples/passing/ArrayType.purs+1 examples/passing/AutoPrelude.purs+8 examples/passing/AutoPrelude2.purs+9 examples/passing/Church.purs+2 examples/passing/Conditional.purs+2 examples/passing/Dollar.purs+2 examples/passing/Monad.purs+2 src/Language/PureScript/Sugar/Names.hs+17−3
examples/manual/passing/Import.purs+3
@@ -1,5 +1,7 @@
11module M1 where
22
3+ import Prelude ()
4+
35 id :: forall a. a -> a
46 id = \x -> x
57
@@ -7,6 +9,7 @@ module M1 where
79
810module M2 where
911
12+ import Prelude ()
1013 import M1
1114
1215 main = \_ -> foo 42
examples/manual/passing/RedefinedFixity.purs+6
@@ -1,15 +1,21 @@
11module M1 where
22
3+import Prelude ()
4+
35($) f a = f a
46
57infixr 1000 $
68
79module M2 where
810
11+import Prelude ()
12+
913import M1
1014
1115module M3 where
1216
17+import Prelude ()
18+
1319import M1
1420import M2
1521
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
examples/passing/ArrayType.purs+1
@@ -1,5 +1,6 @@
11module Main where
22
3+import Prelude ()
34import Data.Array
45
56test :: [] Number
examples/passing/AutoPrelude.pursnew file+8
@@ -0,0 +1,8 @@
1+module Main where
2+
3+import Debug.Trace
4+
5+f x = x * 10
6+g y = y - 10
7+
8+main = trace $ show $ (f <<< g) 100
examples/passing/AutoPrelude2.pursnew file+9
@@ -0,0 +1,9 @@
1+module Main where
2+
3+import qualified Prelude as P
4+import Debug.Trace
5+
6+f :: forall a. a -> a
7+f = P.id
8+
9+main = P.($) trace ((f P.<<< f) "Done")
examples/passing/Church.purs+2
@@ -1,5 +1,7 @@
11module Main where
22
3+ import Prelude ()
4+
35 type List a = forall r. r -> (a -> r -> r) -> r
46
57 empty :: forall a. List a
examples/passing/Conditional.purs+2
@@ -1,5 +1,7 @@
11module Main where
22
3+ import Prelude ()
4+
35 fns = \f -> if f true then f else \x -> x
46
57 not = \x -> if x then false else true
examples/passing/Dollar.purs+2
@@ -1,5 +1,7 @@
11module Main where
22
3+import Prelude ()
4+
35($) :: forall a b. (a -> b) -> a -> b
46($) f x = f x
57
examples/passing/Monad.purs+2
@@ -1,5 +1,7 @@
11module Main where
22
3+ import Prelude ()
4+
35 type Monad m = { return :: forall a. a -> m a
46 , bind :: forall a b. m a -> (a -> m b) -> m b }
57
src/Language/PureScript/Sugar/Names.hs+17−3
@@ -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--