@@ -171,10 +171,10 @@ instance StateModel s => Show (FailingAction s) where
171171 show (ActionFail (ActionWithPolarity a pol)) = show pol ++ " : " ++ show a
172172
173173data 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
179179data Witnesses r where
180180 Do :: r -> Witnesses r
@@ -294,9 +294,10 @@ prettyWitnesses :: Witnesses () -> [String]
294294prettyWitnesses (Witness a w) = (" _ <- forAllQ $ exactlyQ $ " ++ show a) : prettyWitnesses w
295295prettyWitnesses Do {} = []
296296
297+ -- TODO: show initial state is
297298instance StateModel s => Show (DynLogicTest s ) where
298- show (BadPrecondition ss bad s) =
299- prettyTestSequence (usedVariables ss <> allVariables bad) ss
299+ show (BadPrecondition is ss bad s) =
300+ prettyTestSequence (usedVariables is ss <> allVariables bad) ss
300301 ++ " \n -- In state: "
301302 ++ show s
302303 ++ " \n "
@@ -309,12 +310,12 @@ instance StateModel s => Show (DynLogicTest s) where
309310 f
310311 | p == PosPolarity = " action"
311312 | 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 "
313+ show (Looping is ss) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n -- Looping"
314+ show (Stuck is ss s) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n -- Stuck in state " ++ show s
315+ show (DLScript is ss) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n "
315316
316- usedVariables :: forall s . StateModel s => TestSequence s -> VarContext
317- usedVariables = go initialAnnotatedState
317+ usedVariables :: forall s . StateModel s => Annotated s -> TestSequence s -> VarContext
318+ usedVariables s = go s
318319 where
319320 go :: Annotated s -> TestSequence s -> VarContext
320321 go aState TestSeqStop = allVariables (underlyingState aState)
@@ -342,10 +343,11 @@ restrictedPolar (ActionWithPolarity a _) = restricted a
342343-- `Actions` sequence into a proper `Property` that can be run by QuickCheck.
343344forAllScripts
344345 :: (DynLogicModel s , Testable a )
345- => DynFormula s
346+ => s
347+ -> DynFormula s
346348 -> (Actions s -> a )
347349 -> Property
348- forAllScripts = forAllMappedScripts id id
350+ forAllScripts s = forAllMappedScripts s id id
349351
350352-- | `Property` function suitable for formulae without choice.
351353forAllUniqueScripts
@@ -365,16 +367,17 @@ forAllUniqueScripts s f k =
365367-- | Creates a `Property` from `DynFormula` with some specialised isomorphism for shrinking purpose.
366368forAllMappedScripts
367369 :: (DynLogicModel s , Testable a )
368- => (rep -> DynLogicTest s )
370+ => s
371+ -> (rep -> DynLogicTest s )
369372 -> (DynLogicTest s -> rep )
370373 -> DynFormula s
371374 -> (Actions s -> a )
372375 -> Property
373- forAllMappedScripts to from f k =
376+ forAllMappedScripts s to from f k =
374377 QC. withSize $ \ n ->
375378 let d = unDynFormula f n
376379 in forAllShrinkBlind
377- (Smart 0 <$> sized ((from <$> ) . generateDLTest d))
380+ (Smart 0 <$> sized ((from <$> ) . generateDLTest ( Metadata mempty s) d))
378381 (shrinkSmart ((from <$> ) . shrinkDLTest d . to))
379382 $ \ (Smart _ script) ->
380383 withDLScript d k (to script)
@@ -405,17 +408,24 @@ validDLTest test prop =
405408 Stuck {} -> property Discard
406409 _other -> counterexample (show test) False
407410
408- generateDLTest :: DynLogicModel s => DynLogic s -> Int -> Gen (DynLogicTest s )
409- generateDLTest d size = generate chooseNextStep d 0 (initialStateFor d) size
411+ generateDLTest :: DynLogicModel s => Annotated s -> DynLogic s -> Int -> Gen (DynLogicTest s )
412+ generateDLTest s d size = do
413+ generate chooseNextStep d 0 s size
410414
411415onDLTestSeq :: (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)
416+ onDLTestSeq f (BadPrecondition is ss bad s) = BadPrecondition is (f ss) bad s
417+ onDLTestSeq f (Looping is ss) = Looping is (f ss)
418+ onDLTestSeq f (Stuck is ss s) = Stuck is (f ss) s
419+ onDLTestSeq f (DLScript is ss) = DLScript is (f ss)
420+
421+ setDLTestState :: Annotated s -> DynLogicTest s -> DynLogicTest s
422+ setDLTestState is (BadPrecondition _ ss bad s) = BadPrecondition is ss bad s
423+ setDLTestState is (Looping _ ss) = Looping is ss
424+ setDLTestState is (Stuck _ ss s) = Stuck is ss s
425+ setDLTestState is (DLScript _ ss) = DLScript is ss
416426
417- consDLTest :: TestStep s -> DynLogicTest s -> DynLogicTest s
418- consDLTest step = onDLTestSeq (step :> )
427+ consDLTest :: Annotated s -> TestStep s -> DynLogicTest s -> DynLogicTest s
428+ consDLTest is step = setDLTestState is . onDLTestSeq (step :> )
419429
420430consDLTestW :: Witnesses () -> DynLogicTest s -> DynLogicTest s
421431consDLTestW w = onDLTestSeq (addW w)
@@ -433,15 +443,15 @@ generate
433443 -> m (DynLogicTest s )
434444generate chooseNextStepFun d n s size =
435445 if n > sizeLimit size
436- then return $ Looping TestSeqStop
446+ then return $ Looping s TestSeqStop
437447 else do
438448 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
449+ useStep (BadAction (Witnesses ws bad)) _ = return $ BadPrecondition s (TestSeqStopW ws) bad s
450+ useStep StoppingStep _ = return $ DLScript s TestSeqStop
441451 useStep (Stepping step d') _ =
442452 case discardWitnesses step of
443453 var := act ->
444- consDLTest step
454+ consDLTest s step
445455 <$> generate
446456 chooseNextStepFun
447457 d'
@@ -451,15 +461,12 @@ generate chooseNextStepFun d n s size =
451461 useStep NoStep alt = alt
452462 foldr
453463 (\ step k -> do try <- chooseNextStepFun s n step; useStep try k)
454- (return $ Stuck TestSeqStop s)
464+ (return $ Stuck s TestSeqStop s)
455465 [preferred, noAny preferred, d, noAny d]
456466
457467sizeLimit :: Int -> Int
458468sizeLimit size = 2 * size + 20
459469
460- initialStateFor :: StateModel s => DynLogic s -> Annotated s
461- initialStateFor _ = initialAnnotatedState
462-
463470stopping :: DynLogic s -> DynLogic s
464471stopping EmptySpec = EmptySpec
465472stopping Stop = Stop
@@ -589,22 +596,24 @@ keepTryingUntil n g p = do
589596 if p x then return $ Just x else scale (+ 1 ) $ keepTryingUntil (n - 1 ) g p
590597
591598shrinkDLTest :: DynLogicModel s => DynLogic s -> DynLogicTest s -> [DynLogicTest s ]
592- shrinkDLTest _ (Looping _) = []
599+ shrinkDLTest _ (Looping _ _ ) = []
593600shrinkDLTest d tc =
594- [ test | as' <- shrinkScript d (getScript tc), let pruned = pruneDLTest d as'
595- test = makeTestFromPruned d pruned,
601+ [ test | as' <- shrinkScript (underlyingState $ getInitialState tc)
602+ d (getScript tc)
603+ , let pruned = pruneDLTest (getInitialState tc) d as'
604+ test = makeTestFromPruned (getInitialState tc) d pruned,
596605 -- Don't shrink a non-executable test case to an executable one.
597606 case (tc, test) of
598- (DLScript _, _) -> True
599- (_, DLScript _) -> False
607+ (DLScript _ _ , _) -> True
608+ (_, DLScript _ _ ) -> False
600609 _ -> True
601610 ]
602611
603612nextStateStep :: StateModel s => Step s -> Annotated s -> Annotated s
604613nextStateStep (var := act) s = computeNextState s act var
605614
606- shrinkScript :: forall s . DynLogicModel s => DynLogic s -> TestSequence s -> [TestSequence s ]
607- shrinkScript = shrink' initialAnnotatedState
615+ shrinkScript :: forall s . DynLogicModel s => s -> DynLogic s -> TestSequence s -> [TestSequence s ]
616+ shrinkScript is = shrink' ( Metadata mempty is)
608617 where
609618 shrink' :: Annotated s -> DynLogic s -> TestSequence s -> [TestSequence s ]
610619 shrink' s d ss = structural s d ss ++ nonstructural s d ss
@@ -648,8 +657,8 @@ shrinkWitness AfterAny{} _ = []
648657
649658-- The result of pruning a list of actions is a prefix of a list of actions that
650659-- 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
660+ pruneDLTest :: forall s . DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> TestSequence s
661+ pruneDLTest is dl = prune [dl] is
653662 where
654663 prune [] _ _ = TestSeqStop
655664 prune _ _ TestSeqStop = TestSeqStop
@@ -710,27 +719,29 @@ demonicAlt ds = foldr1 (Alt Demonic) ds
710719
711720propPruningGeneratedScriptIsNoop :: DynLogicModel s => DynLogic s -> Property
712721propPruningGeneratedScriptIsNoop 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
722+ forAll initialState $ \ s ->
723+ forAll (sized $ \ n -> choose (1 , max 1 n) >>= generateDLTest (Metadata mempty s) d) $ \ test ->
724+ getScript test == pruneDLTest (getInitialState test) d (getScript test)
725+
726+ getInitialState :: DynLogicTest s -> Annotated s
727+ getInitialState (BadPrecondition is _ _ _) = is
728+ getInitialState (Looping is _) = is
729+ getInitialState (Stuck is _ _) = is
730+ getInitialState (DLScript is _) = is
720731
721732getScript :: DynLogicTest s -> TestSequence s
722- getScript (BadPrecondition s _ _) = s
723- getScript (Looping s) = s
724- getScript (Stuck s _) = s
725- getScript (DLScript s) = s
733+ getScript (BadPrecondition _ s _ _) = s
734+ getScript (Looping _ s) = s
735+ getScript (Stuck _ s _) = s
736+ getScript (DLScript _ s) = s
726737
727- makeTestFromPruned :: forall s . DynLogicModel s => DynLogic s -> TestSequence s -> DynLogicTest s
728- makeTestFromPruned dl = make dl initialAnnotatedState
738+ makeTestFromPruned :: forall s . DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> DynLogicTest s
739+ makeTestFromPruned st dl = make dl st
729740 where
730741 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
742+ | b : _ <- badActions @ s d s = BadPrecondition s TestSeqStop b s
743+ | stuck d s = Stuck s TestSeqStop s
744+ | otherwise = DLScript s TestSeqStop
734745 make d s (TestSeqWitness a ss) =
735746 onDLTestSeq (TestSeqWitness a) $
736747 make
@@ -747,13 +758,7 @@ makeTestFromPruned dl = make dl initialAnnotatedState
747758-- | If failed, return the prefix up to the failure. Also prunes the test in case the model has
748759-- changed.
749760unfailDLTest :: 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
761+ unfailDLTest d test = makeTestFromPruned (getInitialState test) d $ pruneDLTest (getInitialState test) d (getScript test)
757762
758763stuck :: DynLogicModel s => DynLogic s -> Annotated s -> Bool
759764stuck EmptySpec _ = True
@@ -778,8 +783,8 @@ stuck (ForAll _ _) _ = False
778783stuck (Monitor _ d) s = stuck d s
779784
780785scriptFromDL :: DynLogicTest s -> Actions s
781- scriptFromDL (DLScript s) = Actions $ sequenceSteps s
782- scriptFromDL _ = Actions []
786+ scriptFromDL (DLScript is s) = Actions (underlyingState is) $ sequenceSteps s
787+ scriptFromDL test = Actions (underlyingState $ getInitialState test) []
783788
784789sequenceSteps :: TestSequence s -> [Step s ]
785790sequenceSteps (TestSeq ss) =
@@ -818,8 +823,8 @@ badActions (ForAll _ _) _ = []
818823badActions (Monitor _ d) s = badActions d s
819824
820825applyMonitoring :: DynLogicModel s => DynLogic s -> DynLogicTest s -> Property -> Property
821- applyMonitoring d (DLScript s) p =
822- case findMonitoring d initialAnnotatedState s of
826+ applyMonitoring d (DLScript is s) p =
827+ case findMonitoring d is s of
823828 Just f -> f p
824829 Nothing -> p
825830applyMonitoring _ Stuck {} p = p
0 commit comments