[Haskell-beginners] Mixed typeclasses
Mateusz Neumann
mateusz at neumanny.net
Fri Feb 8 21:21:55 CET 2013
On Fri, 08 Feb 2013 15:23:51 +0100
Nikita Danilenko <nda at informatik.uni-kiel.de> wrote:
> Hi,
>
> what you are trying to accomplish is to derive a less specific
> instance from a more specific one (e.g. trying to derive Eq a, when
> you have Ord a). While this might seem natural from a mathematical
> point of view, in Haskell it is not. There are several possible
> solutions, one of those is given by the error message itself (turn on
> the language extension FlexibleInstances). Different instances of
> this general problem are discussed in [1], [2], [3].
>
> Now as for your problem itself: You define a new type class, which
> already requires an Eq constraint. Then you try to derive an Eq
> context for certain types from the fact that they belong to the type
> class ThatsMyProblem. The context of ThatsMyProblem already provides
> an Eq context by definition and conceptually it should not be
> possible to define (==) in terms of "fromMyProblem", because the
> concept of "fromMyProblem" requires a definition of (==).
>
> Regards,
>
> Nikita
>
> [1]
> http://stackoverflow.com/questions/8633470/illegal-instance-declaration-when-declaring-instance-of-isstring
>
> [2]
> http://stackoverflow.com/questions/4826630/type-class-problem-concerning-flexibleinstances
>
> [3]
> http://connectionrequired.com/blog/2009/07/my-first-introduction-to-haskell-extensions-flexibleinstances/
>
> On 02/08/13 13:32, Mateusz Neumann wrote:
> > Hi
> >
> > I came across a problem, which I deeply believe, might be solved in
> > Haskell in much nicer way (than I did it). I have:
> >
> > class (Eq a) => ThatsMyProblem a where
> > fromMyProblem :: a -> Int
> > toMyProblem :: Int -> a
> >
> > data MyType1
> > = MyType1_1
> > | MyType1_2
> > | MyType1_3 Int
> > deriving (Show)
> > instance Eq MyType1 where
> > (==) a b = fromMyProblem a == fromMyProblem b
> > instance ThatsMyProblem MyType1 where
> > [...]
> >
> > data MyType2
> > = MyType2_1
> > | MyType2_2 Int
> > deriving (Show)
> > instance Eq MyType2 where
> > (==) a b = fromMyProblem a == fromMyProblem b
> > instance ThatsMyProblem MyType2 where
> > [...]
> >
> > data MyType3
> > = MyType3_1
> > | MyType3_2
> > | MyType3_3 Int
> > deriving (Show)
> > instance Eq MyType3 where
> > (==) a b = fromMyProblem a == fromMyProblem b
> > instance ThatsMyProblem MyType3 where
> > [...]
> >
> >
> > I would very much like to create one single instance like this:
> >
> > instance (FutureVal a) => Eq a where
> > (==) x y = fromFVal x == fromFVal y
> >
> > but that does not seem to work, as I get an error stating "Illegal
> > instance declaration for `Eq a' (All instance types must be of the
> > form (T a1 ... an) where a1 ... an are *distinct type variables*,
> > and each type variable appears at most once in the instance head.
> > Use -XFlexibleInstances if you want to disable this.) In the
> > instance declaration for `Eq a'"
> >
> > Could you please point me out my mistake and/or direct me to some
> > documentation?
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
>
Thanks a lot for your explanation and links. I think, I would stick to
'strict' ghc (without language extensions). But maybe some redesign in
my code would not be that bad idea after all :)
--
Mateusz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 230 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130208/6eeb49b7/attachment.pgp>
More information about the Beginners
mailing list