TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Into CPS, Never to Return

137 pointsby g0xA52A2A5 months ago

8 comments

upghost5 months ago
In case anyone is wondering, &quot;when would I EVER use this (in hand-written code)?&quot;, it&#x27;s a trick that makes DSL (domain specific language) and small language implementation much easier. A great reference for this is Peter Norvig&#x27;s Paradigms of Artificial Intelligence Programming, when he implements a subset of Prolog and bolsters the functionality using CPS[1].<p>The second, although more obscure, is that you can use it in languages that do not have &quot;non-local exits&quot; 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.<p>Lastly and probably most controversially, you can make an extensible &quot;if&quot; 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 &quot;append only&quot; code without continually growing the &quot;cond&quot;. Again, most teams don&#x27;t like this, so it depends on the circumstances, but might be nice to have in your back pocket if you need it.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;norvig&#x2F;paip-lisp&#x2F;blob&#x2F;main&#x2F;docs&#x2F;chapter12.md#compiling-logic-programs">https:&#x2F;&#x2F;github.com&#x2F;norvig&#x2F;paip-lisp&#x2F;blob&#x2F;main&#x2F;docs&#x2F;chapter12...</a>
评论 #42512734 未加载
评论 #42511446 未加载
评论 #42512246 未加载
评论 #42513072 未加载
评论 #42513287 未加载
评论 #42514613 未加载
Joker_vD5 months ago
There is actually a way to produce CPS without lots of administrative redexes, which uses the same main trick that &quot;higher-order transform&quot; utilizes: tracking the whether the expression being transformed is in tail context or general context, and translating accordingly — but without using meta-continuations because boy are those mind-boggling.<p>Instead, the normal loops are used, and the subresults are accumulated in some builder structure which is then finalized to produce a complete tree of a CPS expression. The main trick is to figure out just <i>what</i> this builder structure should be, and it turns out it&#x27;s a tree zipper of sorts!<p>The margins of this comment are not big enough to sketch the whole thing but it&#x27;s actually not as formidable as it sounds (&quot;tree zipper&quot;, wooooh): it&#x27;s mostly just a stack of tree-building instructions which can be easily extended&#x2F;completed. An alternative would be to have a <i>mutable</i> CPS expression as the result, and keep appending things into it, but it requires lots of tree traversal and lot of care to not accidentally leave the thing in not quite constructed state which is way too easy in my experience.<p>I think I&#x27;ll make and publish a gist with it when I get home because I don&#x27;t believe I&#x27;ve ever seen it done this way anywhere.
评论 #42515914 未加载
评论 #42515856 未加载
purplesyringa5 months ago
&gt; If you don’t want to do any of this trampoline stuff, you can also do the One Big Switch approach which stuffs each of the funs and conts into a case in a massive switch statement. Calls become gotos.<p>Just wanted to add that this is very similar to how async&#x2F;await JavaScript code was compiled for runtimes that didn&#x27;t support generators back in the day: <a href="http:&#x2F;&#x2F;facebook.github.io&#x2F;regenerator&#x2F;" rel="nofollow">http:&#x2F;&#x2F;facebook.github.io&#x2F;regenerator&#x2F;</a>
assbuttbuttass5 months ago
CPS is a cool technique, and makes advanced constructs like call&#x2F;cc trivial to implement. Using CPS techniques in code can feel like magic.<p>But CPS isn&#x27;t really the state of the art for functional compilers anymore. It&#x27;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<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;A-normal_form" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;A-normal_form</a>
评论 #42515282 未加载
评论 #42512270 未加载
childintime5 months ago
The author references [scrapscript](<a href="https:&#x2F;&#x2F;scrapscript.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;scrapscript.org&#x2F;</a>), hopefully we&#x27;ll hear more about it.
评论 #42511808 未加载
评论 #42512768 未加载
评论 #42511507 未加载
_zoltan_5 months ago
I thought I&#x27;m going to read a piece about US&#x27;s Child Protective Services, and clicked. Was a letdown :-)
评论 #42511899 未加载
评论 #42511647 未加载
fovc5 months ago
It’s also useful in typed languages to introduce an existentially quantified type
评论 #42521875 未加载
yapyap5 months ago
not gonna lie this title made me think this was gonna be some child protective services horror story or something
评论 #42516511 未加载