[Haskell-beginners] Class and Instance Question

James Cook mokus at deepbondi.net
Fri May 20 14:29:24 CEST 2011


On May 19, 2011, at 9:05 PM, Adam C.Emerson wrote:

> Good morning,
>
> As my first "real" toy project (something trivial but with some
> pretense of usefulness at least to me) in Haskell, I had been trying
> to write an XDR encoder/decoder based on top of Data.Binary.

I have an unreleased XDR library[1] you're welcome to use or hack on.   
The source is not exactly beginner-friendly, but it's fairly easy to  
use (IMO, of course) and has been working very well for my purposes.   
It's more or less complete, but very unpolished so it may be more  
hassle than its worth for you to try to figure out how to use it.  If  
you're interested, though, I wouldn't mind putting some time into  
polishing and documenting it.  Or if you'd prefer to finish your own I  
absolutely understand and wouldn't be offended in the least. ;)

> As such,
> I have a typeclass:
>
> class Encodable t where
>    -- | Encode a value in the Put monad.
>    put :: t -> Put
>    -- | Decode a value in the Get monad
>    get :: Get t
>
> And I have various instances, all of which seem to work, except for
> two.  My thought was to treat a list of Chars as a string and a list
> of "encodable" types as a counted array.

Other people have responded with suggestions about how to achieve  
this, so I'll just mention that in my library I chose not to provide a  
class instance for strings, because there are so many ambiguities  
about how it could be encoded that I didn't feel comfortable making  
the choice once and for all.

-- James

[1] https://github.com/mokus0/xdr



More information about the Beginners mailing list