====== Avoiding name conflict with Prelude ====== The avoidance of prelude may make sense for the following reasons: * reprogramming prelude functions to understand what it does * reprogramming prelude functions to create improved versions * avoiding conflicts with same name Methods: * Hide functions from prelude. * The directive ''{-# LANGUAGE NoImplicitPrelude #-}'' can be used to avoid implicit import of module ''Prelude'' * If you import ''Prelude'' by ''import qualified Prelude as '' then the directive above is not neccessary, because Prelude will only be imported qualified. However, the directive above will not harm. * Alternatively, you can import the funtions that you need from module Prelude if they do not conflict with other functions. NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions. ===== Example #1 ===== 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) ===== Example #2 ===== 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 ===== Example #3 ===== 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 ===== Example #4 ===== 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 ===== Example #5 ===== 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" | ^^^^^^^ ===== Example #6 ===== 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 () | ^^ ===== ✎ ===== ~~DISCUSSION~~