An unofficial, read-only Hacker News reader. Join the beta →

The need for a formally verifiable mainstream programming language (opens external site)

307 points by todsacerdoti 18 hours ago | blainehansen.me | 297 comments on HN

View full thread · View on Hacker News

Comment thread (48)

  1. 30 years ago, my then boss, a PhD in industrial automation theory, told me formal verification of even simple systems is impossible due to the practically infinite number of states systems can occupy. I have been thinking about this a lot over the years, and I still think this is the case, even more so than before with the bloat that introduces insanely fuzzy edges to any state graph. I wonder if there is really any chance of this becoming a viable concept for systems more complex than a tic-tac-toe game.
    Replies (10)
    1. It's possible to design with verifiability in mind (e.g. something like seL4)
    2. It's impossible to prove by brute-forcing the states of an arbitrary program, but it's possible on practice for programs that were built with verification in mind: pure functions wherever applicable, state invariants defined and verified for every stateful part. If you aren't able to prove a specific invariant at compile-time, you leave it as assertion which means you can still prove that either you get the correct output or an error, but never the wrong output or crash/vulnerability.

      I think today it's actually more possible than ever. Writing programs in this way is hard and tedious but LLM can excel at that. I also think that a hybrid of Z3+LLM is orders of magnitude stronger than pure Z3. LLMs generated a formal proof of GRH, surely than can prove banking app correct.

      The most vulnerable part that will remain will be the formalization of requirements. That's impossible to solve fully because you never know what the human had in mind exactly when he ordered the program to be written.

      Replies (2)
      1. Can these programs do anything useful?
        Replies (3)
        1. Well, seL4 arguably can do something useful, so... yes, they can?
        2. Z3 was reportedly responsible for ~40% of pre-launch bugs caught for Windows 7
        3. I’ve used Z3 to help with proving type system soundness in a DSL for RPCs. It’s pretty nifty.
      2. > The most vulnerable part that will remain will be the formalization of requirements. That's impossible to solve fully because you never know what the human had in mind exactly when he ordered the program to be written.

        I don't think it necessarily needs to be solved. You can combine formal verification with tests! Use tests to show that the program occasionally does what you want. Use verification to show that the program never does what you don't want (e.g. crash). The formal requirements can be quite weak this way, yet you still get a high level of assurance overall.

        And you'll be surprised how many invariants you'll actually need for the simple "don't crash" requirement -- to prove anything at all in a complex program, underlying components need to be correct, or it becomes hard/impossible to prove anything at all about the code using those components.

        So (admittedly without any personal experience other than studying this area an university more than decade ago) I expect even a simple "don't crash" requirement gets you the majority of the formal verification benefits. (as long as you have some tests to ensure the implementation isn't just returning a constant)

        Replies (2)
        1. Tests still encode requirements. The problem is exactly the same there. You can’t write tests if you don’t know the intended behaviour.
        2. Depends on the goal. If you just want software to be generally safer, with less bugs and higher quality then this is sufficient, yes.

          If you want actual guarantees that e.g. money won't disappear from your account some day or your messages won't be read by a third-party, then formal verification alone doesn't grant that.

          Consider that Wifi WPA2 encryption was formally verified and yet broken. In simple terms the bug was in the protocol specification [1]. The "code is the law" crypto folks have even more fun stories to share.

          [1] https://www.galois.com/articles/formal-methods-krack-vulnera...

    3. We're now able to do things like try to prove that no fuzz test with less than 32 MB of input will crash this program.

      Formal verification of full behavior is impossible. But formal verification of important properties is within the reach of AI.

      Replies (1)
      1. Depends on the complexity
        Replies (1)
        1. Trivially true. But the complexity that can be handled is useful in the real world. And what can be done is being scaled up quickly.

          See https://www.anthropic.com/research/formal-verification for more.

    4. Reminds me of pure functional languages. Sounds so nice, no icky state or side effects. Doesn't actually help for real problems.
      Replies (5)
      1. How on earth they not help with real problems? Also, their very point is that most of the program is pure, but they most definitely have IO and other kind of side effects, it just lives at the very edge of the system.

        Haskell has interesting uses in industry, but in general in the last decade or so the functional paradigm has become retrofitted to basically each mainstream language. Like java's streams/lambdas, algebraic data types in basically every modern language (rust), immutable by default, etc.

        Replies (1)
        1. You've got to remember that the average developer is a Javascript or Python coder who lacks the mathematical ability to do even the most rudimentary abstract alegbra, and so cannot write or understand a production Haskell program, let alone understand how abstractions can eliminate whole classes of bugs.
          Replies (4)
          1. Who is credentializing these devs, hiring them, and how are they getting by?
            Replies (3)
            1. AI has made them into 10x devs now. 10x the output, of putting together simple solutions to known problems with a few components. Basically following a recipy, like a suit is customized for each customer.
            2. It's a simple majority issue.

              If the majority say "you don't need a degree" then you don't need a degree.

              If the majority believes in "move fast and break things" then things stay broken.

            3. College degree, coding boot camp, or no formal credentials but they built side projects to show to potential employers. Also there was an extreme demand for software devs at certain points in time pre AI coding, where they just needed people to write basic stuff in whatever framework.
          2. Java has sum and product types with exhaustive pattern matching, lambdas and streams, and is the next more widespread language just after python and js.

            JS devs are doing monadic development (async is just that), and plenty of lambda passings, with the predominant frontend framework model being one where the view is a pure function of state, driven by reactive changes of the state.

            Functional won vast areas in mainstream programming. What you are saying may have been true 15 years ago, but not anymore.

            Replies (1)
            1. Yes, what prevailed is doing a reasonable mix of paradigms instead of going all-in on something. Pure functional was never popular, but pure OOP was, and it sucked.
          3. This is the problem that excites me the most about agentic coding

            I think software development and the tools underlying it could be wildly more powerful and exploit the underlying computer algebra more directly if dev tools didn't have to cater to the lowest common denominator imperative language dev

            LLMs are valuable to me not because they can replace the types of coding these devs do, but because they have a far more deeper 'understanding' of the foundational math and algebra than could ever be expected of the average dev

            There's ways software engineering could engage much more directly with these concepts to produce more sound and expressive results if the "user" could be safely assumed to be an IMO level mathematician like a sufficiently powerful LLM is

            Replies (1)
            1. > LLMs are valuable to me not because they can replace the types of coding these devs do, but because they have a far more deeper 'understanding' of the foundational math and algebra than could ever be expected of the average dev

              This is the correct way to look at using LLMs for coding using their "jagged intelligence". The key here is that the developer has to know enough of the formal methods mathematics to guide the LLM within proper guardrails such that it cuts down on the non-determinism. You don't have to be a master mathematician who knows every single detail but just have enough conceptual understanding of the domain that you can create modular components and link them together using pre/post/invariants/assertions and type-driven design. You keep overall control while leaving the drudgery to the LLM.

              Here is one process methodology that i wrote up - https://news.ycombinator.com/item?id=50319347

              Continue this thread (1 replies) →

          4. I'm not the average developer. And by the way, I've heard that rant before, always from that one SWE on the team who's going to be the hardest person to work with by focusing on all the wrong tiny details.
      2. Tell me about a real problem you have.
        Replies (1)
        1. My job had me working on a custom version of ejabberd across like 2 years. For those unfamiliar, that's in Erlang, which is probably one of the more convenient to use pure functional languages. It worked, but it was so cumbersome. BEAM is cool though, so they made Elixir to use that without sticking to pure functional.

          Or like, make any web backend.

      3. So none of the problems Nix solves are "real"?

        Do you really think all the math proofs being done with Lean could work at all if it was imperative as opposed to a purely functional, dependentally typed language

        Replies (1)
        1. For general software engineering, which is the topic of the article, not math proofs
          Replies (1)
      4. You run your program in a host that does all the icky side effect things.
      5. There are plenty of people solving real problems with Haskell, enough to pay their bills.
    5. I mean in most cases a mathematician is not going around and checking each and every case, why would software do that?

      Formal verification a la cocq/Agda/lean works by constructive logic. (Disregard the middle paragraphs, I'm sure I'm not telling you anything new)

      The type system is strong enough to encode various statements, even about variables (dependent type system, this is a valid type: a + b == b + a)

      The only way to create an instance for this type is by proving the statement. The == type can only be created by trivially equal expressions, so it will accept a == a. So in the definition of the above you will go step by step, each step itself being accepted by the compiler, and at the end you get a proof and a compiling program, a bit similarly to how you would do it on paper.

      So I guess it boils down to. Could a mathematician figure out some elegant proof for the system that somehow makes "checking each state" reduced to an easier statement? If yes, then formal verification can also do so. If no, then LLMs may be able to scale up what is feasibly provable, but there is no real alternative. Real world testing is just scratching the surface of the state space.

      All in all, that's why it's paramount that systems are designed with complexity in mind. We can compose simple function into a simple function and have it explode in complexity, but it's also possible to choose good primitives that are easy to "mock out" (often both in practice and theory) and not significantly increase the whole system's complexity.

    6. Haha no. I am sorry but we still have issues with null pointers. So lets not get ahead of ourselves and try to model the real world. People thinking "woe is me the world is so complicated lets keep using C" is bizarre.
    7. Your boss was not quite right.

      There is a "Correctness-by-Construction" approach to programming which allows one to severely restrict the state space of a program from the beginning such that every statement/block/function only operates on a much smaller "valid" set of program sub-states. This is using classical logic via predicates as explained by Hoare Triples, Dijkstra's wp-calculus, Morgan's refinement calculus and later Meyer's Design-by-Contract.

      To do deductive verification statically, the basic idea is to group all computation paths (i.e. sequence of state transitions) over the entire program state space into a much smaller set of logical constraints (aka verification conditions) which is then handed off to a SAT/SMT solver to be proved/disproved.

      An alternative is constructive logic using types and lambda calculus as pointed out by a sibling commentator.

      There have been lots of systems which have been formally proved/verified to various extents/levels as can be seen in Industrial-Strength Formal Methods in Practice by Hinchey/Bowen and many other similar books.

      Replies (2)
      1. I don't think only verification is enough for safe programs. How program behaves when error happens is probably more important than happy path logic. Programming is layers of abstraction on top several more - unless all of them are formally verified - program can be never be truly verified. Think of different hardware, software, time, timezone, os/drivers/shell/vm/container/language runtime versions etc.
        Replies (2)
        1. > Think of different hardware, software, time, timezone, os/drivers/shell/vm/container/language runtime versions etc.

          The original comment was about industrial automation and in that environment a lot of those issues are not that relevant. Unless we're talking really big systems, a lot of it is very well specified controllers which run without shell/VM/container/runtime. They just talk to some fancy general GUI on a different chip/board to load the data/config. But the state space of a lot of actual practical hardware doing the work is not that complex. Bare metal, careful io timing, state transitions and some control loops gets you very very far.

          To be clear, there are bigger, complicated systems, but simpler devices with a clock, PID loop and I2C outnumber them by likely a few orders of magnitude - we can start there.

          Replies (1)
          1. It can definitely help if scope/runtime is limited. I myself have played with something similar : https://github.com/statelyai/xstate Modelling entire interaction as state machine so program always remains/lands in known states. IMO FSMs probably better suited as it has error modelling and handling inbuilt.
        2. That is the wrong way to think about Formal Methods/Specification/Verification.

          Verification is not about "happy path logic" but all computation paths (happy, error, cross-layer etc.) over the entire state space of a "system".

          The system can include everything from hardware circuits all the way to distributed applications if needed. Across layers/components it works by composition of contracts where a higher-layer guarantees based on its lower layer's contract guarantees. The book i listed gives examples.

          See also my comment here - https://news.ycombinator.com/item?id=50664740

          Replies (1)
          1. Partially I agree. But when it comes to errors/not-happy path - I am talking about pesky issues, driver/hardware errors which can never be mapped to any "Type". There is a reason every statically-typed programming language has Root Error type. For eg. Java has `Throwable` with two subtypes - `Error` and `Exception`. A `RuntimeException` is type that does give handle to programmers and JVM guarantees that it will be thrown. But sometimes one has to catch root "Throwable" or "Error" types in order to prevent JVM crash. It will be tedius to have formal verification theorems referring to "Throwable" in order to be formally verifiable. I guess It can be done - only if languages like Lean are expressive enough - but only using LLMS, for humans it will be quite complex. IMO - not worth. It will be repeat of "elegant" functional programming languages failing to go mainstream.
            Replies (1)
            1. My previous comments already pointed to Meyer's "Applying Design by Contract" paper which shows how to integrate exception handling with DbC. Here it is directly - https://news.ycombinator.com/item?id=48358691

              Basically, you have to keep your module internals "pure", distinguish between domain exceptions (i.e. checked) vs. environmental runtime exceptions (i.e. unchecked) and push all "impure" operations to the boundary of the module.

              Also see the following paper which shows how to formally model hardware faults;

              Faulty Logic: Reasoning about Fault Tolerant Programs - http://www.cs.princeton.edu/~dpw/papers/faultylogic_esop2010...

      2. I think what he likely meant was: its impossible to do formal verification of programs the way they are written and deployed.

        - ignoring how programs are written, they are usually compiled and linked, which introduces states unknown to the original programmer,

        - ignoring how they are compiled, they are executed in environments with variability, including temporal; I remember tracing a problem related to a longer trace on one of the paths of the PCB, and materially different outcomes depending on which trace the signal happen to travel on (it was a high-frequency P-I-D controller, but for whatever reason the microcontroller could transmit the same signal on one or the other path). The particular bug was traced with a oscilloscope, by a graybeard, not algebra.

        The way I see it, in order to get verifiability to actual programs, one would need to cover the state space of the entire system - algo, compiled binaries, operating system, hardware, and the weather on the other side of the planet for a good measure.

        I am not an expert in the area, in any way - just a regular software dude. Appreciate all the responses in this thread!

        Replies (1)
        1. See my comment here - https://news.ycombinator.com/item?id=50666297

          It depends upon what all parts of a "system" you want to encompass under "formal verification". There is nothing preventing you from verifying everything from hardware all the way to user-facing software or only certain layers in the chain.

          As an analogy, the FIPS/Common Criteria security standards have multiple levels of certification going from simple signed binary blob to a complete hardware and software lockdown system.

          You can do the exact same thing with formal verification of your system if needed. LLMs have made this possible if driven properly.

          Replies (1)
          1. Which level do you mean? If you mean EALs, to my knowledge, even the highest level never meant to be verification of the implementation but verification of the design.
    8. Lots of those states will be numbers that can reasoned about using ranges, which helps somewhat.
    9. I'm surprised about this bit of wisdom imparted to you. Componentization addresses the 'whole system state space' issue, does it not? In our modern societies, we routinely use insanely complex mechanisms built by very average workers. The key seems to be discreet parts. The state space of individual parts is not huge and its seems to me (disclaimer: not a PhD /g) hierarchical systems can afford manageable state space complexity at each layer of the hierarchical system.
    10. A lot has changed in 30 years. Symbolic model checking was just in its academic infancy (early '90s), and abstraction techniques, especially counterexample-guided abstraction refinement (CEGAR, early '00s) were significant breakthrough. All these ideas converged into e.g., SLAM which is what Microsoft pushed for Windows driver verification, which I believe should fit your "more complex than a tic-tac-toe game" goalpost.

      And this was 25 years ago! We now have advanced formal techniques for inductive reasoning like IC3 that run on top of SMT solvers, enabling verification of (some) bona-fide infinite state systems. Of course the problem itself is undecidable in the general case, but in practice these tools do get the job done. We have formally verified OS kernels, Lean kernels, SAT solvers out there :)