Ambiguous occurrence `Just'

I am an absolute beginner. Going through LYAH using emacs.

My current Set up:

  • Ubuntu 12.04 LTS (Use Experience - beginner)
  • GNU Emacs 23 (Use Experience - beginner)
    • able to work in haskell major mode

Finding difficult to follow instructions (to bring haskell libraries) at Point 2 described here.

Also need guidance to enable Scion IDE.

Problem:

.hs code

data Maybe a = Nothing | Just a

While running the code, I get following error:

Please ignore typo error, Originally posted:

*Main> just "Haha"  

interactive>:339:1: Not in scope: `just'

This is the real error (added after Tikhon Jelvis' comment):

*Main> Just "Haha"  
interactive>:341:1:  
    Ambiguous occurrence `Just'  
    It could refer to either `Main.Just',    
                             defined at /home/optimight/baby.hs:89:26  
                          or `Prelude.Just',  
                             imported from `Prelude' at /home/optimight/baby.hs:1:1  
                             (and originally defined in `Data.Maybe')  

Answers


Your error just tells you that there are two possible versions of Just and GHCi does not know which one to pick.

Every Haskell program implicitly imports a whole bunch of functions and data types. These form the "prelude". One of these types is Maybe. This means that every single program already has access to a type just like the one you defined, with exactly the same names.

You can overcome this in two ways. The best option would be to just choose different names:

data MyMaybe a = MyJust a | MyNothing

(I'm sure you can come up with better names than that though :P.)

The other option would be to import the Prelude explicitly, hiding Maybe:

import Prelude hiding (Maybe (..))

This line at the top of your program just tells Haskell to import everything it normally imports except Maybe and all of it's constructors (Just and Nothing).

Now you will run into one more problem: GHCi does not know how to render a value of your Maybe type into a string to show on the prompt. You will get an error like this:

<interactive>:1:1:
No instance for (Show (Maybe [Char]))
  arising from a use of `print'
Possible fix: add an instance declaration for (Show (Maybe [Char]))
In a stmt of an interactive GHCi command: print it

What you need to do is tell the compiler how a Maybe value looks as a string. Happily, this is extremely easy. In fact, it's so easy even the computer can do it! If you define your type as:

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

then the compiler will write a show function (which is basically toString from other languages) for you. Now your original statement (Just "Haha") should work properly.

Also: enabling Scion is a completely different question entirely. I don't think it's worth bothering with it until you've learned more Haskell and are actually working on some sort of larger project. For now, the standard Haskell mode should be more than enough.


Need Your Help

iOS 5 - Camera Overlay Clone

objective-c ios

Is there an open source nib out that that's a clone of the overlay view that Apple uses for its Camera.app? I'm currently using UIImagePickerController with picker.showCameraControls = YES, but I n...

HTML5 Video with Fullscreen Mode in IE9 and Firefox

html5 video fullscreen

I am curently working on a HTML5 video Plugin and below is my code and trying to work with custom controls.