Pikaurd
Pikaurd

Reputation: 1124

An Ambiguous error in example of Real World Haskell

I am very new to Haskell. Today when I reading the book and practice it's example I got an error. Here is the source code Nullable.hs in Chapter 3 page 57.

import Prelude hiding (Maybe)

{-- snippet Nullable --}
data Maybe a = Just a
             | Nothing
{-- /snippet Nullable --}

{-- snippet wrappedTypes --}
someBool = Just True

someString = Just "something"
{-- /snippet wrappedTypes --}


{-- snippet parens --}
wrapped = Just (Just "wrapped")
{-- /snippet parens --}

when I type ghci Nullable.hs got:

GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling Main             ( Downloads/examples/ch03/Nullable.hs, interpreted )

Downloads/examples/ch03/Nullable.hs:9:11:
    Ambiguous occurrence `Just'
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15
                          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28

Downloads/examples/ch03/Nullable.hs:11:13:
    Ambiguous occurrence `Just'
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15
                          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28

Downloads/examples/ch03/Nullable.hs:16:10:
    Ambiguous occurrence `Just'
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15
                          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28

Downloads/examples/ch03/Nullable.hs:16:16:
    Ambiguous occurrence `Just'
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15
                          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28
Failed, modules loaded: none.
Prelude> 

I thought this problem caused by scope so I add a prefix to "Just" like this someBool = Main.Just True and try again got:

[1 of 1] Compiling Main             ( nu.hs, interpreted )
Ok, modules loaded: Main.
*Main> Just 1

<interactive>:1:0:
    No instance for (Show (Maybe t))
      arising from a use of `print' at <interactive>:1:0-5
    Possible fix: add an instance declaration for (Show (Maybe t))
    In a stmt of an interactive GHCi command: print it

Now I can confirm that it is not only caused by scope error. But I can not handle it…

What would be a way to do this? Any suggestion will be appreciate.

Upvotes: 1

Views: 785

Answers (1)

Edward Z. Yang
Edward Z. Yang

Reputation: 26742

No, the original error was caused by a scope error, and so when you qualified it explicitly you fixed one error but introduced another.

You can fix the other error by adding a deriving (Show) line to your original code:

data Maybe a = Just a
         | Nothing
    deriving (Show)

Upvotes: 2

Related Questions