为何在Idris中不始终使用Inf而非Lazy?
Great question—you’re right to notice the tight relationship between Lazy and Inf in Agda, since they share the same underlying implementation but differ only in how totality checking is handled. Let’s walk through the key production scenarios where you’d need to use Lazy instead of Inf.
Key Scenarios to Use Lazy in Production
When strict total termination is non-negotiable
Lazy’s totality check ignores the lazy annotation entirely, performing the same deep termination analysis as non-lazy code. This is critical for systems where you must guarantee that a function will terminate for all valid inputs—think safety-critical software, formal verification pipelines, or code where termination is a prerequisite for correctness. For example, a function calculating a finite mathematical result (like factorial for natural numbers) needs this strict check to ensure it never enters an infinite loop.Inf’s productivity check only ensures that infinite structures can be generated incrementally; it won’t catch non-termination in functions meant to produce finite results.Working with non-infinite data and computations
If your code deals primarily with finite data structures or pure computations that aren’t generating infinite streams/trees,Lazyis the better choice. The deep totality check forLazywill validate that your functions terminate for all finite inputs, whereasInf’s checker doesn’t prioritize this. UsingLazyhere helps catch bugs early—like accidental infinite recursion in a function meant to process a finite list—before they make it to production.Maintaining compatibility with legacy codebases
If you’re working on an existing Agda project that relies onLazy’s totality guarantees, switching toInfcould break established correctness properties. Legacy code may have been written with the assumption thatLazy’s strict termination checks are in place, so replacing it withInfmight introduce uncaught non-termination issues. Sticking withLazypreserves the existing safety guarantees and avoids regression.Integrating with tools that depend on strict totality proofs
Some Agda tooling, automated theorem provers, or code generators are built to work with the results ofLazy’s deep totality checks. These tools rely on the guarantee that functions terminate to perform their work—whether that’s verifying a theorem, generating executable code, or validating system invariants.Inf’s productivity-focused checks won’t provide the same level of proof, so usingLazyis necessary to keep these integrations working correctly.
Quick Recap
Inf shines when you’re working with infinite structures and want behavior closer to Haskell’s laziness, prioritizing productivity over strict termination. But Lazy is indispensable when you need rock-solid guarantees that your code will terminate for all inputs, especially in finite computation, safety-critical systems, or when maintaining compatibility with existing code and tooling.
内容的提问来源于stack exchange,提问作者luochen1990

