Changes between Version 34 and Version 35 of PrimBool


Ignore:
Timestamp:
Sep 19, 2013 10:22:15 AM (7 months ago)
Author:
jstolarek
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PrimBool

    v34 v35  
    223223Below is a summary of implementation details and decisions: 
    224224 
    225   * The new comparison primops return a value of type `Int#`: `1#` represents `True` and `0#` represents `False`. The `Int#` type was chosen because on Haskell it is more common to use signed Int type insetad of unsigned Word. By using `Int#` the users can easily convert unboxed result into a boxed value, without need to use `word2Int#` and `int2word#` primops. 
    226  
    227   * Unlike C, `2#` or `-3#` don't represent a Boolean value.  More concretely, you can use `tagToEnum#` to convert one of these `Int#` values to a `Bool`, but `tagToEnum#` does no error checking, so it would be Very Very Bad to call it on `2#`. 
    228  
    229   * As a small side-task, four new logical bitwise primops have been implemented: `andI#`, `orI#`, `xorI#` and `negI#` (#7689). These operate on values of type `Int#`. Earlier we had only bitwise logical primops operating on values of type `Word#`. 
    230  
    231   * Names of the existing comparison primops were changed. Operators had `$` added before `#`, others had `I` added before the `#` (this is a mnemonic denoting that this primop returns and `Int#`). Examples: 
    232  
    233 {{{ 
    234 >=$#      :: Int#    -> Int#    -> Int# 
    235 /=$##     :: Double# -> Double# -> Int# 
    236 gtCharI#  :: Char#   -> Char#   -> Int# 
    237 eqWordI#  :: Word#   -> Word#   -> Int# 
    238 ltFloatI# :: Float#  -> Float#  -> Int# 
    239 leAddrI#  :: Addr#   -> Addr#   -> Int# 
    240 }}} 
    241  
    242   * A new module `GHC.PrimWrappers` was added to ghc-prim library. This module contains wrappers for comparison primops. These wrappers have names identical to removed primops and return a `Bool`. Examples: 
    243  
    244 {{{ 
    245 gtChar# :: Char# -> Char# -> Bool 
    246 gtChar# a b = tagToEnum# (a `gtCharI#` b) 
    247  
    248 (>=#) :: Int# -> Int# -> Bool 
    249 (>=#) a b = tagToEnum# (a >=$# b) 
    250  
    251 eqWord# :: Word# -> Word# -> Bool 
    252 eqWord# a b = tagToEnum# (a `eqWordI#` b) 
    253  
    254 (/=##) :: Double# -> Double# -> Bool 
    255 (/=##) a b = tagToEnum# (a /=$## b) 
    256  
    257 ltFloat# :: Float# -> Float# -> Bool 
    258 ltFloat# a b = tagToEnum# (a `ltFloatI#` b) 
    259  
    260 leAddr# :: Addr# -> Addr# -> Bool 
    261 leAddr# a b = tagToEnum# (a `leAddrI#` b) 
    262 }}} 
    263  
    264 Thanks to these wrappers the change is almost backwards compatible. '''The only thing primop users need to change in their existing code to make it work again is adding import of GHC.!PrimWrappers module.''' 
    265  
    266   * Functions for comparing `Integer` type, implemented in integer-gmp and integer-simple libraries, received a similar treatment. Technically they are not primops, because they are implemented in Haskell (in case of integer-gmp also with FFI), but they pretend to be ones. There are six primops for comparing `Integer` values: 
     225  * The new comparison primops return a value of type `Int#`: `1#` represents `True` and `0#` represents `False`. The `Int#` type was chosen because in Haskell it is more common to use signed `Int` type insetad of unsigned `Word`. By using `Int#` the users can easily convert unboxed result into a boxed value, without need to use `word2Int#` and `int2word#` primops. 
     226 
     227  * Unlike C, `2#` or `-3#` don't represent a Boolean value.  More concretely, you can use `tagToEnum#` to convert one of these `Int#` values to a `Bool`, but `tagToEnum#` does no error checking, so it would be Very Very Bad to call it on `2#`. Our plan is to provide safe `isTrue#` and `isFalse#` functions, which will check whether its `Int#` parameter is a valid representation of `True` (i.e. it is `1#`) or `False` (i.e. it is `0#`). This is not possible at the moment due to deficiency in the code generator (see #8326), but we do provide `isTrue#` function for you to use (defined in `GHC.Types`, re-exported by `GHC.Base` and `GHC.Exts`). Currently it is an alias to `tagToEnum#` and is therefore unsafe, but once we solve #8326 we will turn it into a safe function. 
     228 
     229  * As a small side-task, four new logical bitwise primops have been implemented: `andI#`, `orI#`, `xorI#` and `negI#` (#7689). These operate on values of type `Int#`. Earlier we only had bitwise logical primops operating on values of type `Word#`. 
     230 
     231  * Functions for comparing values of `Integer` type are not primops from technical point of view, because they are implemented in Haskell (in case of integer-gmp also with FFI), but they pretend to be ones. There are six primops for comparing `Integer` values: 
    267232{{{ 
    268233eqInteger#  :: Integer -> Integer -> Int# 
     
    273238geInteger#  :: Integer -> Integer -> Int# 
    274239}}} 
    275 Each of these functions has a wrapper that calls `tagToEnum#` and returns a `Bool`. These wrappers are: `eqInteger`, `neqInteger`, `leInteger`, `ltInteger`, `gtInteger` and `geInteger`. 
    276  
    277   * Six primops are an exception to the rules above: `sameMutableArray#`, `sameMutableByteArray#`, `sameMutableArrayArray#`, `sameMutVar#`, `sameMVar#` and `sameTVar#`. Their names have remained the same as before and new wrappers created for them lack `#` at the end of their name. We made that decission because this naming feels more consistent and these primops are rarely used so we expect that they won't break a lot of existing code. 
    278  
    279   * Other libraries that were modified to work with the new primops are: base, ghc-prim and primitive. The only required modifications were imports of the GHC.!PrimWrappers module in modules that use the primops. 
     240Each of these functions has a wrapper that calls `isTrue#` and returns a `Bool`. These wrappers are: `eqInteger`, `neqInteger`, `leInteger`, `ltInteger`, `gtInteger` and `geInteger`. 
     241 
     242  * Other libraries that were modified to work with the new primops are: array, base, dph, ghc-prim, primitive and template-haskell. 
    280243 
    281244=== Benchmarks ===