Ertugrul ,<br _moz_dirty="" />Thanks for you very clear explanation.<br _moz_dirty="" />Without committing to some concrete representation such as list I do
not know how to specify constructors in the class (see below). As you point out a class may not be appropriate for an actual application, but I am investigating the strengths and weaknesses of class as a unit of *specification*.<br _moz_dirty="" />Regards,<br _moz_dirty="" />Pat<br _moz_dirty="" /><br _moz_dirty="" />-- Class with functional dependency<br _moz_dirty="" />class QUEUE_SPEC_CLASS2 a q | q -> a where<br _moz_dirty="" /> newC2 :: q a -- ??<br _moz_dirty="" /> sizeC2 :: q a -> Int<br _moz_dirty="" /> restC2 :: q a -> Maybe (q a)<br _moz_dirty="" /> insertC2 :: q a -> a -> q a<br _moz_dirty="" />-- Without committing to some concrete representation such as list I do not know how to specify constructor for insertC2 ?? = ??<br _moz_dirty="" /> insertC2 newC2 a = newC2 -- wrong<br _moz_dirty="" /> isEmptyC2 :: q a -> Bool<br _moz_dirty="" /> isEmptyC2 newC2 = True<br _moz_dirty="" />-- isEmptyC2 (insertC2 newC2 a) = False wrong<br _moz_dirty="" /><br _moz_dirty="" /><div style="font-family: 'Times New Roman'; font-size: 16px;"><br /><br /><span>On 22/07/12, <b class="name">Ertugrul Söylemez </b> <es@ertes.de> wrote:</span><blockquote cite="mid:20120722172617.2f5d4f38@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 />> {-<br />> Below is a *specification* of a queue.<br />> If possible I would like to write the equations in type class.<br />> Does the type class need two type variables?<br />> How do I represent the constructors?<br />> Can the equations be written in the type class rather than the<br />> instance? -}<br /><br />(Side note: When opening a new topic, please don't /reply/ to a post,<br />but post it separately by creating a new mail.)<br /><br />The type class needs to know the element type, so your observation is<br />correct. Usually, as in your case, the element type follows from the<br />data structure type, and you will want to inform the type system about<br />this connection. There are basically three ways to do it. The first is<br />using MultiParamTypeClasses and FunctionalDependencies:<br /><br /> class Stacklike a s | s -> a where<br /> empty :: s a<br /> null :: s a -> Bool<br /> push :: a -> s a -> s a<br /> pop :: s a -> Maybe a<br /> size :: s a -> Int<br /> tail :: s a -> Maybe (s a)<br /><br />Another way is using an associated type (TypeFamilies). This is<br />cleaner, but much more noisy in the type signatures:<br /><br /> class Stacklike s where<br /> type StackElement s<br /><br /> empty :: s (StackElement s)<br /> null :: s (StackElement s) -> Bool<br /> push :: StackElement s -> s (StackElement s) -> s (StackElement s)<br /> pop :: s (StackElement s) -> Maybe (StackElement s)<br /> size :: s (StackElement s) -> Int<br /> tail :: s (StackElement s) -> Maybe (s (StackElement s))<br /><br />Finally once you realize that there is really no need to fix the element<br />type in the type class itself, you can simply write a type class for the<br />type constructor, similar to how classes like Functor are defined:<br /><br /> class Stacklike s where<br /> empty :: s a<br /> null :: s a -> Bool<br /> push :: a -> s a -> s a<br /> pop :: s a -> Maybe a<br /> size :: s a -> Int<br /> tail :: s a -> Maybe (s a)<br /><br />The big question is whether you want to write a class at all. Usually<br />classes are used to capture patterns, not operations.<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>