2

In the Haskell tutorial I'm reading through there is an example that works fine in GHCI (7.10.2) but fails to produce output in a Notebook (both Jupyter, 4.0 or Kronos-Haskell).

The issue seems to involve a function

lockerLookup :: Int -> LockerMap -> Either String Code

which in the tutorial and in GHCI produces a result, for example

ghci> lockerLookup 101 lockers  
Right "JAH3I"

in a Notebook gives an error

No instance for (Show (Either String Code)) arising from a use of ‘print’
In a stmt of an interactive GHCi command: print it

Is this the correct behavior in a Notebook? Is there something I can do to produce the expected output in a Notebook?


import qualified Data.Map as Map    

data LockerState = Taken | Free deriving (Show, Eq)    
type Code = String    
type LockerMap = Map.Map Int (LockerState, Code) 

lockerLookup :: Int -> LockerMap -> Either String Code  
lockerLookup lockerNumber lockerMap =   
    case Map.lookup lockerNumber lockerMap of   
        Nothing -> Left $ "Locker number " ++ show lockerNumber ++ " doesn't exist!"  
        Just (state, code) -> if state /= Taken
            then Right code
            else Left $ "Locker " ++ show lockerNumber ++ " is already taken!"

lockers :: LockerMap  
lockers = Map.fromList   
    [(100,(Taken,"ZD39I"))  
    ,(101,(Free,"JAH3I"))  
    ,(103,(Free,"IQSA9"))  
    ,(105,(Free,"QOTSA"))  
    ,(109,(Taken,"893JJ"))  
    ,(110,(Taken,"99292"))  
    ]  
orome
  • 45,163
  • 57
  • 202
  • 418
  • strange - can you do a `:i Either` and look for the `Show` instance for it (maybe you use some non-default prelude) – Random Dev Aug 18 '15 at 15:48
  • @Carsten: `data Either a b = Left a | Right b -- Defined in ‘Data.Either’ at :1:1` – orome Aug 18 '15 at 15:52
  • no list of instances? Strange but AFAIK it should be right there (by `deriving ...`) - I don't know Jupyter nor Kronos - can you tell us how `ghci` is called? – Random Dev Aug 18 '15 at 15:56
  • @Carsten: That's all Notebook shows. A popup with just that one line. Maybe there's a way to see the instances too, but I don't know. – orome Aug 18 '15 at 16:00
  • Are you sure that IHaskell currently [supports IPython/Jupyter 4](https://github.com/gibiansky/IHaskell/issues/556)? – Bakuriu Aug 18 '15 at 16:15
  • @Carsten IHaskell does **not** call GHCi. It uses the GHC libraries to parse and execute the code. – Bakuriu Aug 18 '15 at 16:16
  • @Bakuriu: The same thing happens in Kronos-Haskell. And I'm [using IPython 3.0](http://stackoverflow.com/a/32076954/656912). – orome Aug 18 '15 at 16:16
  • @Bakuriu as I said I don't know anything about IHaskell etc. - it was just an guess based on the output given - I hope you both can figure it out - good luck – Random Dev Aug 18 '15 at 17:09
  • fwiw, I tried your code in my build of IHaskell and it worked. Using 7.10 built with stack. I think there's something corrupted with your setup. – ErikR Aug 18 '15 at 18:00
  • @user5402: You tried it in a notebook? – orome Aug 18 '15 at 18:01
  • Yup - did it in a notebook. I basically followed the build recipe in the IHaskell README for stack, but I also used `stack setup --no-system-ghc` to make sure it used it's own ghc and tools. – ErikR Aug 18 '15 at 18:25
  • @user5402: What version of GHC does that end up being? – orome Aug 18 '15 at 18:31
  • 1
    7.10.2 (la di la di da filler) – ErikR Aug 18 '15 at 18:44
  • @user5402: Maybe I have some config issue then. But Haskell/Cabal is such a mess (learning fresh and really astounded how messed up the install and config and package management is) I may just try another language. – orome Aug 18 '15 at 18:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87307/discussion-between-user5402-and-raxacoricofallapatorius). – ErikR Aug 18 '15 at 18:52
  • @Carsten: It turns out (*blush*) that you were on the right track. Earlier on the notebook I had `Either a b = Left a | Right b`, which (remarkably!) Haskell let me do without posting any warning, redefining `Either`!. That's why there were no instances and why (right in front of us!) it said it was defined in "". Removing or not executing that statement fixes the issue (and has `:t Either` report instances as expected). – orome Aug 19 '15 at 11:48
  • @Carsten: The [chat transcript](http://chat.stackoverflow.com/rooms/87307/discussion-between-user5402-and-raxacoricofallapatorius) shows how we convinced ourselves it was [something else](http://stackoverflow.com/a/32081764/656912). – orome Aug 19 '15 at 11:50

1 Answers1

3

If you are using the Haskell Platform, make sure that the directory

~/Library/Haskell/bin

is present and appears before /usr/bin in your PATH.

That's where the Platform installs new binaries, so you'll need it your PATH to access apps you build and to use upgraded versions of tools which come with the Platform (e.g. cabal, happy, alex, etc.)

Also, you can use these instructions to install IHaskell yourself:

https://github.com/gibiansky/IHaskell#install-haskell-tools

orome
  • 45,163
  • 57
  • 202
  • 418
ErikR
  • 51,541
  • 9
  • 73
  • 124