In the 18 months before the Riemann hypothesis was solved by AI, Grant Sanderson of the excellent educational mathematics YouTube channel 3Blue1Brown had been interviewing mathematicians, asking them “How would you feel if AI produced a Lean-verified proof of the Riemann hypothesis, incomprehensible to humans?” The answer was almost universally some variant of “such a proof would have some value, but ultimately the real value is in advancing our understanding of mathematics, not merely in having the proof”.

As software engineers, we struggle with a similar question. “How would you feel if you could press a button, and out comes an enormous piece of code, incomprehensible to humans, that solves the problem?” Many of us at Well-Typed are recovering academics, occasionally falling off the wagon and writing the odd research paper, and we value understanding for understanding’s sake. We might therefore have a reaction similar to that of our mathematician colleagues to the proof of the Riemann hypothesis. Yet, we must acknowledge that in industry there is real value in the object itself, even if we don’t necessarily understand it.

The real challenge, of course, is in determining whether or not the AI-produced code indeed “solves the problem”. We typically don’t have the benefit of a terse mathematical statement of the desired outcome that we can verify the code against. Therefore, how can we be certain that the code is correct, if we do not understand it? And if we are not certain, can we really in good conscience ship such code to customers?

Mathematical proofs are timeless: the Riemann hypothesis will be just as solved tomorrow as it is today. The same is not true for software: even if a piece of software “solves the problem” today, it is not at all certain that it will still do so tomorrow. Software must be maintained, refactored and updated; if the code is incomprehensible, that will be almost impossible to do without risking regressions. Perhaps we can hope that AI will be able to do it, but again, can we really in good conscience ship code that we “hope” can be maintained?

Decades of computer science research have been dedicated to software development methodology, programming languages, and testing approaches that all help verify that a piece of code “solves the problem”. Ever since Well-Typed was founded nearly 20 years ago we have been committed to furthering the cause of Haskell precisely because we feel Haskell is a good language for doing exactly that.

The same combination of features that makes Haskell an excellent choice for writing code we can be confident about, makes Haskell an excellent choice for writing AI-assisted code. Indeed, this becomes all the more important when the alternative is “an enormous piece of code, incomprehensible to humans”.

First and foremost, Haskell is a pure functional programming language. This is important for many reasons:

  • It allows us to reason equationally about code. Even the most basic properties, such as e + e = 2 * e, do not hold in most other programming languages!
  • Property-based testing, using randomized inputs, can be applied much more easily in a pure language, and is essential for finding bugs in our code in places we didn’t even think to look for them, avoiding bias in our thinking.
  • Purity makes types much more meaningful, allowing us to distinguish between a mathematical function of type Int -> Int and a program of type Int -> IO Int. These are very different objects with very different properties.
  • Even when state is unavoidable, purity forces us to explicitly track which state we depend on, limiting the context we need to know about to understand a piece of code.
  • Purity is an enabler for many other features (we will list some below). Most importantly, it enables laziness, a key ingredient for writing composable code.

Second, Haskell’s sophisticated type system can give us compiler-verified guarantees about our code, as well as powerful tools for abstraction.

  • Algebraic datatypes allow us to express that different data is available in different cases, and only in those cases. This is often key to making invalid states unrepresentable, and the compiler warns us when we forget to handle a case.
  • Polymorphism and the resulting parametricity give us many properties of functions for free.
  • Abstraction over type constructors (higher-kinded type classes) is critical for capturing many patterns, such as monads.
  • Type families allow us to state type-level relationships; GADTs establish links between types and terms.
  • Types can be used to localize state, packaging up a stateful function in a pure interface (for example, using the ST monad).
  • Types guide refactoring: when we change a type, the compiler points out every place in the code that needs to change accordingly. This makes even large changes tractable.
  • Type-driven generic programming allows us to write code once that can be applied to data of many different shapes. This remains important even when code becomes cheap to write: one generic program means one program to understand and verify.

Finally, the GHC runtime provides many features that are instrumental for writing code in a compositional manner. To name but a few:

  • Garbage collection means we don’t need to worry about ownership.
  • Green threads and an advanced I/O manager mean we can easily decompose code into concurrent tasks.
  • Software transactional memory (STM), in turn made possible by purity, means we don’t have to worry about things like global lock ordering.
  • Support for asynchronous exceptions makes it possible to provide general combinators such as timeout.

Moreover, Haskell is a well-established language, with a compiler that has been refined over the course of more than 30 years, a managed runtime with good support for profiling and debugging, and a mature ecosystem. This makes Haskell not just a good choice in theory, but also in practice.

Of course, the fact that Haskell gives us these tools does not mean that code written in Haskell automatically benefits from them. An AI-generated Haskell program might be just as incomprehensible as one written in C, providing hardly any more certainty that it is correct. It is therefore paramount that when we use LLMs to generate Haskell code, we guide the AI towards generating code that takes advantage of all of these tools. It is not sufficient to have a piece of code that “solves the problem”; we must be able to verify that it does indeed solve the problem, and for that, being able to understand the code is essential.

At Well-Typed we have the in-house expertise to deal with these issues. Most of us have more than a decade of experience writing Haskell code, some more than two. Many of us have done academic research on programming language design, verification, and testing (Andrea’s ICFP paper on Cubical Agda, Ben’s ISMM paper on the low-latency garbage collector, my own Haskell Symposium paper on property-based testing, to name just a few). We remain committed to our core values of providing high-quality software, and are applying the computer science principles we hold so dearly to produce AI-assisted code that we can be proud of. Indeed, we feel these principles, and the use of Haskell, have never been more important than now.

If you or your company would like our assistance with these problems (or anything else Haskell-related), feel free to reach out at info@well-typed.com; we’d be delighted to talk to you.