Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  interface/abstract class: what is the haskell way?
      (Daniel Trstenjak)
   2. Re:  interface/abstract class: what is the        haskell way?
      (Emmanuel Touzery)
   3. Re:  Mixed typeclasses (Nikita Danilenko)
   4. Re:  interface/abstract class: what is the haskell way?
      (Daniel Trstenjak)
   5. Re:  Mixed typeclasses (Mateusz Neumann)
   6. Re:  interface/abstract class: what is the        haskell way?
      (Emmanuel Touzery)


----------------------------------------------------------------------

Message: 1
Date: Fri, 8 Feb 2013 14:24:02 +0100
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
        haskell way?
To: beginners@haskell.org
Message-ID: <20130208132402.GA20752@machine>
Content-Type: text/plain; charset=us-ascii


Hi Emmanuel,

On Fri, Feb 08, 2013 at 01:51:26PM +0100, Emmanuel Touzery wrote:
> But that way I must still have public getEventProvider() function which
> returns the record, that I call by convention without some compiler
> enforcement, which doesn't sound right.

In some way you have to tell your program the available providers. How
should a compiler enforce this?

> I think what I am trying to achieve is a very common problem and I maybe
> suggested a bit too strongly how I would code it in OO languages, maybe it
> should be arranged completely differently in idiomatic haskell?

I think that the 'record of functions' is quite idiomatic Haskell for
this use case.

> Otherwise which Oleg as you talking about, maybe I would read that original
> post too.

I had pretty much the same issue and Oleg gave pretty much the same answer.

And there's really only one Oleg ;): http://okmij.org/ftp/

There's no way you could miss him hanging around in Haskell land for some time.


Greetings,
Daniel



------------------------------

Message: 2
Date: Fri, 8 Feb 2013 14:31:58 +0100
From: Emmanuel Touzery <etouz...@gmail.com>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
        haskell way?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Message-ID:
        <CAC42Re=cAztO_CN=glu1fnrnuxuickoet5dcjeqbv2xocon...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

>
> On Fri, Feb 08, 2013 at 01:51:26PM +0100, Emmanuel Touzery wrote:
> > But that way I must still have public getEventProvider() function which
> > returns the record, that I call by convention without some compiler
> > enforcement, which doesn't sound right.
>
> In some way you have to tell your program the available providers. How
> should a compiler enforce this?
>

It's already pretty good like that but, in an OO language basically if you
write a new such "module" you know you must implement the interface, and
that's all you need to know: the type of the interface. Then the compiler
enforces all the names of methods and return and parameter types.
So all you need to know is the name of the interface.

Here there is an "interface", the record of functions. But each module must
basically define a function returning that record. Sure, the module must
implement that function, the same as you must implement it in OO languages,
but each module can also make up its mind for the name of the function.

I mean it's basically nitpicking at this point, and it's because I'm used
to one way. I'm just used that most of the time haskell is better in
(almost) every  way to OO languages, here I think it's maybe a bit less
good, that's all.

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130208/75f9923a/attachment-0001.htm>

------------------------------

Message: 3
Date: Fri, 08 Feb 2013 15:23:51 +0100
From: Nikita Danilenko <n...@informatik.uni-kiel.de>
Subject: Re: [Haskell-beginners] Mixed typeclasses
To: beginners@haskell.org
Message-ID: <51150a77.90...@informatik.uni-kiel.de>
Content-Type: text/plain; charset="iso-8859-1"

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@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130208/c477d359/attachment-0001.htm>

------------------------------

Message: 4
Date: Fri, 8 Feb 2013 15:46:50 +0100
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
        haskell way?
To: beginners@haskell.org
Message-ID: <20130208144650.GA22930@machine>
Content-Type: text/plain; charset=us-ascii


Hi Emmanuel,

On Fri, Feb 08, 2013 at 02:31:58PM +0100, Emmanuel Touzery wrote:
> It's already pretty good like that but, in an OO language basically if you
> write a new such "module" you know you must implement the interface, and
> that's all you need to know: the type of the interface. Then the compiler
> enforces all the names of methods and return and parameter types.
> So all you need to know is the name of the interface.

All the type enforcement is still done on the 'EventProvider', all you
need to know is the 'EventProvider' data type, so there's not really a
difference in this regard.

> Here there is an "interface", the record of functions. But each module must
> basically define a function returning that record. Sure, the module must
> implement that function, the same as you must implement it in OO languages,
> but each module can also make up its mind for the name of the function.

You could also name the classes implementing the interface in any way,
but will probably choose telling und helpful names.


Greetings,
Daniel



------------------------------

Message: 5
Date: Fri, 8 Feb 2013 21:21:55 +0100
From: Mateusz Neumann <mate...@neumanny.net>
Subject: Re: [Haskell-beginners] Mixed typeclasses
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Message-ID: <20130208212155.1d4fb926@dragonfly.localdomain>
Content-Type: text/plain; charset="us-ascii"

On Fri, 08 Feb 2013 15:23:51 +0100
Nikita Danilenko <n...@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@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-0001.pgp>

------------------------------

Message: 6
Date: Sat, 9 Feb 2013 08:39:43 +0100
From: Emmanuel Touzery <etouz...@gmail.com>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
        haskell way?
To: beginners@haskell.org
Message-ID:
        <CAC42Re=VGRAgND1TRQQKCK52AW9EXoRyzJZU53UyH=fsl3k...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

> > Here there is an "interface", the record of functions. But each module
must
> > basically define a function returning that record. Sure, the module must
> > implement that function, the same as you must implement it in OO
languages,
> > but each module can also make up its mind for the name of the function.
>
> You could also name the classes implementing the interface in any way,
> but will probably choose telling und helpful names.

Right, in an OO language i name the subclass, here the function. I was
thinking the way i was because i import those modules qualified so the
module name already gives me a description.

Thank you!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130209/73184f13/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 56, Issue 16
*****************************************

Reply via email to