[Haskell-cafe] Re: Compiler backend question

John Meacham john at repetae.net
Mon Jan 7 16:21:28 EST 2008


On Mon, Jan 07, 2008 at 09:27:17PM +0100, Peter Verswyvelen wrote:
> If your compiler (pretty amazing job btw) does whole program
> optimization, can it remove the dictionary (aka v-table in C/C++
> parlance?) overhead of type classes? Because if I understand it
> correctly, unless one uses existential types, a dictionary can be
> compiled away since the type in question is known at compile time?

Both jhc and ghc can completely remove the dictionary parameter whenever
the type is known at compile time because an implicit SPECIALIZATION
rule is generated that gets triggered whenever an overloaded function is
called on a known type.

Jhc can do a bit more though when the type is not fully known at compile
time. it has a fairly interesting implementation of type classes, rather
than turning each type class parameter into a dictionary of functions,
it passes a reification of the type itself as a single argument whenever
type class arguments are present (and used). This means that the
generated code looks something like

(+) :: (a::*) -> a -> a -> a
(+) t x y = case t of 
        Int -> primIntAdd x y
        Integer -> primIntegerAdd x y
        ...

standard optimizations then can apply to eliminating and coalescing the
type scrutinizations.


        John


-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list