[HN Gopher] Into CPS, Never to Return
___________________________________________________________________
Into CPS, Never to Return
Author : g0xA52A2A
Score : 44 points
Date : 2024-12-25 19:43 UTC (3 hours ago)
(HTM) web link (bernsteinbear.com)
(TXT) w3m dump (bernsteinbear.com)
| upghost wrote:
| In case anyone is wondering, "when would I EVER use this (in
| hand-written code)?", it's a trick that makes DSL (domain
| specific language) and small language implementation much easier.
| A great reference for this is Peter Norvig's Paradigms of
| Artificial Intelligence Programming, when he implements a subset
| of Prolog and bolsters the functionality using CPS[1].
|
| The second, although more obscure, is that you can use it in
| languages that do not have "non-local exits" to terminate a
| deeply nested computation early or return to an earlier point in
| the call stack. For example, Clojure does not have nonlocal
| exits, as only the final form of the function is returned.
| However, using CPS, you can terminate the expression early and
| return to the original caller without executing the rest of the
| function. You probably only want to use this in specialized cases
| though or it may upset your team, they are tricky to debug.
|
| Lastly and probably most controversially, you can make an
| extensible "if" statement using CPS if you are in a dynamic
| language and you have no other tools to do so. Admittedly I do
| sometimes use this in ClojureScript. This allows you to write
| "append only" code without continually growing the "cond". Again,
| most teams don't like this, so it depends on the circumstances,
| but might be nice to have in your back pocket if you need it.
|
| [1]: https://github.com/norvig/paip-
| lisp/blob/main/docs/chapter12...
| remexre wrote:
| It's also really helpful with defunctionalization; search
| "defunctionalize the continuation" for stuff on that
| _zoltan_ wrote:
| I thought I'm going to read a piece about US's Child Protective
| Services, and clicked. Was a letdown :-)
| TapamN wrote:
| I was hoping, but not expecting, for it to be about the Capcom
| arcade board.
| assbuttbuttass wrote:
| CPS is a cool technique, and makes advanced constructs like
| call/cc trivial to implement. Using CPS techniques in code can
| feel like magic.
|
| But CPS isn't really the state of the art for functional
| compilers anymore. It's complex for a human to read and hard to
| optimize. Something like A-normal form is much easier to work
| with for a compiler intermediate representation
|
| https://en.wikipedia.org/wiki/A-normal_form
| childintime wrote:
| The author references [scrapscript](https://scrapscript.org/),
| hopefully we'll hear more about it.
| tekknolagi wrote:
| See my other blog posts and GitHub if you would like some more!
___________________________________________________________________
(page generated 2024-12-25 23:00 UTC)