Beginners Digest, Vol 56, Issue 14
Subject: Re: [Haskell-beginners] Can someone help to unnest this do cascade To: beginners@haskell.org, Martin Drautzburg martin.drautzb...@web.de Message-ID: op.wr6eyp08pz0...@zen5.arnhem.chello.nl Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes On Wed, 06 Feb 2013 22:31:05 +0100, Martin Drautzburg martin.drautzb...@web.de wrote: Can someone please walk me through it and possibly show ways to avoid the massive nesting. dtz = do SndSeq.withDefault SndSeq.Block $ \h - do Client.setName (h :: SndSeq.T SndSeq.DuplexMode) Haskell-Melody Port.withSimple h out (Port.caps [Port.capRead, Port.capSubsRead, Port.capWrite]) (Port.types [Port.typeMidiGeneric, Port.typeApplication]) $ \p - do Queue.with h $ \q - do c - Client.getId h let me = Addr.Cons c p conn - parseDestArgs h me [20:0] Queue.control h q Event.QueueStart Nothing Queue.control h q (Event.QueueTempo (Event.Tempo 1000)) Nothing return () I like to divide large functions into several smaller ones: dtz = SndSeq.withDefault SndSeq.Block f1 where f1 h = do Client.setName (h :: SndSeq.T SndSeq.DuplexMode) Haskell-Melody Port.withSimple h out (Port.caps [Port.capRead, Port.capSubsRead, Port.capWrite]) (Port.types [Port.typeMidiGeneric, Port.typeApplication]) (f2 h) f2 h p = Queue.with h (f3 h p) f3 h p q = do c - Client.getId h let me = Addr.Cons c p conn - parseDestArgs h me [20:0] Queue.control h q Event.QueueStart Nothing Queue.control h q (Event.QueueTempo (Event.Tempo 1000)) Nothing return () f1, f2 and f3 might be replaced with more meaningful names. The return () at the end can be removed; such things can be found with hlint[0]. Regards, Henk-Jan van Tuyl [0] http://hackage.haskell.org/package/hlint -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- -- Message: 8 Date: Fri, 8 Feb 2013 10:50:30 +0100 From: Emmanuel Touzery etouz...@gmail.com Subject: [Haskell-beginners] interface/abstract class: what is the haskell way? To: beginners@haskell.org beginners@haskell.org Message-ID: CAC42RemzdcMe57wz_1kKKAfHhcygP6KWnetrsWdj7=iymnc...@mail.gmail.com Content-Type: text/plain; charset=iso-8859-1 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 -- next part -- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20130208/e1b0b746/attachment.htm -- ___ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 56, Issue 14 *
Beginners Digest, Vol 56, Issue 15
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 mate...@neumanny.net Subject: [Haskell-beginners] Mixed typeclasses To: beginners@haskell.org Message-ID: 20130208133224.20991c0a@dragonfly.localdomain 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 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=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 fuuze...@fuuzetsu.co.ukwrote: 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
Beginners Digest, Vol 56, Issue 16
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 thehaskell 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 thehaskell 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