listhaskellalternative-functor

Haskell - Why is Alternative implemented for List


I have read some of this post Meaning of Alternative (it's long)

What lead me to that post was learning about Alternative in general. The post gives a good answer to why it is implemented the way it is for List.

My question is:

Is there perhaps an algorithm that uses Alternative and a List might be passed to it so define it to hold generality?

I thought because Alternative by default defines some and many, that may be part of it but What are some and many useful for contains the comment:

To clarify, the definitions of some and many for the most basic types such as [] and Maybe just loop. So although the definition of some and many for them is valid, it has no meaning.

In the "What are some and many useful for" link above, Will gives an answer to the OP that may contain the answer to my question, but at this point in my Haskelling, the forest is a bit thick to see the trees.

Thanks


Solution

  • There's something of a convention in the Haskell library ecology that if a thing can be an instance of a class, then it should be an instance of the class. I suspect the honest answer to "why is [] an Alternative?" is "because it can be".

    ...okay, but why does that convention exist? The short answer there is that instances are sort of the one part of Haskell that succumbs only to whole-program analysis. They are global, and if there are two parts of the program that both try to make a particular class/type pairing, that conflict prevents the program from working right. To deal with that, there's a rule of thumb that any instance you write should live in the same module either as the class it's associated with or as the type it's associated with.

    Since instances are expected to live in specific modules, it's polite to define those instances whenever you can -- since it's not really reasonable for another library to try to fix up the fact that you haven't provided the instance.