31

I have some difficulties to understand when use and when not use typeclass in my code. I mean create my own, and not use already defined typeclasses, of course. By example (very stupid example), should I do:

data Cars = Brakes | Wheels | Engine
data Computers = Processor | RAM | HardDrive  

class Repairable a where
    is_reparaible :: a -> Bool

instance Repairable Cars where
    is_repairable (Brakes) = True
    is_repairable (Wheels) = False
    is_repairable (Engine) = False

instance Repairable Computers where
    is_repairable (Processor) = False
    is_repairable (RAM)       = False
    is_repairable (HardDrive) = True

checkState :: (Reparaible a) => a -> ... 
checkState a = ...

(Obviously, this is an stupid, incomplete example).

But this is a lot for a little use, no? Why I shouldn't do something simple and only defining functions without defining new data types and typeclasses (with their instances).

This example is too simple, but in facts I often see somethings like that (new data types+typeclasses+instances) when I browse Haskell code on github instead of only defining functions.

So, when I should create new data types, typeclasses etc and when should I use functions?

Thanks.

2 Answers 2

49

Why I shouldn't do something simple and only defining functions without defining new data types and typeclasses (with their instances).

Why indeed? You could just define:

checkState :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
checkState is_repairable repairs destroy a
    = if (is_repairable a) then repairs a else destroy a

People misuse type classes all the time. It doesn't mean that it's idiomatic.

To answer your more general question, here are some rules of thumb for when to use type classes and when not to use them:

Use type classes if:

  • There is only one correct behavior per given type

  • The type class has associated equations (i.e. "laws") that all instances must satisfy

Don't use type classes if:

  • You are trying to just namespace things. That's what modules and namespaces are for.

  • A person using your type class cannot reason about how it will behave without looking at the source code of the instances

  • You find that the extensions you have to turn on are getting out of control

5
  • Yeah! This is a very complete answer, thank you very much! Your "don't use type classes if..." will be particulary helpful to choose the good way to do something.
    – vildric
    Jun 14, 2013 at 2:22
  • 1
    I would add to this "don't use typeclasses just for one method", although this isn't an absolutely rigid rule, more like a general hint. Jun 15, 2013 at 14:00
  • 3
    @MathematicalOrchid That's sort of part of the "needs laws" rule since you will rarely have laws for a type class with only one method (with the exception of something like SemiGroup, where you have the associativity law) Jun 15, 2013 at 14:45
  • 2
    I don't feel that this answer really explains why any of this is bad or good. It simply states it as if it were fact, and without any explanation I feel that it is worse than no answer. Feb 1, 2020 at 23:48
  • "That's what modules and namespaces are for." What do you mean by namespaces in haskell? I tried to do namespacing using type classes, but it quickly got ugly so I came here searching for alternatives. But it seems haskell has only modules, not namespaces.
    – Al.G.
    Jan 11 at 8:34
6

You can often use a data type instead of a type-class, e.g.

data Repairable a = Repairable 
   { getRepairable :: a
   , isRepairable :: Bool
   , canBeRepairedWith :: [Tool] -> Bool  -- just to give an example of a function
   } 

Of course you need to pass this value explicitly, but this can be a good thing if you have multiple choices (e.g. think of Sum and Product as possible Monoids for numbers). Except that you have more or less the same expressiveness as for a type-class.

1
  • 2
    I'm not the downvoter, but . . . this answer doesn't really answer the question IMHO. The OP is asking when typeclasses should be used. This answer mentions an alternative to typeclasses, but doesn't say when to use that alternative. (If anything, it seems to presuppose an understanding of when typeclasses make sense, and offers an alternative that makes sense in many of the same cases.)
    – ruakh
    Apr 17, 2014 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.