[Haskell-cafe] Factoring into type classes

Patai Gergely patai_gergely at fastmail.fm
Mon Jan 19 12:40:04 EST 2009


> As a side curiosity, I would love to see an example of any data structure
> which has more than one Functor instance.  Especially those which have
> more than one useful functor instance.

data Record a b = R { field1 :: a, field2 :: b }

If I want to use fmap to transform either field, I have to declare the
type to have the corresponding type variable at the end, i.e. choosing
"Record a b" or "Record b a" is already a design decision, and it is
driven by the standard Functor class in this case. I can define custom
functions fmap1 and fmap2 manually, but then I don't get the advantages
of overloading, like fmapping over a data structure containing my
records.

Now I understand that I can't get everything, and my question is mainly
what to do when such a dilemma comes up. Those who have already
encountered such a dilemma: how did it come up and what did you do to
solve it?

Gergely

-- 
http://www.fastmail.fm - Same, same, but different...



More information about the Haskell-Cafe mailing list