The avoidance of prelude may make sense for the following reasons:
Methods:
{-# LANGUAGE NoImplicitPrelude #-} can be used to avoid implicit import of module PreludePrelude by import qualified Prelude as <Prefix> then the directive above is not neccessary, because Prelude will only be imported qualified. However, the directive above will not harm.NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions.
Hide functions from prelude.
module Main where import Prelude hiding( reverse ) main :: IO () main = do putStrLn $ reverse "1234abcd" reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Qualified import of preload overrides implicit import.
module Main where import qualified Prelude as P main :: P.IO () main = do P.putStrLn P.$ reverse "1234abcd" reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Output:
dcba4321
The same with directive {-# LANGUAGE NoImplicitPrelude #-}.
{-# LANGUAGE NoImplicitPrelude #-} module Main where import qualified Prelude as P main :: P.IO () main = do P.putStrLn P.$ reverse "1234abcd" reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Output:
dcba4321
With import of all functions that are needed only.
module Main where import Prelude (IO, ($), putStrLn, Show) main :: IO () main = do putStrLn $ reverse "1234abcd" reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Output:
dcba4321
Shows what happens without measures, like the above.
module Main where main :: IO () main = do putStrLn $ reverse "1234abcd" -- <<-- error here reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Compiler error:
app\Main.hs:7:16: error:
Ambiguous occurrence `reverse'
It could refer to
either `Prelude.reverse',
imported from `Prelude' at app\Main.hs:1:8-11
(and originally defined in `GHC.List')
or `Main.reverse', defined at app\Main.hs:10:1
|
7 | putStrLn $ reverse "1234abcd"
| ^^^^^^^
Shows what happens with directive {-# LANGUAGE NoImplicitPrelude #-} only.
{-# LANGUAGE NoImplicitPrelude #-} module Main where main :: IO () -- <<-- error here main = do putStrLn $ reverse "1234abcd" reverse :: [a] -> [a] reverse l = rev l [] where rev [] acc = acc rev (e:r) acc = rev r (e : acc)
Compiler error:
app\Main.hs:6:9: error:
Not in scope: type constructor or class `IO'
|
6 | main :: IO ()
| ^^