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

Marius Nita marius at cs.pdx.edu
Thu Aug 19 14:30:31 EDT 2004

On Thu, Aug 19, 2004 at 05:42:10PM +0100, Sam Mason wrote:
> Hi,
> 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.

You could "fix" it by type-annotating encode:

  test = decode . (encode :: String->Int)

It makes sense that it doesn't work unannotated, since in the general
case, you'd have more than one instance of Foo. Certainly you wouldn't
want functions to stop working when you add one more instance.

I think this sort of abstract manipulation is only possible with
existential types. For example:

  data T = forall t.MkT (String -> t) (t -> String)

  x = MkT (read :: String->Int) show
  y = MkT (read :: String->Float) show

  test (MkT encode decode) = decode . encode


  *Main> test x "0"
  *Main> test y "6.7"


More information about the Haskell-Cafe mailing list