[GHC] #6135: Unboxed Booleans

GHC cvs-ghc at haskell.org
Thu Jun 7 14:15:31 CEST 2012


#6135: Unboxed Booleans
---------------------------------+------------------------------------------
    Reporter:  benl              |       Owner:                  
        Type:  feature request   |      Status:  new             
    Priority:  normal            |   Milestone:                  
   Component:  Compiler          |     Version:  7.4.1           
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by simonpj):

  * difficulty:  => Unknown


Comment:

 Good point.  I had not fully grokked that reason for having unboxed
 `Bool#`.  See also #605.

 However I don't think it would be a good plan to define
 {{{
 data Bool = B# Bool#
 }}}
 For one thing, the definition of `Bool` is in the language spec.  Well I
 suppose we could define
 {{{
 fromBool :: Bool -> Bool#
 toBool   :: Bool# -> Bool
 }}}
 (Actually these already exist, more or less, in the form of `tagToEnum#`
 and `dataToTag#`.)

 I suppose we could then define `not`, `(&&)` etc thus:
 {{{
 not :: Bool -> Bool
 not b = toBool (not# (fromBool b))

 (&&) :: Bool -> Bool -> Bool
 a && b = toBool (and# (fromBool a) (fromBool b))
 }}}
 What would happen to optimisations?  As thing stand we get
 {{{
 not (not x)

 = { inline not, twice }
   case (case x of { T -> F; F -> T }) of { T -> F; F -> T }

 = { case of case transformation }
   case x of
      T -> case F of { T -> F; F -> T }
      F -> case T of { T -> F; F -> T }

 = { simplify cases }
   case x of
      T -> T
      F -> F

 = { case of identity }
   x
 }}}
 (And then we simplify to just `x`, but that's a separate matter.)  To get
 this behaviour with the new stuff we would presumably say:
 {{{
 not (not x)

 = { inline not, twice }
   toBool (not# (fromBool (toBool (not# (fromBool x)))))

 = { RULE fromBool (toBool v) = v }
   toBool (not# (not# (fromBool x)))

 = { RULE not# (not# v) = v }
   toBool (fromBool x)

 = { RULE fromBool (toBool v) = v }
   x
 }}}
 If anything this seems simpler.  We'd also need constant folding stuff
 like
 {{{
    case x of
      True -> ....(fromBool x)....
 ===>
    case x of
      True -> ....True#....
 }}}
 but that should not be hard.

 I was not optimistic when I started writing this, but now I've got this
 far it does actually look plausible.  It is certainly true that
 conditionals with a lot of conditions can generate an absolute rats-nest
 of join points -- and that in turn can inhibt inlining.  So the change
 would help that too.

 I suppose that `Bool#` might be better spelt `Word1#`.

 Can anyone think of any other wrinkles to consider?

 Simon

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/6135#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler



More information about the Glasgow-haskell-bugs mailing list