TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Into CPS, Never to Return

137 点作者 g0xA52A2A5 个月前

8 条评论

upghost5 个月前
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 个月前
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 个月前
&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 个月前
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 个月前
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 个月前
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 个月前
It’s also useful in typed languages to introduce an existentially quantified type
评论 #42521875 未加载
yapyap5 个月前
not gonna lie this title made me think this was gonna be some child protective services horror story or something
评论 #42516511 未加载