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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Show HN: Monocle – bidirectional code generation library

148 点作者 lucasluitjes大约 3 年前
I just published a bidirectional code generation library. Afaik it&#x27;s the first of its kind, and it opens up a lot of possibilities for cool new types of dev tools. The PoC is for ruby, but the concept is very portable. <a href="https:&#x2F;&#x2F;blog.luitjes.it&#x2F;posts&#x2F;monocle-bidirectional-code-generation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.luitjes.it&#x2F;posts&#x2F;monocle-bidirectional-code-gen...</a>

17 条评论

ruricolist大约 3 年前
There is a fair amount of academic work on bidirectional tree transformation with lenses, e.g. &lt;<a href="https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~jnfoster&#x2F;papers&#x2F;lenses.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~jnfoster&#x2F;papers&#x2F;lenses.pdf</a>&gt;. It breaks down to proving three operations (Get, Put, Create) that observe three laws (called GetPut, PutGet, and CreateGet); these give you bidirectional transformations you can compose arbitrarily. Later work introduces concepts like &quot;quotienting&quot; (for when you actually want the transformation to be lossy in certain ways) or &quot;discerning&quot; (non-total) lenses.
评论 #31004124 未加载
narush大约 3 年前
Super cool. Bidirectional code generation is something I&#x27;ve spent a bit of time thinking about: I&#x27;ve been building a spreadsheet that generates Python code when you edit it [1], but some of our users also want the ability to edit the Python code they generate and have that reflect in the sheet itself.<p>Template -&gt; Code -&gt; Template is one really hard part of this, and something this tool seems to take a really good approach to. If you&#x27;re interested in related subjects (like transpiliation), I&#x27;d recommend this overview article as a great approach [2]. From this article: &quot;Now, there is a single biggest mistake we see in persons trying to implement a transpiler without experience in this: they try to generate directly the code of the target language from the AST of the original language.&quot;<p>Question to the OP - you mention you parse the Ruby AST - do you also transform this into an AST of your template syntax before generating the template? Aka, do you avoid this sin, or is it not an issue for you?<p>With Mito, there is additional complexity beyond just going from Template -&gt; Code -&gt; Template, in that we also need to understand _which_ variables are being changed and in what way. This is necessary because a spreadsheet stores other data about your variables beyond just their current value. As an example, which of these columns in a dataframe are a result of a formula vs. being in the original dataset isn&#x27;t something that is just stored in the dataframe itself.<p>I haven&#x27;t tried too hard, but I don&#x27;t think there&#x27;s a general solution; it feels like it requires some sort of symbolic execution in the general case, and is tough to do well even in simple cases. Our &quot;fail loudly and early&quot; equivalent feels like it would be a lot higher than the 10-20% this tool can deliver!<p>Anyways, bidirectional spreadsehet code generation is low on the priorities... but it&#x27;s a fun one to dream about :-)<p>[1] <a href="https:&#x2F;&#x2F;trymito.io" rel="nofollow">https:&#x2F;&#x2F;trymito.io</a> [2] <a href="https:&#x2F;&#x2F;tomassetti.me&#x2F;how-to-write-a-transpiler&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tomassetti.me&#x2F;how-to-write-a-transpiler&#x2F;</a>
评论 #31003577 未加载
评论 #31008366 未加载
lucasluitjes大约 3 年前
Whoops, looks like I should&#x27;ve posted this as a link rather than text containing a link. Here&#x27;s something clickable: <a href="https:&#x2F;&#x2F;blog.luitjes.it&#x2F;posts&#x2F;monocle-bidirectional-code-generation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.luitjes.it&#x2F;posts&#x2F;monocle-bidirectional-code-gen...</a>
评论 #31002205 未加载
SandyAndyPerth大约 3 年前
Yeah back in the 90&#x27;s it was called &quot;round-tripping&quot;<p><a href="https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;rhapsody&#x2F;8.2?topic=developing-roundtripping-code" rel="nofollow">https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;rhapsody&#x2F;8.2?topic=developing-ro...</a><p>I did a lot of code generation work in those years, working on the two dominant Mac-based generators (AppMaker and Prototyper) but was never ambitious enough to try round-tripping because of the horrors of parsing C++.
评论 #31009114 未加载
kevlened大约 3 年前
This is a powerful idea! If I understand Monocle&#x27;s use case, JS has similar AST parsing and code generation tools that are used broadly. There may be some ideas to learn from that community.<p>JS AST specs: estree [0] and babel&#x27;s AST [1]<p>Parsers: babel [2], acorn [3], or espree [4]<p>Transformers: babel, recast [5], or jscodeshift [6]<p>Codegen: babel or escodegen [7]<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;estree&#x2F;estree" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;estree&#x2F;estree</a><p>[1] <a href="https:&#x2F;&#x2F;babeljs.io&#x2F;docs&#x2F;en&#x2F;babel-parser#output" rel="nofollow">https:&#x2F;&#x2F;babeljs.io&#x2F;docs&#x2F;en&#x2F;babel-parser#output</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;babel&#x2F;babel" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;babel&#x2F;babel</a><p>[3] <a href="https:&#x2F;&#x2F;github.com&#x2F;acornjs&#x2F;acorn" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;acornjs&#x2F;acorn</a><p>[4] <a href="https:&#x2F;&#x2F;github.com&#x2F;eslint&#x2F;espree" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;eslint&#x2F;espree</a><p>[5] <a href="https:&#x2F;&#x2F;github.com&#x2F;benjamn&#x2F;recast" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;benjamn&#x2F;recast</a><p>[6] <a href="https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;jscodeshift" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;jscodeshift</a><p>[7] <a href="https:&#x2F;&#x2F;github.com&#x2F;estools&#x2F;escodegen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;estools&#x2F;escodegen</a>
froh42大约 3 年前
Haha, the old Java GUI builders in the 90s did something like this. You could either drag around the window (and the code would be updated) or modify the code (within limits) and it would parse it into the GUI builder.<p>Who&#x27;s old enough to remember the Symantec Visual Cafe IDE?
评论 #31003249 未加载
评论 #31003148 未加载
eyelidlessness大约 3 年前
This is neat! I’m curious if you see this being extended for other languages, or the concept being applied in other projects?<p>As for similar concepts, several projects by builder.io have some overlap. Most notably Mitosis[1], but I’d be shocked if TS-Lite[2] isn’t using similar techniques. Potentially Qwik[3] as well but I’m not sure, I would have bet that’s using Mitosis but it looks like that’s the other way around.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;mitosis" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;mitosis</a><p>2: <a href="https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;ts-lite&#x2F;tree&#x2F;main&#x2F;packages&#x2F;core" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;ts-lite&#x2F;tree&#x2F;main&#x2F;packages&#x2F;core</a><p>3: <a href="https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;qwik" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;BuilderIO&#x2F;qwik</a>
评论 #31006463 未加载
fortysixdegrees大约 3 年前
My dream would be bidirectional openAPI. I used it for a project and liked it but after the generators are run, making changes becomes more not less work to keep the spec and code in sync.<p>Something like this would be amazing if it could be integrated as a generator. I&#x27;d love to do it if I had the time
评论 #31016185 未加载
SandyAndyPerth大约 3 年前
What you&#x27;ve done looks pretty smart and definitely worth a deeper look. My main interest is in visual design generating code, especially for animation timing.<p>The concept may be portable - the devil is in the millions of details on which I&#x27;ve seen many promising tools bog down and die.<p>Also, please, don&#x27;t say _first of its kind_ unless you&#x27;ve done enough research to be confident.
sarma912大约 3 年前
Cool idea! Was wondering if you could elaborate a little on what type of new dev tools would benefit from this. One of the prototypes I worked on was bidirectional code gen for no-code tools but it felt like it might not be the most useful thing.
评论 #31006663 未加载
moralestapia大约 3 年前
Have you read&#x2F;study about Category Theory?<p>I find it incidentally related that you named your project &quot;Monocle&quot; while it does something quite similar to a concept there called lenses, so have a look at it if you have a chance!<p>Btw, really neat idea.
评论 #31002787 未加载
vlovich123大约 3 年前
Is the codebase flexible enough to add other source and templating languages? What would be involved in that?
评论 #31003116 未加载
评论 #31002990 未加载
tlb大约 3 年前
Presumably the results in the reverse direction are sometimes ambiguous, so x1 -&gt; y -&gt; x2.<p>I wonder it there’s a general code improver that tries this transform on every function looking for a shorter x2 than x1.
cloogshicer大约 3 年前
Looks super cool!<p>For those more experienced in Programming Language Theory, how does code generation slot into PL theory? Is there some kind of common formalism for it?
chrisseaton大约 3 年前
Code generation... for data models - is some important context not in the post (I assumed it would be a Ruby compiler and decompiler, for example.)
评论 #31006489 未加载
simne大约 3 年前
Cool. How about speed? Cold you run benchmarks and show results?
sam0x17大约 3 年前
Love that it&#x27;s for Rails. Might actually use this!