Skip to content

no way to talk about unpacking sum types / unpacking tuples

Suppose I have the following Haskell module:

{-# LANGUAGE BangPatterns #-}

-- strict 'Maybe'
data StrictMaybe a = SNothing | SJust !a

-- lazy 'Maybe'
data Maybe a = Nothing | Just a

data BoxedInner = BoxedInner
  !Int
  !(Maybe Int)

data StillBoxedInner = StillBoxedInner
  !Int
  !(StrictMaybe Int)

mBoxed :: BoxedInner
mBoxed = BoxedInner 0 (Just 0)

mWantThisToUnbox :: StillBoxedInner
mWantThisToUnbox = StillBoxedInner 1 (Just 1)

the 'StrictMaybe' can unbox the first 'Int' into 1#, but the 1 inside of the just cannot unbox. When compiled to core with -O2 what I'd want to see is something like:

mWantThisToUnbox = StillBoxedInner 0# (# | 1# #)

(where (# (# #) | a #) is an unboxed sum type with two constructors, one nullary and one unary)

but instead the Int inside 'SJust' remains boxed.

Even with something like the following:

{-# LANGUAGE UnboxedSums #-}
data UMaybe a = (# (# #) | a #)

something like 'UMaybe Int' would still result in 'Int' being boxed.

This same thing occurs with unpacked tuples, consider something like:

data StrictTuple = StrictTuple
  !Int
  !(Int, Int)

Anything inside the (Int, Int) tuple will not unbox, despite having a bang pattern there.

Trac metadata
Trac field Value
Version 8.4.3
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC andrewthad, osa1
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information