3

I am learning Haskell and trying to understand the Monoid typeclass. At the moment, I am reading the haskellbook and it says the following about the pattern (monoid):

One of the finer points of the Haskell community has been its propensity for recognizing abstract patterns in code which have well-defined, lawful representations in mathematics.

What does the author mean by abstract patterns?

3
  • 1
    Familiar with future/promise concepts? These are instances of more abstract Monad concept. Or take map function in JS or Array.Select() method in C#. These are examples of abstract pattern called Functor.
    – arrowd
    Jun 30, 2017 at 12:29
  • redd.it/68nspj is probably related.
    – jeiea
    Jun 30, 2017 at 12:42
  • 1
    Since you talked about monoids: [] ++ [7], "" ++ "foo", 0 + 3, 1 * 3. Do you recognize the recurring, reliable trait? This is a pattern and it has a nice representation in math. Patterns are inherently abstract and to call them abstract patterns might be unnecessary.
    – user6445533
    Jun 30, 2017 at 13:25

1 Answer 1

6

Abstract in this sense is the opposite of concrete. This is probably one of the key things to understand about Haskell.

What is a concrete thing? Well, most values in Haskell are concrete. For example 'a' :: Char. The letter 'a' is a Char value, and it's a concrete value. Char is a concrete type. But in 1 :: Num a => a, the number 1 is actually a value of any type, so long as that type has the set of functions that the Num typeclass sets out as mandatory. This is an abstract value! We can have abstract values, abstract types, and therefore abstract functions. When the program is compiled, the Haskell compiler will pick a particular concrete value that supports all of our requirements.

Haskell, at its core, has a very simple, small but incredibly flexible language. It's very similar to an expression of maths, actually. This makes it very powerful. Why? because most things that would be built in language constructs in other languages are not directly built into Haskell, but defined in terms of this simple core.

One of the core pieces is the function, which, it turns out, most of computation is expressible in terms of. Because so much of Haskell is just defined in terms of this small simple core, it means we can extend it to almost anywhere we can imagine.

Typeclasses are probably the best example of this. Monoid, and Num are examples of typeclasses. These are constructs that allow programmers to use an abstraction like a function across a great many types but only having to define it once. Typeclasses let us use the same function names across a whole range of types if we can define those functions for those types. Why is that important or useful? Well, if we can recognise a pattern across, for example, all numbers, and we have a mechanism for talking about all numbers in the language itself, then we can write functions that work with all numbers at once. This is an abstract pattern. You'll notice some Haskellers are quite interested in a branch of mathematics called Category Theory. This branch is pretty much the mathematical definition of abstract patterns. Contrast this ability to encode such things with the inability of other languages, where in other languages the patterns the community notice are often far less rigorous and have to be manually written out, and without any respect for its mathematical nature. The beauty of following the mathematics is the extremely large body of stuff we get for free by aligning our language closer with mathematics.

This is a good explanation of these basics including typeclasses in a book that I helped author: http://www.happylearnhaskelltutorial.com/1/output_other_things.html

Because functions are written in a very general way (because Haskell puts hardly any limits on our ability to express things generally), we can write functions that use types which express such things as "any type, so long as it's a Monoid". These are called type constraints, as above.

Generally abstractions are very useful because we can, for example, write on single function to operate on an entire range of types which means we can often find functions that do exactly what we want on our types if we just make them instances of specific typeclasses. The Ord typeclass is a great example of this. Making a type we define ourselves an instance of Ord gives us a whole bunch of sorting and comparing functions for free.

This is, in my opinion, one of the most exciting parts about Haskell, because while most other languages also allow you to be very general, they mostly take an extreme dip in how expressive you can be with that generality, so therefore also are less powerful. (This is because they are less precise in what they talk about, because their types are less well "defined").

This is how we're able to reason about the "possible values" of a function, and it's not limited to Haskell. The more information we encode at the type level, the more toward the specificity end of the spectrum of expressivity we veer. For example, to take a classic case, the function const :: a -> b -> a. This function requires that a and b can be of absolutely any type at all, including the same type if we like. From that, because the second parameter can be a different type than the first, we can work out that it really only has one possible functionality. It can't return an Int, unless we give it an Int as its first value, because that's not any type, right? So therefore we know the only value it can return is the first value! The functionality is defined right there in the type! If that's not mindblowing, then I don't know what is. :)

As we move to dependent types (that is, a type system where types are first class, which means also that ordinary values can be encoded in the type system), we can get closer and closer to having the type system specify specifically what the constraints of possible functionality are. However, the kicker is, it doesn't necessarily speak about the implementation of the functionality unless we want it to, because we're in control of how abstract it is, but while maintaining expressivity and much precision. That's pretty fascinating, and amazingly powerful.

Much math can be expressed in the language that underpins Haskell, the lambda calculus.

5
  • 1
    I think typeclasses rather represent the notion of generalization, because they don't suppress details but define type constraints.
    – user6445533
    Jun 30, 2017 at 13:49
  • @ftor Type variables are the thing that generalises IMO. Typeclasses let us constrain that generalisation more or less. We can do the same thing with "method dictionaries", though not as flexibly, so one might say that functions with type variables provide the most flexilibity in terms of abstraction? Typeclasses are definitely more convenient. Jun 30, 2017 at 17:48
  • Agreed, parametric polymorphic functions are even more generalized than those with ad-hoc polymorphic behavior. I think polymorphism per se has the effect of generalization, whether constrained or unconstrained. Functions on the other hand abstract over expressions. So with polymorphic functions you have always both, abstraction (over expressions) and generalization (in their applicability) - at least this is my current understanding, which might change in the future :D
    – user6445533
    Jun 30, 2017 at 21:46
  • Yeah! parameterisation of any kind is abstraction. If I have an expression that represents printing the name "Julian" to the screen, then that's less flexible and abstract than if I write a function that takes any String and prints it to the screen. Types allow us to be more expressive, which lets us distinguish between things. Parameterised types allow us to maintain that specificity while also being general which gives us power. Fascinating isn't it! Jun 30, 2017 at 23:06
  • The most interesting thing coming out of this, to me, is that while most "modern" languages are capable of being very very general, because they have these types that can effectively be any type, they're not capable of also being very expressive, while maintaining that generality, which takes away their power! (ie you can do less). So interesting. Jun 30, 2017 at 23:08

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.