[Haskell-cafe] Comments from OCaml Hacker Brian Hurt

Dan Doel dan.doel at gmail.com
Thu Jan 15 18:36:42 EST 2009


On Thursday 15 January 2009 6:21:28 pm David Menendez wrote:
> On Thu, Jan 15, 2009 at 5:32 PM, Andrew Coppin
>
> <andrewcoppin at btinternet.com> wrote:
> > As an aside, the integers form two different monoids. Haskell can't
> > [easily] handle that. Does anybody know of a language that can?
>
> Some of the ML-derived languages can do that. Essentially, your code
> takes another module which implements a monoid as an argument.
>
> The catch is that you have to explicitly provide the monoid
> implementation in order to use your code.

You can do that in Haskell, as well, although it will end up uglier than ML. 
You can write your own dictionary type:

  data Monoid a = Monoid { unit :: a , bin :: m -> m -> m }

And pass that around:

  twice :: Monoid m -> m -> m
  twice mon m = bin mon m m

And even manually simulate locally opening the dictionary with a where clause

  twice mon m = m ++ m
   where (++) = bin mon

This is, after all, what GHC translates type classes into behind the scenes 
(although that isn't necessarily how they must be implemented).

Some folks even argue that type classes are overused and this style should be 
significantly more common.

-- Dan


More information about the Haskell-Cafe mailing list