[Haskell-beginners] Re: Why is this type ambiguous?

Maurí­cio CA mauricio.antunes at gmail.com
Mon Oct 26 21:34:23 EDT 2009


 >> But isn't the type of 'dummy' defined by 'return dummy' beeing
 >> a possible return value (and, so, dummy :: b)?

 > No. let-bindings are polymorphic, so dummy :: forall a. a

Oh, I once learned that some code of mine didn't work because
lambda bindings are monomorphic. Now I can use it for good.

    genericCast :: (Storable a, Storable b) => a -> IO b
    genericCast v = return undefined >>= \dummy ->
      allocaBytes (max (sizeOf v) (sizeOf dummy)) $ \p ->
        poke p v >> if False then return dummy else peek (castPtr p)

In ghci:

    > let a = 4 :: WordPtr
    > b <- genericCast a :: IO (Ptr ())
    > b
    0x00000004


Thanks!
Maurício



More information about the Beginners mailing list