Hacker Newsnew | comments | show | ask | jobs | submit login
A Difficult Distraction (github.com)
23 points by raganwald 2068 days ago | 1 comment

Very interesting read, especially with respect to rewriting source code.

One reason why monadic control flow works so well in Haskell is because the sugar and implementation are completely decoupled. The sugar layer simply rewrites "do { x <- foo; bar x }" to "foo >>= \x -> bar x". Very easy to do and undo, and it provides no semantics of its own; the transformed and untransformed versions are exactly equivalent. The actual control of the control flow is in the >>= function, and function calls are very easy to reason about compared to arbitrary source code rewriting. You are, after all, just passing some data to a function, which returns a result, which is passed to another function.

Simple and powerful.

This is hard to do in Ruby and Perl because code depends too much on the code(+); control flow is not function applications, it's a magic oracle that looks at the text of your program and uses that as a guide on what to do next. This makes modifying the control flow difficult; you have to understand the text, and then use that understanding to transform the text, which almost requires you to be as clever as the magical oracle itself. The end result is something that works really well... until something that the transformer didn't expect happens, and then you are SOL until you can find enough cleverness to work around that problem. Repeat until your rewriter is essentially a compiler for its own special programming language.

(+) Example: "my $foo; $foo = 42" compiles, "my $foo;" compiles, but "$foo = 42" does not compile. "$foo = 42" is only valid code in the presence of a "my $foo" somewhere before it. Hard to reason about!


Applications are open for YC Winter 2016

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact