Specialization with Constraints

前端 未结 1 614
迷失自我
迷失自我 2020-12-12 23:31

I\'m having problems getting GHC to specialize a function with a class constraint. I have a minimal example of my problem here: Foo.hs and Main.hs. The two files compile (G

相关标签:
1条回答
  • 2020-12-12 23:54

    GHC also gives an option to SPECIALIZE a type-class instance declaration. I tried this with the (expanded) code of Foo.hs, by putting the following:

    instance (Num r, V.Vector v r, Factored m r) => Num (VT v m r) where 
        {-# SPECIALIZE instance ( Factored m Int => Num (VT U.Vector m Int)) #-}
        VT x + VT y = VT $ V.zipWith (+) x y
    

    This change, though, did not achieve the desired speedup. What did achieve that performance improvement was manually adding a specialized instance for the type VT U.Vector m Int with the same function definitions, as follows:

    instance (Factored m Int) => Num (VT U.Vector m Int) where 
        VT x + VT y = VT $ V.zipWith (+) x y
    

    This requires adding OverlappingInstances and FlexibleInstances in LANGUAGE.

    Interestingly, in the example program, the speedup obtained with the overlapping instance remains even if you remove every SPECIALIZE and INLINABLE pragma.

    0 讨论(0)
提交回复
热议问题