[Haskell-cafe] Playing with ATs again

Alexey Khudyakov alexey.skladnoy at gmail.com
Tue Aug 3 13:45:26 EDT 2010


On 03.08.2010 21:25, Andrew Coppin wrote:
> Now suppose that instead of a container type with an associated element
> type, what we want is several pairs of types having a one-to-one
> relationship, and we want to be able to traverse that relationship in
> either direction. What's the best way to do that?
>
> (I tried just having two mirror image classes like the one above. But
> that doesn't work; there's no guarantee that going one way and then
> going back the other way will definitely take you back to where you
> started. I tried to make that a constraint, and got an error message
> that basically says "this feature is not implemented". Great. Still,
> there's probably a better way to do this.)
>
That's possible with multiparameter type classes and fundeps. Without 
any further words I'm pasting code snippet:

 > class Relator a b | a -> b, b -> a where
 >   to   :: a -> b
 >   from :: b -> a
 >
 > data Knight = Knight deriving Show
 > data Dragon = Dragon deriving Show
 >
 > instance Relator Knight Dragon where
 >   to   _ = Dragon
 >   from _ = Knight

I'm not sure that it's what you want.


More information about the Haskell-Cafe mailing list