cabal icon indicating copy to clipboard operation
cabal copied to clipboard

`import Prelude hiding ...` isn't working in a `cabal repl` shell

Open yaxu opened this issue 1 year ago • 2 comments

Describe the bug

Hiding functions from Prelude with import Prelude hiding ... doesn't work in cabal repl. It works fine in ghci.

To Reproduce Steps to reproduce the behavior:

  • make a dummy library package with cabal init
  • add containers to build-depends
  • run cabal repl

Try hiding something from prelude:

ghci> import Prelude hiding (map)
ghci> import Data.Map
ghci> :t map
<interactive>:1:1: error: [GHC-87543]
    Ambiguous occurrence ‘map’.
    It could refer to
       either ‘Prelude.map’,
              imported from ‘Prelude’ at src/MyLib.hs:1:8-12
              (and originally defined in ‘ghc-internal-9.1201.0:GHC.Internal.Base’),
           or ‘Data.Map.map’,
              imported from ‘Data.Map’
              (and originally defined in ‘Data.Map.Internal’).

A more minimal test:

ghci> import Prelude hiding (map)
ghci> :t map
map :: (a -> b) -> [a] -> [b]

It seems hiding is ignored.

Expected behavior

function from Prelude gets hidden to avoid error when overloading it from another package.

It works in a plain ghci shell:

ghci> import Prelude hiding (map)
ghci> import Data.Map
ghci> :t map
map :: (a -> b) -> Map k a -> Map k b

System information

  • linux
  • cabal-3.14.1.1, ghc-9.12.1 (latest), and cabal-3.12.1.0.0 / ghc-9.4.8 (recommended)

yaxu avatar Feb 09 '25 20:02 yaxu

The issue isn't that hiding "isn't working", but rather that the Prelude is already imported implicitly, and reimports but with a hiding clause doesn't impact the original (implicit) import. I do see that your ghci is behaving as you intend, but perhaps there's a XNoImplicitPrelude in some .ghci or a user setting, that cabal isn't reading?

Test hiding on double-import with "Stuff" module, remove stuff, retry with hiding:

module Stuff where

data A
  = One
  | Two

data B
  = B
  { test :: A
  }
ghci> :t B
<interactive>:1:1: error: [GHC-88464]
    Data constructor not in scope: B

ghci> :load Stuff
[1 of 1] Compiling Stuff            ( Stuff.hs, interpreted )
Ok, one module loaded.
ghci> :show modules
Stuff            ( Stuff.hs, interpreted )
ghci> :show imports
:module +*Stuff -- added automatically
ghci> :t B
B :: A -> B
ghci> import Stuff hiding (B)
ghci> :t B
B :: A -> B
ghci> :module - Stuff
ghci> import Stuff hiding (B)
ghci> :t B
<interactive>:1:1: error: [GHC-88464]
    Data constructor not in scope: B

Test removing implicit Prelude, then reimport with hiding clause, then importing Data.Map. It is odd the implicit Prelude doesn't appear in imports until explicitly imported, and doesn't appear in modules, which is probably a bug, but for ghci, not cabal.

ghci> :t map
map :: (a -> b) -> [a] -> [b]
ghci> :show modules
ghci> :show imports
ghci> :module - Prelude
ghci> :t map
<interactive>:1:1: error: [GHC-88464]
    Variable not in scope: map

ghci> import Prelude hiding (map)
ghci> :t map
<interactive>:1:1: error: [GHC-88464]
    Variable not in scope: map
    Suggested fix:
      Perhaps use one of these:
        ‘max’ (imported from Prelude), ‘fmap’ (imported from Prelude),
        ‘mapM’ (imported from Prelude)

ghci> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b
ghci> import Data.Map
ghci> :t map
map :: (a -> b) -> Map k a -> Map k b 

Try cabal repl --repl-options="-XNoImplicitPrelude", then you should be where you wanted to do the hiding import on the Prelude. Or you can also remember to remove the Prelude module, then reimport with hiding

DNEAVES avatar Mar 28 '25 23:03 DNEAVES

Thanks very much @DNEAVES for the detailed response!

This ghci behaviour to allow hiding things in Prelude seems present across all ghci versions that I've tried, which is quite a lot, via the tidal project which is actively used by many people.

However I now can't reproduce the original problem I was having before. At least, I'm able to put import Prelude hiding (all) in a module, load with cabal repl, and all is gone. So closing this for now - will repoen if I hit the problem again.

Thanks again and apologies if this is just noise.

yaxu avatar Mar 30 '25 07:03 yaxu