Skip to content
0:00 / 0:00

#combinators#comonads#haskell

Combinator libraries (also, comonads!)

Samuel Gélineau

299 subscribers • 1 video on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.

Intermediate. Assumes working knowledge.

1.9K views11 years ago1 recommendation

Programming languagesSoftware designHaskellFunctional programmingDesign patterns

Samuel Gélineau explains the combinator library pattern in Haskell and how comonads can be used to structure functional programs.

A talk I gave at the Haskellers Montréal meetup. meetup.com/Haskellers-Montreal-Meetup/events/218862541/

Why it's here

1 person recommends this

What people say. Recommended as a great explanation of the combinator library pattern and how to build complex systems from simple primitives.

Moments people linked to

u/gelisamin r/haskell • Recommends

On “is some Haskell philosophy close to the unix philosophy?”

...partially? > Write ~~programs~~ *functions* that do one thing and do it well. Not really. We're quite proud of the fact that laziness allows a single function to perform both sorting in O(n log n) and min in O(n) for example. > Write ~~programs~~ *functions* to work together. Absolutely! One of my favourite part of Haskell is the combinator libraries pattern, in which you write larger solutions by combining smaller functions using a few simple combinators. > Write ~~programs~~ *functions* to handle text streams, because that is a universal interface. Absolutely not! We hate stringly-typed values, we much prefer to use very precise types which make illegal values unrepresentable.

u/gelisamin r/haskell • Recommends

On “What Gui libray do you use/recommend”

Are you familiar with combinator libraries? They allow you to describe larger and larger systems by combining smaller systems together using a handful of combinators, starting from primitive systems which cannot be further decomposed. Since there is only a handful of primitives and combinators, such libraries might *look* limited at a first glance, but there is an infinite number of ways to arrange those combinators into varied and arbitrarily complex systems. An FRP library is a combinator library in which each system produces a bunch of events. The way to create a bigger system is not by handling those events, but by combining systems together: for example, a primitive system producing…

u/gelisamin r/haskell • Mentions

On “I am an Electrical Engineer with limited background in software. Should I learn Haskell?”

> There seems to be an abundance of jobs in software so maybe learning Haskell is a good place to start? Sadly, Haskell jobs are still quite rare at the moment. Your C++ experience will be much more helpful when looking for jobs. That being said, the fact that Haskell is very different from C++ makes it useful to learn if only because it will allow you to have a broader set of potential solutions to consider than if you only knew the C++ way. See for example my video on combinator libraries, intended for Haskell beginners, in which I demonstrate how Haskell-style solutions can be useful in other languages.

u/gelisamin r/haskell • Mentions

On “Once you've seen the beautiful, you don't want to go back to the ugly”

So the good news is that there are techniques I've learned in Haskell which transfer very well to other languages. One bad news, of course, is that there are also techniques which don't transfer, and that's frustrating. But the bad news I want to talk about is that even those techniques which do transfer well, you might not get to use them either :( Here's a story. After rescuing a buggy project by applying FP principles to a Java app, I felt like knowing Haskell had given me super powers. But then I moved on to a more conservative team, on a C++ project. C++ is really not a bad language for putting functional programming idioms to good use. And so when the team was looking for an alte…
11View on Reddit

u/gelisamin r/haskell • Mentions

On “Suggestions about an 1 hour talk of Haskell?”

Simon Peyton Jones has great advice on how to give a talk, and his talks are really good. Personally, I often give presentations at our local Haskell meetup, and as I prepare the slides I often realize that I need to explain this and that prerequisite before I can talk about my main subject, which either causes my presentation to become much longer than I originally planned, or if the duration is fixed, it means I have much less time to cover the main subject and so I have to cover it in a very shallow fashion. So my advice is: pick a very small subject, cover it in depth if you can, but think of your audience. Finally, if you need a really concrete suggestion, my go-to intro talk for an a…

u/gelisamin r/haskell • Mentions

On “thought about comment in Haskell”

> let say I have big function somehow and to be understood the reader will read it carefully to figure out what is going on, wouldn't it be better if there is a comment that describe in nutshell the behavior of the function is doing A comment would be one way to make that function easier to understand, but refactoring the code so that it is itself easier to understand would be a bigger win, because it will then also be easier to modify. > could you explain why you write the why? I think well-written code can be self-explanatory. By reading well-written code, it should be quite clear what it does, but knowing that is not sufficient! If it's not clear _why_ the code does what it does, the…
7View on Reddit

u/gelisamin r/haskell • Mentions

On “Is Arrow a superclass of Applicative?”

> I was wondering if there is any way one can at least qualitatively relate (not necessarily a strict superclass or subclass) these typeclasses. Okay, let me try. Since Monad instances have kind `* -> *` while Arrow instances have kind `* -> * -> *`, we're not even talking about the same kind of entities, so neither set is completely included in the other. They truly describe completely disjoint sets of type constructors, so neither is a super class of the other. In order to compare them qualitatively, we'll have to drop those indices. So in a type like `fmap :: (a -> b) -> f a -> f b`, I'll drop the indices and keep only the information of whether the argument is a value constructed with…

u/gelisamin r/haskell • Mentions

On “Categorical dual of functional programming”

Despite my other comment: okay, I'll bite :) The reason FP is often compared to OOP and imperative programming isn't because FP is dual to one or the other, but because OOP and imperative programming is the dominant programming paradigm in the industry. If Prolog's logic programming was the dominant paradigm, FP fans would be comparing FP to logic programming instead. For this reason, when we give approximations to FP such as "FP is what you get when you take programming with immutable values seriously", that only makes sense because the dominant paradigm is to use immutable variables everywhere. Prolog's values are also immutable, so we wouldn't be saying that if logic programming was the…

u/gelisamin r/haskell • Mentions

On “Parsing 2d arrays from text?”

I see that you first obtain opaque strings from the input, as early as possible, and then parse those strings into the structure you want via a pure function. That's good, a very Haskell-like approach! I also see that your pure parsing function is made out of folds. That's a very functional approach, but perhaps surprisingly, not a very Haskell-like approach! At least not in this case. In Haskell, we like to divide our code into small independent primitives, and to combine them into slightly more complicated ones. For example, `(+)` is a simple binary primitive which works on individual numbers, and `foldr (+) 0` is a way to combine `(+)` with a starting count to obtain a more complicated…

u/gelisamin r/haskell • Mentions

On “Diagrams to present functional program structure”

Hmm! I don't know of any such notation, but remember that refactoring is easy in Haskell, so you might not need as much ahead-of-time preparation as you think. Another concern I have about such a notation is that we have many different ways to compose smaller parts into larger parts, it's not always objects composed of other objects like in OOP. So I think you should first decide on how you want to combine your top-level components, and then pick an appropriate notation for that, and then if that isn't detailed enough, decide how each of those are composed from smaller components, and draw a separate diagram for each one, possibly with different notations. Or, to make things simple, you co…

u/gelisamin r/haskell • Mentions

On “Learning Curves for different programming languages (including Haskell)”

I did go through a phase in which the solution to everything was a monad transformer. I'm past this now, except the solution to everything is now a combinator library. Is that also just a phase?

u/gelisamin r/haskell • Mentions

On “I want to give a talk about Haskell, need some help”

For an audience who doesn't know Haskell, I like to talk about combinator libraries, because it's a concept which translates well to other languages and so you can give them a glimpse of what it's like to program in Haskell without having to explain Haskell's syntax.

u/gelisamin r/haskell • Mentions

On “What does the free monad offer that macros don't?”

Hmm, I'm sure there are better resources somewhere, but I'm afraid I don't remember where I learned how to do that. For now, here is a presentation I gave about combinator libraries. A custom monad, applicative or category is simply a combinator library whose most important combinator is `(<*>)`, `(>>=)`, or `(.)`.

u/gelisamin r/haskell • Mentions

On “"Haskell is Not For Production and Other Tales" - Katie Miller @ Linux.conf.au 2016 (Haxl@Facebook talk)”

This is valuable feedback for the community! To summarize my understanding: we should focus on describing Haskell's benefits, not on dispelling myths about disadvantages of Haskell. I happen to have a link to this video whose first 10 minutes compare the advantages and disadvantages of using Haskell, and I do focus on the advantages. If you have 10 minutes to spare, I'd be quite interested to know if you find it more convincing.

u/gelisamin r/haskell • Mentions

On “Easy concurrency with functional programming”

Hmm, that's true, sorry for the confusing example. I once wrote a combinator library in Java based on callbacks and mutable state, this would be a better example but it's not nearly as well-known. The gist of it is that I defined a DSL for describing one-time events in terms of other one-time events, for example by stating that this event occurs once all the other events in a list have occurred. This was clearly a declarative combinator library, even though it was implemented using callbacks and mutable state. So I'd say that I was writing in a functional style, and that a language in which this was the predominant style should be called a functional language. Again, this is my understand…
1View on Reddit