Hacker News new | ask | show | jobs
by thesecretsquad 3 days ago
I'm not half as smart as most of the people on HN and I don't know much about formal verification, so maybe this is a dumb question....

My understanding of this is: You write a "contract" that defines the specification for some functionality, including inputs, outputs, preconditions, post-conditions, etc. and the contract can be formally verified by some proof checker that says it's logically consistent. Then you build the implementation and the implementation can be proven to abide by the contract (or not).

But somebody has to write the contract (either a person or AI). If the contact specifies behavior that is logically consistent as far as a proof checker is concerned, but the behavior is defined incorrectly, you could have a situation where "you built the thing right", but "you didn't build the right thing".

So someone still needs to make sure the contracts are specifying the correct behavior. Once you know you have, then I suppose the formal verification is good because everybody (people and AI) know that it guarantees its behavior.

My question then is: Assuming everything I just said isn't nonsense, aren't we still in the same place as before? Someone needs to know how to specify (and program) the right behavior?

14 comments

It is a gap, but the spec is often declarative, on what needs to be true, rather than imperative, about all the exact steps. Hence, less prone to error. You're right that it proves the protocol, but not necessarily the implementation.

A not-quite-there way to close the gap is that you can generate traces from the spec/proof, a sequence of states and events that must hold true. and then you can use that trace to replay back in tests, and if the implementation is correct, then it should mirror the trace.

I am the Truss author. In Truss, the proof associated with a program explicitly proves it meets the contract. You're right the contract is declarative, and the proof is what proves the compliance of the imperative code to the contract.
The spec is also nice in that higher level abstractions check they're using the lower level ones according to actual specified behaviour not intended behaviour.

E.g. with a normal programming language a bug in the hash-map internals that rarely returns the wrong element is undectable. With a spec, if the spec says it can ever return the wrong element, I'm going to run head first into that when I try and prove that my pathfinding algorithm using that hashmap for memoization is correct.

I don’t need to specify all steps, you need to know what correct means in your system. Then you find the steps to get there. The spec for a sorting algorithm says that the output is a permutation and that no element at I > J is smaller than the element at J. This is true for any sorting algorithm no matter the steps ie quick sort merge sort will have different steps but same spec
> spec is often declarative

ppl always come with weird workarounds to put in loops and if conditions and then throw it all away to use a general purpose programming languge

Maybe try not doing that?
You are not in the same place because the specification does not have to run on a computer. For instance, it could be horribly slow if it were actually executed. In the realm of optimization formal specification really shines because it is the situation where something relatively simple can be specified but the implementation is very complex. Furthermore, a specification does not in all case have to specify everything but instead can specify the essential properties that a system has. For instance, a permission system could have the property that a user who lacks a certain permission can never obtain information that he does not have the right to see but the detail of how that is implemented are not in the specifications. Also, for instance, specify that a system cannot crash or leak memory.

In situations where the specification is as complex as the program one should really wonder if one is doing the right thing.

It helps substantially because it disentangles subjective and objective programming.

Like if you want a program that sorts a list that is a subjective goal because you could just as easily choose a shuffled list, or a different sorting, or any other sort of list ordering.

Once you've established what you want fulfilling that is an objective task.

Today's programming languages entangle subjectivity and objectivity together, and better decoupling them means we can automate the objective parts which dramatically reduces the programming left over for humans.

The spec is far smaller than the implementation. For example take the TimSort case whose implementation in Java and Python had a bug that was there for years until someone tried to formally prove it correct. I also think many valuable properties do not need specific domain judgement like never crashes, no out of bounds access, no data races etc. A spec says "what" not the "how" you still need someone who understands the requirement but they don't have to get every single edge case right. If a model writes both the implementation and the proof, the checker doesn't care who wrote them. Human review shrinks from "read all the generated code" to "read the spec".
> The spec is far smaller than the implementation.

That heavily depends on what problem you're trying to solve. The very example from TFA is quite illustrative:

    contract ReplaceElement {
        function replace(words: &mutable Slice<UInt64>, index: UInt64, value: UInt64) -> UInt64
            requires index < words.len
            ensures result == old(words[index])
                and snapshot(words) == update(old(snapshot(words)), index, value)
            terminates
            effects { read(words), write(words) }
    }

    implementation InlineReplaceElement of ReplaceElement {
        function replace(words: &mutable Slice<UInt64>, index: UInt64, value: UInt64) -> UInt64 {
            let previous = words[index]
            words[index] = value
            previous
        }
    }
Or take, for instance, this spec in English prose: "given a predicate that returns true or false for each value, and a list of values, `filter` returns a new list that contains exactly the values in the original for which the predicate returned true". I guarantee you that turning this into the Truss contract would produce a significantly larger amount of code that the implementation that would consist of just the following 5 lines, modified accordingly:

    result := []{}
    for _, v := range list {
        if predicate(v) { result = append(result, v) }
    }
    return result
And of course, please note that the spec I've given is defective: it doesn't say anything about the order of the elements in the resulting list, or the presence/absence of duplicates. Also, you probably will have to insist on the predicate being a pure or at least a repeatable (reproducible?) function — I don't know how you'll spec `filter` when it's given a non-idempotent predicate.
Yes, but there's an implicit assumption that many parts of the contract are easier to understand than the corresponding code.

Take sorting as an example. Verifying any one instance is easy even in something like Python

  all(a<=b for a,b in zip(L,L[1:]))
Which wasn't really made for the task. If you have theorem proving as part of your contracts then that just gives you extra confidence above and beyond however else you're verifying them. Note that this is much easier to understand even if you don't know Python than a typical sorting algorithm.

I've personally never seen a contract system properly handle business rules well (the set of rules is usually about as verbose and we'll abstracted as the top-level code implementing them, and bugs are typically exactly the issue you posed, that the rules were improperly specified), so I'm skeptical of this approach.

My MSc is a spec for a business system. It’s a state machine in Alloy. For accounting. Basically it is free of a lot of accounting errors. It is painful to write such a spec. Very much so. But it pays off handsomely. Throwing my thesis in an LLM generates amazing software. It took two years to write so yep that’s not something you can do easily. On the other hand asking an LLM to write a spec given the natural language version of the thesis say yields a terrible Alloy model! So it pays off if your software is mission critical
I think my point is that for business logic I often see code like

  candidates.remove_all(x -> x.tracking_disallowed)
And specs like

  require candidates.all(x -> !x.tracking_disallowed);
Getting the spec right is not just equally hard to getting the code right; it's equally hard in _exactly_ the same ways.

And that doesn't invalidate the use of specs and formal verification. I use them a lot, especially any time I touch nontrivial concurrent code (which is most concurrent code).

It also doesn't invalidate it for any specific use case (accounting seems like a classic example where there are known invariants which are easy to prove and harder to get right).

As a new language tailored for LLMs though, the presence of specs doesn't seem to me like it'll solve enough of the normal classes of errors to pay for its complexity budget. It doesn't seem, by itself, like the next big thing. Without some solution to that "business logic" problem I mentioned, which very well might include formal methods as a component, my baseline prediction would be a resounding lack of notable success for the project on almost any axis the author cares about. I'm happy to be proven wrong of course; I really do like when people succeed, especially with clever new ideas; but my normal optimism is wavering here.

You can write a contract in natural language also and get more expressiveness, or do both. I think this language (formal contracts only) is a bit ambitious or maybe just not general yet? Regardless, you (humans and LLMs) can actually reason about contract correctness in terms of whether it is over or under specified, and if it is well grounded. It is also easier to make informal judgements (again, by humans or LLMs) about building the right thing compared to looking at implementation code. Lastly, when you do find bugs, you can reason about them at the contract level and figure out who to blame and what to fix more quickly (via contract improvements and then implementation alignment). So we are in a better place than before where we only have implementation code as a source of truth (aka vibe coding).
It’s like how type checking proves your program to be correct in terms of types.

Similar to that concept but applied to more than just types.

In fact specifications can be written in the form of types. So for example:

   Func(int, int) -> int

Is saying this function takes two integers and the output has to be an integer. That is part of a specification.

You can also write:

  Func(int + N, int + M) -> int + M + N
And that’s the sort of thing formal verification is. But it also sort of changes the way you program because you have to build your program from smaller pieces that are axiomatic.

For my explanation you can understand the specification part but writing a program that can be proved to fit the specifications automatically (via the axioms) is harder to illustrate. It’s all very mathy.

Yes, you could say the same things about types, tests, or any other verification (testing) technique. They are all tools in a toolbox. Traditionally contracts and proofs have not been well integrated with the language and the cost benefit analysis hasn't been that great compared to other techniques, at least for most projects. We will see more efforts like this one that are trying to change the cost/benefit by integrating with the programming language.
Verification is only meaningful with respect to specification, so yeah, either you know what properties to prove, or trust the LLM to come up with the properties you should know. On the other hand it helps a lot in implementing things according to spec
The product person should be able to work with the AI agent to make a high-level specification — after all, they will need to communicate that intent at some point.

I’m not sure where software engineers would take over, but given a sufficient spec, the system ought to be a direct translation of that spec. Ideally the spec itself should compile to realize system.

Isn't code that works a sufficient spec? By works I mean, meets the stakeholder requirements as they understand them.
Yes, but usually that’s only readable by software engineers, and mixes up the intent (essential complexity) with implementation details (accidental complexity).

In practice, that framing also loses the distinction between expected and actual system behaviors; bugs are impossible by definition because — that’s the spec!

We’re in need of a high-level specification language that suits a product person in partnership with an LLM. Say goodbye to lengthy requirements documents and hello to very rapid implementations — high-fidelity system implementations at the speed at which product can request them.

Maybe software engineers would have a role connecting some of the lower-level pieces or maybe it would completely displace jobs for many of us. As inconvenient as that would be, the purpose of computing is to build useful systems, and a process that does that an order of magnitude better than what we do today would be the right thing to do in the long run. It might even create more demand for software engineers, in that paradoxical way that innovation sometimes works. Hard to say.

I would assume that the contract is much more concise than the implementation to satisfy that contract. Therefore, it's much easier for a human or AI to maintain.

It's a bit like writing and maintaining a declarative SQL query (new-style) vs writing a query planner (old-style programming).

Were already trusting LLMs to write code, which means it has to translate the human language to both system requirements and make sure the implementation fits those requirements. Why not remove half the problem?

There is also something excellent formal code produces: modularity

>My question then is: Assuming everything I just said isn't nonsense, aren't we still in the same place as before?

You have a point, but the missing piece is the context size needed to iteratively write said contract.

Another angle to look at it -- we already do something like this (TDD) and it works, even if the classic argument says you can have bugs and gaps in your tests Yes you can, but you can also have good and helpful tests. Also type systems while not being tests are from the same general area -- they express constraints which can be wrong.

Yet another angle to look at it -- it's easier to write a lot of declarative stateless code plus a small library solving general case of the statefullness than writing ton of stateful imperative code. From the correctness standpoint of course.

Add: contract being smaller than implementation is mentioned here, but it's a correlated metric to what I mean above. Even if the contract is longer, it could in theory allow refining different desired properties and their interactions in separate context windows. The contract being longer will also mean it specifies the behaviour in more details.

SDD is different from TDD though. Tests are just a parallel interpretation of the spec in SDD (parallel to the implementation being tested), while in TDD tests are considered a source of truth instead of the implementation (in SDD the spec is the source of truth).
Correct. Formal verification is indeed mostly vibes slopbros are currently swapping as a buzzword.

The hard part was never expressing the intent in a given language. The hard part is (and always has been) coming up with that intent in the first place.

The concept presented here is soemthing we do anyway in both OO and functional languages. Some syntaxes are more imperative and others are more declarative but nothing ever stops you from shipping behaviours or even tests as a package.