Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: How do I perform things inside a Monad? (Daniel Trstenjak)
2. Re: interface/abstract class: what is the haskell way?
(Mateusz Kowalczyk)
3. Re: interface/abstract class: what is the haskell way?
(Daniel Trstenjak)
4. Mixed typeclasses (Mateusz Neumann)
5. Re: interface/abstract class: what is the haskell way?
(Emmanuel Touzery)
6. Re: interface/abstract class: what is the haskell way?
(Emmanuel Touzery)
----------------------------------------------------------------------
Message: 1
Date: Fri, 8 Feb 2013 12:01:04 +0100
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] How do I perform things inside a
Monad?
To: [email protected]
Message-ID: <20130208110104.GA11149@machine>
Content-Type: text/plain; charset=us-ascii
Hi Martin,
On Fri, Feb 08, 2013 at 07:18:38AM +0100, Martin Drautzburg wrote:
> The thing is, I tried to split this monster up into several functions. For
> this, my functions got quite a number of parameters. Passing bar baz to the
> toplevel function would add even more parameters.
You might use the where clause:
foo bar baz = withFoo doFoo
where
doFoo foo = do
blub
withOtherThing doOtherThing
doOtherThing thing = do
blah
something bar baz
Greetings,
Daniel
------------------------------
Message: 2
Date: Fri, 08 Feb 2013 10:58:51 +0000
From: Mateusz Kowalczyk <[email protected]>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
haskell way?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Greetings,
I have recently asked for a difference between an interface as we know
it from OOP and a type class in Haskell. Although it's not an answer to
your question, you might find it useful. You can find the conversation
archived on gmane at [1]
[1] - http://comments.gmane.org/gmane.comp.lang.haskell.beginners/11341
On 08/02/13 09:50, Emmanuel Touzery wrote:
> Hello,
>
> i wrote two programs in haskell which have the same problem: they
> define a common datatype (let's say Event for instance), and they have
> several modules, each one importing a list of Event from a specific data
> source.
>
> So all these modules have a similar api:
>
> getEvents :: <params> -> IO [Event]
>
> And maybe a couple extra functions, more or less the same for each module.
>
> In OO, I would make a base class, like EventProvider, with a couple
> abstract methods and in the main class of my app, I would have a list of
> EventProvider and loop over them. That way to add a new EventProvider, I
> would just add the import and an element in that list.
>
> Currently in haskell I duplicate the function calls for each provider.
> And because there is no interface constraint, each module has a slightly
> different API.
>
> The "obvious" way to do in haskell what I would do in OO would be
> through type classes. However I realize type classes are not quite
> interfaces. I'm wondering what would be the "haskell way" to solve this
> problem?
>
> For sure type classes do the job. But is it the idiomatic way of
> solving this problem?
>
> Thank you!
>
> Emmanuel
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Fri, 8 Feb 2013 12:43:50 +0100
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] interface/abstract class: what is the
haskell way?
To: [email protected]
Message-ID: <20130208114350.GA12726@machine>
Content-Type: text/plain; charset=us-ascii
Hi Emmanuel,
On Fri, Feb 08, 2013 at 10:50:30AM +0100, Emmanuel Touzery wrote:
> The "obvious" way to do in haskell what I would do in OO would be through
> type classes. However I realize type classes are not quite interfaces. I'm
> wondering what would be the "haskell way" to solve this problem?
>
> For sure type classes do the job. But is it the idiomatic way of solving
> this problem?
The problem is, that without the use of extensions it's not possible to have
something like:
class EventProvider a where
events :: a -> IO [Event]
instance EventProvider Prov1 where ...
instance EventProvider Prov2 where ...
-- won't compile, because Prov1 and Prov2 have different types
providers :: EventProvider a => [a]
providers = [Prov1, Prov2]
To express something like this you need existential quantification:
{-# LANGUAGE ExistentialQuantification #-}
data AnyProvider = forall a. (EventProvider a) => AnyProvider a
providers :: [AnyProvider]
providers = [AnyProvider Prov1, AnyProvider Prov2]
But after trying ExistentialQuantification I got the impression, that
it just doesn't fit nicely into the language, that you can get quite
fast to a point where your head explodes by looking at the type errors ;).
So, like others already said (thanks Oleg ;), a record of functions can get you
quite far.
In your case youd could have something like:
data EventProvider = EventProvider {events = IO [Event]}
mkProv1 prov1 = EventProvider {events = do
-- read from prov1
}
mkProv2 prov2 = EventProvider {events = do
-- read from prov2
}
Greetings,
Daniel
------------------------------
Message: 4
Date: Fri, 8 Feb 2013 13:32:24 +0100
From: Mateusz Neumann <[email protected]>
Subject: [Haskell-beginners] Mixed typeclasses
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
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?
--
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/3b197522/attachment-0001.pgp>
------------------------------
Message: 5
Date: Fri, 8 Feb 2013 13:46:20 +0100
From: Emmanuel Touzery <[email protected]>
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 <[email protected]>
Message-ID:
<CAC42Re=cH5JJf=kqWsCqnwUp-oeGUVFbRfQKc4_o9=kt1Uj=_...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thank you; I did read that thread, and it helped me better grasp
typeclasses. However it confused me even more as to which is the most
idiomatic solution in haskell for my problem. This thread is partly the
reason why I asked this time ;-)
On Fri, Feb 8, 2013 at 11:58 AM, Mateusz Kowalczyk
<[email protected]>wrote:
> Greetings,
>
> I have recently asked for a difference between an interface as we know
> it from OOP and a type class in Haskell. Although it's not an answer to
> your question, you might find it useful. You can find the conversation
> archived on gmane at [1]
>
> [1] - http://comments.gmane.org/gmane.comp.lang.haskell.beginners/11341
>
> On 08/02/13 09:50, Emmanuel Touzery wrote:
> > Hello,
> >
> > i wrote two programs in haskell which have the same problem: they
> > define a common datatype (let's say Event for instance), and they have
> > several modules, each one importing a list of Event from a specific data
> > source.
> >
> > So all these modules have a similar api:
> >
> > getEvents :: <params> -> IO [Event]
> >
> > And maybe a couple extra functions, more or less the same for each
> module.
> >
> > In OO, I would make a base class, like EventProvider, with a couple
> > abstract methods and in the main class of my app, I would have a list of
> > EventProvider and loop over them. That way to add a new EventProvider, I
> > would just add the import and an element in that list.
> >
> > Currently in haskell I duplicate the function calls for each provider.
> > And because there is no interface constraint, each module has a slightly
> > different API.
> >
> > The "obvious" way to do in haskell what I would do in OO would be
> > through type classes. However I realize type classes are not quite
> > interfaces. I'm wondering what would be the "haskell way" to solve this
> > problem?
> >
> > For sure type classes do the job. But is it the idiomatic way of
> > solving this problem?
> >
> > Thank you!
> >
> > Emmanuel
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130208/3a5fe214/attachment-0001.htm>
------------------------------
Message: 6
Date: Fri, 8 Feb 2013 13:51:26 +0100
From: Emmanuel Touzery <[email protected]>
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 <[email protected]>
Message-ID:
<cac42renaac0xh6k4nogmoru-hqtvxy0cpvqngkd5sejbn22...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thank you. I think a record of functions is a nice way.
I would write the implementation of the record in each module. I would have
this record to be the outside API to my module, all other functions would
be hidden.
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.
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?
Otherwise which Oleg as you talking about, maybe I would read that original
post too.
Thank you!
Emmanuel
On Fri, Feb 8, 2013 at 12:43 PM, Daniel Trstenjak <
[email protected]> wrote:
>
> Hi Emmanuel,
>
> On Fri, Feb 08, 2013 at 10:50:30AM +0100, Emmanuel Touzery wrote:
> > The "obvious" way to do in haskell what I would do in OO would be
> through
> > type classes. However I realize type classes are not quite interfaces.
> I'm
> > wondering what would be the "haskell way" to solve this problem?
> >
> > For sure type classes do the job. But is it the idiomatic way of solving
> > this problem?
>
> The problem is, that without the use of extensions it's not possible to
> have something like:
>
> class EventProvider a where
> events :: a -> IO [Event]
>
> instance EventProvider Prov1 where ...
> instance EventProvider Prov2 where ...
>
> -- won't compile, because Prov1 and Prov2 have different types
> providers :: EventProvider a => [a]
> providers = [Prov1, Prov2]
>
>
> To express something like this you need existential quantification:
>
> {-# LANGUAGE ExistentialQuantification #-}
>
> data AnyProvider = forall a. (EventProvider a) => AnyProvider a
>
> providers :: [AnyProvider]
> providers = [AnyProvider Prov1, AnyProvider Prov2]
>
>
> But after trying ExistentialQuantification I got the impression, that
> it just doesn't fit nicely into the language, that you can get quite
> fast to a point where your head explodes by looking at the type errors ;).
>
> So, like others already said (thanks Oleg ;), a record of functions can
> get you quite far.
>
>
> In your case youd could have something like:
>
> data EventProvider = EventProvider {events = IO [Event]}
>
>
> mkProv1 prov1 = EventProvider {events = do
> -- read from prov1
> }
>
> mkProv2 prov2 = EventProvider {events = do
> -- read from prov2
> }
>
>
> Greetings,
> Daniel
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130208/8778a6f4/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 56, Issue 15
*****************************************