r/Compilers • • 21d ago

Almide – a language I'm building to make AI-written code safer

Hi. I've been working on a language called Almide and wanted to show it here.

The motivation is simple. AI agents write a lot of my code now and I can't really read all of it anymore. I think that's going to be true for a lot of people soon. So I wanted a language where more of the safety comes from the compiler and less from somebody reviewing.

I think Rust is the language you'd want AI to write. But honestly I'm not a strong Rust programmer and I never got used to ownership. So in Almide you just don't write it. No ownership, no lifetimes, no free. The compiler infers it (Perceus style, the Koka approach). On native it generates Rust and lets Rust drop things, on Wasm it does reference counting.

type Order = { id: Int, items: List[String], total: Int }

fn large(orders: List[Order]) -> List[Order] =
  orders |> list.filter((o) => o.total > 500)

fn main() -> Unit = {
  let orders: List[Order] = [
    { id: 1, items: ["pen", "ink"], total: 260 },
    { id: 2, items: ["desk"], total: 900 },
  ]
  let big = large(orders)
  println("${list.len(orders)} orders, ${list.len(big)} large")
  for o in orders {
    println("${o.id}: ${list.join(o.items, ", ")}")
  }
}

orders gets passed to large() and then I use it again in the loop. In Rust I'd have to think about that. Here the compiler figures out whether it needs to copy or bump a refcount. The downside is you can't see where that happens.

Here's the part I actually care about. I don't trust my own compiler. I built it with AI agents, so why would I? So the rule is that the compiler doesn't get to grade its own work. Every time it builds Wasm it has to write down evidence of what it did, every place it shares or frees a value, every array index it checked. Then a separate program reads that, and if it doesn't add up there is no output file. You just get an error telling you the compiler has a bug.

And then I thought, ok, but the checker can have bugs too. So now there are four of them, built in different ways. Two come out of the Rocq proofs (the proof kernel itself and a checker generated from the proofs), one is hand-written Rust, and one opens the finished Wasm file and checks that the evidence is about instructions that are really in there. In CI the first three have to give the same answer on every test program. It has already caught a real miscompile in my compiler.

It is not done, I want to be clear about that. On your own machine only the Rust checker runs, the others are CI. Native builds don't have this check. The proofs cover the checker, not the whole compiler. And if your program is simply wrong none of this saves you. But I really think this is how it should work for code nobody reads.

It's pre-1.0 and has bugs. I used AI agents a lot to build the compiler itself. I don't have data showing agents write Almide better than Rust.

repo: https://github.com/almide/almide

playground: https://almide.github.io/playground/

If anyone has thoughts on the "you can't see the copies" problem I'd be glad to hear them.

0 Upvotes

10 comments sorted by

4

u/jcastroarnaud 21d ago

AI agents write a lot of my code now and I can't really read all of it anymore.

I think that your whole problem starts with just that: letting AI do the work. AI-generated text (and code) isn't deterministic, and will have so-called "hallucinations", no matter how you slice it. As your case shows, the cost of minimizing its errors is getting higher than the cost of human development. It's a trust issue, not a correctness issue.

Try to write, by yourself, a compiler for a very small subset of Almide, just enough to write a compiler using it. Write (or let AI write) a very through suite of unit tests, so much that you can trust that the compiler works as intended. Then, bootstrap from this Almide subset to the real language.

0

u/Klutzy_Afternoon_503 21d ago

Thanks for the comment. To give you an idea of the size, Almide's Rust implementation is already about 450,000 lines across 28 crates. On top of that there's the standard library, the tests, the Wasm and native backends, an IR interpreter, and the verification stuff.

So my problem right now isn't really building a small compiler from scratch. It's more like, the implementation got this big, no human is going to read all of it, so how far can I check it independently and widen the part I can actually trust.

At the moment I compare the results of several execution paths on the programs they support, and on some paths I verify certificates. That doesn't prove the whole compiler is correct though.

Using a small hand-written compiler as the base of trust is an interesting idea. I'd like to think about how a small core like that could be combined with the big implementation I already have.

3

u/jcastroarnaud 21d ago

Almide's Rust implementation is already about 450,000 lines across 28 crates.

Oh my, that's a large project! I wonder how much of it is cruft.

Put on your systems design hat, and consider the usual structure of a compiler: lexer, parser, IR generator, optimizers, object language generator, runtime environment, tests for all these, and so on. Mot probably these are all crates already. Make your pet AI decompose these to smaller modules/crates by functionality, just above class/function level; your eyes in the code are needed to instruct the AI how the code must be sliced down. You will need to bite the bullet and scroll (not read) through the current code, at least once, to do that.

The intention is to make each part small enough to be understood completely by you, even if you can't see the whole. Ensure that the API of each crate is clear and simple. Then you can test/verify each part, trusting the AI results, and combine them to make the whole compiler. AI must not ever regenerate the whole compiler, just each part alone and the "glue" compositor module(s).

1

u/Klutzy_Afternoon_503 21d ago

I get why 450k lines raises questions. If I saw that number on a new language, I'd want to know what all that code is doing too.

First, I should be clearer about what that number is. It's physical lines of Rust under crates/, so it includes comments, blank lines and the tests that live inside the crates. The standard library, the proofs and the test suites are separate and not in it.

You asked how much of it is cruft, so I went and measured. Once you take out comments, blanks, generated files and tests, the real code is around 290k lines. Out of 18,843 functions, 17 aren't referenced by name anywhere, and about 1.5% of the code is duplicated. So it doesn't look like mostly dead weight. But you were right about one thing. The biggest crate has 87 files that were split by size, not by what they do. That's exactly the kind of slicing you were talking about.

As for why it's this big at all, there are two reasons. One is that Almide isn't just a parser and a code generator. There's the native path and the Wasm path, an interpreter, diagnostics, and the verification machinery, and each of those is working on a different problem. The other is that it's in the middle of a migration. There's an older code generator (I call it v0) and a newer one (v1) that everything is moving to. The test programs all go through v1 now, but v0 is still in the tree as a fallback, around 54k lines of it. So some of the size is the before and the after sitting side by side. I think that happens in any real project that's partway through replacing something.

I'm not going to claim every line is needed, or that the compiler is correct. That's on me to keep digging into and to show. But I do want to explain why it got this big, what each part is for, and where the complexity actually comes from.

I'd really like to go through the architecture with you, piece by piece, for as long as it takes. If there's a part you think could be smaller, let's dig into it together.

2

u/jcastroarnaud 21d ago

The biggest crate has 87 files that were split by size, not by what they do. That's exactly the kind of slicing you were talking about.

Then the situation is worse than I thought, because I expected a crate per compiler component.

The other is that it's in the middle of a migration.

The migration from v0 to v1 explains part of the size of the project, although not all. If v1's code generator is the same size as v0, both together are almost 1/4 of the entire codebase.

I'd really like to go through the architecture with you, piece by piece (...)

I don't have the time or patience to get involved in anyone's project: I have too many of my own to juggle.

What I expect is that you decompose your project, at the design level, then leverage the power of your AI to implement, test and verify each component separately. Make sure to have a clear, simple, and stable API for each component/crate. You will end up with a few hundred crates, each one manageable by a human, and that's fine. Then your compiler will be composed from these components, also with your AI's work. Here are some Wikipedia articles with the required concepts:

https://en.wikipedia.org/wiki/Component-based_software_engineering
https://en.wikipedia.org/wiki/Structured_analysis
https://en.wikipedia.org/wiki/Structure_chart

I have no more to say. Bye.

1

u/Klutzy_Afternoon_503 20d ago

Understood, and thanks for taking the time. The point about decomposing at the design level before handing anything to the AI is the one I'm taking away.

1

u/[deleted] 21d ago

This is a compiler project of your own that comprises 450,000 lines of Rust?

That is huge for a one-person project (mine are about 30K lines).

But apparently you used tools to generate it? How big would it have been if you wrote it manually, or is your project too ambitious for that?

the real code is around 290k lines.

You can do the same exercise on any codebase, including my 30Kloc one.

Out of 18,843 functions...

This is the real problem: given one of those functions, would you know what it does, how it works, how you might change it? If you had to write each one, you'd know! And there wouldn't be so many.

There's the native path and the Wasm path, an interpreter, diagnostics, and the verification machinery,

My 30Kloc product, which is about a 0.3MB binary:

  • Can generate EXE/DLL files directly
  • Can dump ASM/IL intermediate forms
  • Can interpret the IL form
  • Can run the generated native code in-memory
  • Can generate a single amalgamated source file
  • Can generate a private executable/library binary format

1

u/Klutzy_Afternoon_503 20d ago edited 20d ago

Thanks for taking an interest.

Yes, this is a compiler I'm building by leaning hard on AI coding myself. And you're right, I can't tell you what each individual function does.

What I can explain is what each crate is responsible for. If this were a compiler written by hand, I agree with you, the author should be able to explain all of it.

That's exactly why I don't ask anyone to trust the compiler. Almide's compiler has to write down evidence for what it did, and a separate, much smaller program checks it. The rule that checker applies is proven in Rocq. That's 654 theorems and lemmas, none admitted. Every program in the test corpus builds with that evidence, on both Wasm and Rust. I can't read all of it, so I'm trying to make it something you can check without reading it.

The project is chasing goals that are each really hard on their own. Performance on par with Rust, memory safety without writing ownership or lifetimes, and small Wasm output. Hello world as Wasm is about 37 KB in Rust even when you tune everything for size. In Almide it's 325 bytes. On speed I'm not at Rust's level everywhere yet.

If I wrote all of that by hand it would probably take me more than ten years, and I don't want to wait that long. From here on there's only going to be more and more stuff made by AI. I'm actually in favor of that. I think it's a wonderful thing when what someone has in their head can take shape. What I want is to be able to say that with Almide, there are parts of that you can protect.

Your 30K LOC product sounds really cool. Just hearing that tells me how much skill you have. I'm not thinking "just let AI write the compiler." I only want to try delivering, in the form of a compiler, the kind of value that's possible because of AI. I'd really like to hear your honest opinion, don't hold back.

1

u/ElephantElectrical31 21d ago edited 21d ago

hi there,

Building a programming language from scratch with this kind of memory management, even with AI, is a crazy idea. And I know what I'm talking about.

I work on Nikaia, a language that also aims to make AI programming safe. It comes with its own solver, and it also has no lifetime annotations, similar to your approach, and also compiling to Rust.

Maybe we could exchange ideas and, in the long term, exchange components.

Let your AI scan the Git code to understand it. It's worth the tokens: https://nikaia-lang.org/

1

u/Klutzy_Afternoon_503 20d ago

Hi, thanks for the message! It's nice to meet someone working on similar ideas.

I read through your solver docs, and the idea of proving assertions at build time and removing the corresponding runtime checks caught my attention. I'd love to hear more about how that works. It's something I've wanted for Almide but haven't gotten to yet.