The blog post is an exploration of an alternative way to structure code in Haskell.
Why is the bar such that Haskell blog posts have to either demonstrate something clearly better then the status quo or that they need to explain the fundamentals of the language?
The audience is going to meet the article where they're at.
It's fine for, say, a blog post aimed at Haskellers to assume Haskell knowledge, but when posted on a board largely consisting of people without Haskell knowledge, it's natural that you're going to get at least a few people saying, "hey, I don't understand this".
But I'll be honest - I'm familiar with Haskell and the ML module system and the underlying concept (that typeclasses and modules are roughly equivalent in some sense), but I'm unfamiliar with Backpack so I still struggled to follow it a little. The target audience is an extremely narrow niche. So I think it's just somewhat poorly written; it doesn't feel like the author really had an audience in mind, other than themselves. There's probably ways of writing this - without spending too much time regurgitating the basics - that would be more palatable to more people.
> The audience is going to meet the article where they're at.
I hear you on this point but anyone can post anything on this forum. The burden should not be on the author to write a post that aligns with whatever forum their blog might get posted onto.
The author is free to ignore any and all complaints they consider unfounded. It’s not even like the author is recieving any complaints personally; they have to come here to see any. And if they come here, they will get to read the viewpoint visible from here.
... which won't mean much to someone who doesn't know Haskell. The kind of person who might click on this link thinking "types and classes are the same thing, what does he mean 'type class'".
(don't @ me with your pedantic bullshit about how types and classes are different, I don't care).
And I wouldn't be surprised if there were more retired left handed surgeons in their 50s living in rural Switzerland than people who understand what he's talking about.
Which highlighted the fact that typeclasses can basically be thought of as an additional vtable argument passed to every function that has typeclass constraints. (And, indeed, thinking about them this way often allows one to avoid typeclasses entirely and achieve the same kind of polymorphism in simpler and more flexible ways.)
Backpacks can achieve something similar, except by declaring an abstract "signature" for your dependency, rather than a function argument. (Backpacks can also be used to do a lot more than this, of course, but that's outside the scope of the OP article.)
This was a wonderful comment that added a lot of value to the article for me. I have a random curiosity if this was generated by a model or human written. I'm hoping for the later as this seems too well processed to be a LLM, but I'd like to check my assumption.
I'm a pretty average programmer/ex-programmer/hn-commenter in this context. I have never programmed in Haskell though in college I programmed with the language "fp" that is naturally functional.
I can tell this is article is about a common, wide debate in CS; should languages have "strict" structures like type class or loose structures like "objects". This related-to but not the same as the debate on whether to have pure functional languages or ad-hoc imperative languages.
I know in programming practice, everything ad-hoc has won but programming language "theory", everything strict has won.
Now in this context, I understand the post as advocating a certain kind of loose data typing with the strict-world of Haskell. Which I'd imagine won't make any ideologue happy. But seems like an OK contribution to the debate.
To add my own takes on everything, as hners must do, I think the strict structures of functional programming have quite a bit of merit for various purposes. BUT - they aren't intuitive/the-easy-way-to-everything-once-you-know/etc. AND they aren't a way to solve the software crisis.
In this case that’s the >>= from Maybe.Monad. As long as you satisfy the signature, it’s happy . do has nothing to do with Monads! Who lied to you?
Could have been a stronger point by using a non-monadic >>=.
I've been doing a lot of parsing lately and I find I don't need to reach all the way for monad (applicative is usually enough). But I guess that's what ApplicativeDo[1] is for.
We’ve got to be explicit now which Functor or Monad we’re importing, and you can’t have do notation for different Monads in the same module.
This is a bit rough for usability (not to mention also undermines the point above).
But overall I like the approach of trying something radically new.
ApplicativeDo can be thought of as a compile-time transformation that turns sequential code into possibly parallel* code just by analyzing the way variables are used. Once I've had that realization I really appreciated it more.
*: By "possibly parallel" I mean the blocks of code are known to be independent from each other. They could be actually executed in parallel if the Applicative instance does so.
Indeed so, but that would require a few months worth of work from the reader, unfortunately.
(I have a t-shirt with a lambda in a circle, reminiscent of the anarchist emblem, and words "no class, no state". It's definitely possible to explain to a passer-by who never studied FP what it refers to, but not in such a way that the joke remained funny. Possibly the same deal is with the bumper sticker saying "my other car is cdr".)
"Humor can be dissected, as a frog can, but the thing dies in the process and the innards are discouraging to any but the pure scientific mind." - E. B. White.
Some observational humor is timeless. Consider this entry from the same collection of Sumerian proverbs: "The dog understands 'Take it!', but it does not understand 'Put it down!'"[0] later repopularized as "No Take, Only Throw"[1].
Likely never. The Half-Life has the lambda with the top crooked, neatly inscribed in a circle [1]. The anarchist version has everything straight, and with the three sticks protruding out of the circle (like the A would). The visual rhyme is obvious.
The CS understanding of "No class, no state" is just about at the edge of something Aphex Twin might care about. But the stylized A really isn't that similar to a Lambda
How to reuse `readFile` `writeFile` program with this module trick?
Assuming `IO.readFile` and `IO.writeFile` is replaced by HTTP requests.
I can define `writeFile` and `readFile` in a type class and then implement the effect for HTTP variant, hiding the HTTP client beneath.
Is it just wiring it up in mixins, cabal file?
I think general conclusion is that there's no need for dependency injection, environment objects, or similar tricks if module system is rich enough.
For a long time I questioned why Python needs anything but `async def` or `def` (async should be achievable through module or `yield` usage) and `import` statements, to achieve maximal reuse, given dynamic nature of language and modules. We could ignore all object-oriented features, decorators, replace them with modules. Would be flatter and readable compared to bloated feature set.
even for someone moderately interested in FP, this one goes above my head and the only take-away I can get from it is "maybe use ocaml instead of haskell"
I'm reasonably versed in Haskell and my response would be that it shouldn't make that much difference to you what they've written in here. I've yet to see any code in the wild using the backpack extension.
It's more of an experiment: what if we take this beloved feature from OCaml to port it to Haskell (backpack), and see if it can replace a different beloved Haskell feature (type classes).
it's experimenting with a feature that was added to Haskell but just never caught on. it's theoretically very powerful but it's far enough from idiomatic Haskell that it sees basically zero usage in the wild as the cost for using it is that very few people can understand what you've done.
OCaml (also SML) has a really powerful module system; it's so powerful that to call it a "module" system is maybe misleading to the average developer. You might think of a "module" system as a way of dividing your program into different files, and the ML module system certainly subsumes that case, but it goes beyond that.
The key to this is that modules in ML are actually kind of a separate programming language. Not only can you define modules, you can define "functor modules" - modules that are kind of like functions; you can pass modules as arguments to functors to produce new modules. And there's a (structural!) type system here, too: the modules a functor can accept are specified to have a specific structure, and if you try to pass an incompatible module to a functor you get a type error.
(Incidentally: the naming of functors in the module system is really unfortunate here, because it overlaps with the name of functors in category theory/Haskell.)
This sounds extremely abstract and kind of insane; it's easier to understand it in practice. A typical example might be that you want to define a hash table, which requires some kind of hash operation for a key. What you'd do idiomatically in ML is define a HashTable functor which takes, as an argument, a module X consisting of a type t and and a hash function. This would generate a HashTable(X) module specialized to that particular key type.
What's interesting here is that there's an overlap here with things like interfaces in Java or typeclasses in Haskell, where your HashTable type would demand that the key type adheres to a Hashable interface. It turns out they're kind of (kind of!) just different takes on the same thing. There's even some interest in the OCaml world in trying to "close the gap" with a feature called "modular implicits" [0].
The other thing to know is that there's an esoteric feature of Haskell called "Backpack", which is an attempt to bring the ML module system to Haskell. It's not exactly widely used or anything like that, but it's there and been in GHC for several years now.
This article is basically just demonstrating how Backpack lets you use modules in much the same way as you'd traditionally use typeclasses in Haskell.
The problem is that this is not only about Haskell's fundamentals. It's about them being extended with backpack, an extension that very few people use.
And honestly, I don't see the point there either. I know that backpack is meant to solve some problem everybody has, but I haven't been able to fully understand what that problem is. Specifically on this article, I don't see the gain over using a type class.
> Have you ever seen a Number grazing in the fields? Or a Functor chirping in the trees? No? That’s because they’re LIES. LIES told by the bourgeoisie to keep common folk down.
Grumbling about how typeclasses could just be normal datatypes has been an undercurrent in the Haskell world for a long time, particularly as a way to solve the orphan instances problem. However, the syntactic grease provided by typeclasses is pretty appealing, and in the end I think this may have died down because people developed engineering solutions to the orphan instances problem (mostly "don't") that mitigated the theoretical problems enough that even most Haskellers don't care anymore... but I guess a few do still somewhat, because such things never truly go away.
(And yes, I recognize the quoted sentence as being humorous and labeling the entire article as not entirely serious... my point is that it is not entirely unserious either. The idea has been kicking around seriously for a while.)
There is a really interesting interview with Simon Peyton-Jones referenced on HN yesterday.He talks a lot about why Haskell came about, and some of the thinking behind the design choices that were made.
I see a lot of critical comments on here.
The blog post is an exploration of an alternative way to structure code in Haskell.
Why is the bar such that Haskell blog posts have to either demonstrate something clearly better then the status quo or that they need to explain the fundamentals of the language?
The audience is going to meet the article where they're at.
It's fine for, say, a blog post aimed at Haskellers to assume Haskell knowledge, but when posted on a board largely consisting of people without Haskell knowledge, it's natural that you're going to get at least a few people saying, "hey, I don't understand this".
But I'll be honest - I'm familiar with Haskell and the ML module system and the underlying concept (that typeclasses and modules are roughly equivalent in some sense), but I'm unfamiliar with Backpack so I still struggled to follow it a little. The target audience is an extremely narrow niche. So I think it's just somewhat poorly written; it doesn't feel like the author really had an audience in mind, other than themselves. There's probably ways of writing this - without spending too much time regurgitating the basics - that would be more palatable to more people.
> The audience is going to meet the article where they're at.
I hear you on this point but anyone can post anything on this forum. The burden should not be on the author to write a post that aligns with whatever forum their blog might get posted onto.
The author is free to ignore any and all complaints they consider unfounded. It’s not even like the author is recieving any complaints personally; they have to come here to see any. And if they come here, they will get to read the viewpoint visible from here.
Maybe if the post title used the word "Haskell" it wouldn't attract the opinions of people not interested in Haskell? Pretty obvious stuff.
It contains "type class".
... which won't mean much to someone who doesn't know Haskell. The kind of person who might click on this link thinking "types and classes are the same thing, what does he mean 'type class'".
(don't @ me with your pedantic bullshit about how types and classes are different, I don't care).
Delightfully weird and niche article.
And I wouldn't be surprised if there were more retired left handed surgeons in their 50s living in rural Switzerland than people who understand what he's talking about.
The article requires familiarity with Haskell, as well as the concept of Backpacks: https://blog.ezyang.com/2016/10/try-backpack-ghc-backpack/
The author then uses Backpacks to achieve ad-hoc polymorphism without typeclasses.
There is a well-known article from a long time ago which was conceptually similar: https://www.haskellforall.com/2012/05/scrap-your-type-classe...
Which highlighted the fact that typeclasses can basically be thought of as an additional vtable argument passed to every function that has typeclass constraints. (And, indeed, thinking about them this way often allows one to avoid typeclasses entirely and achieve the same kind of polymorphism in simpler and more flexible ways.)
Backpacks can achieve something similar, except by declaring an abstract "signature" for your dependency, rather than a function argument. (Backpacks can also be used to do a lot more than this, of course, but that's outside the scope of the OP article.)
This was a wonderful comment that added a lot of value to the article for me. I have a random curiosity if this was generated by a model or human written. I'm hoping for the later as this seems too well processed to be a LLM, but I'd like to check my assumption.
I think Oleg Kiselyov also has a post somewhere explaining how you can use Ocaml modules to do something similiar to Haskell typeclasses.
I'm a pretty average programmer/ex-programmer/hn-commenter in this context. I have never programmed in Haskell though in college I programmed with the language "fp" that is naturally functional.
I can tell this is article is about a common, wide debate in CS; should languages have "strict" structures like type class or loose structures like "objects". This related-to but not the same as the debate on whether to have pure functional languages or ad-hoc imperative languages.
I know in programming practice, everything ad-hoc has won but programming language "theory", everything strict has won.
Now in this context, I understand the post as advocating a certain kind of loose data typing with the strict-world of Haskell. Which I'd imagine won't make any ideologue happy. But seems like an OK contribution to the debate.
To add my own takes on everything, as hners must do, I think the strict structures of functional programming have quite a bit of merit for various purposes. BUT - they aren't intuitive/the-easy-way-to-everything-once-you-know/etc. AND they aren't a way to solve the software crisis.
I've been doing a lot of parsing lately and I find I don't need to reach all the way for monad (applicative is usually enough). But I guess that's what ApplicativeDo[1] is for.
This is a bit rough for usability (not to mention also undermines the point above).But overall I like the approach of trying something radically new.
[1] https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/appl...
ApplicativeDo can be thought of as a compile-time transformation that turns sequential code into possibly parallel* code just by analyzing the way variables are used. Once I've had that realization I really appreciated it more.
*: By "possibly parallel" I mean the blocks of code are known to be independent from each other. They could be actually executed in parallel if the Applicative instance does so.
Does that mean it may or may not run the code in parallel once compiled? Doesn't seem very ergonomic to the developer if true.
Why isn't it ergonomic? You just read the documentation or source for the Applicative class in question to find out.
If the Applicative class chooses to decide randomly whether to run in parallel, that's the class's fault, not the language or the compiler's problem.
I feel like this article would be much more approachable if it didn't assume readers already know Ocaml and Haskell and their module system
Indeed so, but that would require a few months worth of work from the reader, unfortunately.
(I have a t-shirt with a lambda in a circle, reminiscent of the anarchist emblem, and words "no class, no state". It's definitely possible to explain to a passer-by who never studied FP what it refers to, but not in such a way that the joke remained funny. Possibly the same deal is with the bumper sticker saying "my other car is cdr".)
"Humor can be dissected, as a frog can, but the thing dies in the process and the innards are discouraging to any but the pure scientific mind." - E. B. White.
Unexplained humour dies over time. Think of the classic 'a dog walks into an inn and says "I think I'll open the other one"'.
Some observational humor is timeless. Consider this entry from the same collection of Sumerian proverbs: "The dog understands 'Take it!', but it does not understand 'Put it down!'"[0] later repopularized as "No Take, Only Throw"[1].
[0] https://etcsl.orinst.ox.ac.uk/proverbs/t.6.1.05.html#t6105.p... [1] https://knowyourmeme.com/memes/no-take-only-throw
Contemporary rephrasing: "<InsertSubject> can be dissected, like a frog. It turns out that nobody is very interested and the frog dies of it."
> a lambda in a circle
How often do people think you're a Half-Life fan instead?
Likely never. The Half-Life has the lambda with the top crooked, neatly inscribed in a circle [1]. The anarchist version has everything straight, and with the three sticks protruding out of the circle (like the A would). The visual rhyme is obvious.
[1]: https://en.wikipedia.org/wiki/Gordon_Freeman#/media/File:Gor...
[2]: https://en.wikipedia.org/wiki/Anarchist_symbolism#/media/Fil...
or Aphex Twin
The CS understanding of "No class, no state" is just about at the edge of something Aphex Twin might care about. But the stylized A really isn't that similar to a Lambda
Haskell knowledge doesn't actually help because this isn't using the (very barebones) Haskell module system, but the Backpack extension: https://www.microsoft.com/en-us/research/wp-content/uploads/...
Thanks, I scanned through all the comments/links but this is the actual resource one wants to read to get familiar with Backpack.
Yeah, it’s better to not assume anything and be stuck in the basics forever, right?
It would also be a useless article. It's fine to write for an audience, if you're not in the target audience, move on.
It's okay not to know things
Uh so? There's a lot of value in articles that don't rehash the basics for the noob reader.
Yeah... meanwhile, in lesser languages, type classes is all we have.
How to reuse `readFile` `writeFile` program with this module trick?
Assuming `IO.readFile` and `IO.writeFile` is replaced by HTTP requests. I can define `writeFile` and `readFile` in a type class and then implement the effect for HTTP variant, hiding the HTTP client beneath.
Is it just wiring it up in mixins, cabal file?
I think general conclusion is that there's no need for dependency injection, environment objects, or similar tricks if module system is rich enough.
For a long time I questioned why Python needs anything but `async def` or `def` (async should be achievable through module or `yield` usage) and `import` statements, to achieve maximal reuse, given dynamic nature of language and modules. We could ignore all object-oriented features, decorators, replace them with modules. Would be flatter and readable compared to bloated feature set.
even for someone moderately interested in FP, this one goes above my head and the only take-away I can get from it is "maybe use ocaml instead of haskell"
I'm reasonably versed in Haskell and my response would be that it shouldn't make that much difference to you what they've written in here. I've yet to see any code in the wild using the backpack extension.
It's more of an experiment: what if we take this beloved feature from OCaml to port it to Haskell (backpack), and see if it can replace a different beloved Haskell feature (type classes).
it's experimenting with a feature that was added to Haskell but just never caught on. it's theoretically very powerful but it's far enough from idiomatic Haskell that it sees basically zero usage in the wild as the cost for using it is that very few people can understand what you've done.
I enjoyed this, hadn't used backpack and this is a nice tutorial
I just picture Charlie from “It’s Always Sunny in Philadelphia” connecting the dots.
As someone who has zero OCaml knowledge...can someone explain what's going on?
OCaml (also SML) has a really powerful module system; it's so powerful that to call it a "module" system is maybe misleading to the average developer. You might think of a "module" system as a way of dividing your program into different files, and the ML module system certainly subsumes that case, but it goes beyond that.
The key to this is that modules in ML are actually kind of a separate programming language. Not only can you define modules, you can define "functor modules" - modules that are kind of like functions; you can pass modules as arguments to functors to produce new modules. And there's a (structural!) type system here, too: the modules a functor can accept are specified to have a specific structure, and if you try to pass an incompatible module to a functor you get a type error.
(Incidentally: the naming of functors in the module system is really unfortunate here, because it overlaps with the name of functors in category theory/Haskell.)
This sounds extremely abstract and kind of insane; it's easier to understand it in practice. A typical example might be that you want to define a hash table, which requires some kind of hash operation for a key. What you'd do idiomatically in ML is define a HashTable functor which takes, as an argument, a module X consisting of a type t and and a hash function. This would generate a HashTable(X) module specialized to that particular key type.
What's interesting here is that there's an overlap here with things like interfaces in Java or typeclasses in Haskell, where your HashTable type would demand that the key type adheres to a Hashable interface. It turns out they're kind of (kind of!) just different takes on the same thing. There's even some interest in the OCaml world in trying to "close the gap" with a feature called "modular implicits" [0].
The other thing to know is that there's an esoteric feature of Haskell called "Backpack", which is an attempt to bring the ML module system to Haskell. It's not exactly widely used or anything like that, but it's there and been in GHC for several years now.
This article is basically just demonstrating how Backpack lets you use modules in much the same way as you'd traditionally use typeclasses in Haskell.
[0] https://www.cl.cam.ac.uk/~jdy22/papers/modular-implicits.pdf
Thank you for such a concise summary. I had no idea that types in ocaml are that powerful.
[dead]
In the 2010-2020 era, readers of Hacker News used to know how to read Haskell and had strong opinions (pro and con) of it.
That era is now over.
The problem is that this is not only about Haskell's fundamentals. It's about them being extended with backpack, an extension that very few people use.
And honestly, I don't see the point there either. I know that backpack is meant to solve some problem everybody has, but I haven't been able to fully understand what that problem is. Specifically on this article, I don't see the gain over using a type class.
There are still dozens of us!
Maybe in 2010, but you have an idealized view of 2020 HN.
peak FP debate was 2015/2016-ish
Orrrrr just use a lisp?
"Functors" "Monads" "Typeclasses" <- concepts dreamed up by the utterly deranged
This is in reference to a meme (https://knowyourmeme.com/memes/stop-doing-math) and made relevant by the first paragraph of the post.
> Have you ever seen a Number grazing in the fields? Or a Functor chirping in the trees? No? That’s because they’re LIES. LIES told by the bourgeoisie to keep common folk down.
Grumbling about how typeclasses could just be normal datatypes has been an undercurrent in the Haskell world for a long time, particularly as a way to solve the orphan instances problem. However, the syntactic grease provided by typeclasses is pretty appealing, and in the end I think this may have died down because people developed engineering solutions to the orphan instances problem (mostly "don't") that mitigated the theoretical problems enough that even most Haskellers don't care anymore... but I guess a few do still somewhat, because such things never truly go away.
(And yes, I recognize the quoted sentence as being humorous and labeling the entire article as not entirely serious... my point is that it is not entirely unserious either. The idea has been kicking around seriously for a while.)
There is a really interesting interview with Simon Peyton-Jones referenced on HN yesterday.He talks a lot about why Haskell came about, and some of the thinking behind the design choices that were made.
https://news.ycombinator.com/item?id=45242530
That was great, thank you
It's okay not to know things
"Hello I would like x<-[1,2..] apples please"
And I'll take 3 of those.
"A monad is just a monoid in the category of endofunctors"
what's the problem?
flatMap that s**!
"You just made those words up right now".
I heard it's just like a burrito.
A Burrito is a Monad:
https://williamcotton.com/articles/a-burrito-is-a-monad
Burrito's compose surprisingly well.
Unlike your 's
Fair point's's, I'll use my standard excuse of not being a native speaker. :)
I bet reading native speakers confused you.
How does this compose iterators?