Is class dictionary passing the result of an implementation choice or theoritical necessity?

Here is another more theoretical problem which shows that typeclasses cannot always be compiled via dictionary passing. The setting requires subtyping, so it isn’t directly applicable to Haskell, but it would be applicable to a language based on algebraic subtyping (e.g. https://dl.acm.org/doi/10.1145/3093333.3009882 or https://dl.acm.org/doi/10.1145/3409006).
In that setting, an if-then else expression like the following:

if b then (5 :: Int) else "hello"

has the inferred type Int \/ String. And if we read typeclasses as predicates on types, as in the original articles by Blott, Wadler and Mark P Jones, then this type should satisfy the covariant predicate Show. So we should be able to write:

show (if b then (5 :: Int) else "hello")

But I do not immediately see how we would compile this using dictionary passing, since the information which typeclass instance to use is only available at runtime.

2 Likes