<div style="font-family: 'Times New Roman'; font-size: 16px;">Ertugrul,<br _moz_dirty="" />Thank you for your detailed and helpful reply.<br _moz_dirty="" />I was unaware of the distinction between data/value and type constructors.<br _moz_dirty="" />Regards,<br _moz_dirty="" />Pat<br _moz_dirty="" /><br _moz_dirty="" /><br /><br /><span>On 31/07/12, <b class="name">Ertugrul Söylemez </b> <es@ertes.de> wrote:</span><blockquote cite="mid:20120801002227.5c1fc018@tritium.streitmacht.eu" class="iwcQuote" style="border-left: 1px solid #00F; padding-left: 13px; margin-left: 0;" type="cite"><div class="mimepart text plain">Patrick Browne <patrick.browne@dit.ie> wrote:<br /><br />> Thanks for all the very useful feed back on this thread.<br />> I would like to present my possibly incorrect summarized view:<br />> Class signatures can contain placeholders for constructors.<br />> These place-holder-constructors cannot be used in the class to define<br />> functions (I assume other in-scope constructors can be used). In the<br />> instance a real constructor can be substituted for the<br />> place-holder-constructor. Does this restrict the type of equation<br />> that can be used in a type class? It seems that some equations<br />> respecting the constructor discipline are not allowed.<br /><br />Your intuition seems to be near the truth, although your terminology is<br />currently wrong. Let's look at an example:<br /><br /> class Functor f where<br /> fmap :: (a -> b) -> (f a -> f b)<br /><br />The 'f' in the class header is probably what you call a "placeholder for<br />constructors". This is not a placeholder, but a type variable. It<br />represents a type. Incidentally in this case it indeed represents a<br />constructor, namely a /type/ constructor (like Maybe). This is an<br />important distinction, because generally when we talk about<br />"constructors", we mean /value/ constructors (like Just or Nothing):<br /><br /> data Maybe a = Just a | Nothing<br /><br />Here Maybe is a type constructor. This is because it's not a type in<br />its own right, but is applied to another type (like Int) to yield an<br />actual type (Maybe Int). The type Maybe is applied to is represented<br />by the type variable 'a' in the code above. To simplify communication<br />we often call Maybe itself also a type, but it's really not.<br /><br />Let's write the Functor instance for Maybe. It is common to use a<br />helper function (a so-called fold function), which allows us to express<br />many operations more easily. It's called 'maybe' for Maybe:<br /><br /> maybe :: b -> (a -> b) -> Maybe a -> b<br /> maybe n j (Just x) = j x<br /> maybe n j Nothing = n<br /><br /> instance Functor Maybe where<br /> fmap f = maybe Nothing (Just . f)<br /><br />This is the instance for Maybe. The type variable 'f' from the class<br />now becomes a concrete type constructor Maybe. In this instance you<br />have f = Maybe, so the type of 'fmap' for this particular instance<br />becomes:<br /><br /> fmap :: (a -> b) -> (Maybe a -> Maybe b)<br /><br />The notable thing here is that this is really not a<br />placeholder/replacement concept, but much more like a function and<br />application concept. There is nothing that stops you from having type<br />variables in an instance:<br /><br /> instance Functor (Reader e) where<br /><br />As you can see there is still what you called a "placeholder" in this<br />instance, so the placeholder concept doesn't really make sense here.<br />The declaration can be read as:<br /><br /> "For every type 'e' the type 'Reader e' is an instance of the<br /> Functor type class."<br /><br /><br />> I appreciate that in Haskell the most equations occur in the<br />> instances, [...]<br /><br />Not at all. When programming Haskell you write lots and lots of<br />equations outside of class instances. Whenever you write "=" you<br />introduce an equation, for example in top-level definitions and in 'let'<br />and 'where' bindings.<br /><br /><br />> [...] but from my earlier post: "I merely wish to identify the<br />> strengths and weakness of *current Haskell type classes* as a pure<br />> *unit of specification*"<br /><br />I think you will be interested in this Stack Overflow answer:<br /><br /> <<a href="http://stackoverflow.com/a/8123973" target="_blank">http://stackoverflow.com/a/8123973</a>><br /><br />Even though the actual question answered is different, it does give a<br />nice overview of the strengths and weaknesses of type classes.<br /><br /><br />Greets,<br />Ertugrul<br /><br />-- <br />Not to be or to be and (not to be or to be and (not to be or to be and<br />(not to be or to be and ... that is the list monad.<br /></div></blockquote></div>
<br clear=all> Tá an teachtaireacht seo scanta ó thaobh ábhar agus víreas ag Seirbhís Scanta Ríomhphost de chuid Seirbhísí Faisnéise, ITBÁC agus meastar í a bheith slán. <a href="http://www.dit.ie">http://www.dit.ie</a><br>
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. <a href="http://www.dit.ie">http://www.dit.ie</a>