Skip to content

Why fay does not support type classes

chrisdone edited this page Apr 22, 2013 · 3 revisions

So type-classes are implemented with a dictionary informed by the type system. See the original type-classes paper for a fuller description.

When you write:

class Read a where
  read :: String -> a
instance Read Int where
  read "42" = 42
foo = read "42" * 2

The naive/proper translation is to something like:

data Read a = Read
  { read :: String -> a }
inst_Read_Int = Read
  { read = \"42" -> 42 }
foo = read inst_Read_Int "42" * 2

The step that we cannot perform is the third step, translating read "42" to read inst_Read_Int "42", because we do not know what type read "42" should be return, because we don't have a type checker/type information.

However, some type-classes don't need a dictionary because they have no methods, and so are just used as constraints to add extra safety to polymorphic types. Those you can use.