[Haskell-cafe] Avoiding name collisions by using value spaces instead of modules

Cale Gibbard cgibbard at gmail.com
Sun Jan 8 12:54:14 EST 2006


On 08/01/06, Brian Hulley <brianh at metamilk.com> wrote:
> For example, suppose I'm writing a module M that deals with grammar, where
> the elements in a grammar rule are parameterised so that rules can be
> written using strings but processed as if we'd used ints instead:
>
>     data Element a = Terminal a | Nonterminal a | Action a
>
>     data Rule a = Rule (Element a) [[Element a]]
>
> Now I want to convert elements and rules from a to Int, so at the moment I
> have to write:
>
>     convertElement :: Element a -> CM (Element Int)
>     ...
>
>     convertRule :: Rule a -> CM (Rule Int)
>
> for some appropriate monad CM.
> Whereas I would have much preferred to use just the word "convert" in both
> cases. It is tremendously annoying to have to suffix everything with the
> type name.

This is what typeclasses are for.

class Convert c where
    convert :: c a -> CM (c Int)

 - Cale


More information about the Haskell-Cafe mailing list