[Haskell-cafe] Problem trying to get class Bounded to work

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue May 23 11:47:07 EDT 2006

Hello Jacques,

Tuesday, May 23, 2006, 7:13:33 PM, you wrote:

>>>>malloc :: Storable a => IO (Ptr a)
>>>>malloc  = doMalloc undefined
>>>>  doMalloc       :: Storable b => b -> IO (Ptr b)
>>>>  doMalloc dummy  = mallocBytes (sizeOf dummy)
>>>Is there any reason to not code this as
>>>malloc :: Storable a => IO (Ptr a)
>>>malloc  = mallocBytes $ sizeof undefined


1) in your code type of 'undefined' is unknown. it can be any type,
while we need 'undefined' of one concrete type, which is given in
function signature. compiler will just complain that type is not fixed

2) what we really need is to write

malloc :: Storable a => IO (Ptr a)
malloc  = mallocBytes $ sizeof (undefined::a)

i.e. fix type of 'undefined' to 'a'. but that is impossible in

3) GHC provides language extension (see above in the thread), that
allow to use 'a' in function body if function was declared using
'forall', so this works in GHC

malloc :: forall a . Storable a => IO (Ptr a)
malloc  = mallocBytes $ sizeof (undefined::a)

4) this example is borrowed from std libs, what is common for hugs,
ghc and nhc, so they can't use ghc-specific solution. instead, this
complex trick is used. it's very unobvious, but at least it works with
any H98-compatible compiler

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list