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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Overloading the lambda abstraction in Haskell (2022)

69 点作者 0823498723498725 个月前

4 条评论

comex5 个月前
The fun thing is that this sounds like &quot;just&quot; a more type-safe version of the way DSLs are usually implemented in imperative languages, particularly scripting languages.<p>In Python, if I have:<p><pre><code> def foo(x): return x**2 - 1 </code></pre> I can plug in a concrete argument:<p><pre><code> &gt;&gt;&gt; foo(2) 3 </code></pre> But I can also plug in abstract values provided by some library where all operators are overloaded to return another AST node. For instance:<p><pre><code> &gt;&gt;&gt; from z3 import * &gt;&gt;&gt; expr = foo(Int(&#x27;x&#x27;)) &gt;&gt;&gt; expr x**2 - 1 &gt;&gt;&gt; type(expr) &lt;class &#x27;z3.z3.ArithRef&#x27;&gt; </code></pre> And then I can perform abstract operations on the resulting AST node, like solving for x:<p><pre><code> &gt;&gt;&gt; solve(expr == 0) [x = -1] </code></pre> Of course this is completely non-type-safe. And without purity, there&#x27;s no guarantee that `foo(Int(&#x27;x&#x27;))` is truly equivalent to running foo on an abstract value `x`. If there&#x27;s an `if` statement in there, the condition will either pass or not, and the returned AST node will only include that control flow path. The DSL in question tries to block that by preventing AST nodes from being converted to boolean, but it&#x27;s not perfect. Haskell has an advantage there.<p>On the other hand, for the situation at the end of the post, where the author wants to model imperative computations, it seems like an imperative language would make for easier embedding. In an imperative language, each AST node can be given its own unique ID when it&#x27;s constructed. So there&#x27;s no trouble distinguishing between, e.g., calling some function bar() and then using the return value twice, versus calling bar() twice. No need to use a separate &lt;- operator for assignments.<p>But to be fair, explicitly distinguishing side effects is kind of the whole point of Haskell!<p>(And if you wanted badly enough to do it in Haskell without requiring &lt;-, you could use unsafePerformIO to assign AST node IDs.)
评论 #42548908 未加载
评论 #42548291 未加载
评论 #42549649 未加载
_jackdk_5 个月前
The linear functions&#x2F;SMC work is really cool, but I am surprised to see the author calling it more mature than the compiling to CCCs work. The linear-smc library hasn&#x27;t had an upload in a while, and it&#x27;s currently missing any haddocks beyond the extracted type signatures. It&#x27;s also a shame that it had to build its own typeclass for monoidal categories instead of using the one in <a href="https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;categories" rel="nofollow">https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;categories</a> .<p>Meanwhile KittyHawk did actually use the compile-to-CCCs work to compile Haskell to C in <a href="https:&#x2F;&#x2F;github.com&#x2F;sellout&#x2F;compiling-anything-to-categories">https:&#x2F;&#x2F;github.com&#x2F;sellout&#x2F;compiling-anything-to-categories</a> and someone from there talks about it at <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=VUBj8NW7uMA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=VUBj8NW7uMA</a><p>I think the linear-smc work is more exciting and I hope it matures with a bit more elbow grease. There was an ICFP talk that accompanied the linked paper ( <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=90OJz0QE4qE" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=90OJz0QE4qE</a> ) and monoidal categories can model lots of &quot;boxes and wires&quot; things. Linear functions potentially give you a much more ergonomic DSL for building up those boxes-and-wires models, and in a way that lets you write abstractions that work over any model.
评论 #42551937 未加载
cryptonector5 个月前
What a delightful blog post. One need not fully understand all the prologue bits to understand the interface and implementation produced.<p>This technique could be very useful in developing other DSLs on Haskell. Basically the author found a simple monadic-looking mapping of Haskell functions onto a different Category where you can do interesting things with those functions. In TFA&#x27;s case the idea is to write flow diagrams where &quot;boxes&quot; embed [user-provided] Haskell functions, then the machinery can do whatever might be expected for diagrams, like: render visual representations of them, evaluate them, analyze them, etc. Because the boxes embed Haskell functions the flow diagrams can do useful work, so they&#x27;re more than just documentation, they can be code and documentation. Basically this is a flow diagram programming language as a DSL embedded in Haskell.<p>Imagine using this technique to build things like networking stacks, say. Or emulators of various sorts (CPUs&#x2F;ISAs, say). Or maybe one could use this to make something like a PLC in Haskell. It&#x27;s really quite a neat and powerful idea, especially if it really does compile to efficient code.<p>What else might one use this for?
gsf_emergency5 个月前
Kinda tangential (since the linear vs Cartesian distinction wasn&#x27;t made a big deal of here) but I liked the clarifying discussion (to layman me) of how &quot;linear&quot; and &quot;affine&quot; are related to the usage ones learns in uh middle school?<p><a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;haskell&#x2F;comments&#x2F;txk3mn&#x2F;why_are_they_called_linear_and_affine_types&#x2F;?rdt=53183" rel="nofollow">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;haskell&#x2F;comments&#x2F;txk3mn&#x2F;why_are_the...</a><p>[And even the comment from the athletic river horse is thrilling to read]