Most active commenters
  • yen223(4)
  • wruza(3)

←back to thread

Parser Combinators Beat Regexes

(entropicthoughts.com)
120 points mooreds | 11 comments | | HN request time: 0.52s | source | bottom
Show context
yen223 ◴[] No.43639551[source]
Parser combinators are one of those ideas that is really powerful in practice, but will never be mainstream because it had the bad fortune of coming from the functional programming world. It's a shame too, because parser combinators are what made parsers click for me.

Take the hard problem of parsing a language, break it down into the much simpler problem of reading tokens, then write simple "combinators" (and, or, one_or_many, etc etc) to combine these simple parsers into larger more complex parsers.

You could almost build an entire parser combinator library from scratch from memory, because none of the individual components are complicated.

replies(13): >>43639775 #>>43639805 #>>43639834 #>>43640597 #>>43641009 #>>43641205 #>>43641459 #>>43641675 #>>43642100 #>>43642148 #>>43643853 #>>43644151 #>>43650405 #
1. skybrian ◴[] No.43639834[source]
Never is a long time. Other ideas from functional language have become mainstream.
replies(1): >>43639896 #
2. yen223 ◴[] No.43639896[source]
I would be very happy to be proven wrong here.

But I view parser combinators in the same lens as lenses, another powerful approach to solving a specific problem that is unlikely to receive mainstream recognition because of all the Haskell.

replies(3): >>43639982 #>>43640004 #>>43643966 #
3. wk_end ◴[] No.43639982[source]
FWIW lenses are lovely in Typescript with monocle-ts, and extremely useful when - speaking of functional ideas hitting the mainstream! - writing Redux reducers to provide updates for immutable data.
replies(1): >>43640499 #
4. BoiledCabbage ◴[] No.43640004[source]
> But I view parser combinators in the same lens as lenses

Hmm, anything else you have in that list I should be looking into? As I also agree they are two of the most interesting things I got from learning Haskell. (Along with type driven reasoning, monoids and functors).

replies(1): >>43640188 #
5. yen223 ◴[] No.43640188{3}[source]
The big one that springs to mind, though it's not a Haskell idea, are algebraic effects and effects handler
6. ◴[] No.43640499{3}[source]
7. wruza ◴[] No.43643966[source]
It's unlikely because people don't get why jump through so many hoops when you can

  new = copy(old)
  new.foo.bar.baz = new_baz
  return new
It won't come out of Haskell because it's uniquely a solution to a specific Haskell problem. Mutating a fresh copy through a "lens" when you have `=` is a level of geekage that cannot become mainstream.
replies(2): >>43647066 #>>43649028 #
8. kqr ◴[] No.43647066{3}[source]
This is a common misconception. Lenses (or more generally optics) are not limited to targeting single fields. They can target composites, alternatives, etc. They go way beyond a normal accessor dot and can do stuff that cannot be done in mainstream languages.

Some of the other ideas from lenses are becoming mainstream, like the .? conditional accessor in e.g. C# and EcmaScript.

replies(1): >>43648783 #
9. wruza ◴[] No.43648783{4}[source]
They don't surpass Turing Machine level, so I'm not convinced.

The idea of ?. was there since the beginning of times. You don't have to dig deep to realize that (x ? x.y : nil) and (x ? x : y) sucks. Even gnu C had (x ?: y) for quite a while. It's just that the designers of to-become-mainstream languages all start with the same "density" and ignore the damn obvious, and then rather than fixing it, chew on the same dilemmas they all inevitably introduce. I sometimes wonder if they have written regular code at all or started inventing languages right after school.

10. yen223 ◴[] No.43649028{3}[source]
This comment proves my point about how "all the Haskell" hinders mainstream adoption of a thing. It's very easy for folks to miss the point of the thing!

Lenses and more generally optics are a lot more powerful than just dot-accessors. They are closer in spirit to XPath or jq, except that they are general enough to work on most data structures, not just XML or Json.

replies(1): >>43652398 #
11. wruza ◴[] No.43652398{4}[source]
It doesn't. "All the haskell" is your protective decoy against "it's not that useful when you're not chained to the wall". Show me a lens with a bunch of other fp tricks and I'll write dumb inline code for it that does that same thing, but without twisting anyones brain or trying to not miss its thin point.