Skip to content

Commit 78e4327

Browse files
Random generation of initial states
1 parent 64d1d21 commit 78e4327

File tree

7 files changed

+149
-113
lines changed

7 files changed

+149
-113
lines changed

quickcheck-dynamic/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ changes.
99

1010
## UNRELEASED
1111

12+
* **BREAKING**: Made `initialState` `Gen state` instead of a `state` and introduced `setup :: state -> m ()` to `RunModel`.
13+
- To migrate an existing model simply replace `initialState = MyState{..}` with `initialState = pure $ MyState{..}` and everything
14+
else should work straight-forwardly.
15+
1216
* **BREAKING**: Removed `Realized`
1317
- To migrate uses of `Realized` with `IOSim`, index the state type on the choice of `RunModel` monad
1418
and index the relevant types:

quickcheck-dynamic/src/Test/QuickCheck/DynamicLogic.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ forAllNonVariableQ :: QuantifyConstraints (HasNoVariables a) => Quantification a
131131
forAllNonVariableQ q = DL $ \s k -> DL.forAllQ (hasNoVariablesQ q) $ \(HasNoVariables x) -> k x s
132132

133133
runDL :: Annotated s -> DL s () -> DL.DynFormula s
134-
runDL s dl = unDL dl s $ \_ _ -> DL.passTest
134+
runDL s dl = (unDL dl s $ \_ _ -> DL.passTest)
135135

136136
forAllUniqueDL
137137
:: (DL.DynLogicModel s, Testable a)
@@ -146,7 +146,8 @@ forAllDL
146146
=> DL s ()
147147
-> (Actions s -> a)
148148
-> Property
149-
forAllDL d = DL.forAllScripts (runDL initialAnnotatedState d)
149+
forAllDL d p =
150+
forAllBlind initialState $ \st -> DL.forAllScripts st (runDL (Metadata mempty st) d) p
150151

151152
forAllMappedDL
152153
:: (DL.DynLogicModel s, Testable a)
@@ -157,4 +158,5 @@ forAllMappedDL
157158
-> (srep -> a)
158159
-> Property
159160
forAllMappedDL to from fromScript d prop =
160-
DL.forAllMappedScripts to from (runDL initialAnnotatedState d) (prop . fromScript)
161+
forAll initialState $ \st ->
162+
DL.forAllMappedScripts st to from (runDL (Metadata mempty st) d) (prop . fromScript)

quickcheck-dynamic/src/Test/QuickCheck/DynamicLogic/Internal.hs

Lines changed: 93 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ instance StateModel s => Show (FailingAction s) where
171171
show (ActionFail (ActionWithPolarity a pol)) = show pol ++ " : " ++ show a
172172

173173
data DynLogicTest s
174-
= BadPrecondition (TestSequence s) (FailingAction s) (Annotated s)
175-
| Looping (TestSequence s)
176-
| Stuck (TestSequence s) (Annotated s)
177-
| DLScript (TestSequence s)
174+
= BadPrecondition (Annotated s) (TestSequence s) (FailingAction s) (Annotated s)
175+
| Looping (Annotated s) (TestSequence s)
176+
| Stuck (Annotated s) (TestSequence s) (Annotated s)
177+
| DLScript (Annotated s) (TestSequence s)
178178

179179
data Witnesses r where
180180
Do :: r -> Witnesses r
@@ -295,8 +295,10 @@ prettyWitnesses (Witness a w) = ("_ <- forAllQ $ exactlyQ $ " ++ show a) : prett
295295
prettyWitnesses Do{} = []
296296

297297
instance StateModel s => Show (DynLogicTest s) where
298-
show (BadPrecondition ss bad s) =
299-
prettyTestSequence (usedVariables ss <> allVariables bad) ss
298+
show (BadPrecondition is ss bad s) =
299+
"-- Initial state: "
300+
++ show is
301+
++ prettyTestSequence (usedVariables is ss <> allVariables bad) ss
300302
++ "\n -- In state: "
301303
++ show s
302304
++ "\n "
@@ -309,12 +311,25 @@ instance StateModel s => Show (DynLogicTest s) where
309311
f
310312
| p == PosPolarity = "action"
311313
| otherwise = "failingAction"
312-
show (Looping ss) = prettyTestSequence (usedVariables ss) ss ++ "\n pure ()\n -- Looping"
313-
show (Stuck ss s) = prettyTestSequence (usedVariables ss) ss ++ "\n pure ()\n -- Stuck in state " ++ show s
314-
show (DLScript ss) = prettyTestSequence (usedVariables ss) ss ++ "\n pure ()\n"
315-
316-
usedVariables :: forall s. StateModel s => TestSequence s -> VarContext
317-
usedVariables = go initialAnnotatedState
314+
show (Looping is ss) =
315+
"-- Initial state: "
316+
++ show is
317+
++ prettyTestSequence (usedVariables is ss) ss
318+
++ "\n pure ()\n -- Looping"
319+
show (Stuck is ss s) =
320+
"-- Initial state: "
321+
++ show is
322+
++ prettyTestSequence (usedVariables is ss) ss
323+
++ "\n pure ()\n -- Stuck in state "
324+
++ show s
325+
show (DLScript is ss) =
326+
"-- Initial state: "
327+
++ show is
328+
++ prettyTestSequence (usedVariables is ss) ss
329+
++ "\n pure ()\n"
330+
331+
usedVariables :: forall s. StateModel s => Annotated s -> TestSequence s -> VarContext
332+
usedVariables s = go s
318333
where
319334
go :: Annotated s -> TestSequence s -> VarContext
320335
go aState TestSeqStop = allVariables (underlyingState aState)
@@ -342,10 +357,11 @@ restrictedPolar (ActionWithPolarity a _) = restricted a
342357
-- `Actions` sequence into a proper `Property` that can be run by QuickCheck.
343358
forAllScripts
344359
:: (DynLogicModel s, Testable a)
345-
=> DynFormula s
360+
=> s
361+
-> DynFormula s
346362
-> (Actions s -> a)
347363
-> Property
348-
forAllScripts = forAllMappedScripts id id
364+
forAllScripts s = forAllMappedScripts s id id
349365

350366
-- | `Property` function suitable for formulae without choice.
351367
forAllUniqueScripts
@@ -365,16 +381,17 @@ forAllUniqueScripts s f k =
365381
-- | Creates a `Property` from `DynFormula` with some specialised isomorphism for shrinking purpose.
366382
forAllMappedScripts
367383
:: (DynLogicModel s, Testable a)
368-
=> (rep -> DynLogicTest s)
384+
=> s
385+
-> (rep -> DynLogicTest s)
369386
-> (DynLogicTest s -> rep)
370387
-> DynFormula s
371388
-> (Actions s -> a)
372389
-> Property
373-
forAllMappedScripts to from f k =
390+
forAllMappedScripts s to from f k =
374391
QC.withSize $ \n ->
375392
let d = unDynFormula f n
376393
in forAllShrinkBlind
377-
(Smart 0 <$> sized ((from <$>) . generateDLTest d))
394+
(Smart 0 <$> sized ((from <$>) . generateDLTest (Metadata mempty s) d))
378395
(shrinkSmart ((from <$>) . shrinkDLTest d . to))
379396
$ \(Smart _ script) ->
380397
withDLScript d k (to script)
@@ -405,17 +422,24 @@ validDLTest test prop =
405422
Stuck{} -> property Discard
406423
_other -> counterexample (show test) False
407424

408-
generateDLTest :: DynLogicModel s => DynLogic s -> Int -> Gen (DynLogicTest s)
409-
generateDLTest d size = generate chooseNextStep d 0 (initialStateFor d) size
425+
generateDLTest :: DynLogicModel s => Annotated s -> DynLogic s -> Int -> Gen (DynLogicTest s)
426+
generateDLTest s d size = do
427+
generate chooseNextStep d 0 s size
410428

411429
onDLTestSeq :: (TestSequence s -> TestSequence s) -> DynLogicTest s -> DynLogicTest s
412-
onDLTestSeq f (BadPrecondition ss bad s) = BadPrecondition (f ss) bad s
413-
onDLTestSeq f (Looping ss) = Looping (f ss)
414-
onDLTestSeq f (Stuck ss s) = Stuck (f ss) s
415-
onDLTestSeq f (DLScript ss) = DLScript (f ss)
430+
onDLTestSeq f (BadPrecondition is ss bad s) = BadPrecondition is (f ss) bad s
431+
onDLTestSeq f (Looping is ss) = Looping is (f ss)
432+
onDLTestSeq f (Stuck is ss s) = Stuck is (f ss) s
433+
onDLTestSeq f (DLScript is ss) = DLScript is (f ss)
434+
435+
setDLTestState :: Annotated s -> DynLogicTest s -> DynLogicTest s
436+
setDLTestState is (BadPrecondition _ ss bad s) = BadPrecondition is ss bad s
437+
setDLTestState is (Looping _ ss) = Looping is ss
438+
setDLTestState is (Stuck _ ss s) = Stuck is ss s
439+
setDLTestState is (DLScript _ ss) = DLScript is ss
416440

417-
consDLTest :: TestStep s -> DynLogicTest s -> DynLogicTest s
418-
consDLTest step = onDLTestSeq (step :>)
441+
consDLTest :: Annotated s -> TestStep s -> DynLogicTest s -> DynLogicTest s
442+
consDLTest is step = setDLTestState is . onDLTestSeq (step :>)
419443

420444
consDLTestW :: Witnesses () -> DynLogicTest s -> DynLogicTest s
421445
consDLTestW w = onDLTestSeq (addW w)
@@ -433,15 +457,15 @@ generate
433457
-> m (DynLogicTest s)
434458
generate chooseNextStepFun d n s size =
435459
if n > sizeLimit size
436-
then return $ Looping TestSeqStop
460+
then return $ Looping s TestSeqStop
437461
else do
438462
let preferred = if n > size then stopping d else noStopping d
439-
useStep (BadAction (Witnesses ws bad)) _ = return $ BadPrecondition (TestSeqStopW ws) bad s
440-
useStep StoppingStep _ = return $ DLScript TestSeqStop
463+
useStep (BadAction (Witnesses ws bad)) _ = return $ BadPrecondition s (TestSeqStopW ws) bad s
464+
useStep StoppingStep _ = return $ DLScript s TestSeqStop
441465
useStep (Stepping step d') _ =
442466
case discardWitnesses step of
443467
var := act ->
444-
consDLTest step
468+
consDLTest s step
445469
<$> generate
446470
chooseNextStepFun
447471
d'
@@ -451,15 +475,12 @@ generate chooseNextStepFun d n s size =
451475
useStep NoStep alt = alt
452476
foldr
453477
(\step k -> do try <- chooseNextStepFun s n step; useStep try k)
454-
(return $ Stuck TestSeqStop s)
478+
(return $ Stuck s TestSeqStop s)
455479
[preferred, noAny preferred, d, noAny d]
456480

457481
sizeLimit :: Int -> Int
458482
sizeLimit size = 2 * size + 20
459483

460-
initialStateFor :: StateModel s => DynLogic s -> Annotated s
461-
initialStateFor _ = initialAnnotatedState
462-
463484
stopping :: DynLogic s -> DynLogic s
464485
stopping EmptySpec = EmptySpec
465486
stopping Stop = Stop
@@ -589,22 +610,28 @@ keepTryingUntil n g p = do
589610
if p x then return $ Just x else scale (+ 1) $ keepTryingUntil (n - 1) g p
590611

591612
shrinkDLTest :: DynLogicModel s => DynLogic s -> DynLogicTest s -> [DynLogicTest s]
592-
shrinkDLTest _ (Looping _) = []
613+
shrinkDLTest _ (Looping _ _) = []
593614
shrinkDLTest d tc =
594-
[ test | as' <- shrinkScript d (getScript tc), let pruned = pruneDLTest d as'
595-
test = makeTestFromPruned d pruned,
596-
-- Don't shrink a non-executable test case to an executable one.
615+
[ test
616+
| as' <-
617+
shrinkScript
618+
(underlyingState $ getInitialState tc)
619+
d
620+
(getScript tc)
621+
, let pruned = pruneDLTest (getInitialState tc) d as'
622+
test = makeTestFromPruned (getInitialState tc) d pruned
623+
, -- Don't shrink a non-executable test case to an executable one.
597624
case (tc, test) of
598-
(DLScript _, _) -> True
599-
(_, DLScript _) -> False
625+
(DLScript _ _, _) -> True
626+
(_, DLScript _ _) -> False
600627
_ -> True
601628
]
602629

603630
nextStateStep :: StateModel s => Step s -> Annotated s -> Annotated s
604631
nextStateStep (var := act) s = computeNextState s act var
605632

606-
shrinkScript :: forall s. DynLogicModel s => DynLogic s -> TestSequence s -> [TestSequence s]
607-
shrinkScript = shrink' initialAnnotatedState
633+
shrinkScript :: forall s. DynLogicModel s => s -> DynLogic s -> TestSequence s -> [TestSequence s]
634+
shrinkScript is = shrink' (Metadata mempty is)
608635
where
609636
shrink' :: Annotated s -> DynLogic s -> TestSequence s -> [TestSequence s]
610637
shrink' s d ss = structural s d ss ++ nonstructural s d ss
@@ -648,8 +675,8 @@ shrinkWitness AfterAny{} _ = []
648675

649676
-- The result of pruning a list of actions is a prefix of a list of actions that
650677
-- could have been generated by the dynamic logic.
651-
pruneDLTest :: forall s. DynLogicModel s => DynLogic s -> TestSequence s -> TestSequence s
652-
pruneDLTest dl = prune [dl] initialAnnotatedState
678+
pruneDLTest :: forall s. DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> TestSequence s
679+
pruneDLTest is dl = prune [dl] is
653680
where
654681
prune [] _ _ = TestSeqStop
655682
prune _ _ TestSeqStop = TestSeqStop
@@ -710,27 +737,29 @@ demonicAlt ds = foldr1 (Alt Demonic) ds
710737

711738
propPruningGeneratedScriptIsNoop :: DynLogicModel s => DynLogic s -> Property
712739
propPruningGeneratedScriptIsNoop d =
713-
forAll (sized $ \n -> choose (1, max 1 n) >>= generateDLTest d) $ \test ->
714-
let script = case test of
715-
BadPrecondition s _ _ -> s
716-
Looping s -> s
717-
Stuck s _ -> s
718-
DLScript s -> s
719-
in script == pruneDLTest d script
740+
forAll initialState $ \s ->
741+
forAll (sized $ \n -> choose (1, max 1 n) >>= generateDLTest (Metadata mempty s) d) $ \test ->
742+
getScript test == pruneDLTest (getInitialState test) d (getScript test)
743+
744+
getInitialState :: DynLogicTest s -> Annotated s
745+
getInitialState (BadPrecondition is _ _ _) = is
746+
getInitialState (Looping is _) = is
747+
getInitialState (Stuck is _ _) = is
748+
getInitialState (DLScript is _) = is
720749

721750
getScript :: DynLogicTest s -> TestSequence s
722-
getScript (BadPrecondition s _ _) = s
723-
getScript (Looping s) = s
724-
getScript (Stuck s _) = s
725-
getScript (DLScript s) = s
751+
getScript (BadPrecondition _ s _ _) = s
752+
getScript (Looping _ s) = s
753+
getScript (Stuck _ s _) = s
754+
getScript (DLScript _ s) = s
726755

727-
makeTestFromPruned :: forall s. DynLogicModel s => DynLogic s -> TestSequence s -> DynLogicTest s
728-
makeTestFromPruned dl = make dl initialAnnotatedState
756+
makeTestFromPruned :: forall s. DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> DynLogicTest s
757+
makeTestFromPruned st dl = make dl st
729758
where
730759
make d s TestSeqStop
731-
| b : _ <- badActions @s d s = BadPrecondition TestSeqStop b s
732-
| stuck d s = Stuck TestSeqStop s
733-
| otherwise = DLScript TestSeqStop
760+
| b : _ <- badActions @s d s = BadPrecondition s TestSeqStop b s
761+
| stuck d s = Stuck s TestSeqStop s
762+
| otherwise = DLScript s TestSeqStop
734763
make d s (TestSeqWitness a ss) =
735764
onDLTestSeq (TestSeqWitness a) $
736765
make
@@ -747,13 +776,7 @@ makeTestFromPruned dl = make dl initialAnnotatedState
747776
-- | If failed, return the prefix up to the failure. Also prunes the test in case the model has
748777
-- changed.
749778
unfailDLTest :: DynLogicModel s => DynLogic s -> DynLogicTest s -> DynLogicTest s
750-
unfailDLTest d test = makeTestFromPruned d $ pruneDLTest d steps
751-
where
752-
steps = case test of
753-
BadPrecondition as _ _ -> as
754-
Stuck as _ -> as
755-
DLScript as -> as
756-
Looping as -> as
779+
unfailDLTest d test = makeTestFromPruned (getInitialState test) d $ pruneDLTest (getInitialState test) d (getScript test)
757780

758781
stuck :: DynLogicModel s => DynLogic s -> Annotated s -> Bool
759782
stuck EmptySpec _ = True
@@ -778,8 +801,8 @@ stuck (ForAll _ _) _ = False
778801
stuck (Monitor _ d) s = stuck d s
779802

780803
scriptFromDL :: DynLogicTest s -> Actions s
781-
scriptFromDL (DLScript s) = Actions $ sequenceSteps s
782-
scriptFromDL _ = Actions []
804+
scriptFromDL (DLScript is s) = Actions (underlyingState is) $ sequenceSteps s
805+
scriptFromDL test = Actions (underlyingState $ getInitialState test) []
783806

784807
sequenceSteps :: TestSequence s -> [Step s]
785808
sequenceSteps (TestSeq ss) =
@@ -818,8 +841,8 @@ badActions (ForAll _ _) _ = []
818841
badActions (Monitor _ d) s = badActions d s
819842

820843
applyMonitoring :: DynLogicModel s => DynLogic s -> DynLogicTest s -> Property -> Property
821-
applyMonitoring d (DLScript s) p =
822-
case findMonitoring d initialAnnotatedState s of
844+
applyMonitoring d (DLScript is s) p =
845+
case findMonitoring d is s of
823846
Just f -> f p
824847
Nothing -> p
825848
applyMonitoring _ Stuck{} p = p

0 commit comments

Comments
 (0)