Skip to content

Commit a911cea

Browse files
authored
Kore.Reachability: decouple result type of applyClaims and applyAxioms (#2200)
* Kore.Reachability: use ApplyResult * ApplyResult: add ReachedBottom case; clean-up * Clean-up * rewrite rule application strategy: remove case handling for when result is bottom * ApplyResult: documentation * AppliedRule: short documentation * applyResultToClaimState: factor out case handling in transitionRule * deriveResults: handle bottom case
1 parent bf8269a commit a911cea

File tree

4 files changed

+49
-57
lines changed

4 files changed

+49
-57
lines changed

kore/src/Kore/Reachability/AllPathClaim.hs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ import Kore.Internal.TermLike
3939
)
4040
import qualified Kore.Internal.TermLike as TermLike
4141
import Kore.Reachability.Claim
42-
import Kore.Reachability.ClaimState
43-
( ClaimState (..)
44-
, retractRewritable
45-
)
4642
import Kore.Rewriting.RewritingVariable
4743
( RewritingVariableName
4844
, mkRuleVariable
@@ -141,19 +137,19 @@ instance Claim AllPathClaim where
141137
applyClaims claims = deriveSeqClaim _Unwrapped AllPathClaim claims
142138

143139
applyAxioms axiomss = \claim ->
144-
foldM applyAxioms1 (Remaining claim) axiomss
140+
foldM applyAxioms1 (ApplyRemainder claim) axiomss
145141
where
146-
applyAxioms1 claimState axioms
147-
| Just claim <- retractRewritable claimState =
142+
applyAxioms1 applied axioms
143+
| Just claim <- retractApplyRemainder applied =
148144
deriveParAxiomAllPath axioms claim
149145
>>= simplifyRemainder
150146
| otherwise =
151-
pure claimState
147+
pure applied
152148

153-
simplifyRemainder claimState =
154-
case claimState of
155-
Remaining claim -> Remaining <$> simplify claim
156-
_ -> return claimState
149+
simplifyRemainder applied =
150+
case applied of
151+
ApplyRemainder claim -> ApplyRemainder <$> simplify claim
152+
_ -> return applied
157153

158154
instance From (Rule AllPathClaim) Attribute.PriorityAttributes where
159155
from = from @(RewriteRule _) . unRuleAllPath
@@ -195,7 +191,7 @@ deriveParAxiomAllPath
195191
=> [Rule AllPathClaim]
196192
-> AllPathClaim
197193
-> TransitionT (AppliedRule AllPathClaim) simplifier
198-
(ClaimState AllPathClaim)
194+
(ApplyResult AllPathClaim)
199195
deriveParAxiomAllPath rules =
200196
derivePar' _Unwrapped AllPathRewriteRule rewrites
201197
where

kore/src/Kore/Reachability/Claim.hs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ License : NCSA
44
-}
55
module Kore.Reachability.Claim
66
( Claim (..)
7+
, ApplyResult (..)
78
, AppliedRule (..)
9+
, retractApplyRemainder
810
, strategy
911
, TransitionRule
1012
, Prim
@@ -110,7 +112,6 @@ import Kore.Log.WarnStuckClaimState
110112
import Kore.Reachability.ClaimState hiding
111113
( claimState
112114
)
113-
import qualified Kore.Reachability.ClaimState as ClaimState
114115
import Kore.Reachability.Prim
115116
import Kore.Rewriting.RewritingVariable
116117
import Kore.Step.AxiomPattern
@@ -182,30 +183,32 @@ class Claim claim where
182183
=> claim
183184
-> Strategy.TransitionT (AppliedRule claim) m claim
184185

185-
{- TODO (thomas.tuegel): applyClaims and applyAxioms should return:
186-
187-
> data ApplyResult claim
188-
> = ApplyRewritten !claim
189-
> | ApplyRemainder !claim
190-
191-
Rationale: ClaimState is part of the implementation of transitionRule, that
192-
is: these functions have hidden knowledge of how transitionRule works
193-
because they tell it what to do next. Instead, they should report their
194-
result and leave the decision up to transitionRule.
195-
196-
-}
197186
applyClaims
198187
:: MonadSimplify m
199188
=> [claim]
200189
-> claim
201-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
190+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
202191

203192
applyAxioms
204193
:: MonadSimplify m
205194
=> [[Rule claim]]
206195
-> claim
207-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
196+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
197+
198+
{- | 'ApplyResult' is the result of a rewriting step, like 'applyClaims' or 'applyAxioms'.
208199
200+
Both 'ApplyRewritten' and 'ApplyRemainder' wrap a newly formed claim.
201+
Its left hand side is constructed from either the application of rewrite rules,
202+
or, respectively, from the remainder resulting after this procedure.
203+
-}
204+
data ApplyResult claim
205+
= ApplyRewritten !claim
206+
| ApplyRemainder !claim
207+
deriving (Show, Eq)
208+
deriving (Functor)
209+
210+
{- | 'AppliedRule' represents the rule applied during a rewriting step.
211+
-}
209212
data AppliedRule claim
210213
= AppliedAxiom (Rule claim)
211214
| AppliedClaim claim
@@ -271,7 +274,7 @@ deriveSeqClaim
271274
-> (ClaimPattern -> claim)
272275
-> [claim]
273276
-> claim
274-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
277+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
275278
deriveSeqClaim lensClaimPattern mkClaim claims claim =
276279
getCompose
277280
$ Lens.forOf lensClaimPattern claim
@@ -336,32 +339,30 @@ transitionRule claims axiomGroups = transitionRuleWorker
336339
| otherwise -> pure (Claimed a)
337340
| otherwise = pure claimState
338341

339-
-- TODO (virgil): Wrap the results in GoalRemainder/GoalRewritten here.
340-
--
341-
-- thomas.tuegel: "Here" is in ApplyClaims and ApplyAxioms.
342-
--
343-
-- Note that in most transitions it is obvious what is being transformed
344-
-- into what, e.g. that a `ResetGoal` transition transforms
345-
-- `GoalRewritten` into `Goal`. However, here we're taking a `Goal`
346-
-- and transforming it into `GoalRewritten` and `GoalRemainder` in an
347-
-- opaque way. I think that there's no good reason for wrapping the
348-
-- results in `derivePar` as opposed to here.
349-
350342
transitionRuleWorker ApplyClaims (Claimed claim) =
351343
applyClaims claims claim
344+
>>= return . applyResultToClaimState
352345
transitionRuleWorker ApplyClaims claimState = pure claimState
353346

354347
transitionRuleWorker ApplyAxioms claimState
355348
| Just claim <- retractRewritable claimState =
356349
applyAxioms axiomGroups claim
350+
>>= return . applyResultToClaimState
357351
| otherwise = pure claimState
358352

353+
applyResultToClaimState (ApplyRewritten a) = Rewritten a
354+
applyResultToClaimState (ApplyRemainder a) = Remaining a
355+
359356
retractSimplifiable :: ClaimState a -> Maybe a
360357
retractSimplifiable (Claimed a) = Just a
361358
retractSimplifiable (Rewritten a) = Just a
362359
retractSimplifiable (Remaining a) = Just a
363360
retractSimplifiable _ = Nothing
364361

362+
retractApplyRemainder :: ApplyResult a -> Maybe a
363+
retractApplyRemainder (ApplyRemainder a) = Just a
364+
retractApplyRemainder _ = Nothing
365+
365366
isRemainder :: ClaimState a -> Bool
366367
isRemainder (Remaining _) = True
367368
isRemainder _ = False
@@ -650,7 +651,7 @@ derivePar'
650651
-> (RewriteRule RewritingVariableName -> Rule claim)
651652
-> [RewriteRule RewritingVariableName]
652653
-> claim
653-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
654+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
654655
derivePar' lensRulePattern mkRule =
655656
deriveWith lensRulePattern mkRule
656657
$ Step.applyRewriteRulesParallel Unification.unificationProcedure
@@ -669,7 +670,7 @@ deriveWith
669670
-> Deriver m
670671
-> [RewriteRule RewritingVariableName]
671672
-> claim
672-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
673+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
673674
deriveWith lensClaimPattern mkRule takeStep rewrites claim =
674675
getCompose
675676
$ Lens.forOf lensClaimPattern claim
@@ -694,7 +695,7 @@ deriveSeq'
694695
-> (RewriteRule RewritingVariableName -> Rule claim)
695696
-> [RewriteRule RewritingVariableName]
696697
-> claim
697-
-> Strategy.TransitionT (AppliedRule claim) m (ClaimState claim)
698+
-> Strategy.TransitionT (AppliedRule claim) m (ApplyResult claim)
698699
deriveSeq' lensRulePattern mkRule =
699700
deriveWith lensRulePattern mkRule . flip
700701
$ Step.applyRewriteRulesSequence Unification.unificationProcedure
@@ -704,7 +705,7 @@ deriveResults
704705
=> (Step.UnifiedRule representation -> AppliedRule claim)
705706
-> Step.Results representation
706707
-> Strategy.TransitionT (AppliedRule claim) simplifier
707-
(ClaimState.ClaimState (Pattern RewritingVariableName))
708+
(ApplyResult (Pattern RewritingVariableName))
708709
-- TODO (thomas.tuegel): Remove claim argument.
709710
deriveResults fromAppliedRule Results { results, remainders } =
710711
addResults <|> addRemainders
@@ -715,13 +716,10 @@ deriveResults fromAppliedRule Results { results, remainders } =
715716
addResult Result { appliedRule, result } = do
716717
addRule appliedRule
717718
case Foldable.toList result of
718-
[] ->
719-
-- If the rule returns \bottom, the claim is proven on the
720-
-- current branch.
721-
pure Proven
719+
[] -> addRewritten Pattern.bottom
722720
configs -> Foldable.asum (addRewritten <$> configs)
723721

724-
addRewritten = pure . Rewritten
725-
addRemainder = pure . Remaining
722+
addRewritten = pure . ApplyRewritten
723+
addRemainder = pure . ApplyRemainder
726724

727725
addRule = Transition.addRule . fromAppliedRule

kore/src/Kore/Reachability/OnePathClaim.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ import Kore.Internal.TermLike
3636
)
3737
import qualified Kore.Internal.TermLike as TermLike
3838
import Kore.Reachability.Claim
39-
import Kore.Reachability.ClaimState
40-
( ClaimState
41-
)
4239
import Kore.Rewriting.RewritingVariable
4340
( RewritingVariableName
4441
, mkRuleVariable
@@ -214,7 +211,7 @@ deriveSeqAxiomOnePath
214211
=> [Rule OnePathClaim]
215212
-> OnePathClaim
216213
-> TransitionT (AppliedRule OnePathClaim) simplifier
217-
(ClaimState OnePathClaim)
214+
(ApplyResult OnePathClaim)
218215
deriveSeqAxiomOnePath rules =
219216
deriveSeq' _Unwrapped OnePathRewriteRule rewrites
220217
where

kore/test/Test/Kore/Reachability/MockAllPath.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Kore.Debug
3232
import qualified Kore.Internal.MultiOr as MultiOr
3333
import Kore.Reachability.Claim
3434
( AppliedRule (..)
35+
, ApplyResult (..)
3536
, Claim (..)
3637
)
3738
import qualified Kore.Reachability.Claim as Claim
@@ -381,16 +382,16 @@ derivePar
381382
:: (MockClaim -> MockAppliedRule)
382383
-> [MockRule]
383384
-> MockClaim
384-
-> Transition.TransitionT MockAppliedRule m MockClaimState
385+
-> Transition.TransitionT MockAppliedRule m (ApplyResult MockClaim)
385386
derivePar mkAppliedRule rules (MockClaim (src, dst)) =
386387
goals <|> goalRemainder
387388
where
388389
goal (Rule rule@(_, to)) = do
389390
Transition.addRule (mkAppliedRule (MockClaim rule))
390-
(pure . ClaimState.Rewritten . MockClaim) (to, dst)
391+
(pure . ApplyRewritten . MockClaim) (to, dst)
391392
goalRemainder = do
392393
let r = Foldable.foldl' difference src (fst . unRule <$> applied)
393-
(pure . ClaimState.Remaining . MockClaim) (r, dst)
394+
(pure . ApplyRemainder . MockClaim) (r, dst)
394395
applyRule rule@(Rule (fromMockClaim, _))
395396
| fromMockClaim `matches` src = Just rule
396397
| otherwise = Nothing

0 commit comments

Comments
 (0)