Skip to content

Commit 034078d

Browse files
committed
Carry recursion depth in TooManyIterations and check when handling
1 parent ecfa5d2 commit 034078d

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ catch_ (EquationT op) hdlr = EquationT $ do
102102

103103
data EquationFailure
104104
= IndexIsNone Term
105-
| TooManyIterations Int Term Term
105+
| TooManyIterations Int Int Term Term
106106
| EquationLoop [Term]
107107
| TooManyRecursions [Term]
108108
| SideConditionFalse Predicate
@@ -114,9 +114,10 @@ instance Pretty (PrettyWithModifiers mods EquationFailure) where
114114
pretty (PrettyWithModifiers f) = case f of
115115
IndexIsNone t ->
116116
"Index 'None' for term " <> pretty' @mods t
117-
TooManyIterations count start end ->
117+
TooManyIterations depth count start end ->
118118
vsep
119119
[ "Unable to finish evaluation in " <> pretty count <> " iterations"
120+
, "at recursion depth " <> pretty depth
120121
, "Started with: " <> pretty' @mods start
121122
, "Stopped at: " <> pretty' @mods end
122123
]
@@ -334,6 +335,7 @@ iterateEquations direction preference startTerm = do
334335
config <- getConfig
335336
currentCount <- countSteps
336337
when (coerce currentCount > config.maxIterations) $ do
338+
currentRecursionDepth <- getRecusionDepth
337339
-- FIXME if this exception is caught in evaluatePattern',
338340
-- then CtxAbort is a wrong context for it.
339341
-- We should emit this log entry somewhere else.
@@ -347,7 +349,7 @@ iterateEquations direction preference startTerm = do
347349
logMessage . renderOneLineText $
348350
"Final term:" <+> pretty' @mods currentTerm
349351
throw $
350-
TooManyIterations currentCount startTerm currentTerm
352+
TooManyIterations currentRecursionDepth currentCount startTerm currentTerm
351353
pushTerm currentTerm
352354
-- simplify the term using the LLVM backend first
353355
llvmResult <- llvmSimplify currentTerm
@@ -446,13 +448,14 @@ evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
446448
evaluatedConstraints <- predicates <$> getState
447449
pure Pattern{constraints = evaluatedConstraints, term = newTerm, ceilConditions}
448450
where
449-
-- when TooManyIterations exception occurs while evaluating the top-level term (not a side-condition),
450-
-- it is safe to keep the partial result and ignore the exception. Otherwise we
451-
-- would be throwing away useful work.
451+
-- when TooManyIterations exception occurred while evaluating the top-level term,
452+
-- i.e. not in a recursive evaluation of a side-condition,
453+
-- it is safe to keep the partial result and ignore the exception.
454+
-- Otherwise we would be throwing away useful work.
452455
keepTopLevelResults :: LoggerMIO io => EquationFailure -> EquationT io Term
453456
keepTopLevelResults = \case
454-
err@(TooManyIterations _ _ partialResult) ->
455-
getRecusionDepth >>= \case
457+
err@(TooManyIterations recursionDepth _ _ partialResult) ->
458+
case recursionDepth of
456459
0 -> pure partialResult
457460
_ -> throw err
458461
err -> throw err

0 commit comments

Comments
 (0)