Skip to content

Commit d63d128

Browse files
committed
Extract strategy from class Goal
The strategy belongs to and is common to all reachability proofs.
1 parent 215a34c commit d63d128

File tree

4 files changed

+28
-132
lines changed

4 files changed

+28
-132
lines changed

kore/src/Kore/Strategies/Goal.hs

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ License : NCSA
44
-}
55
module Kore.Strategies.Goal
66
( Goal (..)
7+
, strategy
78
, TransitionRule
89
, Prim
910
, FromRulePattern (..)
@@ -13,10 +14,8 @@ module Kore.Strategies.Goal
1314
, extractClaims
1415
, unprovenNodes
1516
, proven
16-
, onePathFirstStep
17-
, onePathFollowupStep
18-
, allPathFirstStep
19-
, allPathFollowupStep
17+
, reachabilityFirstStep
18+
, reachabilityNextStep
2019
, getConfiguration
2120
, getDestination
2221
, transitionRule
@@ -196,12 +195,8 @@ class Goal goal where
196195
=> goal -> Rule goal
197196
goalToRule = coerce
198197

199-
strategy
200-
:: goal
201-
-> [goal]
202-
-> [Rule goal]
203-
-> Stream (Strategy Prim)
204-
198+
-- TODO (thomas.tuegel): isTriviallyValid should be part of
199+
-- checkImplication. why is this so slow?
205200
isTriviallyValid :: goal -> Bool
206201

207202
checkImplication
@@ -309,8 +304,6 @@ instance Goal OnePathRule where
309304

310305
isTriviallyValid = isTriviallyValid' _Unwrapped
311306

312-
strategy _ _ _ = onePathFirstStep :> Stream.iterate id onePathFollowupStep
313-
314307
deriveSeqOnePath
315308
:: MonadSimplify simplifier
316309
=> [Rule OnePathRule]
@@ -364,8 +357,6 @@ instance Goal AllPathRule where
364357
| all isTriviallyValid proofState = pure Proven
365358
| otherwise = pure proofState
366359

367-
strategy _ _ _ = allPathFirstStep :> Stream.iterate id allPathFollowupStep
368-
369360
deriveParAllPath
370361
:: MonadSimplify simplifier
371362
=> [Rule AllPathRule]
@@ -433,24 +424,6 @@ instance Goal ReachabilityRule where
433424
& fmap (fmap OnePath)
434425
& onePathTransition
435426

436-
strategy
437-
:: ReachabilityRule
438-
-> [ReachabilityRule]
439-
-> [Rule ReachabilityRule]
440-
-> Stream (Strategy Prim)
441-
strategy goal claims axioms =
442-
case goal of
443-
OnePath rule ->
444-
strategy
445-
rule
446-
(mapMaybe maybeOnePath claims)
447-
(fmap ruleReachabilityToRuleOnePath axioms)
448-
AllPath rule ->
449-
strategy
450-
rule
451-
(mapMaybe maybeAllPath claims)
452-
(fmap ruleReachabilityToRuleAllPath axioms)
453-
454427
instance ClaimExtractor ReachabilityRule where
455428
extractClaim (attrs, sentence) =
456429
case fromSentenceAxiom (attrs, Syntax.getSentenceClaim sentence) of
@@ -478,19 +451,6 @@ maybeAllPath :: ReachabilityRule -> Maybe AllPathRule
478451
maybeAllPath (AllPath rule) = Just rule
479452
maybeAllPath _ = Nothing
480453

481-
-- The functions below are easier to read coercions between
482-
-- the newtypes over 'RewriteRule VariableName' defined in the
483-
-- instances of 'Goal' as 'Rule's.
484-
ruleReachabilityToRuleAllPath
485-
:: Rule ReachabilityRule
486-
-> Rule AllPathRule
487-
ruleReachabilityToRuleAllPath = coerce
488-
489-
ruleReachabilityToRuleOnePath
490-
:: Rule ReachabilityRule
491-
-> Rule OnePathRule
492-
ruleReachabilityToRuleOnePath = coerce
493-
494454
ruleAllPathToRuleReachability
495455
:: Rule AllPathRule
496456
-> Rule ReachabilityRule
@@ -592,8 +552,8 @@ transitionRule claims axiomGroups = transitionRuleWorker
592552

593553
transitionRuleWorker _ state = return state
594554

595-
onePathFirstStep :: Strategy Prim
596-
onePathFirstStep =
555+
reachabilityFirstStep :: Strategy Prim
556+
reachabilityFirstStep =
597557
(Strategy.sequence . map Strategy.apply)
598558
[ CheckProven
599559
, CheckGoalStuck
@@ -607,8 +567,8 @@ onePathFirstStep =
607567
, TriviallyValid
608568
]
609569

610-
onePathFollowupStep :: Strategy Prim
611-
onePathFollowupStep =
570+
reachabilityNextStep :: Strategy Prim
571+
reachabilityNextStep =
612572
(Strategy.sequence . map Strategy.apply)
613573
[ CheckProven
614574
, CheckGoalStuck
@@ -623,36 +583,9 @@ onePathFollowupStep =
623583
, TriviallyValid
624584
]
625585

626-
allPathFirstStep :: Strategy Prim
627-
allPathFirstStep =
628-
(Strategy.sequence . map Strategy.apply)
629-
[ CheckProven
630-
, CheckGoalStuck
631-
, CheckGoalRemainder
632-
, Simplify
633-
, TriviallyValid
634-
, CheckImplication
635-
, ApplyAxioms
636-
, ResetGoal
637-
, Simplify
638-
, TriviallyValid
639-
]
640-
641-
allPathFollowupStep :: Strategy Prim
642-
allPathFollowupStep =
643-
(Strategy.sequence . map Strategy.apply)
644-
[ CheckProven
645-
, CheckGoalStuck
646-
, CheckGoalRemainder
647-
, Simplify
648-
, TriviallyValid
649-
, CheckImplication
650-
, ApplyClaims
651-
, ApplyAxioms
652-
, ResetGoal
653-
, Simplify
654-
, TriviallyValid
655-
]
586+
strategy :: Stream (Strategy Prim)
587+
strategy =
588+
reachabilityFirstStep :> Stream.iterate id reachabilityNextStep
656589

657590
{- | The result of checking the direct implication of a proof goal.
658591

kore/src/Kore/Strategies/Verification.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import qualified Data.Graph.Inductive.Graph as Graph
4545
import Data.List.Extra
4646
( groupSortOn
4747
)
48-
import qualified Data.Stream.Infinite as Stream
4948
import Data.Text
5049
( Text
5150
)
@@ -248,7 +247,7 @@ verifyClaim
248247
let
249248
startPattern = ProofState.Goal $ getConfiguration goal
250249
limitedStrategy =
251-
strategy goal claims axioms
250+
strategy
252251
& Foldable.toList
253252
& Limit.takeWithin depthLimit
254253
Strategy.leavesM
@@ -344,10 +343,10 @@ verifyClaimStep target claims axioms executionGraph node =
344343
| otherwise = followupStep
345344

346345
firstStep :: Strategy Prim
347-
firstStep = strategy target claims axioms Stream.!! 0
346+
firstStep = reachabilityFirstStep
348347

349348
followupStep :: Strategy Prim
350-
followupStep = strategy target claims axioms Stream.!! 1
349+
followupStep = reachabilityNextStep
351350

352351
ExecutionGraph { root } = executionGraph
353352

kore/test/Test/Kore/Strategies/AllPath/AllPath.hs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import Data.Sequence
2727
( Seq
2828
)
2929
import qualified Data.Sequence as Seq
30-
import Data.Stream.Infinite
31-
( Stream (..)
32-
)
33-
import qualified Data.Stream.Infinite as Stream
3430
import qualified Generics.SOP as SOP
3531
import qualified GHC.Generics as GHC
3632

@@ -152,7 +148,7 @@ test_transitionRule_CheckImplication :: [TestTree]
152148
test_transitionRule_CheckImplication =
153149
[ unmodified ProofState.Proven
154150
, unmodified (ProofState.GoalRemainder (A, B))
155-
, ProofState.Goal (B, B) `becomes` (ProofState.Goal (Bot, B), mempty)
151+
, ProofState.Goal (B, B) `becomes` (ProofState.Proven, mempty)
156152
]
157153
where
158154
run = runTransitionRule [] [] ProofState.CheckImplication
@@ -180,12 +176,12 @@ test_transitionRule_ApplyClaims =
180176
, unmodified (ProofState.GoalRewritten (A, B))
181177
, [Rule (A, C)]
182178
`derives`
183-
[ (ProofState.Goal (C, C), Seq.singleton $ Rule (A, C))
179+
[ (ProofState.GoalRewritten (C, C), Seq.singleton $ Rule (A, C))
184180
, (ProofState.GoalRemainder (Bot, C), mempty)
185181
]
186182
, fmap Rule [(A, B), (B, C)]
187183
`derives`
188-
[ (ProofState.Goal (B , C), Seq.singleton $ Rule (A, B))
184+
[ (ProofState.GoalRewritten (B , C), Seq.singleton $ Rule (A, B))
189185
, (ProofState.GoalRemainder (Bot, C), mempty)
190186
]
191187
]
@@ -208,12 +204,12 @@ test_transitionRule_ApplyAxioms =
208204
, unmodified (ProofState.GoalRewritten (A, B))
209205
, [Rule (A, C)]
210206
`derives`
211-
[ (ProofState.Goal (C, C), Seq.singleton $ Rule (A, C))
207+
[ (ProofState.GoalRewritten (C, C), Seq.singleton $ Rule (A, C))
212208
, (ProofState.GoalRemainder (Bot, C), mempty)
213209
]
214210
, fmap Rule [(A, B), (B, C)]
215211
`derives`
216-
[ (ProofState.Goal (B , C), Seq.singleton $ Rule (A, B))
212+
[ (ProofState.GoalRewritten (B , C), Seq.singleton $ Rule (A, B))
217213
, (ProofState.GoalRemainder (Bot, C), mempty)
218214
]
219215
]
@@ -244,6 +240,7 @@ test_runStrategy =
244240

245241
, [Rule (A, A)] `proves` (A, B)
246242
, [Rule (A, A)] `proves` (A, C)
243+
, [Rule (A, A)] `disproves` (A, C) $ []
247244

248245
, fmap Rule [(A, B), (A, C)] `proves` (A, BorC)
249246
, fmap Rule [(A, B), (A, C)] `disproves` (A, B ) $ [(C, B)]
@@ -261,7 +258,7 @@ test_runStrategy =
261258
$ Strategy.runStrategy
262259
Unlimited
263260
(Goal.transitionRule [unRule goal] [axioms])
264-
(Foldable.toList $ Goal.strategy (unRule goal) [unRule goal] axioms)
261+
(Foldable.toList Goal.strategy)
265262
(ProofState.Goal . unRule $ goal)
266263
disproves
267264
:: HasCallStack
@@ -347,41 +344,11 @@ newtype instance Goal.Rule Goal =
347344
deriving (Eq, GHC.Generic, Show)
348345

349346
instance Goal.Goal Goal where
350-
strategy _ _ _ =
351-
firstStep :> Stream.iterate id nextStep
347+
checkImplication (src, dst)
348+
| src' == Bot = return Goal.Implied
349+
| otherwise = return $ Goal.NotImplied (src', dst)
352350
where
353-
firstStep =
354-
(Strategy.sequence . map Strategy.apply)
355-
[ ProofState.CheckProven
356-
, ProofState.CheckGoalRemainder
357-
, ProofState.CheckImplication
358-
, ProofState.TriviallyValid
359-
, ProofState.ApplyAxioms
360-
, ProofState.Simplify
361-
, ProofState.TriviallyValid
362-
, ProofState.ResetGoal
363-
, ProofState.TriviallyValid
364-
]
365-
nextStep =
366-
(Strategy.sequence . map Strategy.apply)
367-
[ ProofState.CheckProven
368-
, ProofState.CheckGoalRemainder
369-
, ProofState.CheckImplication
370-
, ProofState.TriviallyValid
371-
, ProofState.ApplyClaims
372-
, ProofState.CheckImplication
373-
, ProofState.Simplify
374-
, ProofState.TriviallyValid
375-
, ProofState.ApplyAxioms
376-
, ProofState.CheckImplication
377-
, ProofState.Simplify
378-
, ProofState.TriviallyValid
379-
, ProofState.ResetGoal
380-
, ProofState.TriviallyValid
381-
]
382-
383-
checkImplication (src, dst) =
384-
return . Goal.NotImplied $ (difference src dst, dst)
351+
src' = difference src dst
385352

386353
-- | The goal is trivially valid when the members are equal.
387354
isTriviallyValid :: Goal -> Bool
@@ -402,7 +369,7 @@ derivePar rules (src, dst) =
402369
where
403370
goal rule@(Rule (_, to)) = do
404371
Transition.addRule rule
405-
(pure . ProofState.Goal) (to, dst)
372+
(pure . ProofState.GoalRewritten) (to, dst)
406373
goalRemainder = do
407374
let r = Foldable.foldl' difference src (fst . unRule <$> applied)
408375
(pure . ProofState.GoalRemainder) (r, dst)

kore/test/Test/Kore/Strategies/OnePath/Step.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,7 @@ runOnePathSteps
892892
coinductiveRewrites
893893
(groupSortOn Attribute.Axiom.getPriorityOfAxiom rewrites)
894894
goal
895-
(Limit.takeWithin
896-
depthLimit
897-
(Foldable.toList $ strategy goal coinductiveRewrites rewrites)
898-
)
895+
(Limit.takeWithin depthLimit (Foldable.toList strategy))
899896
return (sort $ nub result)
900897

901898
assertStuck

0 commit comments

Comments
 (0)