Skip to content

Commit 69d5315

Browse files
Shrink index semantics
1 parent c909f29 commit 69d5315

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/design.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,70 @@ When the `CONTINUE_SHRINKING` bit is part of the exit code, the user is
178178
encouraged to restart the `runner` with the new shrink index, in order to
179179
manually "pump" the shrinker.
180180

181+
### Shrink Index
182+
183+
The shrinking functionality depends on the following semantics of the
184+
`ShrinkIndex`.
185+
186+
```haskell
187+
data PointSchedule
188+
instance PartialOrd PointSchedule
189+
190+
data ShrinkIndex
191+
instance Ord ShrinkIndex
192+
193+
-- Given a point schedule and an index,
194+
-- we produce the corresponding shrunk point schedule
195+
-- if it exists.
196+
lookup :: PointSchedule -> ShrinkIndex -> Maybe PointSchedule
197+
198+
-- Single shrink step
199+
shrink :: PointSchedule -> [PointSchedule]
200+
201+
-- On local success, we try the next shrink index.
202+
succ :: ShrinkIndex -> ShrinkInbdex
203+
204+
-- On failure, we extend the shrink index.
205+
extend :: ShrinkIndex -> ShrinkIndex
206+
207+
-- No shrinking yet
208+
empty :: Index
209+
210+
-- We decide how to pick the next index according to the test result
211+
next :: ShrinkIndex -> PointSchedule -> Bool -> ShrinkIndex
212+
next i pass
213+
| pass && i == empty = empty
214+
| pass && lookup ps (succ i) == Nothing = i -- ^ Minimal counter example
215+
| pass = succ i
216+
| not pass && lookup ps (extend i) == Nothing = i -- ^ Minimal counter example
217+
| otherwise = extend i
218+
219+
uncons :: Index -> Maybe (Int, Index)
220+
221+
222+
-- With the following properties
223+
224+
succ empty == empty
225+
226+
uncons (extend empty) == (0,empty)
227+
228+
lookup ps empty == Just ps
229+
230+
-- The next index cannot produce a bigger point schedule
231+
compare (lookup ps $ next i pass) (lookup ps i) /= GT
232+
233+
-- The next two properties state that the only case where
234+
-- either 'succ' or 'extend' produce an equal sized point schedule
235+
-- is when they correspond to no schedule at all.
236+
237+
compare shrinked succShrinked == EQ <=> (shrinked == Nothing && succShrink == Nothing)
238+
where shrinked = lookup ps i
239+
succShrink = lookup ps (succ i)
240+
241+
compare shrinked extShrinked == EQ <=> (shrinked == Nothing && extShrinked == Nothing)
242+
where shrinked = lookup ps i
243+
extShrinked = lookup ps (extend i)
244+
```
181245

182246
## Alternatives
183247

0 commit comments

Comments
 (0)