[Haskell-cafe] Representing Hierarchies with Typeclasses

Rahul Muttineni rahulmutt at gmail.com
Wed Oct 5 04:31:05 UTC 2016

Hi cafe,

I want to embed Java class hierarchies in Haskell, but I am unable to find
a clean way to do it.

Assume the class hierarchy C derives from B derives from A.

Partial Solution:
type family Super (a :: *) :: *

class Extends a b where
  supercast :: a -> b

instance {-# INCOHERENT #-} (Class a, Class c, Super a ~ b, Super b ~ c) =>
Extends a c where

data A
data B
data C

type instance Super C = B
type instance Super B = A
instance Extends C B
instance Extends B A

This is fine and is successfully able to infer Extends C A, but it's
redundant and cannot infer that Extends D A if we let Super D = C. Is there
a way to only specify the parent-child relationship once and get GHC to
infer the entire hierarchy without the use of UndecidableInstances?  Other
solutions I've tried which avoid redundancy cause an infinite loop in the
context reduction step of GHC.

Rahul Muttineni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20161005/05461776/attachment.html>

More information about the Haskell-Cafe mailing list