[Haskell-beginners] Typeclasses and "inheritance"
Patrick LeBoutillier
patrick.leboutillier at gmail.com
Fri Jul 24 12:16:26 EDT 2009
Hi,
On Thu, Jul 23, 2009 at 4:26 PM, Chaddaï Fouché <chaddai.fouche at gmail.com>wrote:
> On Thu, Jul 23, 2009 at 10:18 PM, Chaddaï
> Fouché<chaddai.fouche at gmail.com> wrote:
> > Your instance would look like that :
>
> Oops... I forgot to trim the extra parameters !
>
> > instance IPAddr IPv4Addr where
> > type Host IPv4Addr = IPv4Host
> > type Mask IPv4Addr = IPv4Mask
> > host (IPv4Addr h _) = h
> > mask (IPv4Addr _ m) = m
>
This is great stuff and exactly what I wanted. Thank you very much guys!
I kept on hacking at it and so far I have this code:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=7428
which unfortunately doesn't compile:
Net/IP.hs:44:39:
Couldn't match expected type `Word (Host a)'
against inferred type `Word (Mask a)'
In the second argument of `($)', namely `(bits h) .&. (bits m)'
In the first argument of `makeIPAddr', namely
`(fromBits $ (bits h) .&. (bits m))'
In the first argument of `($)', namely
`makeIPAddr (fromBits $ (bits h) .&. (bits m))'
In the declaration of the class IPAddr, is there any way to force that the
IPHost and IPMask types are made up from the same IPBits type? Basically I
would like the compiler to enforce that Word (Host a) and Word (Mask a) be
the same type for a specific instance of IPAddr.
Note: I'm not sure how practical all this is going to be in the end (perhaps
a bit to convoluted), but it's an excellent learning exercise for me.
Thanks a lot,
Patrick
>
> One advantage compared to the multiparameter + functional dependencies
> solution is that you can write :
> (IPAddr a) => ...
> In your context rather than introducing h and m when they're not needed.
>
> If you need to write "Host a" several time in a function, you can put
> the following in your context :
> (IPAdrr a, Host a ~ h) => ...
> and use h for Host a thereafter.
>
> --
> Jedaï
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090724/be992793/attachment-0001.html
More information about the Beginners
mailing list