[ fix ] performance issue in totality checking#3775
Open
dunhamsteve wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses an exponential slowdown issue in totality checking and fixes #3696.
Issue
The following code takes appears to freeze during totality checking of
foo:Root cause
Here the term
bar () () () () ()appears as an argument on the RHS. Idris tries to determine if it is smaller than an argument on the left. The LHS has a largeNat. Size compare falls through to the case where we check a constructor application in the pattern (t) against the application ofbar. This recurses to checking the function application against each of the arguments of that constructor:Idris2/src/Core/Termination/CallGraph.idr
Lines 218 to 223 in 6a54860
And if
sizeCompareConfails, we hit fallback code that callssizeCompareApp, which peels off an argument of the function and runs the entire process again, comparingbar () () () ()to100.Idris2/src/Core/Termination/CallGraph.idr
Lines 241 to 242 in 6a54860
So we end up running:
sizeCompare (bar () () () () ()) (S (S (S ...))sizeCompare (bar () () () () ()) (S (S ...))(peel off anSinsizeCompareCon)sizeCompare (bar () () () () ()) (S (S ...))sizeCompare (bar () () () ()) (S (S ...))sizeCompare (bar () () () ()) (S (S (S ...)))(peel off the()insizeCompareApp)sizeCompare (bar () () () ()) (S (S ...))(removeSinsizeCompareCon)sizeCompare (bar () () ()) (S (S (S ...)))(peel off the()insizeCompareApp)Which is exponential and redundant.
Fix
The Foetus paper has a rule like "if y ρ x then (y a) ρ x" (for ρ in <,=). So we do need to check prefixes of the application, but we can do this at the leaves. Instead of rerunning everything in
sizeCompareApp, check whether the LHS is equal or a prefix of the RHS as a function application. Deeper recursion is handled by the earlier branch, going under the constructor.Self-check
implementation, I have updated
CHANGELOG_NEXT.md