Idris 2, Agda, and other languages with totality checking let you write total functions that:
-
Start from finite structures (data), then iterate over them in a way that guarantees that each recursive step reduces the size of the structure. Because the structure gets smaller on every step, the computation is guaranteed to eventually consume it completely and terminate.
-
Start from infinite structures (codata). Functions that iterate over them are more constrained. For example, you can build an infinite list (using “list” in the mathematical sense, not necessarily the List data structure) of numbers called real_numbers. You can then perform operations on it, such as doubling every element or filtering it. However, somewhere along the line, your program must make the result finite. For example, you could double every element and then take a finite number of them.
Data can be computed eagerly (whether it is used or not) or on demand. With totality checking, both evaluation strategies have the same behavior, although their performance can still differ. Eager evaluation is what CPUs are generally designed for and is often easier to optimize, while on-demand evaluation may avoid computing data that is never used.
Without totality checking, a program may enter an infinite loop while computing some data. If that data is never used later, only eager evaluation would enter the loop. With totality checking, this difference disappears because all functions are guaranteed to terminate.
Codata must be computed on demand. Otherwise, an eager evaluation strategy could attempt to evaluate an infinite amount of data before producing any result, so the computation would never finish. With on-demand evaluation, only the portion of the structure that is actually needed is computed.
In the aforementioned languages, ordinary functions and type-level functions are the same. I understand that Dependent Haskell will preserve type families while making them more general. If so, the above should still apply.
Given the above, I wonder:
- Can totality checking be added to GHC after dependent types?
- If so, can it be added in such a way that existing code can be made total without requiring extensive rewrites? It is possible that large parts of a given program would need to be rewritten to make it compatible with totality checking.
- Could totality checking be implemented as a GHC plugin? It may be possible to implement totality checking as an additional set of rules checked by a GHC plugin.
Likewise, could other features needed to make Dependent Haskell sound also be implemented later in GHC or as extensions?
One obvious example is Girard’s paradox. There are several ways to deal with it. The following approach:
https://idris2.readthedocs.io/en/latest/tutorial/miscellany.html#cumulativity
results in code that is almost identical whether cumulativity is present or not. Idris 1 had cumulativity, while Idris 2 does not. As I understand it, existing code should mostly continue to work once the feature is reintroduced, with only extreme edge cases being affected.
Since this does not affect programmers directly and only adds additional rules for the compiler to check, perhaps it could also be implemented as a GHC plugin.
There would still be many other sources of unsoundness not mentioned that would also need to be addressed.
I hope people can give their 2 cents over the ideas i mentioned. This is a topic that interests me a lot but i am not an expert