[Haskell-cafe] Restrictions on associated types for classes

Miguel Mitrofanov miguelimo38 at yandex.ru
Thu Dec 17 09:18:16 EST 2009


Along the same lines:

{-# LANGUAGE GADTs, TypeFamilies #-}
module Assoc where
data EqD k where EqD :: Eq k => EqD k
class MyClass k where
    type AssociatedType k :: *
    evidence :: k -> EqD (AssociatedType k)
instance MyClass () where
     type AssociatedType () = Integer
     evidence _ = EqD
eq :: MyClass k => k -> AssociatedType k -> AssociatedType k -> Bool
-- eq k k1 k2 = k1 == k2 -- doesn't work
eq k k1 k2 = case evidence k of EqD -> k1 == k2 -- works fine

On 17 Dec 2009, at 17:10, Miguel Mitrofanov wrote:

> {-# LANGUAGE GADTs, TypeFamilies #-}
> module Assoc where
> data EqD k where EqD :: Eq k => EqD k
> class MyClass k where
>    data AssociatedType k :: *
>    evidence :: AssociatedType k -> EqD (AssociatedType k)
> eq :: MyClass k => AssociatedType k -> AssociatedType k -> Bool
> -- eq k1 k2 = k1 == k2 -- doesn't work
> eq k1 k2 = case evidence k1 of EqD -> k1 == k2 -- works fine



More information about the Haskell-Cafe mailing list