User Tools

Site Tools


codesnippets:avoidconflictprelude

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 <Prefix> 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 ()
  |         ^^

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/avoidconflictprelude.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki