spotifyovercastrssapple-podcasts

Elm and Haskell with Flavio Corpa

Flavio Corpa joins us to discuss the similarities and differences between Elm and Haskell, and how learning Haskell can help you write better Elm code.
May 8, 2023
#82

Transcript

[00:00:00]
Hello Jeroen.
[00:00:02]
Hello Dillon.
[00:00:03]
You know, I've always wanted to map Haskell concepts to Elm and well I guess it would be f-mapping then, wouldn't it?
[00:00:11]
Well, it really depends on whether you have a list of things that you want to learn or if you have other things you want to learn.
[00:00:17]
I guess you could say maybe this episode would be like a type of class, you could say.
[00:00:24]
Well, today we've got Flavio Korfa joining us. Flavio, thanks so much for coming on the show.
[00:00:32]
Hello, my pleasure.
[00:00:33]
So Flavio, you wrote a really nice set of blog posts about Haskell for Elm and you named the series Giving Names to Stuff.
[00:00:43]
And I like that name and if we could just start with why did you title the series Giving Names to Stuff?
[00:00:51]
It seems like there's something important to you that you wanted to convey to Elm developers with your Haskell background about names and how names relate to type classes.
[00:01:04]
So why do you think names are important and why are they important for your blog series?
[00:01:09]
That's a very interesting question.
[00:01:11]
I always wanted for someone to ask me about the name of the series.
[00:01:16]
And I was also waiting for your puns at the beginning of the episode.
[00:01:21]
I keep forgetting that he's going to do some until he's saying, I'm like, oh yeah, I didn't prepare anything.
[00:01:32]
Get your cringe phrase ready.
[00:01:35]
It's a pretty recent thing. He didn't do this in the beginning. And now he just can't stop.
[00:01:41]
So it's a new thing or it's a modern thing in the podcast then, the puns.
[00:01:46]
I'd say so.
[00:01:50]
So related to Giving Names to Stuff, I first learned Haskell and it took me a while because it's quite a big language to learn and a bit wide.
[00:02:02]
And then at some point I had some front-end experience and JavaScript experience prior to that.
[00:02:07]
At some point I realized that I wanted to try out Elm and that I already knew Elm because the syntax is so similar to Haskell.
[00:02:14]
And the Elm compiler is made in Haskell.
[00:02:17]
So when you learn Haskell, they sort of teach you the other way around.
[00:02:21]
They teach you the names first and then the patterns that you use.
[00:02:24]
But in Elm, it's amazing because you don't really need to know the names of things.
[00:02:28]
You just use them and you gain an intuition for them.
[00:02:31]
And you see them in practice. How are they useful? How they make you a better programmer or why would you need such patterns?
[00:02:38]
So while trying to explain a little bit of Haskell to my Elm friends, I just noticed you just need to know the names for stuff you already use.
[00:02:47]
That's why I chose the name, basically.
[00:02:51]
I love that.
[00:02:53]
So did you think it was important for Elm developers to know the names for these things if they want to try Haskell?
[00:03:03]
Or do you think that there's also value to giving names to these things for an Elm developer, even just working with Elm?
[00:03:11]
Yeah, that's a good question because what happened to me before is that before even learning Haskell, as I mentioned, I was a JavaScript developer.
[00:03:19]
So I tried to learn ReasonML, this new language that Facebook was creating.
[00:03:24]
You know, the creator of React, Jordan Wolk.
[00:03:27]
And then I started to learn some Reason and underneath it was OCaml and I did not know about it.
[00:03:33]
And I knew that or I learned that Reason had something called functors.
[00:03:37]
But when I tried to learn what these functors were, they were basically different from the Haskell functors or the JavaScript functors I already had read about.
[00:03:47]
So it was really, really confusing to me.
[00:03:49]
This name collision in my head that was making me feel anxious, so to speak.
[00:03:56]
So I'd say that learning the names or at least getting familiar with the semantics or what they try to convey makes sense.
[00:04:03]
Even if you want to jump to F-sharp, OCaml, whatever other functional language, it is useful in itself.
[00:04:10]
But the same things are sometimes called different names, unfortunately, in different languages, and so it's a bit confusing, all of it.
[00:04:18]
And naming these things is so hard because I think what I've started to realize is that our initial inclination may be...
[00:04:27]
I imagine many people initially learn an imperative or an object-oriented language.
[00:04:33]
And we tend to think of concepts in terms of what you do with them.
[00:04:39]
Like, what's the inheritance chain?
[00:04:42]
Or this is like a list or this is a sequence or this is a thing that you await that can be done sequentially.
[00:04:51]
And it's like, well, what if we weren't talking about the kind of data something was or the kind of thing something represented,
[00:04:57]
but rather how you transform data in something, right?
[00:05:01]
So actually, you know, we can talk about a list and a task and a JSON decoder in the same way.
[00:05:09]
What do those things have in common?
[00:05:11]
Well, you can manipulate data in a similar way.
[00:05:15]
How do you put a name to that idea?
[00:05:17]
It's very difficult and it's very abstract, but it's a very productive way to work with things.
[00:05:24]
And once you gain that intuition, you can be really productive, it feels like.
[00:05:31]
But it's hard to develop that intuition.
[00:05:34]
And it's an interesting question whether somebody develops that intuition faster when they're given the names first,
[00:05:41]
learning Haskell, or when they're not given the names and just use these concepts in Elm.
[00:05:46]
Yeah, definitely.
[00:05:47]
I remember while doing or learning React in 2013, 2014,
[00:05:53]
I was coming from AngularJS.
[00:05:55]
So, for example, I just wanted to do a very simple thing.
[00:05:57]
I wanted to iterate on a list.
[00:05:59]
And you used to have these ng4, ngRepeat directives, you know, in the code.
[00:06:05]
I was looking for that in React.
[00:06:07]
And then obviously I learned about the function map, you know, and it's like, oh, so there is no special directive to do this.
[00:06:15]
It's just you use a language construct, which is just list.map, so to say.
[00:06:20]
And then you do what you want with the list.
[00:06:22]
But this concept, learning it as a JavaScript developer, was so useful.
[00:06:26]
And then it replicated in all the following languages I learned after that.
[00:06:29]
And it was really, really practical and useful to get acquainted with that.
[00:06:34]
Now I really want to know what was different with the Reason functor.
[00:06:39]
Oh, yeah.
[00:06:40]
So in OCaml and by extension in Reason,
[00:06:44]
so basically one file is not one module as in Elm, as in Haskell as well.
[00:06:51]
You can have multiple modules in one file.
[00:06:53]
So there is a case in which you want to parametrize a module and call it with a specific generic type and use that module with a different type.
[00:07:05]
So these parametrizeable modules, so to speak, how they call them, they call them OCaml functors.
[00:07:09]
But it's a completely different thing as in what we know in Elm and Haskell as a functor.
[00:07:15]
So it was kind of, yeah, mind-boggling to learn this.
[00:07:20]
Gotcha. I can understand the confusion.
[00:07:23]
Yeah, it was very confusing.
[00:07:26]
Okay. So, well, let's dive into it.
[00:07:31]
In your blog series, you wrote about basically three different type classes.
[00:07:36]
You want to kind of enumerate those for us?
[00:07:40]
Sure. Well, I started with the easiest one, I thought.
[00:07:44]
We're talking about functors for a while now.
[00:07:47]
But I started with functor because I think it's one of the most intuitive to think about once you understand the concept of mapping.
[00:07:53]
And we use them every day in Elm in all sorts of code.
[00:07:58]
As soon as you map any kind of data structure, you know that you are using functors underneath.
[00:08:04]
So the next thing that you kind of get to or need to understand is that there is something called applicative functors for when you need to map two or map three, map four, etc.
[00:08:19]
I read a really nice post by, I think it was Joel that also was a guest in a previous episode.
[00:08:27]
It was on this topic.
[00:08:29]
Exactly. So what happens when you run out of maps, right?
[00:08:32]
When you don't have enough map n numbers.
[00:08:36]
And then you can build yourself like using this and map thing.
[00:08:40]
You can basically use the pipe operator and this and map and use as many as you want.
[00:08:45]
And the concept behind these things, the concept that you can apply functors to each other, are called applicative functors.
[00:08:52]
And it's kind of weird.
[00:08:54]
The intuition is not as easy to understand as with functors.
[00:08:58]
But when you learn about them, you start seeing them everywhere.
[00:09:01]
And it's interesting because it's an intermediate step that you need to learn to go to the third step, which is monads.
[00:09:08]
The thing everyone talks about and there are a thousand blog posts about this scary monad thing.
[00:09:14]
Except in the Elm community where we basically don't talk about it.
[00:09:18]
We don't use the M word.
[00:09:20]
In fact, we'll beep out every use of our word monad in the text.
[00:09:24]
Exactly.
[00:09:29]
We will censor it so no one hears the M word anymore.
[00:09:34]
Keep our PG rating.
[00:09:39]
Actually, the only people who talk about monads are the ones who try to convey that information from Haskell to Elm.
[00:09:51]
Yeah, but it's funny because it's also such a common pattern that happens all the time as soon as you want to work with the fakes.
[00:09:59]
Monads are everywhere, even if we ignore them.
[00:10:03]
But when I became a more serious Elm developer, I understood why Evan would choose to just ignore those names.
[00:10:10]
Because they tend to be confusing.
[00:10:12]
And you don't need to know the names to use the patterns.
[00:10:14]
Definitely.
[00:10:15]
Yeah, I find it confusing. Even the term fmap. Does that mean functor map? Is that why it's called fmap?
[00:10:24]
Yeah, because since Haskell started as a research language, it didn't even have a way to do I-O at the beginning.
[00:10:32]
It lived in the world of purity, so you could literally do nothing useful with it, according to its author.
[00:10:42]
So they started to develop some patterns to call things.
[00:10:47]
And sometimes things have a weird name.
[00:10:49]
For example, there's a map function, but it only works for lists.
[00:10:53]
So then once they generalized the functor pattern, so to say, or the functor type class, they said,
[00:10:58]
Hey, map is already taken for lists. So what do we do?
[00:11:01]
Okay, we will just call it fmap. Or something along those lines.
[00:11:06]
Actually, do you know what the history is here? Is it like, these terms come from mathematics, and they were ported to Haskell?
[00:11:15]
Or were they mostly found out in Haskell or other ancestor languages to that?
[00:11:21]
And then people say, Oh, it's just mathematics.
[00:11:24]
Yeah, I can be wrong answering this, but I will give you what I know from my understanding.
[00:11:30]
There is a word, there are a few words that we don't want to say in Haskell as we don't want to say in Elm, which are category theory.
[00:11:37]
What?
[00:11:38]
You know.
[00:11:39]
Oh, really?
[00:11:40]
Do you know about category theory?
[00:11:43]
Yes. I mean, I know of it.
[00:11:47]
I don't know about it.
[00:11:50]
What do you mean Haskell developers don't want to talk about that?
[00:11:52]
That's, isn't that just the whole thing around functors and monads and all those things?
[00:11:58]
Exactly. Yeah, but we try to speak the least possible amount about category theory, because obviously, since it influences all of these crazy names.
[00:12:07]
But sometimes, for example, if you talk to a mathematician who happens to be an expert in category theory, they can tell you that the functor defined in Haskell or the monad defined in Haskell is not actually 100% right.
[00:12:19]
So that's why I don't want to get too much deep into that forest.
[00:12:25]
So for a category theory purist, Haskell is not pure enough for a category theory purist. I see.
[00:12:31]
Exactly.
[00:12:32]
I see. Elm is not pure enough for a Haskell purist. Okay, I see.
[00:12:37]
And O'Camel was right the whole time.
[00:12:40]
Yeah, actually, I think there's an argument to be made for the O'Camel functor. It's just that I don't know the argument.
[00:12:46]
But probably someone with more experience than me that will probably explain why it's called a functor and what it has to be with a category theory functor.
[00:12:55]
Yeah, I'm guessing they didn't come up with that word out of nowhere. Like, oh, let's find a word for this. Functor sounds nice.
[00:13:03]
I think only one person did that ever. And then that's the functor that we hear about everywhere.
[00:13:10]
Yeah, I think while researching or while developing Haskell and learning about all these patterns, obviously, you know that FB has a solid foundation in math.
[00:13:19]
So you turn to something to look up for names, right? And they said, hey, so there is this really abstract branch of mathematics called category theory that happens to have all those fancy and cool names we're looking for.
[00:13:34]
Why don't why not? We can start using them right away. That's my guess. My wild guess.
[00:13:42]
Yeah, I mean, and they, they, there certainly are useful ideas here. It's just like, it feels like there's such a large barrier to entry.
[00:13:53]
But, you know, I have I have thought, like, sometimes you'll see an Elm API that, you know, maybe it has like map two and map three, but it doesn't expose an end map, which you can derive, you know, as Joel talks about in his post running out of maps, you can derive it from those building blocks.
[00:14:15]
But we sometimes don't have the conventions for which of these things should go together. Like if something is applicative, what building blocks should we have?
[00:14:28]
You know, the idea has crossed my mind. What if we had like an Elm review package, some community standard and came up with a set of conventions?
[00:14:38]
And we said, okay, well, like what? Because really, sometimes even just the terms that we use for these ideas, like, is it called concat map? Or is it called and then or is it called or whatever?
[00:14:53]
Exactly. And you find you find instances of these different terms in different packages. And you have to look at the type signatures and know this is sort of the same type of thing that I can do, or have a little bit of a background in Haskell or category theory or whatever, and kind of squint your eyes at it.
[00:15:12]
And you see it's the same thing. But wouldn't it be interesting if we had a community standard and an Elm review package that said, well, what's the name for this group of things? What are the standard function names and signatures?
[00:15:26]
I'm not sure if Elm review, would Elm review be able to look at the type signatures of something in order to do that? Yeah, you can do that. Okay.
[00:15:34]
You can look at the type signatures as long as they're written in the code. Yeah. So then it should be possible to say like, this is a mappable thing. And then it could, it could verify it could give you an error if it doesn't have something called map that has the appropriate signature, right?
[00:15:49]
Yeah. There's also the opposite thing, where, for instance, in Elm review simplify, I tried to simplify code that uses maps. But if I don't know what the type is, then I can't do the simplification. Like if I see list on map used in a certain way, like on an empty list, then I can know, okay, well, this is useless, I can just simplify it to an empty list.
[00:16:13]
But if I have a map function that whose type I don't know, well, I have no clue whether that will, in fact, respect the laws of functors as they do it in Haskell. So, so we like we have a name we have a that is pretty standard, but we don't have all the guarantees that go with it.
[00:16:34]
Like potentially, it's just a map that creates something, or maybe it's a map that has the same signature, but it does something entirely different. And that's, that's a bit annoying, but we don't have that information of does this follow those rules, those laws, because we could do something, a few things that would be quite nicer, I think.
[00:16:52]
But that is the case in Haskell, right? So that's some type says, well, I'm implementing or deriving or whatever, or what the term is, a functor. And therefore, this map now has to prove, does it have to prove that it follows some rules? Or does it not have to?
[00:17:12]
Yeah, you can check if it follows some rules. There are well, you can use one of the most used libraries is QuickCheck for property-based testing. And it comes by default for example, for checkers for the functor laws or the applicative laws or monad laws, and you can you can check.
[00:17:28]
But if you use, for example, the deriving mechanisms from GHC, from the main Haskell compiler, whatever the GHC derives as a functor instance, you can be pretty sure that it's going to be a correct functor.
[00:17:43]
Unless there's more than one way to implement something which happens with some other instances, like with monoid instances, for example, or certain operators or for certain operations, there can be more than one valid implementation, then you need to disambiguate. But it's not so common with functors, I would say.
[00:18:05]
Yeah. So one thing that I was wondering about is like, you made this suite of blog posts for Haskell for Elm developers. Why specifically Elm developers? Is it because all of your colleagues are Elm developers? Or is it because like, we're doing something wrong? Or, well, we are the closest ones to be able to reach for this information, like JavaScript developers, they're not close enough, but Elm developers, they just have to know the names.
[00:18:34]
And it's good.
[00:18:35]
Yeah, exactly. I think knowing Elm makes you closer to Haskell than knowing any other language. Because you could consider to some point Elm to be some kind of subset of the Haskell language, like a very small subset. And then I'm a little lazy as a functional engineer, you know, and then I was wondering, hey, I really want to teach Haskell. What's the best audience to teach Haskell to?
[00:19:02]
Because they have very little to learn, actually, if they want to, to learn Haskell. And also it happened to be that yes, my previous job was was Elm. And I was finding more and more Elm developers in Twitter, especially on Slack, interested into the Haskell language. I was, for example, part of the Haskell channel in the Slack. And there were many questions. So I could see there was some some kind of community or a pool of people that were trying to learn Haskell. And I thought, okay, I'm going to try to teach them.
[00:19:31]
And I thought, hey, I might have something to say in this domain, that might be useful for someone else. And I really, it got me excited, because I love Haskell. And I love Elm. And I said, hey, if whatever I struggled to learn all those years, is useful for somebody else and wants to have a head start on Haskell, I'm happy to write those, you know.
[00:19:51]
Do you think with hindsight, that you would have preferred learning Elm before Haskell? Would that have made it easier? Yeah.
[00:20:01]
Yeah, for sure. Because it's like starting small, and then using stuff and getting proficient with things and then having the next jump. But in retrospective, I cannot regret my decision too much, because I'm hearing from some Elm developers that the gap is actually bigger than I expected when they tried to actually learn Haskell.
[00:20:23]
So going the hardcore path first of trying to learn Haskell, it paid the dividends in the end, you know, it was it was worth it, because I learned Haskell, I learned Elm, and I also got to understand OCaml and Reason and many other functional languages, Fshare, for example, I was trying Fshare for some time.
[00:20:43]
And I could feel that the hardest part of the language I already understood, which was incredible for me. So it was time well spent, definitely. Gotcha.
[00:20:53]
I've always found Haskell to be intimidating, personally. And I mean, I think some of it is like, some of the names and operators seem cryptic to me. And it just is like a lot of concepts that are hard to make sense out of. But I'd be curious to hear your thoughts on like, for an Elm developer, what types of things might they do with Haskell?
[00:21:18]
And if they, you know, if they paid that cost of learning Haskell, what are the cool things that you can build with Haskell?
[00:21:25]
Except an academic career.
[00:21:29]
Exactly. So, you know, now there's all this fuss going on about probably having Elm in the backend. We don't know yet, right? But there's some expectation. But before that, we don't actually know what's going to happen. Hopefully, we will have some news regarding that.
[00:21:48]
But if you wanted to experience this feeling of you being in control of your code, of the compiler assisting you, of checking every possible case of your code, etc. If you wanted to have the same experience you have with Elm, but in the backend, or for something, for a more general purpose programming language that could pretty much do anything, then you needed to look for alternatives.
[00:22:10]
For example, Haskell is one of them. And yeah, even though the learning curve is a little bit different, but you could do what's no Red Ink does, for example, using a small subset of Haskell as well and have packages and imports look like Elm code. And their Haskell pretty much resembles Elm. So that's a very interesting approach as well.
[00:22:31]
Is it called like cherry prelude? Is that what their helper is called that gives you like an Elm-like core?
[00:22:39]
I think I've looked at nri prelude is what they use like a Haskell package that turns your Haskell prelude into an Elm-ish kind of looking prelude. So that's interesting, but maybe they do something else as well.
[00:22:55]
Yeah, Teresa has a package called cherry core, which is a set of basic functions, but maybe they are influenced by each other. I think at some point they may have been using Teresa's cherry core.
[00:23:10]
Interesting. First time I hear about it.
[00:23:12]
So one of the things that always seemed really cool to me, but also a little bit cryptic is the do notation. So maybe we should first talk a little bit about monads. We have talked about them on previous episodes, but do you want to do your best monad elevator pitch?
[00:23:34]
In 10 words or less.
[00:23:37]
Wow, that's gonna be difficult. Well, basically monads allow you to conditionally chain computations, basically. So when you need something like an fmap or a mapping function, but you need to have more power over what to do next, and you end up having to use monads.
[00:24:02]
That's why Elm has something called and then, which reads quite nicely and it's easy to understand. But surprisingly, or the list data type, it's called concat map.
[00:24:17]
So Elm was not consistent with that and then name just for lists, because it's easier to understand that you might want to flat map a list or might want to concat map something. But it's the only case in which it is not consistent.
[00:24:32]
Yeah, Elm does tend to use more domain terms for naming things when possible. But then certain things like mapping, sometimes you can have a more domain like term for mapping, but usually it's just map and it's easier to just in the case of map, it's a pretty clear case where you just say, yeah, you're just mapping this data.
[00:24:54]
Let's just learn the concept that you can map things. But yeah, in the case of like, and thening a list, that could be confusing. I will say since our conversation with Joelle, where we talked about some of these category theory ideas, and I expressed my difficulty with reasoning about and thening a maybe type, I now feel more confident doing that and more comfortable. It feels more intuitive.
[00:25:21]
So having the discussion about some of these things has helped me a little bit with building up an intuition for this a little bit more. So I think there's definitely value to it.
[00:25:32]
So yeah, so in Elm, you can end then in a JSON decoder and you can then have access to the actual decoded value in the chain so far and do something based on that. You can succeed or fail or do another JSON decoder conditionally based on that. Or in a task, you can have access to time.now, task.endthen, and then now you have the time.
[00:26:00]
So Elm developers will be familiar with that general concept of and thening something.
[00:26:06]
Yeah, and interestingly, it comes up a lot while trying to use effects, right? Like side effects, like for example, in I think when Elm developers use the task data type, they probably are going to eventually end up using and then, task and then, and that happens very frequently as well.
[00:26:23]
I will want to go back to talk about effects afterwards, but I think Dillon wants to continue on the do notation.
[00:26:31]
Sure, no problem.
[00:26:32]
Well, I wonder if you can demystify that for me a little bit because I've always whenever I see a little Haskell snippet and it uses the do notation, I'm always thinking like that seems very helpful and interesting, but I don't quite understand what does it do, you might say. And I wonder if you could demystify that for me.
[00:26:54]
Yeah, without showing code is kind of difficult, but I will try my best.
[00:26:59]
Basically, when you want to and then a lot of things, and this happened also in some Elm code, right? For example, I'm looking at the implementation of the map five for task. It has like, task XA, task XB, like a bunch of tasks. And when you want to chain those with and then you end up having lots of nested. And how do you call the indentation?
[00:27:24]
Indentation hell.
[00:27:25]
Indentation, yeah. You have like a callback hell, like an indentation hell.
[00:27:29]
Right, right, totally.
[00:27:30]
And you cannot skip it. And I remember, for example, Martin Janicek opening an Elm format issue saying, hey, is there some way to prevent Elm format to do this indentation thing? And it's not the scope of Elm formats to fix that because it's actually code that needs to be indented semantically, right?
[00:27:50]
So do notation is just the nice thing about it is that it's basically just syntactic sugar. So instead of having to indent every single lambda expression after the bind operator, it lets you a way of opening some block of code. And F sharp has this as well.
[00:28:08]
But it is literally like you open a block of code in Haskell, it starts by two, and then you start to bind things into names that are going to be the result of applying that bind operator and the lambda expression.
[00:28:21]
So instead of all your code going deep and deep into indentation, it allows you to have a more flat, like a vertical align kind of code that basically does the same underneath. The Haskell compiler will desugar this into all the bind and lambda expression function calls.
[00:28:38]
But it just looks more natural or easier for the developer to look at the code with do notation sometimes, because people apparently in the Haskell community, some of them are not all of them are not big fans of do notation.
[00:28:52]
That's a completely different topic.
[00:28:54]
So is the do notation something that you would like to see in Elm as well? Or do you think it's actually better not to have it?
[00:29:02]
Well, that's a that's a good question. I think it would not hurt to have it because it's like a syntactic choice. But you know that Elm is so simple that it doesn't often offer you various ways to do the same thing.
[00:29:18]
It prefers one single approved way of doing things. And this makes in the end things simpler. But for example, people building like for example, Martin Janicek building this Elm in Elm compiler and having all these nested lambda expressions, these people would argue that maybe it might be useful.
[00:29:38]
But being an Elm developer by myself, I've come across in a very few occasions of the need of saying, hey, I would, I would really love to do have you do notation right now.
[00:29:50]
It's like you don't often nest your code that much. I know there, there's a case to be made against well, in favor of people really needing this. So I think it wouldn't hurt. But I understand why it's not included in the language.
[00:30:03]
As someone who has to analyze Elm source codes, like the least amount of options that are available, the nicer it is for me.
[00:30:12]
So exactly. The less amount of work.
[00:30:17]
So I will actively give Evan money to not implement a new feature like that.
[00:30:27]
Some people would say a happy medium would be a formatter that allows you to do that a chain of and then lambdas without the indentation increasing every time you nest a lambda.
[00:30:40]
But yeah, so the do notation gives you a way to sequence things if you're comfortable putting the term sequence. Monads don't necessarily always mean a sequence because they could be a list or whatever. But it's a way of if it were tasks and you said task A, you're basically saying task A returns this value A, task B returns this value B, task C returns this value C.
[00:31:08]
So given the return values for each of these, do task A, then give me the return value of it. Do task B, then give me the return value of it. And then you can combine all of those after the do.
[00:31:18]
So you're basically just grabbing a return value from a sequence of and thenable things. And then you can combine them in an expression at the end of that.
[00:31:32]
So now it is one thing that does stick out to me is and actually you mentioned the implementation of task map five.
[00:31:43]
When I think of task map five, I actually think, well, if it's map five, wouldn't it be nice if I didn't have to end then each of them?
[00:31:52]
Wouldn't it be nice if I could do it applicatively? Because you think like an applicative chain, like end map, you think parallel because you can do a sequence of things and then combine them at the end.
[00:32:08]
Whereas end then you get the value and then you can continue with another value of the same type. So if you do task dot and then you need to resolve that task before you can know how to continue.
[00:32:22]
Whereas in an applicative pipeline, you don't need that. So is that a point of controversy in do or a downside in do?
[00:32:32]
Yeah, that's a very good point because in Haskell, there is a language extension called applicative do.
[00:32:38]
So just as you said, so you can use if you toggle on this extension, you can use do notation with applicatives without needing monads.
[00:32:47]
And I know lots of people when you do not need the sequencing aspect of it, like you don't need to make sure that one happens after the other.
[00:32:56]
They will just turn on applicative do and have do blocks with just applicatives, which is also very useful.
[00:33:03]
Hmm, interesting. Okay.
[00:33:06]
Kind of feels like a cheat, like, yes, I can't do this thing in Haskell. Well, you can just enable this extension.
[00:33:15]
Exactly. That's totally how doing Haskell feels like you want to do all sorts of fancy or tricky type things.
[00:33:26]
You ask somewhere around and they tell you, oh, actually, you know, you can do that with that syntax.
[00:33:31]
You think it should be doable, but you need to enable this and this language extension and then it works.
[00:33:37]
It happens really often.
[00:33:39]
Now we're back at the JavaScript babble days where everybody has their own JavaScript syntax.
[00:33:44]
Exactly.
[00:33:46]
Well, this is actually something I was wondering about.
[00:33:49]
Is all the language extensions a problem for Haskell or is it something that introduces a lot of complexity that people try to reduce their usage of?
[00:34:00]
Or is it like, no, everything enable as many as you can.
[00:34:04]
You will thank me later.
[00:34:07]
Yeah, this is a really controversial topic because there are hundreds of extensions and some of them are widely used.
[00:34:15]
And most Haskell developers are using the same, let's say, 30 to 40 extensions.
[00:34:21]
So interestingly, being a JS developer, Haskell is going the JavaScript way now in the sense that they're gathering as a standard to decide on a standard per year.
[00:34:32]
For example, they gathered on GHC 2021, the committee, and they came up with a language extension called default language, rather.
[00:34:43]
That was GHC 2021 that enables this list of like 20 extensions.
[00:34:48]
For all files?
[00:34:50]
Yes, for all files.
[00:34:52]
And you can enable this in your cabal file, which is like your package.json file in Haskell, and then you can basically remove all these language extensions in from your comments in your files.
[00:35:04]
Because now you're using that version of Haskell, so to speak, quote unquote, with all those language extensions enabled by default.
[00:35:13]
But it is an issue because it adds complexity and some of them are a bit frowned upon if you use them.
[00:35:22]
Because, for example, there is one extension called allow-ambiguous-types, which sometimes is not what you want.
[00:35:30]
It doesn't sound great.
[00:35:32]
Yeah, it doesn't sound great.
[00:35:34]
Just for some crazy compiler logic, people who use this, they know what they're doing, but I would not know what I was doing by enabling that extension.
[00:35:44]
And all these language features, is that coming from the academia world?
[00:35:49]
So, from what I understand, Haskell was developed in academia, where people were trying out a lot of things like how would the language work if we did this, if this feature was in there, and all those kinds of experiments.
[00:36:05]
And so Haskell is just, by default, a language that is supposed to be extensible so that researchers can try out things.
[00:36:13]
Is that correct?
[00:36:15]
Yeah, this is what is happening. It started in academia, but then it made its way to industry.
[00:36:20]
And now it's a language that has a really difficult path ahead because it needs to accommodate both communities.
[00:36:27]
So it's still used in academia, for example, it's used for researching algebraic effects and dependent types and really crazy things like the next generation of programming languages.
[00:36:38]
But also there are companies like mine, like Scribe, which is using it to digitally sign contracts and it needs to work and it needs to be 100% correct.
[00:36:47]
So you need to accommodate both audiences in the same language.
[00:36:52]
Yeah.
[00:36:53]
But it's a nice challenge.
[00:36:56]
So we talked about the I-O monad briefly, we touched on that. Could you tell us a little bit about the role of I-O in Haskell and what that is?
[00:37:07]
So you mentioned that originally the Haskell language, you couldn't really do anything in the real world because it was just a pure thing.
[00:37:16]
And then I-O changed that. So what does I-O introduce that lets you do things in the real world?
[00:37:22]
Well, I-O is like a very special monad and it's used for input, output and performing side effects, basically.
[00:37:30]
So once you use code or with the I-O monad, you know that things can go wrong.
[00:37:38]
So it's like when you see the CMD part of your Elm application, it's that special hole in the language that allows you to do things that can crash.
[00:37:50]
And for example, people use it expecting it to be like a pure monad, but suddenly there are exceptions in Haskell and they can throw exceptions within the I-O monad.
[00:38:03]
And people do not expect a language to be able to throw exceptions. But as soon as you are in the I-O monad realm, many, many things can happen.
[00:38:11]
So it's interesting. It allows you on one side, you cannot do a program that does stuff without input output. So you need to eventually use the I-O monad.
[00:38:21]
But as soon as you start doing things with it, you know that you need to be extra careful because that's where the whole realm of side effects and unwanted things can happen.
[00:38:31]
Right, right. Yeah. So there's this kind of advice with all of these things, whether you're using the terms of monads and functors or not, just using an Elm application.
[00:38:44]
There's this advice to do as much work as you can in the land of simple functions that don't know about these complicated things because that's where failures can happen.
[00:38:55]
I mean, really, it's the same idea as we talked about in a previous episode about Richard's talk, Scaling Elm Applications, the general idea that the more guarantees you can have about a function, the better.
[00:39:09]
So like the less things that something can return, the more narrowly scoped, the more narrowly scoped the inputs, the less your brain has to worry about what could go wrong.
[00:39:19]
But I-O, it can do anything, including catastrophically failing.
[00:39:25]
Exactly.
[00:39:26]
Yeah, so you use it with care. But now when you say exceptions, do you mean, of course, the first thing many people will think with exceptions would be something like a JavaScript exception where it's a control flow mechanism.
[00:39:40]
It changes the way the language works. You can throw and you can catch. What does an exception mean in the context of Haskell and I-O?
[00:39:48]
Yes, it means exactly the same. So you have the control flow of your application. You can deliberately throw and catch exceptions.
[00:39:56]
But for example, because of Haskell's legacy, for example, the list.head function is partial, meaning it's not as in Elm that it returns a maybe.
[00:40:06]
If you accidentally called head on an empty list, it will throw an exception. It will crash.
[00:40:11]
So this is an issue. And Haskell has in the prelude a few partial functions that will crash if you're not careful.
[00:40:21]
And that's why people also created new preludes, standard preludes that don't have these issues that provide you, for example, with a non-empty list, a data type and all sorts of useful stuff.
[00:40:32]
But yeah, if you come to the language thinking about, oh, this is everything going to be pure and perfect, then you're up for a surprise.
[00:40:41]
Right. I mean, Haskell is the other pure language, right?
[00:40:48]
Exactly.
[00:40:49]
And then you don't know. No, it's not that pure.
[00:40:53]
Yeah.
[00:40:54]
And I guess PureScript is also somewhere in that realm, but I don't know how it works. I know even less of PureScript compared to Haskell.
[00:41:04]
Yeah, I know very little about PureScript as well. But interestingly, one thing I like, which is quite mind boggling about Haskell compared to Elm, is that Elm is eager and Haskell is lazy.
[00:41:15]
Ah, I was going to ask about that.
[00:41:18]
So I think it's the only lazy functional programming language that is actually used and it's not useless, you know.
[00:41:28]
But the whole concept of it being lazy and having different algorithms for benefiting from this laziness, it's very mind boggling as well, very surprising to me.
[00:41:39]
Okay. Can you explain what lazy is and what eager is as well? And yeah, what can you do with it or why is it useful?
[00:41:47]
Wow, it's a very philosophical question and hard to explain, but I will do my best.
[00:41:52]
So basically in Haskell, you can define functions that are recursive.
[00:41:59]
For example, you can define recursively the Fibonacci sequence and you can define things that should not compile because they depend on each other.
[00:42:09]
If the compiler tries to parse all of that, it should absolutely crash, but it's lazy in the sense that it doesn't compute things until you willingly request it to do so.
[00:42:19]
So Elm chose to be eager for some reason, because I think technically for HTML on the front end was needed, but Haskell is not.
[00:42:28]
So for example, you define your lazy Fibonacci sequence and you say, take five on this Fibonacci sequence and you're making it just to compute up until number five.
[00:42:38]
It will not compute the rest, it will not crash or it will not recurse infinity until infinity.
[00:42:44]
So, yeah, from what I understand is you can create infinite lists, like all the integers, for instance, which is impossible in practice.
[00:42:55]
But because those are actually not computed eagerly, you can do computations on it and just hope that you're not reaching out to infinity because then you have a problem.
[00:43:09]
And I'm guessing there's some performance overhead with that as well.
[00:43:14]
Yes, like there are some functions that, for example, there is foldAl, l, and foldR.
[00:43:20]
So depending on whether you want to fold from left or right.
[00:43:25]
And in Haskell, foldAl is known to have a performance impact because it being lazy, what it will do is just create thunks and thunks and thunks of code.
[00:43:36]
And because it doesn't eagerly evaluate them until it's at the end, it can cause very huge space leaks.
[00:43:46]
Yes, so a thunk is a delayed computation in a function, right?
[00:43:53]
Yes.
[00:43:54]
And presumably it would be a closure, meaning it carries context with it.
[00:43:59]
So it prevents those things from being released in memory.
[00:44:02]
So there are performance implications to that.
[00:44:05]
Yes, so that's why by default Haskellers tend to use more foldR, surprisingly, when they're just folding lists and they don't care about the associativity.
[00:44:14]
But also there are versions of the function which are called very weirdly foldL', which won't cause a space leak.
[00:44:22]
So they're very bad at naming things.
[00:44:27]
Interesting.
[00:44:28]
I'm kind of confused because you say like people tend to not like the list at foldL because that creates all those thunks and performance issues.
[00:44:39]
So they prefer foldR. But if you do that on an infinite list, then are you trying to get to infinity before you get to number one?
[00:44:49]
Right.
[00:44:50]
Or is it just like people won't do that in that case?
[00:44:54]
Yeah, obviously, folding R on infinity will not work.
[00:45:00]
But for finite lists of things, it definitely will work.
[00:45:05]
But if you see the explanation, for example, there's a blog post from Lexi Lambda, which is really great.
[00:45:11]
She was explaining why foldL is so dangerous to a colleague and it ended up being like a full blog post.
[00:45:17]
And it's so interesting the way this laziness thing enables this to happen and why Haskell is the way it is.
[00:45:25]
So I will probably send you a link afterwards so that you can have a look at it.
[00:45:29]
It's really interesting.
[00:45:31]
Yeah, we'll put it in the show notes.
[00:45:33]
So what are other pitfalls that you can have with laziness and also maybe like cool things that you can do that we can't benefit from in Elm?
[00:45:44]
So from my experience, for example, when you are learning algorithms and you see like the mathematical implementation of an algorithm that deals with infinite lists, for example, or infinite sequences of things.
[00:45:58]
If you see the equations, you can pretty much translate those straight from math notation into Haskell and it will work, which is amazing.
[00:46:07]
But it's not really doable in many other languages.
[00:46:10]
But yeah, I think the huge or the main con against laziness is the space leaks thing.
[00:46:18]
But it enables all sorts of also different way of thinking about algorithms that are only like it's a specialized case for lazy languages that they need to think in a different algorithms to solve the same problems that we that we're used to solving with eager algorithms, interestingly.
[00:46:36]
Okay, so if you learn Haskell, if you go from JavaScript to Haskell, you first have to learn about no mutation, you have to learn about recursion, and now you also have to think about well, laziness is a thing you need to.
[00:46:50]
Okay, yeah, that's that sounds like a lot of things to learn.
[00:46:53]
Yes.
[00:46:54]
And also, yeah, coming back to the Elm question, for example, while doing Elm, you learn about carrying and partial application and higher order functions.
[00:47:02]
And these are functional concepts that are great, and you learn them and you use them instantly.
[00:47:08]
But if you come from JavaScript, you might need to learn all those plus, as you said, the laziness and all the crazy Haskell stuff.
[00:47:16]
So you have like a huge pile of things to learn when you want to get into Haskell from JavaScript.
[00:47:21]
The one place in Elm where I can think of this eager thing coming into play is in tests where you you say test and then give it the test name as a string and then you give it you know, usually you give it a left pipe to avoid parentheses and then you give it a lambda with unit.
[00:47:43]
And so that's just a way of creating laziness explicitly that and that's why the API has that because if you did your test cases without that being in a little lambda with with no data with a unit type argument,
[00:48:01]
then the test runner wouldn't be able to defer execution on a test or, you know, choose to parallelize tests by selectively running different ones on different threads.
[00:48:14]
There are optimizations like that.
[00:48:16]
And also, if you have a test that has an infinite loop in it, you can't control which tests you want to run and say, well, I just want to try running this one. Okay, this one doesn't have an infinite loop. This one is green. Try running these other ones. These ones are red. They halt. They complete.
[00:48:34]
And oh, I tried running this one and it infinitely loops. So you wouldn't be able to do that if it was eager because you'd run your test suite and they all just start running and it's an infinite loop and you don't get any feedback.
[00:48:47]
The one place where I would see this be very useful, at least I would see it, I would use it a lot, is when I want to potentially compute something only once. If I have a value that I need to compute, that is pretty expensive.
[00:49:06]
I have the choice in Elm to either do it once, and then it's done for forever, but at the risk of it being computed unnecessarily. If this is done, if this value is passed to a map, for instance, then it's computed once, even if no one will ever reach for it.
[00:49:26]
The other option is to compute it every time that I need it, which means that if I never need it, I will never compute it. But if I do need it multiple times, then I will compute it multiple times.
[00:49:38]
I feel like laziness here really helps because it will only be computed once it's needed. So that feels quite nice.
[00:49:46]
This is something that I've thought about suggesting for the Elm language as well, but I have not come up with a good enough proposal.
[00:49:57]
Interesting. Also, maybe Evan explained why Elm is eager by default, but I'm sure there are some technical reasons why he chose to, and it would be interesting to read all of those.
[00:50:10]
I'm guessing because it compiles to JavaScript, that's a big part. JavaScript is eager as well. So if you want to make it lazy, then you have to add a whole lot of lazifying layer.
[00:50:24]
Yeah. And a lot of the Elm philosophy is reducing the barrier to entry and being very simple and obvious. And lazy is just, it's like a powerful thing, but a sharp knife that comes with a lot of caveats and things to be careful about.
[00:50:44]
And that's just kind of counter to the Elm philosophy. So I think it fits in the Elm philosophy to be avoiding that. Because I've heard a lot of people talking about gotchas with lazy.
[00:50:58]
So it comes with its fair share of caveats and things to be careful about.
[00:51:03]
Okay. Effects. So you mentioned effects before. I know that there are some effects in Elm as well, like in the implementation of things like platform.command, maybe of task as well.
[00:51:18]
And people who do Haskell seem to be familiar with that. And I have no clue what it is. I do think there was a blog post explaining that a few years ago for Elm, which was really good, but I've forgotten everything that it said.
[00:51:36]
I should reread it. But yeah, can you explain to me what effects are and what are they for?
[00:51:43]
Well, actually it's kind of an interesting topic because for me, effects are just like things or computations that happen on, for example, like on an input output or on a side effect kind of world.
[00:51:57]
But I know that when people refer to effects, they are mostly talking about either effect systems or algebraic effects, which is something that also looks to me really scary and abstract.
[00:52:09]
And Haskell, I know there are a few Haskell libraries that try to implement this concept of algebraic effects, but to my knowledge, none of them is like a hundred percent solved all the edge cases and things to deal with those effect systems.
[00:52:29]
But yeah, for example, there is one effects library called Effectful. It's gaining some traction in the Haskell community and the creator happens to work in the same company I do.
[00:52:41]
So my company is fully switching to, instead of using monad transformers, which is like a way of trying to compose and combine monads, is the de facto standard in Haskell.
[00:52:53]
And then they are switching from this monad transformer stack to Effectful to try to use this effect system in a simple way and in a way that the code looks actually readable and usable without understanding all the machinery behind it.
[00:53:08]
But to be honest, since I haven't dwelled too much into it, I am not an expert in effects at all. So I feel as lost as you both.
[00:53:18]
That's fair. That's fair.
[00:53:20]
Is it the same general idea as it is when we use the term effect in Elm, where it's creating a data type that represents a possible effect without executing it?
[00:53:31]
Like a command, if you pass it to update or init in Elm, a command will do something, whereas an effect needs to be given a perform function to turn it into a command.
[00:53:44]
Is it the same concept or is it a different concept?
[00:53:47]
Yes, I know there is something along those lines, like declaratively dealing with effects and computations. But it's a term that is also used with the same name in different languages for different things. So it's quite confusing as well.
[00:54:03]
Oh no, OCaml, what have you done again?
[00:54:06]
Exactly.
[00:54:12]
So on the topic of effects, back to I-O a little bit. One thing that I've wondered, so I've been implementing some effect-y, I-O-y things in Elm Pages.
[00:54:26]
There's a backend task similar to an Elm task, but in Elm Pages v3, it's full stack, so you can do things in a backend context.
[00:54:37]
And so one of the things as I was designing it was I was thinking about Haskell I-O, and I was like, why is there one type variable in I-O?
[00:54:49]
So in Haskell, you can chain I-O to, I guess, like read from a file, and then given the thing you read from a file, you can map that into something.
[00:55:01]
But if an error happens, you don't know what type of error data, if I'm understanding correctly, that is erased from the type.
[00:55:11]
The possible error data is erased in the I-O type, whereas if you look at the Elm core task type, it's task error value, and you can map error.
[00:55:24]
And you can task.perform says it takes a task that never errors, and you can recover from errors in this, you know, I don't want to say monadic style,
[00:55:38]
because I know it's not technically following monadic laws, but that sort of explicit way of chaining things together and knowing the types based on the type signature.
[00:55:49]
So what's the deal with that in Haskell? Why does I-O not have an error type represented in its type variables?
[00:55:57]
So I know that I-O has this type variable because obviously like a monad needs to have it.
[00:56:04]
Right? Because you think about maybe or either, well, which is called result in Elm, it needs to have something to wrap around because it's a wrapper in the end.
[00:56:16]
That's why all the memes of monad being burritos comes from.
[00:56:23]
But basically, for example, the simplest example of people using the I-O monad to perform side effects or to do stuff like, for example, printing to the command line,
[00:56:35]
they're using I-O and then open and close bracket, like the unit type.
[00:56:39]
So it's like, it's an I-O that is going to perform nothing effect. And you will see these in type signatures all across many Haskell code bases.
[00:56:50]
Why it doesn't have more than one type variable? I really don't know, but it's an interesting question.
[00:56:57]
Yeah. Well, so if you can throw an exception, like when you catch an exception, do you know the type of the exception?
[00:57:06]
Do you catch it? Do you catch it through the monad, the I-O monad interface?
[00:57:12]
Or is it just a completely separate thing that is not specific to I-O to deal with exception handling?
[00:57:20]
Yes, it's specific to I-O and all exceptions are typed.
[00:57:24]
So when you catch an exception, the I-O or Elm, you get lots of information from that exception and you can use it to your will as well.
[00:57:34]
You can then use it to your needs.
[00:57:37]
Interesting. Okay. Well, that's nice that they're typed.
[00:57:41]
That's always like the most frustrating thing for me in TypeScript is that you catch an exception and it just completely forgot what might have happened along the chain.
[00:57:52]
You know nothing about it nearly, right?
[00:57:55]
Right, right. Are you forced to acknowledge that something could throw an exception?
[00:58:00]
And I'm guessing no, anything can just throw an exception and you don't have to annotate it or allow it to fail in any way.
[00:58:07]
Yeah, that's the thing, like the Haskell etiquette or the standards that we try when we do pull request reviews and all that.
[00:58:16]
Obviously, you should know do things without signifying this in type signature.
[00:58:20]
But it's kind of like a paradox or like strange that some prelude functions because of legacy or inherited reasons do throw exceptions when you don't.
[00:58:32]
But it's like, okay, there's a few catches.
[00:58:36]
People know about those functions. They are known to be dangerous.
[00:58:39]
So either they use them with care or they don't use them at all and they use some safe alternative from a well-established library.
[00:58:48]
But obviously, yeah, you are doing Haskell for the same reason you would like to do Elm.
[00:58:54]
You would like to trust 100% in purity and not having to worry about strange stuff.
[00:59:01]
But for example, there is a placeholder called undefined, which is like the debug.todoElm thing that you can just give a name and don't implement thing.
[00:59:12]
So that's a keyword called undefined.
[00:59:15]
Undefined takes a string. Is that it?
[00:59:20]
No, no. Undefined is a value on itself.
[00:59:23]
But it's not a function.
[00:59:25]
Yeah, no, it's not a function.
[00:59:27]
Undefined is not a function in Haskell.
[00:59:32]
But there is an undefined and it's considered to be the bottom value is, you know, you're not meant to to ship code using it.
[00:59:40]
But you can signal that something is not implemented by just putting an undefined there in the code base.
[00:59:47]
Gotcha. OK. Yeah, that's useful.
[00:59:50]
You're not meant to ship code with that, but you could ship code with that.
[00:59:55]
Exactly. I think so.
[00:59:57]
Like there's no checking the package registry or something that prevents you from shipping code with undefined.
[01:00:03]
It's so interesting because like in a way you would you would tend to think of like Haskell as the purists, not Elm.
[01:00:13]
But in a way like Elm really the thing that Elm is a purist about is purity.
[01:00:20]
And Haskell is like less of a purist about that, but maybe more of a purist about like category theory.
[01:00:29]
I mean, I guess to some extent, if it's roots in academia, right.
[01:00:34]
And researchers, they try to design a language with language extensions.
[01:00:40]
Well, they don't have to think about all the edge cases.
[01:00:43]
They care mostly about some area of computation of some area of research.
[01:00:50]
And whether you handle all the edge cases is doesn't really matter.
[01:00:54]
So it's fine to have a few undefined in a language if you're only trying to to figure out to answer a specific question where this is not going to happen.
[01:01:04]
And Elm is really more pragmatic or meant for production.
[01:01:10]
So it makes sense that we have it there, though, I guess some people would say, well, if you can't just call JavaScript through FFI, then you're not very pragmatic.
[01:01:19]
But that's a topic.
[01:01:22]
Yeah. Also, I think there is some kind of check in the Elm package registry that prevents you from publishing code with debug.to on it.
[01:01:33]
Right. So it's not happening, basically.
[01:01:36]
So there are security guards against that.
[01:01:39]
Yeah. Thankfully.
[01:01:40]
I don't know when it appeared, though. I'm guessing it was pretty recent.
[01:01:43]
But because you could at least have applications with debug.crash in 0.18 or 0.17.
[01:01:50]
Right. Yeah, I remember that, actually.
[01:01:53]
Yeah. But I don't know if you could have them in packages. That I don't remember.
[01:01:58]
I was worried about it because recently I contributed to Elm format.
[01:02:03]
I added a few linear parser things.
[01:02:06]
And then someone told me, hey, you are parsing this fat arrow into this slim arrow lambda thing.
[01:02:14]
But prior to Elm 19, someone could have an infix operator being a fat arrow.
[01:02:20]
And I was so scared about my PR not being correct, because the only version of Elm I've used is just 0.19 onwards.
[01:02:30]
And it's like, wow. So I really do need to extend now my PR and add a test for this specific case that I wasn't expecting.
[01:02:40]
I remember the convention was to define the fat arrow as creating a tuple.
[01:02:46]
So you could have key value pairs.
[01:02:49]
And instead of doing, you know, like if you did dict from list with a literal list,
[01:02:57]
and then you give a list of tuples, instead of doing dict from list, a list with tuple, some value, comma, some value, next tuple, some value, comma, some value,
[01:03:08]
you would do list value, fat arrow value, comma, key, fat arrow value.
[01:03:18]
Yeah.
[01:03:19]
So it would be like tuple.pair?
[01:03:21]
Yep, it was exactly like that.
[01:03:24]
Wasn't there like the daughter sign for the same purpose?
[01:03:30]
Like, I remember that people did a model daughter sign command at none or something, quite a lot.
[01:03:36]
Oh, gosh, I'd forgotten about that.
[01:03:39]
It's completely left my memory now.
[01:03:42]
But there was something about getting something with no command or whatever.
[01:03:47]
Yeah.
[01:03:48]
Actually, I think, Flavio, in one of your blog posts, you say something like, oh, in Haskell, you can use the comma operator to make a tuple.
[01:03:57]
Well, I think it was there in Elm, and it was the daughter sign,
[01:04:01]
which I guess was even more confusing for people who use Haskell because the daughter sign is something very different.
[01:04:08]
I think it's the function composition.
[01:04:12]
The daughter sign is the left pipe in Elm.
[01:04:15]
Oh, it's a pipe, yeah.
[01:04:16]
Yeah, daughter is used a lot.
[01:04:18]
Yeah.
[01:04:19]
To prevent the usage of parentheses.
[01:04:21]
Right, right.
[01:04:23]
Yeah, and there also used to be the parentheses comma parentheses operator to create a tuple in Elm.
[01:04:32]
As well?
[01:04:33]
Or with two commas to create a triple.
[01:04:36]
Hmm.
[01:04:37]
That used to be there, and now it's tupled up here.
[01:04:39]
But yeah, so all these things, I mean, it is a very different, like, what makes idiomatic Haskell is so different.
[01:04:47]
And it definitely seems like in the Haskell ecosystem, people really like being able to express things tersely with operators.
[01:04:55]
And in Elm, we shy away from that.
[01:04:58]
Yeah, we don't use those Greek letters as well.
[01:05:01]
Right.
[01:05:04]
One thing you mentioned in your blog post on Functors Flavio is about function composition and about how any chain of list.map with functions or any sort of chain of maps can be turned into a single map with the composition operator.
[01:05:23]
Like greater than, greater than operator, or less than, less than operator, if you're into that sort of thing.
[01:05:29]
But some people in Elm I've noticed shy away from the composition operator.
[01:05:38]
And sometimes I myself find it difficult to reason about things in a pipeline versus a lambda.
[01:05:45]
So I'll opt for doing a lambda.
[01:05:48]
What are your thoughts on this sort of like point-free style?
[01:05:52]
From what I understand, that's very common in Haskell.
[01:05:55]
Yeah, even within Haskell, there is like a fight, you know, that people prefer Terrace programming or Taft programming.
[01:06:03]
Some of them prefer point-free and some of them don't.
[01:06:06]
But personally, I'm a bit of a point-free freak.
[01:06:09]
So I need to refrain myself from using too much point-free code.
[01:06:14]
And I have this issue with Elm as well.
[01:06:16]
So I reach for the composition operators very often, and only when I get some complaints from my Elm colleagues saying this is unreadable or this is not maintainable or blah, blah, blah.
[01:06:28]
Then I will change the code to use Python, for example.
[01:06:31]
But yeah, the thing I wanted to say or to convey in my post that I think I a little bit failed is when you cannot talk about functors or applicatives or monads without speaking about laws.
[01:06:43]
So they must fulfill certain laws.
[01:06:46]
And one of the very simple laws for functors is that they need to preserve composition.
[01:06:51]
So if you compose things that do nothing, for example, if you compose with the identity function, nothing should happen.
[01:06:59]
It's equal to doing basically nothing.
[01:07:02]
And this little law is what makes possible that you can either map n times or just compose n times and then map once, which in JavaScript tends to be like a performance optimization.
[01:07:14]
But it depends on how your language implements these things.
[01:07:18]
Right. Right.
[01:07:20]
So what would be your pitch to somebody to consider using point-free style or something closer to point-free style more often?
[01:07:29]
Well, it depends on the case by case scenario.
[01:07:34]
But, for example, when you're using mapping functions or functions that return a Boolean, like in a condition to some other function, sometimes I feel like by adding a lambda with an extra argument adds a little bit of noise.
[01:07:48]
And if you get used to composition and you, for example, in Elm, the composition operators read really nicely because you can either do left to right composition or right to left composition.
[01:08:00]
And when you see the functions, you clearly you can read in the code, oh, we are running this function, then the next one and then the next one.
[01:08:08]
And I don't really care what the name of the argument or of the lambda is, because I just don't care because it's a function that expects a function.
[01:08:16]
So I would find that sometimes it reads a little bit better, but it's totally a personal choice and a matter of usage and about a personal intuition.
[01:08:26]
So I would not force it on my colleagues where they are learning functional programming, for example.
[01:08:32]
You need to be very, very soft and very not strict about it.
[01:08:38]
I see. So you think that giving names to parameters in anonymous functions is pointless or point free?
[01:08:46]
It's pointless.
[01:08:48]
Only if you're a point freak.
[01:08:55]
Exactly. All the puns were coming.
[01:09:00]
Yeah, it depends on the scenario. But yeah, I think sometimes the lambdas add a little bit noise and they are a bit pointless.
[01:09:08]
So then I prefer point free.
[01:09:11]
I see. That's interesting. Yeah, I think that makes sense. I would say on the other side of that, you know, sometimes I think about like certain designs and idioms resist refactoring.
[01:09:24]
I remember in my Ruby days, people talked a lot about this with certain language constructs that like using unless instead of if.
[01:09:35]
And it's just like, well, OK, you can use unless, but you're introducing potentially a lot of churn if that condition changes in the future.
[01:09:45]
Or if you add an else clause, now are you going to have an unless else? That's just weird.
[01:09:51]
So some people just say, let's just not use unless because it's prone to churn.
[01:09:58]
And one thing that I've found is that like using the composition operator greater than greater than is convenient in some cases.
[01:10:08]
And in some cases I do reach for it.
[01:10:10]
But I do find myself flipping that to a lambda with an explicitly named argument pretty often, and it creates a little bit of churn in the code when I'm refactoring.
[01:10:23]
So that would be one argument against it. Also, it's like there's a certain point where you're like, well, I'm having to think pretty hard about what this is doing when you're chaining like enough things together.
[01:10:35]
And you're like, if I just if this was just like a lambda, I wouldn't have to think quite so hard.
[01:10:41]
So I think like that's one argument I've heard in the opposite direction.
[01:10:45]
I think I think there's a reason for both sides.
[01:10:48]
Maybe the right answer is somewhere in the middle.
[01:10:50]
Yeah, for me, it was there's a talk called Tacit Programming Point Free or Die or something like that that was given at Strange Loop some years ago.
[01:11:00]
And it is more reasonable than the title makes you think.
[01:11:04]
But it is basically Haskell code.
[01:11:07]
And for example, you want you you're used to seeing JavaScript code when people will just open a lambda, say X lambda, then console log of X.
[01:11:16]
Right. So functions with one point, you see that and you say, oh, the point is pointless.
[01:11:23]
So the X is pointless. And you can just say console log as a function.
[01:11:28]
Right. But when it gets crazier in Haskell is when you do not have one function argument, you have two.
[01:11:34]
And they're passed as they are giving A and B to the next function A and B.
[01:11:39]
And you cannot you cannot do this the same way.
[01:11:42]
But there is an operator called the Blackbird operator, which I just learned about like a month ago that allows you to pass these two point function the next function.
[01:11:52]
But the code starts to get really, really messy and really crazy.
[01:11:56]
And that's why it says, you know what, do what's readable.
[01:12:00]
If if that's the programming reads better, sometimes just use it.
[01:12:04]
If it doesn't, please don't overuse it. Basically, but it's a really great talk.
[01:12:08]
Right. Yeah. I mean, at the point that you start reaching for for flip, then you might consider just naming that argument in a lambda.
[01:12:19]
Yeah. Yeah.
[01:12:21]
I miss flip a lot of times in Elm.
[01:12:26]
Yeah, but it's because of me being a pointless freak.
[01:12:31]
Right.
[01:12:35]
I guess that's why carry and uncarry and flip were removed from the prelude of Elm, you know.
[01:12:41]
Right. Now, OK, why don't you go ahead and define those for those of us who haven't used them?
[01:12:47]
I don't think I ever used I don't think I ever used any of those, actually, even back in the 18 days when it was around.
[01:12:54]
I'm sure I did either. But I started basically with 0.19 as well, even though I did it back a little bit back in 0.16.
[01:13:03]
Flip is a function that, for example, expects a function that takes is a function that expects a function that takes two arguments and then returns a result.
[01:13:13]
So you have a B and the result would be C. So sometimes the order of the arguments you expect for a certain function is B, A and then X.
[01:13:24]
But you you cannot change the order because of the way your code works or whatever.
[01:13:28]
So this flip function is very convenient because it lets you flip the arguments of the function you're calling.
[01:13:34]
And it's also a mathematical concept because you can also flip functions mathematically.
[01:13:39]
But it is kind of magic that this does that this works. But if you see the function implementation, it's not magic at all.
[01:13:45]
You know, right. So flip does that. And there there's uncarry.
[01:13:50]
You know that all the functions are carried in Elm, which means that they all receive just one argument and return a function that also returns expects one argument and returns one.
[01:14:01]
And there is a case in which you might want to uncarry a function so that it receives all of the arguments at the same time.
[01:14:09]
And the only way to do this in Elm is to use uncarry. And now instead of expecting arguments one by one, it will just expect a tuple of arguments.
[01:14:18]
And then it will give you a result.
[01:14:21]
Right. Interesting. And most mostly useful for for chaining things in a point freestyle.
[01:14:28]
Yes, I tend to reach for those basically when you are doing crazy point free stuff.
[01:14:34]
You find that you haven't defined your functions in the best order possible because for composition to work properly, the functions need to be data last.
[01:14:43]
Right. And for functions that have expect more than one argument, you sometimes do not choose the correct or the order that you were expecting.
[01:14:51]
And you end up flipping lots of functions.
[01:14:54]
Yeah. Things like dig.union or set.diff. Probably more the diff than union.
[01:15:03]
For diff is really tricky because you think that you're doing the same thing, but depending on the order, you might be diffing two completely different, getting two out.
[01:15:14]
Yeah. So that one definitely I tried to avoid using composition because it's difficult enough already.
[01:15:24]
Yes, it's true.
[01:15:26]
So maybe to round off, we've seen that Elm and Haskell are very similar languages where they look a lot like each other.
[01:15:33]
So it's pretty easy to jump from Elm to Haskell relatively compared to other languages at least. And Haskell has quite a large learning curve.
[01:15:44]
So we've talked about category theory. We've talked about laziness. We've talked about language extensions.
[01:15:49]
Well, we kind of did talk about category theory and you also told us to shut up about that.
[01:15:59]
But yeah, what other concepts are useful to learn in Haskell? Or would someone who goes from Elm, who would go to Haskell, what would they need to learn on top of these?
[01:16:11]
Or in other words, what could your next blog post be about?
[01:16:16]
Nice. So there are a couple of things. For example, we talked a little bit, well, I talk in my posts about infix operators.
[01:16:23]
Elm has some of those, but you cannot user define them anymore.
[01:16:29]
So the most famous ones in Haskell are the ones in the Lens library, which is a library that is very famous because it produced a set of Lens libraries for many other languages.
[01:16:41]
A Lens is basically something that allows you to, it's a functional getter and setter that allows you to manipulate data on a deeply nested data structure, basically.
[01:16:51]
And they come from Haskell and they are useful in JavaScript, for example, to traversing JSON, really huge nested JSONs and doing things on the level you want.
[01:17:01]
And we don't have, unfortunately, per se, Lenses in Elm, but I know that there are people who build their own Lens libraries or they're building code generators for Elm that are able to use Lenses in Elm.
[01:17:15]
But it's not like a common pattern that it's used in the Elm community.
[01:17:18]
There are a few packages out there.
[01:17:21]
At least a couple, right?
[01:17:23]
At least a couple, which is a lot for the Elm community.
[01:17:28]
In terms of packages and for something that is sometimes frowned upon.
[01:17:33]
You have your set of choices.
[01:17:37]
So Lenses would be one thing.
[01:17:40]
Well, getting acquainted with a lot of crazy infix operators is another thing.
[01:17:45]
And also just knowing that there are many ways to do the same thing.
[01:17:50]
Like, for example, there are two or three ways of doing pattern matching in Haskell, whereas in Elm there's only one.
[01:17:56]
The case of syntax.
[01:17:58]
There are pattern guards and pattern synonyms, which are things that are meant to help you simplify pattern matches.
[01:18:05]
And many other constructs that we don't have, but I would bet you don't really actually need in Elm.
[01:18:15]
Also, I'd like to say that even though it's not language related, but ecosystem related.
[01:18:21]
Because of the Haskell academia nature, there is no formatter that came by default with the language as we do have with Elm format.
[01:18:31]
And in the build tool, which is Cabal, there's also a war.
[01:18:35]
There's one called Cabal and one called Stack.
[01:18:38]
And both of them work, but both of them have serious deficiencies.
[01:18:43]
So it's such a bless to have something like the Elm compiler or something that is curated and that comes by default with a formatter with one single style to rule them all.
[01:18:54]
And it's something I really, really, really miss in Haskell sometimes.
[01:18:59]
I would be invited on a Haskell podcast to teach Elm for Haskellers.
[01:19:04]
Flavio would be a much better person to be on that podcast episode as well.
[01:19:09]
I mean, the episode would be pretty short.
[01:19:12]
Like, take Haskell, remove everything, and you've got Elm, basically.
[01:19:18]
And please use a formatter.
[01:19:21]
You know functions? Yeah, we have those.
[01:19:26]
Functions over data? Yeah, that's Elm.
[01:19:30]
I guess something I hear a lot is GADTs, but I don't know if that's a language extension.
[01:19:38]
Yeah, it is a language extension.
[01:19:41]
Generalized algebraic data types.
[01:19:43]
Which is like our custom types, but more powerful.
[01:19:47]
It's a custom type, but it's a polymorphic custom type that you can parametrize just as OCaml functors.
[01:19:53]
So I haven't really reached for GADTs in Haskell,
[01:20:01]
because they come up when you really need to do really abstract, really complex stuff.
[01:20:06]
But for many library authors and compiler creators as well,
[01:20:12]
I know they reach for GADTs very often.
[01:20:16]
And I know there are some measures, some things similar to GADTs in OCaml as well.
[01:20:23]
And people are really crazy about GADTs.
[01:20:25]
But for the average programmer, even the concept of it, even for me, it's crazy.
[01:20:31]
It's very hard to grasp, very hard to use, and very hard to understand.
[01:20:35]
But yeah, there are GADTs.
[01:20:38]
Gotcha.
[01:20:40]
I'm sure Dillon would use GADTs in Elm pages or in the Elm GraphQL for sure.
[01:20:45]
I've definitely thought about some of these things.
[01:20:48]
There are definitely certain compromises to Elm's simplicity.
[01:20:53]
And it's actually impressive how often we can get by without running into limitations
[01:21:01]
with the number of language features that Elm gives us and their simplicity.
[01:21:05]
Like a custom type is such a simple thing.
[01:21:10]
And there's an extreme elegance to just how little there is to that concept.
[01:21:15]
But I definitely have thought as a package and framework author,
[01:21:20]
at times I'm like, hmm, yeah, if there was some way to have a more general way
[01:21:25]
to deal with this, like to be able to let users have a more customizable
[01:21:31]
custom markdown block type.
[01:21:35]
We don't have a lot of tools for extensibility.
[01:21:38]
So we end up using a lot of code generation as framework and tooling authors
[01:21:43]
to create extension points or very clever design.
[01:21:48]
But we've actually managed to figure out a lot of clever tricks to do these things
[01:21:53]
and give a pretty good experience to the user at the end of the day.
[01:21:56]
But it takes a lot of love as a designer of these tools.
[01:21:59]
Yeah.
[01:22:01]
I'm guessing in your case, a Haskell person would come in and say,
[01:22:04]
hey, I have a language extension for this.
[01:22:08]
Or it's just baked in that a type class would solve this problem for you maybe.
[01:22:13]
Exactly. Yes. How could I forget about type classes?
[01:22:16]
This is the main thing that you need to learn from Haskell,
[01:22:19]
for Haskell if you are coming from Elm, basically.
[01:22:23]
And it's also the crown of Haskell, right? Type classes.
[01:22:26]
And it's an idea that I think it was born with Haskell,
[01:22:30]
and it's extended to traits in Rust, and it influenced greatly many other languages.
[01:22:35]
Because it's such a great thing as well.
[01:22:38]
Right. So would a type class allow you to say that this function takes a data structure
[01:22:45]
that you can index into, but you could use an ordered dictionary or an unordered dictionary,
[01:22:54]
and it would support operations on both because it needs a certain class of functions?
[01:22:59]
Exactly. Yeah. Some people compare them to interfaces in object-oriented programming,
[01:23:03]
but they're a little bit more powerful than interfaces.
[01:23:06]
So, for example, we've discussed about monad and functor and applicative functor.
[01:23:11]
Those are basically type classes in Haskell.
[01:23:14]
You can define them, define the methods that they need to implement
[01:23:17]
to be considered instance of a certain type class.
[01:23:20]
And also we have type class tests and property tests that we can check against
[01:23:25]
to make sure that a type is a semi-group, a monoid, or whatever type class.
[01:23:31]
Yeah. So much stuff to discuss.
[01:23:35]
So if somebody wants to do a deeper dive into Haskell,
[01:23:40]
do you have any favorite resources to point them to?
[01:23:43]
I think there's the Elm Guide, which is a good introduction.
[01:23:47]
Definitely.
[01:23:49]
Actually, for me, the ideal learning path would be, for example, learning JavaScript.
[01:23:55]
And then, for example, I read Richard Feldman's Elm in Action,
[01:23:59]
which I think it does a pretty good job teaching Elm to a JavaScript mindset.
[01:24:04]
And then from Elm to Haskell, well, I obviously would recommend my blog posts,
[01:24:09]
my series of blog posts.
[01:24:11]
Yes.
[01:24:12]
But as well, this Haskell book, Learning Haskell from First Principles,
[01:24:17]
it's an excellent resource, even though it's a little bit long.
[01:24:20]
But it just does a perfect job of explaining from almost ground zero what you need to know.
[01:24:26]
It almost teaches you what the string is and a function is
[01:24:30]
before dipping dive into... diving deep, sorry, into Haskell.
[01:24:35]
Is it one of those books where Hello World is chapter 12?
[01:24:39]
Exactly. Yes. Or chapter 20. Yes.
[01:24:42]
Okay.
[01:24:45]
I think Monads, I think Functors is chapter 12,
[01:24:49]
and then Applicatives 13, and then Monads is chapter 16 or something like that.
[01:24:55]
I have to confess, I read chapter one of this book years ago,
[01:25:01]
like six years ago, about lambda calculus.
[01:25:04]
And I diligently went through...
[01:25:06]
Skip chapter one. You can skip it.
[01:25:10]
I diligently went through all of it.
[01:25:12]
I had my pencil and paper and I did those lambda calculus exercises
[01:25:16]
and I understood lambda calculus a little better.
[01:25:18]
And then I'm like, okay, I think that's all.
[01:25:21]
That's it.
[01:25:23]
Exhausted my energy for going through it.
[01:25:26]
Yeah, I think I kind of understand why they decided to put lambda calculus on the first chapter.
[01:25:32]
But I know that many people haven't gone through the first chapter.
[01:25:36]
So it's a terrible entry barrier.
[01:25:38]
You can pretty much read the book without reading the first chapter.
[01:25:42]
But it's basically like we do in Elm, programming with expressions, not statements.
[01:25:48]
So this is an invaluable skill, in my opinion, that will get you really, really far,
[01:25:53]
understanding the whole expressions thing.
[01:25:55]
Right. Yeah. Well, great stuff.
[01:25:58]
And Flavio, thanks again for joining us.
[01:26:00]
And thanks for your blog posts.
[01:26:02]
And looking forward to hearing more in the future.
[01:26:04]
My pleasure. Thank you. Thank you both for having me.
[01:26:07]
Thank you for coming.
[01:26:08]
And Jeroen, until next time.
[01:26:10]
Music.