Totality-checking for Haskell?

Is anyone working on (or interested in) opt-in totality checking for Haskell? I’d love to be able to mark a function or module and have GHC guarantee it never hits bottom: no partial matches, no error/undefined, and no non-termination (perhaps with some notion of productivity for lazy/infinite data?).

It feels like a natural extension of the purity philosophy, treating partiality as an effect to be tracked rather than something always implicitly allowed?

I’ve enjoyed Idris’ totality checking, and I know about Liquid Haskell’s termination checking and Agda’s total-by-default, but I’d love to be able to use it in something more practical for day-to-day programming like Haskell.

I’d love to hear folks’ thoughts on this. Does an extension or plugin dedicated to this sound at all feasible?

Why is liquid haskell not practical? (Note: I’m not making a point, I’m genuinely asking)

Ahh it might well be! I haven’t yet tried reaching for it, as my understanding is that it’s more focused on refinement types and formal verification, and it requires an external SMT solver and supports only one GHC version at a time. I guess I had in mind something lighter and focused just on totality checking? I will take a closer look either way, at the very least to get a better understanding of how intertwined their termination checking is with their approach to refinement types, thanks.

In my understanding, totality checking (and inference) in GHC will unlock a lot of optimization possibilities (especially in conjunction with linear types)

Of course, on obstacle is that we would want totality polymorphism, which probably has gotchas.

My first thought about this was that there’s a reason termination-checking as found in theorem-proving languages hasn’t taken off, which is that there’s a simple subset of it that’s trivial to check by visual inspection (structural recursion), and as soon as you work outside of it, it imposes an unreasonable burden on the programmer to formalize the proof.

I’d be curious if a more “descriptive” approach would work, where we look at recursive Haskell functions that exist in the wild, and we try to come up with ad hoc (but sound) patterns to classify them as terminating and see how much we can cover this way.

Well, the beauty of totality checking is that we can start with very simple conditions (full coverage with no recursion, for example) and gradually get more sophisticated.

There’s also a simple form of total programming in Haskell you can already do using existing tools:

  • Partial matches are already handled by compiler warnings: -Wall.
  • Forbid error/undefined with a linter (hlint).
  • And for recursion, only ever use recursion-schemes.

All of this you can do today.

The only missing bit is an automated termination checker to prevent accidents or cheating, by rejecting recursive functions (other than recursion schemes) as well as badly behaved recursive types (with ill-defined recursion schemes).

Including mutually recursive functions

To give a simple example, mutually recursive types that directly recurse into each other correspond to the types Fix (Compose f g) and g (Fix (Compose f g)) (or Fix (Compose g f)), and you get the following recursion scheme:

cata2 :: (f a -> b) -> (g b -> a) -> Fix (Compose f g) -> b

Fancier kinds of mutual recursion can also be expressed using fancier kinds of functors.