RE: argument order of functions in Data.Bits

On 07 April 2005 07:54, Henning Thielemann wrote:
The version of Data.Bits which is shipped with GHC-6.2 is marked as experimental. So changes in the API are still possible? :-]
My experiments suggest that the functions should have a different order of arguments. Many of the functions of this module alter some bits in a machine word, thus they can be considered as update functions and their type signature should end with a -> a. Then we could easily combine several operations this way
shiftL 2 . clearBit 7 . setBit 4 . setBit 1
instead of flip shiftL 2 . flip clearBit 7 . flip setBit 4 . flip setBit 1
or (`shiftL` 2) . (`clearBit` 7) . (`setBit` 4) . (`setBit` 1) .
I don't see the benefit of urging the programmer to use infix notation instead of prefix notation here, e.g. (w `shift` 2) instead of (shift 2 w).
On the whole I agree, but I'm inclined against changing this because it would break so much code gratuitously. I vote for just putting this down to a small mistake in the original design, and leaving it. If there's overwhelming support for the change of course we'll make it, but I doubt there will be. Also the order of the arguments to shift matches C's shift operators (although they are real infix operators, of course). Cheers, Simon

"Simon Marlow"
shiftL 2 . clearBit 7 . setBit 4 . setBit 1
instead of flip shiftL 2 . flip clearBit 7 . flip setBit 4 . flip setBit 1
or (`shiftL` 2) . (`clearBit` 7) . (`setBit` 4) . (`setBit` 1)
On the whole I agree, but I'm inclined against changing this because it would break so much code gratuitously. I vote for just putting this down to a small mistake in the original design, and leaving it. If there's overwhelming support for the change of course we'll make it, but I doubt there will be.
For what it's worth, the old library NHC.Bits http://cvs.haskell.org/cgi-bin/cvsweb.cgi/nhc98/src/prelude/Bit/Bit.hs implements the 'set' and 'clear' operations with arguments the natural way round, although the left and right shift have the same order as Data.Bits and the C language. I slightly prefer the NHC names ^>> and ^<< for shifting too, since they are naturally infix. Henning's example composition would look like this: (^<< 2) . clear 7 . set 4 . set 1 I suppose if you didn't like the names and argument ordering in Data.Bits, it would be easy enough to layer your own preferred API on top, e.g. module My.Bits where import qualified Data.Bits class Data.Bits a => Bits a where shiftL :: Int -> a -> a shiftR :: Int -> a -> a shiftL = flip Data.Bits.shiftL shiftR = flip Data.Bits.shiftR ... etc Regards, Malcolm

On Thu, 7 Apr 2005, Malcolm Wallace wrote:
"Simon Marlow"
writes: shiftL 2 . clearBit 7 . setBit 4 . setBit 1
instead of flip shiftL 2 . flip clearBit 7 . flip setBit 4 . flip setBit 1
or (`shiftL` 2) . (`clearBit` 7) . (`setBit` 4) . (`setBit` 1)
On the whole I agree, but I'm inclined against changing this because it would break so much code gratuitously. I vote for just putting this down to a small mistake in the original design, and leaving it. If there's overwhelming support for the change of course we'll make it, but I doubt there will be.
For what it's worth, the old library NHC.Bits http://cvs.haskell.org/cgi-bin/cvsweb.cgi/nhc98/src/prelude/Bit/Bit.hs implements the 'set' and 'clear' operations with arguments the natural way round, although the left and right shift have the same order as Data.Bits and the C language.
If both infix operators and alphanumeric function names exist, then one could use different orders as it is done for 'subtract' and '(-)'.
I slightly prefer the NHC names ^>> and ^<< for shifting too, since they are naturally infix. Henning's example composition would look like this:
(^<< 2) . clear 7 . set 4 . set 1
Indeed, removing the 'Bit' suffix from the bit functions would also be nice. That would promote the use of qualification, that is Bit.clear, Bit.set and so on.
participants (3)
-
Henning Thielemann
-
Malcolm Wallace
-
Simon Marlow