문제

The {-# UNPACK #-} pragma tells the compiler to eliminate redundant constructors. Quoting Haskell wiki:

For example, given this:

data T = T {-# UNPACK #-} !(Int,Float)

GHC will represent the type T like this:

data T = T Int Float

eliminating the tuple. This is commonly used to put unboxed Ints directly in a constructor:

data T = T {-# UNPACK #-} !Int

will be represented as

data T = T Int#

I was wondering, does this also work when the field to be unpacked is polymorphic? For example, if I define

data S' a   = S String {-# UNPACK #-} !a

type S1     = S' Int
newtype S2  = S2 (S' Int)

will functions that work with S1 or S2 be optimized?

도움이 되었습니까?

해결책

Quoting from Don's blog about unpacking

It does not make sense for polymorphic fields, since they have variable size, and the compiler does not (except in very specific circumstances) have access to information about what types are in use.

Also you can read Tibell's reply to mailing list.

Unfortunately unpacking doesn't work for polymorphic fields (the new warning for ineffective unpack pragmas in GHC head should warn about this) ...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top