Bits Problem

Simon Peyton-Jones simonpj@microsoft.com
Thu, 17 Apr 2003 12:48:41 +0100


Library and FFI guys (esp Manuel)

| I can see Bool can be an instance of Bit but why does it
| need to be an instance of Num?

We can't remember who made the change or why.  The advantage of making
Num a superclass of bits is that (as someone mentioned) it allows us to
give a default implementation of bit and
> testBit:
>
>     bit i               =3D 1 `shift` i
>     x `testBit` i       =3D (x .&. bit i) /=3D 0

So the choice is

	a) Keep Num as superclass, and offer the default implementations
	b) Remove Num and require instances of Bits to supply bit and
testBit

Myself I'd go for (b), but others on the libraries list may want to
weigh in.  (The FFI folk should be interested because the Bits library
is part of the FFI spec.  Note to Manuel: section 5.2 would be better
with an explicit class declaration for Bits!)

[SimonMar things we could make do with just Eq to implement testBit.]

Simon


| -----Original Message-----
| From: haskell-admin@haskell.org [mailto:haskell-admin@haskell.org] On
Behalf Of Dominic Steinitz
| Sent: 06 April 2003 14:08
| To: Glynn Clements
| Cc: haskell@haskell.org
|=20
| Is this a recent change? I've downloaded Ian Lynagh's DES module and
it
| doesn't compile because of this. I assume it used to. I don't see the
| rationale for this. I can see Bool can be an instance of Bit but why
does it
| need to be an instance of Num?
|=20
| Dominic Steinitz
| ----- Original Message -----
| From: "Glynn Clements" <glynn.clements@virgin.net>
| To: "Dominic Steinitz" <dominic.steinitz@blueyonder.co.uk>
| Cc: <haskell@haskell.org>
| Sent: Sunday, April 06, 2003 2:35 PM
| Subject: Re: Bits Problem
|=20
|=20
| >
| > Dominic Steinitz wrote:
| >
| > > Can anyone explain this? Hugs doesn't complain.
| > >
| > > Prelude> :set --version
| > > The Glorious Glasgow Haskell Compilation System, version 5.04.1
| > >
| > > test.hs:5:
| > >     No instance for (Num Bool)
| > >     arising from the instance declaration at test.hs:5
| > >     In the instance declaration for `Bits Bool'
| > >
| > > module Main(main) where
| > >
| > > import Bits
| > >
| > > instance Bits Bool where
| > >    complement False =3D True
| > >    complement True  =3D False
| >
| > GHC's definition of Bits requires that instances of Bits are also
| > instances of Num.
| >
| > This constraint is required for the default implementations of bit
and
| > testBit:
| >
| >     bit i               =3D 1 `shift` i
| >     x `testBit` i       =3D (x .&. bit i) /=3D 0
| >
| > --
| > Glynn Clements <glynn.clements@virgin.net>
|=20
| _______________________________________________
| Haskell mailing list
| Haskell@haskell.org
| http://www.haskell.org/mailman/listinfo/haskell