Bug? 'Storable' and 'with'

Simon Marlow simonmar@microsoft.com
Mon, 14 Oct 2002 16:13:24 +0100


>         I am using ghc-5.04 and a code like:
>=20
> with c ( \c' -> hPutBuf h c' (sizeOf c))
>=20
> fails with "Fail: Prelude.undefined" when "c" is a user defined type,
> such as a pair:
>=20
> instance (Storable at,Storable bt) =3D> Storable (at,bt) where
> 	 sizeOf (a,b) =3D sizeOf a + sizeOf b
> 	 alignment (a,b) =3D max (alignment a) (alignment b)
> 	 peek p =3D do a <- peek ((castPtr p):: Ptr at)=20
> 		     b <- peekByteOff ((castPtr p):: Ptr bt) (sizeOf a)
> 		     return (a,b)
> 	 poke p (a,b) =3D do poke ((castPtr p):: Ptr at) a
> 			   pokeByteOff ((castPtr p):: Ptr bt)=20
> (sizeOf a) b

sizeOf and alignment are not supposed to evaluate their arguments.  You
might try making the definitions in your instance above a little lazier:

    sizeOf z =3D sizeOf a + sizeOf b where (a,b) =3D z
    alignment z =3D max (alignment a) (alignment b) where (a,b) =3D z

(feel free to use '~' if you prefer).

Cheers,
	Simon