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.

Implementing Algebraic Effects in C

150 pointsby necrodomealmost 8 years ago

6 comments

rravalalmost 8 years ago
This is super interesting and I&#x27;m still going through the paper. For folks with a &quot;Cool, but why&quot; reaction, the introduction elaborates:<p><pre><code> For example, the P language [10] is a language for describing verifiable asyn- chronous state machines, and used for example to implement and verify the core of the USB device driver stack that ships with Microsoft Windows 8. Compiling to C involves a complex CPS-style transformation [19, 26] to enable async&#x2F;await style programming [5] with a receive statement – using the effects library this transformation is no longer necessary and we can generate straightforward C code instead. Similarly, we hope to integrate this library with libuv [29] (the asynchronous C library underlying Node [43]) and improve programming with libuv directly from C or C++ using async&#x2F;await style abstractions [12, 27] </code></pre> Since the linked abstract doesn&#x27;t actually mention this practical application, consider this comment as a goad to encourage more people to click through to the paper :)
评论 #14888306 未加载
评论 #14890178 未加载
frankpfalmost 8 years ago
A little off-topic, but the author of this paper (Daan Leijen) is also the author of Koka[1], a programming language with algebraic effects.<p>IMO, Koka (or something similar) has more potential to become a mainstream language than &quot;traditional&quot; FP languages (Haskell, OCaml, Idris, etc.).<p>Effects seem to be easier to understand than monads (at least on a superficial level) and more modular (I don&#x27;t have a lot of experience with Haskell, so take that with a grain of salt).<p>Its syntax is also very close to C-like languages.<p>Taken from the Koka book[2]:<p><pre><code> fun square1(x : int) : total int { return x*x } fun square2(x : int) : io int { println( &quot;a not so secret side-effect&quot; ) return x*x } </code></pre> `square1` is a pure mathematical function, so its effect is `total`. `square2` has a side-effect because of `println`, so its effect is `io`. This means that `square2` can raise exceptions, not terminate, be non-deterministic, read and write to the heap, and do any input&#x2F;output operations.<p>Note that Koka can infer effects, so these annotations are optional.<p>[1]: <a href="https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;project&#x2F;koka&#x2F;?from=http%3A%2F%2Fresearch.microsoft.com%2Fen-us%2Fprojects%2Fkoka" rel="nofollow">https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;project&#x2F;koka&#x2F;?from=...</a><p>[2]: <a href="https:&#x2F;&#x2F;koka-lang.github.io&#x2F;koka&#x2F;doc&#x2F;kokaspec.html#sec-effect-types" rel="nofollow">https:&#x2F;&#x2F;koka-lang.github.io&#x2F;koka&#x2F;doc&#x2F;kokaspec.html#sec-effec...</a>
评论 #14889011 未加载
评论 #14890863 未加载
评论 #14894924 未加载
评论 #14889656 未加载
daanxalmost 8 years ago
Hi, I am the author of this report -- thank you for the interest :-)<p>Just wanted to add that you can find the library at: <a href="https:&#x2F;&#x2F;github.com&#x2F;koka-lang&#x2F;libhandler" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koka-lang&#x2F;libhandler</a><p>The `dev` branch contains a sample of `libuv` integration. There is still a lot to be done in terms of creating a better interface and providing implementations of standard effects but the core is working quite well by now.<p>Enjoy!
ambulancechaseralmost 8 years ago
the authors cite Matija Pretnar who has an introduction on [algebraic effects](<a href="http:&#x2F;&#x2F;www.eff-lang.org&#x2F;handlers-tutorial.pdf" rel="nofollow">http:&#x2F;&#x2F;www.eff-lang.org&#x2F;handlers-tutorial.pdf</a>). I&#x27;m reading through it now as I have no idea what is going on :)
评论 #14890647 未加载
cryptonectoralmost 8 years ago
If all you want is generators and co-routines, then there&#x27;s a lot of literature on this for C.<p>The original Icon compiler compiled to CPS C. That was pretty cool.<p>One can do much of what Icon did using GCC local functions and computed gotos to get something much closer to not-CPS.<p>There&#x27;s Simon Tatham&#x27;s PuTTY, which uses his co-routine macros, which are an extravagant meta-programming macros around a Duff&#x27;s device. He also has an extensive set of meta-programming macros as well, not related to co-routines.<p>Some Schemes compile to C code where all functions always return immediately, but what they return is a continuation, and the top-level is a for(;;) { next = next(); }.<p>EDIT: I&#x27;m particularly fond of PuTTY. Its SSHv2 implementation all the way up to the end of authentication is a single, huge function. It looks completely synchronous, but it&#x27;s not, because it&#x27;s actually a co-routine. As a result, and in spite of being a single huge function, it&#x27;s actually quite readable.
评论 #14913291 未加载
lindigalmost 8 years ago
Algebraic effects are a hot topic. The name sounds fancy but maybe this intuition helps: an effect is like an exception where the exception handler receives enough information to resume the computation from where it was called. Just like exceptions, effects are dynamically scoped: the handler is always found up in the call stack.
评论 #14890717 未加载