[Haskell-cafe] Types and hashes of hashes, trouble for a Java-programmer...

Dan Doel dan.doel at gmail.com
Mon Apr 13 13:44:02 EDT 2009

On Monday 13 April 2009 11:42:33 am John Smith wrote:
> Hi, a java-programmer running into trouble while trying to learn Haskell.
> I try to make a hash containing hashes but can not getting a value out of
> the innermost hash - I keep running into type-trouble where IO and Maybe
> monad is getting mixed?

I don't know the type of all these things, but here's a try.

> My attempt:
> removeMaybeHash x =
>   case x of
>   Just ref -> ref
>   Nothing -> HashTable.new (==) (\key -> key)

'HashTable.new ...' has type IO (HashTable k v). This means that the type of 
removeMaybeHash must be:

  Maybe (IO (HashTable k v)) -> IO (HashTable k v)

because in the Just case, you're just using 'ref' as the result of the 
function. This is probably not what you want, so you should use 'return ref'. 
This will make it have type:

  Maybe (HashTable k v) -> IO (HashTable k v)

> test = do
>   h <- HashTable.new (==) (\key -> key)
>   h1 <- HashTable.new (==) (\key -> key)
>   HashTable.insert h 3 h1
>   HashTable.insert h1 1 1000
>   maybeOuterHash <- HashTable.lookup h 3
>   res <- removeMaybe (removeMaybeHash maybeOuterHash) 1000

I don't know what the type of removeMaybe is supposed to be, but as used in 
this expression, with the above note about the type of removeMaybeHash, it 
must have type:

  IO (HashTable k v) -> Int -> IO (something)

which is an odd type for a function with name "removeMaybe".

>   return res
> Any clues?

Hopefully those help. As an additional clue, I'd recommend you write some type 
signatures at the top level for what you expect the types of your functions to 
be. That might help point out more specifically where things aren't going as 
you expect.

-- Dan

More information about the Haskell-Cafe mailing list