[Haskell-cafe] Fun with multi-parameter type classes

Sam Mason mason at f2s.com
Thu Aug 19 12:42:10 EDT 2004


I've been getting into Haskell over the past few months and have just
come up against a bit of a brick wall when trying to encapsulate
some of the data structures in my code nicely.  Basically what I
want to have, is a type class where one of the intermediate values
is opaque with respect to code using its implementations.  This is
a simplified version of what I'm trying to accomplish:

   class Foo t where
      encode :: String -> t
      decode :: t -> String

   instance Foo Int where
      encode = read
      decode = show

   test = decode . encode

This currently fails, because the type checker insists on trying
to figure out what its type should be - even though it shouldn't
be needed.

Any suggestions on how to encode this sort of thing?  And if it is
possible, can it be extended to multiple type parameters? as this is
really what I want to use it for.

About the only way I can think of "fixing" it, is by turning the
code inside out - sort of like the way an AST drives the compiler,
but without knowing how it represents things internally.


More information about the Haskell-Cafe mailing list