Specifying Type Class Instances with Type Applications
A common problem when you’re using type class constraints is ambiguity about which specific type class instance should be used. Consider, for example, a program you want to have print out the additive and multiplicative identities of some Natural numbers. You might write something like this:
| showIdentities = |
| let mul = multiplicativeIdentity |
| add = additiveIdentity |
| msg = "The additive identity is: " |
| <> show add |
| <> " and the multiplicative identity is: " |
| <> show mul |
| in print msg |
Unfortunately, this fails to compile! The problem is that multiplicativeIdentity and additiveIdentity both return a type that depends on the type class instance that we’re using, but the compiler ...
Get Effective Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.