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.

Effective Concurrency with Algebraic Effects in Multicore OCaml

148 pointsby slightknackover 3 years ago

12 comments

infogulchover 3 years ago
I first encountered Algebraic Effects in Unison where they&#x27;re called &quot;abilities&quot; [0] via the strangeloop talk from 2 years ago [1]. Just from the little I&#x27;ve seen of it I feel like AE is a fundamental abstraction tool that&#x27;s been missing in programming language design. &quot;Fundamental&quot; as in the same level as <i>function arguments</i>. So many problems that were solved with myriad complex programming language constructs are just absorbed as a trivial user-implementation with an effect system: exceptions, sync vs async, dependency injection, cancellation tokens, dynamic contexts... all of these problems where the essential complexity is a need to have an effect that cuts through the function call stack.<p>I&#x27;m not saying that all our problems are solved and the programming world will now be rainbows and butterflies, I&#x27;m just saying that this feature is the correct framing and abstraction for issues we&#x27;ve run into many times in the past, and it has the potential to greatly simplify and unify the hacky, bespoke, situational solutions we&#x27;ve found.<p>[0]: <a href="https:&#x2F;&#x2F;www.unisonweb.org&#x2F;docs&#x2F;abilities" rel="nofollow">https:&#x2F;&#x2F;www.unisonweb.org&#x2F;docs&#x2F;abilities</a><p>[1]: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;gCWtkvDQ2ZI" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;gCWtkvDQ2ZI</a>
评论 #28844281 未加载
评论 #28844126 未加载
评论 #28855066 未加载
xvilkaover 3 years ago
See also the actual roadmap[1] to the OCaml 5.0 - the first version of OCaml with Multicore upstreamed.<p>[1] <a href="https:&#x2F;&#x2F;discuss.ocaml.org&#x2F;t&#x2F;the-road-to-ocaml-5-0&#x2F;8584" rel="nofollow">https:&#x2F;&#x2F;discuss.ocaml.org&#x2F;t&#x2F;the-road-to-ocaml-5-0&#x2F;8584</a>
评论 #28838493 未加载
gpderettaover 3 years ago
Thanks for the article. OCaml has long been on my short list of languages to learn, and continuations are an hobby of mine. I&#x27;ll have to dig into this deeper.<p>If someone has experience with algebraic effects, I have a question to ask. Why are they needed at all as a type system extension and why can&#x27;t they just be represented with function types? (excuse my Haskell pseudocode, I&#x27;m just a filthy C++ programmer abusing the notation I don&#x27;t really know the language, also don&#x27;t assume lazy semantics):<p><pre><code> newtype Effect a = Effect (a -&gt; Effect a) newtype EffectHandler a = EffectHandler (() -&gt; (a, EffectHandler a)) </code></pre> A function with an effect would have an explicit Effect parameter, while an effect handler would take an EffectHandler as a parameter (and return it as well). You could also add phantom types if you really need to distinguish different effects beyond &#x27;a.<p>The only magic would be in the function that creates the continuation:<p><pre><code> typed_callcc1 :: (Effect a -&gt; Effect a) -&gt; EffectHandler a </code></pre> Of course you could generalize Effect and EffectHandler into a bidirectional typed continuation:<p><pre><code> newtype Cont a b = Cont (a -&gt; (b, Cont a b)) </code></pre> I don&#x27;t pretend to fully understand algebraic effects but from what I see they are pretty much equivalent, except that there is no explicit effect parameter, just the type (so the continuation is not exactly first class and it is logically passed implicitly). For similar reasons, I think you can&#x27;t hide them in a closure. What is the gain? What am I missing?
评论 #28845131 未加载
评论 #28844805 未加载
评论 #28850218 未加载
nextaccounticover 3 years ago
Something I rarely see addressed: why was multicore ocaml blocked on having full-fledged effects? Couldn&#x27;t multicore have landed years ago, and then gradually insert effects in the language?
评论 #28838774 未加载
评论 #28838649 未加载
dganover 3 years ago
Wow Algebraic Effects are a totally new thing i never seen mentionned so far I always thought that async &#x2F;await was the best known way to handle resumable computation<p>Totally blew my mind
评论 #28842590 未加载
square_usualover 3 years ago
Effectful concurrency feels so different from the async&#x2F;await model I&#x27;m used to, but I&#x27;m really looking forward to playing around with it when OCaml 5 drops. Does anybody here have a more &quot;ELI5&quot; tier guide to it?
评论 #28838814 未加载
brundolfover 3 years ago
I&#x27;ve actually been playing with a similar idea in JavaScript, having pure functions generate &quot;Plans&quot; for async actions which are then executed later by other code. They can be thought of as Promises that haven&#x27;t happened yet.<p>A neat side-effect (no pun intended) of doing things this way is that, unlike Promises, Plans can be stored as constants (or cached) and re-used multiple times.<p>I&#x27;m sure it&#x27;s nowhere near as advanced or flexible as the OP, but it seems to be in the same general spirit
评论 #28845283 未加载
brundolfover 3 years ago
&gt; allowing the programmer to separate the expression of an effectful computation from its implementation<p>How does this compare with IO monads? Seems like they accomplish roughly the same goal
评论 #28844314 未加载
评论 #28843727 未加载
flyingchipmannover 3 years ago
Is this what Dan talked about in react hooks origins? <a href="https:&#x2F;&#x2F;overreacted.io&#x2F;algebraic-effects-for-the-rest-of-us&#x2F;" rel="nofollow">https:&#x2F;&#x2F;overreacted.io&#x2F;algebraic-effects-for-the-rest-of-us&#x2F;</a>
评论 #28845796 未加载
rstarastover 3 years ago
(2015)
kwhitefootover 3 years ago
It would be handy to have a bit of explanation about what the term <i>algebraic effect</i> means.
评论 #28843132 未加载
johnnycerberusover 3 years ago
Isn&#x27;t Java capable of the same thing now that it has algebraic data types with records + sealed classes + pattern matching? Given that Java already has a fine concurrency story, isn&#x27;t OCaml a hard sell to someone that&#x27;s not into compiler development to depend on its rich ecosystem?
评论 #28839054 未加载
评论 #28839064 未加载
评论 #28838909 未加载