Opened 5 years ago

Closed 5 years ago

#7112 closed bug (wontfix)

No inlining in the presence of non-instantiated phantom type

Reported by: dreixel Owned by:
Priority: low Milestone:
Component: Compiler Version: 7.4.1
Keywords: Cc: nfrisby
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Consider the following code:

data U a = U

g :: [U a]
g = [U]

f :: [Int]
f = map (\U -> 2) g

Compiled with -O1, g does not get inlined, and f stays unchanged. Inline pragmas don't seem to help. However, if we change the type signature of g to instantiate the phantom type parameter, such as with g :: [U ()], then f is nicely optimised to become [2]. I believe this is a bug, and we'd like f to be simplified even when g has its most general type signature.

Change History (3)

comment:1 Changed 5 years ago by simonpj

difficulty: Unknown
Priority: normallow

This is a bit perplexing, I agree. Here is what is happening. In the monomorphic case we get

g :: [U ()]
g = Cons (U ()) 
         (U ())
         (Nil (U ())

f = map ... g

So the rule for 'map' that cancels with a 'Cons' can "see" the Cons in g's RHS. But in the polymorphic case we have:

g :: forall a. [U a]
g = /\a. Cons (U a) 
              (U a)
              (Nil (U a)

f = map ... (g ())

And now the fact that 'g' is a 'Cons' is not so obvious any more. Inlining g is not a great plan because doing so means the Cons will be dynamically allocated rather than statically allocated.

Moreover, the RULES for map (actually they are for foldr) are very specific (see GHC.Base):

"foldr/single"  forall k z x. foldr k z [x] = k x z
"foldr/nil"     forall k z.   foldr k z []  = z 

even a list of length 2 would't optimise. (There's a reason for this; if the list is of length 1000 we don't want unroll the map.)

So although it's odd I don't think it's important enough to burn cycles on. Or do you havea compelling reason in some larger context?

comment:2 Changed 5 years ago by nfrisby

Cc: nfrisby added

comment:3 Changed 5 years ago by igloo

Resolution: wontfix
Status: newclosed

If this isn't important enough to spend time on, then let's just close the ticket.

Note: See TracTickets for help on using tickets.