@@ -222,59 +222,27 @@ rewriteStep cutLabels terminalLabels pat = do
222
222
NE. fromList $
223
223
map (\ (r, p) -> (ruleLabelOrLocT r, uniqueId r, p)) rxs
224
224
225
+ -- | Rewrite rule application transformer: may throw exceptions on non-applicable or trivial rule applications
226
+ type RewriteRuleAppT m a = ExceptT RewriteRuleAppException m a
227
+
228
+ data RewriteRuleAppException = RewriteRuleNotApplied | RewriteRuleTrivial deriving (Show , Eq )
229
+
230
+ runRewriteRuleAppT :: Monad m => RewriteRuleAppT m a -> m (RewriteRuleAppResult a )
231
+ runRewriteRuleAppT action =
232
+ runExceptT action >>= \ case
233
+ Left RewriteRuleNotApplied -> pure NotApplied
234
+ Left RewriteRuleTrivial -> pure Trivial
235
+ Right result -> pure (Applied result)
236
+
225
237
data RewriteRuleAppResult a
226
238
= Applied a
227
239
| NotApplied
228
240
| Trivial
229
241
deriving (Show , Eq , Functor )
230
242
231
- newtype RewriteRuleAppT m a = RewriteRuleAppT { runRewriteRuleAppT :: m (RewriteRuleAppResult a )}
232
- deriving (Functor )
233
-
234
- instance Monad m => Applicative (RewriteRuleAppT m ) where
235
- pure = RewriteRuleAppT . return . Applied
236
- {-# INLINE pure #-}
237
- mf <*> mx = RewriteRuleAppT $ do
238
- mb_f <- runRewriteRuleAppT mf
239
- case mb_f of
240
- NotApplied -> return NotApplied
241
- Trivial -> return Trivial
242
- Applied f -> do
243
- mb_x <- runRewriteRuleAppT mx
244
- case mb_x of
245
- NotApplied -> return NotApplied
246
- Trivial -> return Trivial
247
- Applied x -> return (Applied (f x))
248
- {-# INLINE (<*>) #-}
249
- m *> k = m >> k
250
- {-# INLINE (*>) #-}
251
-
252
- instance Monad m => Monad (RewriteRuleAppT m ) where
253
- return = pure
254
- {-# INLINE return #-}
255
- x >>= f = RewriteRuleAppT $ do
256
- v <- runRewriteRuleAppT x
257
- case v of
258
- Applied y -> runRewriteRuleAppT (f y)
259
- NotApplied -> return NotApplied
260
- Trivial -> return Trivial
261
- {-# INLINE (>>=) #-}
262
-
263
- instance MonadTrans RewriteRuleAppT where
264
- lift :: Monad m => m a -> RewriteRuleAppT m a
265
- lift = RewriteRuleAppT . fmap Applied
266
- {-# INLINE lift #-}
267
-
268
- instance Monad m => MonadFail (RewriteRuleAppT m ) where
269
- fail _ = RewriteRuleAppT (return NotApplied )
270
- {-# INLINE fail #-}
271
-
272
- instance MonadIO m => MonadIO (RewriteRuleAppT m ) where
273
- liftIO = lift . liftIO
274
- {-# INLINE liftIO #-}
275
-
276
- instance LoggerMIO m => LoggerMIO (RewriteRuleAppT m ) where
277
- withLogger l (RewriteRuleAppT m) = RewriteRuleAppT $ withLogger l m
243
+ returnTrivial , returnNotApplied :: Monad m => RewriteRuleAppT m a
244
+ returnTrivial = throwE RewriteRuleTrivial
245
+ returnNotApplied = throwE RewriteRuleNotApplied
278
246
279
247
{- | Tries to apply one rewrite rule:
280
248
@@ -310,7 +278,7 @@ applyRule pat@Pattern{ceilConditions} rule =
310
278
failRewrite $ InternalMatchError $ renderText $ pretty' @ mods err
311
279
MatchFailed reason -> do
312
280
withContext CtxFailure $ logPretty' @ mods reason
313
- fail " Rule matching failed "
281
+ returnNotApplied
314
282
MatchIndeterminate remainder -> do
315
283
withContext CtxIndeterminate $
316
284
logMessage $
@@ -417,12 +385,6 @@ applyRule pat@Pattern{ceilConditions} rule =
417
385
failRewrite :: RewriteFailed " Rewrite" -> RewriteRuleAppT (RewriteT io ) a
418
386
failRewrite = lift . (throw)
419
387
420
- notAppliedIfBottom :: RewriteRuleAppT (RewriteT io ) a
421
- notAppliedIfBottom = RewriteRuleAppT $ pure NotApplied
422
-
423
- trivialIfBottom :: RewriteRuleAppT (RewriteT io ) a
424
- trivialIfBottom = RewriteRuleAppT $ pure Trivial
425
-
426
388
checkConstraint ::
427
389
(Predicate -> a ) ->
428
390
RewriteRuleAppT (RewriteT io ) (Maybe a ) ->
@@ -457,7 +419,7 @@ applyRule pat@Pattern{ceilConditions} rule =
457
419
458
420
-- simplify the constraints (one by one in isolation). Stop if false, abort rewrite if indeterminate.
459
421
unclearRequires <-
460
- catMaybes <$> mapM (checkConstraint id notAppliedIfBottom pat. constraints) toCheck
422
+ catMaybes <$> mapM (checkConstraint id returnNotApplied pat. constraints) toCheck
461
423
462
424
-- unclear conditions may have been simplified and
463
425
-- could now be syntactically present in the path constraints, filter again
@@ -473,7 +435,7 @@ applyRule pat@Pattern{ceilConditions} rule =
473
435
SMT. IsInvalid -> do
474
436
-- requires is actually false given the prior
475
437
withContext CtxFailure $ logMessage (" Required clauses evaluated to #Bottom." :: Text )
476
- RewriteRuleAppT $ pure NotApplied
438
+ returnNotApplied
477
439
SMT. IsValid ->
478
440
pure [] -- can proceed
479
441
checkEnsures ::
@@ -483,7 +445,7 @@ applyRule pat@Pattern{ceilConditions} rule =
483
445
let ruleEnsures =
484
446
concatMap (splitBoolPredicates . coerce . substituteInTerm matchingSubst . coerce) rule. ensures
485
447
newConstraints <-
486
- catMaybes <$> mapM (checkConstraint id trivialIfBottom pat. constraints) ruleEnsures
448
+ catMaybes <$> mapM (checkConstraint id returnTrivial pat. constraints) ruleEnsures
487
449
488
450
-- check all new constraints together with the known side constraints
489
451
solver <- lift $ RewriteT $ (. smtSolver) <$> ask
@@ -492,10 +454,10 @@ applyRule pat@Pattern{ceilConditions} rule =
492
454
(lift $ SMT. checkPredicates solver pat. constraints mempty (Set. fromList newConstraints)) >>= \ case
493
455
SMT. IsInvalid -> do
494
456
withContext CtxSuccess $ logMessage (" New constraints evaluated to #Bottom." :: Text )
495
- RewriteRuleAppT $ pure Trivial
457
+ returnTrivial
496
458
SMT. IsUnknown SMT. InconsistentGroundTruth -> do
497
459
withContext CtxSuccess $ logMessage (" Ground truth is #Bottom." :: Text )
498
- RewriteRuleAppT $ pure Trivial
460
+ returnTrivial
499
461
SMT. IsUnknown SMT. ImplicationIndeterminate -> do
500
462
-- the new constraint is satisfiable, continue
501
463
pure ()
0 commit comments