Opened 8 years ago

Last modified 4 years ago

#5326 new bug

Polymorphic instances aren't automatically specialised

Reported by: reinerp Owned by: simonpj
Priority: low Milestone:
Component: Compiler Version: 7.0.3
Keywords: specialisation Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Related to #255. Given (roughly) the example from that ticket:

f :: (Storable a, Eq a) => T a

g :: T (Ptr a)
g = f

we find that g is not specialised. Adding a SPECIALISE pragma fixes this, but it ought not to be necessary.

The following module is a complete example:

module C where

class C a where f :: a -> a

newtype Id a = Id a
instance C (Id a) where f = id

g :: C a => Int -> a -> a
g 0 a = a
g n a = g (n-1) (f a)

h :: Int -> Id a -> Id a
h = g

j :: Int -> Id Int -> Id Int
j = g

We find that h passes a dictionary to g:

C.h =
  \ (@ a_alq) (w_smq :: Int) (w1_smu :: C.Id a_alq) ->
    case w_smq of _ { I# ww_sms ->
    C.$wg @ (C.Id a_alq) (C.$fCId @ a_alq) ww_sms w1_smu
    }

whereas j is specialised as desired, getting its own worker and no dictionaries:

C.j =
  \ (w_smg :: Int) (w1_smk :: C.Id Int) ->
    case w_smg of _ { I# ww_smi ->
    (C.$wj ww_smi w1_smk)
    `cast` (sym (C.NTCo:Id Int)
            :: Int ~ C.Id Int)
    }

If we add

{-# SPECIALISE g :: Int -> Id a -> Id a #-}

then h is specialised as desired.

Change History (7)

comment:1 Changed 8 years ago by igloo

Milestone: 7.4.1
Owner: set to simonpj

Thanks for the report.

comment:2 Changed 8 years ago by igloo

Milestone: 7.4.17.6.1
Priority: normallow

comment:3 Changed 7 years ago by igloo

Milestone: 7.6.17.6.2

comment:4 Changed 5 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:5 Changed 5 years ago by thoughtpolice

Milestone: 7.10.17.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:6 Changed 4 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:7 Changed 4 years ago by thomie

Milestone: 8.0.1
Note: See TracTickets for help on using tickets.