frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Type-constrained code generation with language models

https://arxiv.org/abs/2504.09246
160•tough•7h ago

Comments

jiggawatts•6h ago
This was an obvious next step. Most current products can only restrict the token prediction to valid JSON or a specific JSON schema at best. There's no reason that this should be the only grammar available for constrained output mode.

The real challenge will be to make this detect and switch languages automatically. For example, a snippet of code could include a LaTeX formula in a comment and SQL in a string literal. There are many more examples, such as regex inside a shell script, and so on.

The obvious next step after that is back-tracking. It's possible to emit a token that is valid, but then allows no further completions that are valid. In other words, the model can paint itself into a corner. To my knowledge, no current online LLM service uses any kind of backtracking, they run in append ("forwards") mode only.

helltone•6h ago
Backtracking idea is interesting, could maybe diffusion help? At some point it turns into sat solving.
grafmax•5h ago
Sat solving I guess because types encode proofs?
foota•6h ago
I believe Microsoft introduced a framework that did this sort of backtracking that you're suggesting. I'm not sure how much traction it got.
tough•5h ago
SRLCG: Self-Rectified Large-Scale Code Generation with Multidimensional Chain-of-Thought and Dynamic Backtracking

https://arxiv.org/abs/2504.00532

IterGen: Iterative Semantic-aware Structured LLM Generation with Backtracking

https://arxiv.org/abs/2410.07295

ROCODE: Integrating Backtracking Mechanism and Program Analysis in Large Language Models for Code Generation

https://arxiv.org/abs/2411.07112v1

pizza•2h ago
Another one: SequenceMatch: Imitation Learning for Autoregressive Sequence Modelling with Backtracking https://arxiv.org/abs/2306.05426

There was also an hn thread: https://news.ycombinator.com/item?id=36425375

ArcaneMoose•5h ago
I think TypeScript is uniquely positioned to be the optimal language for LLMs. Tons of training data (benefiting from all the JS examples as well) plus the structure of types for LLMs to follow and tools to enforce.
yoyohello13•5h ago
God help us…
marviel•5h ago
what do you dislike about it?
treyd•57m ago
TypeScript is arguably one of the weaker statically typed languages, with how it allows `any` to quietly violate the type checked assumptions. It makes it harder to do a lot of the basic typing mistakes in JS, but it doesn't prevent them by any means, especially if you have to interface with (typeless) JS code.

So for these reasons alone I would be against using TS as a lingua franca for LLM codegen (as is GP I assume). As another commenter mentioned, LLMs have a tendency to throw their hands^Hlogits up when presented with complex TS type errors and just resort to using `any` to get it to compile (and probably hiding bugs).

And that doesn't even touch the issues with the JS/TS ecosystem and runtimes more broadly.

OutOfHere•5h ago
There are languages that constrain types a lot more tightly than TypeScript, e.g. Kotlin, Rust, and Haskell. The more constrained the types, the more correct the program could be.
mindwok•5h ago
Yep, and Rust famously goes beyond this by modelling memory ownership at compile time.

In fact, the more behaviour we can model at compile time the better when it comes to LLMs - there's some cool ideas here like transpiling Rust into languages for formal verification. See https://github.com/formal-land/coq-of-rust as an example.

Formal verification was one of those things that was previously so annoying to do that it rarely made it past academic use cases or extremely important libraries, but I think LLMs take the tedium out of it. Perhaps formal verification will have a "test driven development" type of moment in the sun thanks to this.

koakuma-chan•5h ago
Can LLMs properly code in Rust yet? There is way more TypeScript code out there compared to Rust, and I doubt structured output can alleviate this.
steveklabnik•4h ago
They can, yes.
pram•5h ago
LLMs work well with any static analysis tool. I frequently instruct Claude to use stuff like “go vet” and “deadcode” when it goes on a tear and writes a bunch of broken trash and declares mission accomplished.
koakuma-chan•5h ago
> LLMs work well with any static analysis tool.

tsc error messages are so bad that every time my LLM sees one of those "SomeType is not assignable to SomeLongAssTypeDontEvenTryToUnderstandWhatsGoingOnHere<<<<>>>>>>>>>>>>>>>>>>>>" it just gives up and casts to any. goes for python too.

floydnoel•4h ago
ha, that's always been my biggest gripe with ts
AaronAPU•3h ago
I can’t be the only one who hopes this was a joke.
babyent•1h ago
It’s better sure but as a power TS user it still sucks at generating better code, and consistently fucks up with generics (or doesn’t use them) or simple types sometimes.
homebrewer•5h ago
Hejlsberg mentioned the ability to quickly provide accurate type information to LLMs as one of the reasons for rewriting tsc into Go:

https://youtu.be/10qowKUW82U?t=3186

tough•5h ago
But isn't TypeScript already a typed language to begin with?
habitue•5h ago
This is about the speed with which the compiler can advise an LLM that a particular thing checks or doesn't check. Typescript is much slower than Go
tough•5h ago
okay so basically the faster compiling means a tigher feedback loop for the LLM to -know- if the code compiles or not etc? interesting

is go faster than rust?

yoyohello13•5h ago
Go’s compiler is WAY faster than Rust’s. As far as speed of the actual program, Rust will generally be faster.
notnullorvoid•5h ago
Go or Rust compiler speeds won't have any effect here. The program in this context is the TypeScript compiler.
koakuma-chan•5h ago
cargo check is WAY faster than go build
Thaxll•5h ago
Working with both I can say that this is a big no, go mod is as fast if not faster, usually Go dep are much faster because Go does not import as much dependencies as Rust.
koakuma-chan•4h ago
In Rust you only need to compile your dependencies once. After that it's just your app because dependencies don't change.
binary132•2h ago
that is also the case in Go…?
notnullorvoid•5h ago
> is go faster than rust?

Depends on how you write the Go or Rust code. The most optimal Rust re-write of the TypeScript compiler would very likely be faster than the most optimal version in Go. However they didn't want to do a re-write, they wanted to port the existing compiler codebase written in TS. Go like TS (ultimately the JS runtime) also has GC which makes a 1-to-1 port much easier.

PartiallyTyped•4h ago
No. Ignore the other comments.

The reason for this decision is that they wanted a near 1:1 port of the typescript code to go, keeping design and structure almost identical.

You can’t do that in rust as easily because of all the cyclical references and indirection involved.

A rust port would be a rewrite. This is merely a migration.

raincole•4h ago
> is go faster than rust

No.

They rewrote in go because go is similar enough to typescript, while being faster than typescript.

Source: https://github.com/microsoft/typescript-go/discussions/411

nikolayasdf123•3h ago
maybe rust is faster. but to make fast rust code (as compared to fast go code) is barely impossible. borderline unusable language, both to humans and to LLMs (unless we get super-intelligence, maybe it can write good rust easily)
int_19h•1h ago
Go has a very simple type system that is easy to typecheck on every token.

TypeScript has a type system that is complex enough, you can literally implement wasm inside it (and then use that to run e.g. Doom: https://socket.dev/blog/typescript-types-running-doom)

notnullorvoid•5h ago
The general idea seems very promising, I had been hoping someone would do something like this since seeing JSON schema structured outputs for LLMs.

Need to dig in a bit more on the implementation, but I was surprised that the paper didn't mention hooking into existing language service/server. There's more than types that an LLM could leverage from existing language tooling. Auto imports is a good example, it is handy for the human developer to keep a linear writing flow, something a LLM needs even more.

tough•5h ago
The code can be found here: https://github.com/eth-sri/type-constrained-code-generation
compacct27•5h ago
Honestly it's already working great in Cursor. Even adapting one type structure to another is quickly handled.
slt2021•5h ago
we really need LLM trained on AST, instead of token, is there any research on this?
tough•5h ago
ASTrust: Towards More Trustworthy and Interpretable LLMs for Code through Syntax-Grounded Explanations

https://arxiv.org/abs/2407.08983

AST-T5: Structure-Aware Pretraining for Code Generation and Understanding

https://arxiv.org/abs/2401.03003

CodeGRAG: Bridging the Gap between Natural Language and Programming Language via Graphical Retrieval Augmented Generation

https://arxiv.org/abs/2405.02355

koakuma-chan•5h ago
The vibe code society would benefit way more if libraries hosted their docs in a way that's easy to copy and paste into an LLM.
tough•5h ago
many docs now include llms.txt https://llmstxt.org/
koakuma-chan•5h ago
I saw that but it doesn't work for me. I use Gemini 2.5 Pro Preview right now, and it cannot fetch content from links. What I am looking for is a large text file with public API class, function, etc. signatures, plain text documentation and code examples.
tough•5h ago
https://ai-sdk.dev/llms.txt
koakuma-chan•5h ago
Depends on the library I guess, I spent 12~ hours today vibe coding with LiveKit and their /llms.txt is https://docs.livekit.io/llms.txt
davidz•55m ago
thanks for the feedback. let us see how we can organize this better for compat with diff LLMs.
muglug•4h ago
Really cool results!

That this research comes out of universities, and not large AI labs, makes me think those labs believe that larger models are still the way to go.

aibrother•4h ago
+1 this seems like healthy development
bmc7505•4h ago
The correct way to do this is with finite model theory but we're not there yet.
_jayhack_•4h ago
Also worth checking out MultiLSPy, effectively a python wrapper around multiple LSPs: https://github.com/microsoft/multilspy

Used in multiple similar publications, including "Guiding Language Models of Code with Global Context using Monitors" (https://arxiv.org/abs/2306.10763), which uses static analysis beyond the type system to filter out e.g. invalid variable names, invalid control flow etc.

nikolayasdf123•3h ago
nice. the speed of AI development is accelerating so fast
cpfiffer•3h ago
We (.txt, the outlines people) had a brief thread about this paper on twitter if you're interested: https://x.com/dottxtai/status/1922322194379551128
hongbo_zhang•1h ago
We published a similar paper for MoonBit: Explore the Design of an AI-Friendly Programming Language https://conf.researchr.org/details/icse-2024/llm4code-2024-p...
int19h•53m ago
Been using Devin for a few months now, for Typescript and Python.

I've never seen it check-in uncompilable code, but watching the Devin console I can see it building and using the code to ensure commits are not complete garbage. When it has checked-in compilable and almost right but slightly wrong code, automatically running lint and tests (it doesn't always run them before checking in) from ci triggers it to push a fix on its own.

Feedback loops are nice, but they can be expensive, and time consuming (oh look at me complain that it takes Devin a whopping 15 minutes to complete a task) so I can definitely see the value in type constraints.

informal007•38m ago
Would it better if we move the feedback loops into RL-stage of LLM training?

Are there some related works?

Bus stops here: Shanghai lets riders design their own routes

https://www.sixthtone.com/news/1017072
83•anigbrowl•1h ago•34 comments

RPG in a Box

https://rpginabox.com/
56•skibz•3d ago•5 comments

Type-constrained code generation with language models

https://arxiv.org/abs/2504.09246
161•tough•7h ago•55 comments

Flattening Rust’s learning curve

https://corrode.dev/blog/flattening-rusts-learning-curve/
160•birdculture•7h ago•121 comments

Show HN: I’ve built an IoT device to let my family know when I’m in a meeting

https://nullonerror.org/2025/05/11/i-have-built-an-iot-device-to-let-my-family-know-when-i-am-in-a-meeting/
63•delduca•2d ago•38 comments

Branch Privilege Injection: Exploiting branch predictor race conditions

https://comsec.ethz.ch/research/microarch/branch-privilege-injection/
354•alberto-m•13h ago•133 comments

Replicube: A puzzle game about writing code to create shapes

https://store.steampowered.com/app/3401490/Replicube/
31•poetril•4h ago•3 comments

Google is building its own DeX: First look at Android's Desktop Mode

https://www.androidauthority.com/android-desktop-mode-leak-3550321/
267•logic_node•15h ago•214 comments

How “The Great Gatsby” took over high school

https://www.newyorker.com/books/page-turner/how-the-great-gatsby-took-over-high-school
27•pseudolus•16h ago•27 comments

Show HN: HelixDB – Open-source vector-graph database for AI applications (Rust)

https://github.com/HelixDB/helix-db/
147•GeorgeCurtis•12h ago•64 comments

Failed Soviet Venus lander Kosmos 482 crashes to Earth after 53 years in orbit

https://www.space.com/space-exploration/launches-spacecraft/failed-soviet-venus-lander-kosmos-482-crashes-to-earth-after-53-years-in-orbit
129•taubek•3d ago•88 comments

Build real-time knowledge graph for documents with LLM

https://cocoindex.io/blogs/knowledge-graph-for-docs/
114•badmonster•10h ago•18 comments

Launch HN: Miyagi (YC W25) turns YouTube videos into online, interactive courses

171•bestwillcui•17h ago•95 comments

Mipmap selection in too much detail

https://pema.dev/2025/05/09/mipmaps-too-much-detail/
29•luu•3d ago•7 comments

Multiple security issues in GNU Screen

https://www.openwall.com/lists/oss-security/2025/05/12/1
363•st_goliath•18h ago•218 comments

Airbnb is in midlife crisis mode

https://www.wired.com/story/airbnb-is-in-midlife-crisis-mode-reinvention-app-services/
114•thomasjudge•10h ago•189 comments

PDF to Text, a challenging problem

https://www.marginalia.nu/log/a_119_pdf/
255•ingve•14h ago•151 comments

Show HN: Mycelium

https://github.com/mycweb/mycelium
8•brendoncarroll•3d ago•1 comments

It Awaits Your Experiments

https://www.rifters.com/crawl/?p=11511
150•pavel_lishin•14h ago•55 comments

Garbage collection of object storage at scale

https://www.warpstream.com/blog/taking-out-the-trash-garbage-collection-of-object-storage-at-massive-scale
61•ko_pivot•3d ago•8 comments

Coffee for people who don't like coffee

https://ostwilkens.se/blog/coffee
60•ostwilkens•3d ago•155 comments

The world could run on older hardware if software optimization was a priority

https://twitter.com/ID_AA_Carmack/status/1922100771392520710
639•turrini•19h ago•592 comments

A visual history of the safety pin

https://museumofeverydaylife.org/current-exhibitions/a-visual-history-of-the-safety-pin
20•andsoitis•2d ago•0 comments

Fingers wrinkle the same way every time they’re in the water too long

https://www.binghamton.edu/news/story/5547/do-your-fingers-wrinkle-the-same-way-every-time-youre-in-the-water-too-long-new-research-says-yes
103•gnabgib•6h ago•40 comments

I learned Snobol and then wrote a toy Forth

https://ratfactor.com/snobol/
126•ingve•2d ago•32 comments

OpenTelemetry protocol with Apache Arrow

https://opentelemetry.io/blog/2025/otel-arrow-phase-2/
81•tanelpoder•12h ago•15 comments

Using obscure graph theory to solve programming languages problems

https://reasonablypolymorphic.com/blog/solving-lcsa/
46•matt_d•9h ago•6 comments

A tool to verify estimates, II: a flexible proof assistant

https://terrytao.wordpress.com/2025/05/09/a-tool-to-verify-estimates-ii-a-flexible-proof-assistant/
35•jjgreen•3d ago•0 comments

Cardiac: A CARDboard Illustrative Aid to Computation [pdf]

https://www.cs.drexel.edu/~bls96/museum/CARDIAC_manual.pdf
27•throwaway71271•8h ago•13 comments

Map of Palaeohispanic Coins and Inscriptions

http://hesperia.ucm.es/consulta_hesperia/mapas.php
17•brendanashworth•5h ago•1 comments