[Haskell-cafe] Representing Hierarchies with Typeclasses

Rahul Muttineni rahulmutt at gmail.com
Wed Oct 5 05:22:51 UTC 2016


Taking inspiration from
http://stackoverflow.com/questions/24775080/how-to-establish-an-ordering-between-types-in-haskell,
this comes out to be the solution:
---
type family Super (a :: *) :: *

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

instances Extends a a
instance {-# INCOHERENT #-} (Super a ~ b, Extends b c) => Extends a c
---
But this solution does rely on UndecidableInstances, but since it works
it's fine.


On Wed, Oct 5, 2016 at 10:01 AM, Rahul Muttineni <rahulmutt at gmail.com>
wrote:

> 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.
>
> Thanks,
> Rahul Muttineni
>



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


More information about the Haskell-Cafe mailing list