#14752 closed bug (fixed)

Unboxed sums documentation looks wrong

Reported by: dfeuer Owned by:
Priority: high Milestone: 8.4.1
Component: Documentation Version: 8.2.2
Keywords: UnboxedSums Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D4379
Wiki Page:

Description

According to the documentation,

In the case of enumeration types (like Bool), the unboxed sum layout only has an Int32 field (i.e. the whole thing is represented by an integer).

This does not actually seem to be true.

right :: Bool -> (# (# #) | Bool #)
right x = (# | x #)

test :: (# (# #) | Bool #) -> Bool
test (# (# #) | #) = False
test (# | _ #) -> True

If the documentation were correct, I would expect test (right undefined) to be undefined, but it is True.

Either we should fix the documentation to match reality or the reality to match the documentation.

Change History (7)

comment:1 Changed 20 months ago by osa1

If you generate STG and Cmm for this program:

{-# LANGUAGE UnboxedSums, UnboxedTuples, MagicHash #-}

module Lib where

type UnboxedBool = (# (# #) | (# #) #)

unboxBool :: Bool -> UnboxedBool
unboxBool True = (# (# #) | #)
unboxBool False = (# | (# #) #)

boxBool :: UnboxedBool -> Bool
boxBool (# (# #) | #) = True
boxBool (# | (# #) #) = False

you'll see that UnboxedBool is clearly represented as single integer.

In your program, I don't understand why you expect your Bool to be unboxed. You can put bottom values in unboxed tuples/sums so everything seems fine here.

comment:2 Changed 20 months ago by dfeuer

osa1, everything is fine except the documentation I quoted. Perhaps it should refer to some type Bool# = ... instead of to Bool?

comment:3 Changed 20 months ago by osa1

Ah, right. I have no objections to improving the documentation. Maybe we should say something like "Unboxed sums with no fields are represented as a single Int32". I agree that using Bool there may be confusing.

Last edited 20 months ago by osa1 (previous) (diff)

comment:4 Changed 20 months ago by dfeuer

Differential Rev(s): Phab:D4379
Status: newpatch

osa1, could you please review my patch and make sure I didn't get anything terribly wrong?

comment:5 Changed 20 months ago by David Feuer <David.Feuer@…>

In d987f71/ghc:

Improve unboxed sum documentation

* Clarify the representation of sums without fields.
* Try to improve language, clarity, and examples.

Fixes #14752

Reviewers: osa1, bgamari

Reviewed By: osa1

Subscribers: rwbarton, thomie, carter

GHC Trac Issues: #14752

Differential Revision: https://phabricator.haskell.org/D4379

comment:6 Changed 20 months ago by dfeuer

Status: patchmerge

comment:7 Changed 19 months ago by bgamari

Milestone: 8.4.28.4.1
Resolution: fixed
Status: mergeclosed
Note: See TracTickets for help on using tickets.