173 | | It seems that the best solution to the problem would be implementing second of the above workarounds as a separate primop. Alternatively we can implement primitive bitwise `or`, `and` and `xor` that work on `Int`s instead of `Word`s and define new logical operators using bitwise operators in one of the libraries. |
| 175 | A good beginning would be to implementing second of the above workarounds as a primop. Then we need to create primops that return unboxed values instead of a thunk. The big question is should an unboxed version of Bool be introduced into the language? |
| 176 | === First approach === |
| 177 | |
| 178 | Treat `Bool` as a boxed version of primitive `Bool#`. `True` would be equivalent of `B# True#`, `False` of `B# False#`: |
| 179 | {{{ |
| 180 | data Bool = B# True# | B# False# |
| 181 | |
| 182 | -- B# :: Bool# -> Bool |
| 183 | }}} |
| 184 | |
| 185 | Not sure if this can be considered equivalent to what the Haskell Report says about Bool. We need to ensure that `Bool#` is populated only by `True#` and `False#` and that these two are translated to `1#` and `0#` in the Core. It should be **impossible** to write such a function at Haskell level: |
| 186 | |
| 187 | {{{ |
| 188 | g :: Bool -> Int -> Int |
| 189 | g (B# b) (I# i) = I# (b + i) |
| 190 | }}} |
| 191 | |
| 192 | This approach might require one additional case expression to inspect the value of `Bool` at the Core level. For example: |
| 193 | |
| 194 | {{{ |
| 195 | f :: Int -> Int -> Int |
| 196 | f x y = if x > y |
| 197 | then x |
| 198 | else y |
| 199 | }}} |
| 200 | |
| 201 | would compile to: |
| 202 | |
| 203 | {{{ |
| 204 | case x of _ { I# xP -> |
| 205 | case y of _ { I# yP -> |
| 206 | case ># xP yP of _ { |
| 207 | B# bP -> case bP of _ { 1# -> e1; 0# -> e2 } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | }}} |
| 212 | |
| 213 | This would complicate Core a bit but it should be possible to compile such Core to exactly the same result as with normal `Bool`. This code assumes that `>#` has type `Int# -> Int# -> Bool`, but to truly avoid branching in the Core we need `.># :: Int# -> Int# -> Bool#` so that we get a primitive value that doesn't need to be inspected using case expression but can be directly used by primitive logical operators. |
| 214 | |
| 215 | === Second approach === |
| 216 | |
| 217 | Second approach assumes creating type `Bool#` that is independent of type `Bool`. Boxing and unboxing would have to be done explicitly via additional functions: |
| 218 | |
| 219 | {{{ |
| 220 | data Bool = True | False -- no changes here |
| 221 | |
| 222 | bBox :: Bool# -> Bool |
| 223 | bBox 1# = True |
| 224 | bBox 0# = False |
| 225 | |
| 226 | bUnbox :: Bool -> Bool# |
| 227 | bUnbox True = 1# |
| 228 | bUnbox False = 0# |
| 229 | }}} |
| 230 | |
| 231 | `Bool#` could not be implemented as an ADT because it is unlifted and unboxed, while ADT value constructors need to be boxed and lifted (see comments in [[GhcFile(compiler/types/TyCon.lhs)]]). There would need to be some magical way of ensuring that `Bool#` is populated only by `#0` and `1#` and that these values cannot be mixed with unboxed integers. Perhaps this could be done by preventing programmer from explicitly creating values of that type (can this be done?) and allow her only to use values returned from functions. |
| 232 | |
| 233 | Another problem with this approach is that it would introduce primitive logical operations `||#` and `&&#` with type `Int# -> Int# -> Int#` - it is questionable whether anyone would want such operations available to the programmer. I think it is desirable to have primitive logical operators of type `Bool# -> Bool# -> Bool#`. |