10
$\begingroup$

As far as I know, a Haskell function with type classes constraints is internally compiled to a function with additional arguments that receive dictionaries with the necessary implementations of each particular type class.

  • Are there any other possibilities how compile type classes?
  • If so, what are their (dis)advantages?
  • And what compilers use them?
$\endgroup$
5
  • 4
    $\begingroup$ There's quite a bit of literature on the subject. Maybe start with Implementing Type Classes by J. Peterson, M. Jones. You could also look at Type Classes as Objects and Implicits by Oliveira et al, which talks about this in the context of Scala, but has quite a large section on related work. $\endgroup$ Commented Sep 6, 2012 at 15:34
  • 1
    $\begingroup$ I found a link to the paper: Implementing Type Classes. John Peterson and Mark P. Jones. $\endgroup$
    – Petr
    Commented Sep 6, 2012 at 16:37
  • 1
    $\begingroup$ @MartinBerger make this an answer ? $\endgroup$ Commented Sep 6, 2012 at 20:07
  • $\begingroup$ The other one can be found here. $\endgroup$ Commented Sep 6, 2012 at 20:08
  • $\begingroup$ @Suresh Venkat, I didn't want to make it an answer because I am not sure these two are the best, or even good answers. It's been a while since I looked at implementations of functional languages. Maybe some of the resident FP bods can chime in. $\endgroup$ Commented Sep 6, 2012 at 20:12

1 Answer 1

8
$\begingroup$

JHC uses a different approach. The compiler's intermediate language is a dependently typed lambda-calculus where there is no distinction between types and values. JHC therefore can perform a case analysis on the type parameter of a function and call the correct overloaded function directly.

The JHC website goes into some depth on the implementation, as well as its advantages over the standard dictionary passing implementation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.