Skip to content

Commit 2e1a306

Browse files
emarzionttuegel
andauthored
Always output fully-simplified states (#2303)
* executionStrategy: Simplify always appears after Rewrite * limitedExecutionStrategy: Add missing documentation Co-authored-by: Thomas Tuegel <[email protected]>
1 parent 08b378f commit 2e1a306

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

kore/src/Kore/Step.hs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,27 @@ retractRemaining _ = Nothing
102102
-}
103103
executionStrategy :: Stream (Strategy Prim)
104104
executionStrategy =
105-
(Strategy.sequence . fmap Strategy.apply)
106-
[ Begin
107-
, Simplify
108-
, Rewrite
109-
]
110-
& Stream.iterate id
105+
step1 Stream.:> Stream.iterate id stepN
106+
where
107+
step1 =
108+
(Strategy.sequence . fmap Strategy.apply)
109+
[ Begin
110+
, Simplify
111+
, Rewrite
112+
, Simplify
113+
]
114+
stepN =
115+
(Strategy.sequence . fmap Strategy.apply)
116+
[ Begin
117+
, Rewrite
118+
, Simplify
119+
]
120+
121+
{- | The sequence of transitions under the specified depth limit.
122+
123+
See also: 'executionStrategy'
111124
125+
-}
112126
limitedExecutionStrategy :: Limit Natural -> [Strategy Prim]
113127
limitedExecutionStrategy depthLimit =
114128
Limit.takeWithin depthLimit (toList executionStrategy)
@@ -119,7 +133,7 @@ data Prim
119133
= Begin
120134
| Simplify
121135
| Rewrite
122-
deriving (Show)
136+
deriving (Eq, Show)
123137

124138
{- The two modes of symbolic execution. Each mode determines the way
125139
rewrite rules are applied during a rewrite step.

kore/test/Test/Kore/Step.hs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
module Test.Kore.Step
22
( test_stepStrategy
3+
, test_executionStrategy
34
) where
45

56
import Prelude.Kore
67

8+
import Hedgehog
9+
( Gen
10+
)
11+
import qualified Hedgehog
12+
import qualified Hedgehog.Gen
13+
import qualified Hedgehog.Range
714
import Test.Tasty
15+
import Test.Tasty.Hedgehog
816

917
import qualified Control.Exception as Exception
1018
import qualified Control.Lens as Lens
@@ -298,6 +306,47 @@ test_stepStrategy =
298306
Right _ ->
299307
assertFailure "Expected exception LimitExceeded"
300308

309+
test_executionStrategy :: [TestTree]
310+
test_executionStrategy =
311+
[ testProperty "every step contains Rewrite" $ Hedgehog.property $ do
312+
strategies <- Hedgehog.forAll genStrategies
313+
for_ strategies $ \strategy -> do
314+
Hedgehog.annotateShow strategy
315+
Hedgehog.assert (hasRewrite strategy)
316+
317+
, testProperty "Simplify is the last sub-step" $ Hedgehog.property $ do
318+
strategies <- Hedgehog.forAll genStrategies
319+
let strategy = last strategies
320+
Hedgehog.annotateShow strategy
321+
Hedgehog.assert (isLastSimplify strategy)
322+
]
323+
where
324+
genStrategies :: Gen [Strategy Prim]
325+
genStrategies = do
326+
let range = Hedgehog.Gen.integral (Hedgehog.Range.linear 1 16)
327+
depthLimit <- Limit <$> range
328+
pure (limitedExecutionStrategy depthLimit)
329+
330+
hasRewrite :: Strategy Prim -> Bool
331+
hasRewrite = \case
332+
Strategy.Seq s1 s2 -> hasRewrite s1 || hasRewrite s2
333+
Strategy.And s1 s2 -> hasRewrite s1 || hasRewrite s2
334+
Strategy.Or s1 s2 -> hasRewrite s1 || hasRewrite s2
335+
Strategy.Apply p -> p == Rewrite
336+
Strategy.Stuck -> False
337+
Strategy.Continue -> False
338+
339+
isLastSimplify :: Strategy Prim -> Bool
340+
isLastSimplify = \case
341+
Strategy.Seq s Strategy.Continue -> isLastSimplify s
342+
Strategy.Seq s Strategy.Stuck -> isLastSimplify s
343+
Strategy.Seq _ s -> isLastSimplify s
344+
Strategy.And s1 s2 -> isLastSimplify s1 && isLastSimplify s2
345+
Strategy.Or s1 s2 -> isLastSimplify s1 && isLastSimplify s2
346+
Strategy.Apply p -> p == Simplify
347+
Strategy.Stuck -> False
348+
Strategy.Continue -> False
349+
301350
simpleRewrite
302351
:: TermLike VariableName
303352
-> TermLike VariableName

0 commit comments

Comments
 (0)