diff --git a/hedgehog/src/Hedgehog/Gen.hs b/hedgehog/src/Hedgehog/Gen.hs index e5982c5f..34d639f4 100644 --- a/hedgehog/src/Hedgehog/Gen.hs +++ b/hedgehog/src/Hedgehog/Gen.hs @@ -94,6 +94,7 @@ module Hedgehog.Gen ( -- ** Combinations & Permutations , subsequence + , subset , shuffle -- ** Abstract State Machine diff --git a/hedgehog/src/Hedgehog/Internal/Gen.hs b/hedgehog/src/Hedgehog/Internal/Gen.hs index 7874489c..5c0c92b6 100644 --- a/hedgehog/src/Hedgehog/Internal/Gen.hs +++ b/hedgehog/src/Hedgehog/Internal/Gen.hs @@ -129,6 +129,7 @@ module Hedgehog.Internal.Gen ( -- ** Combinations & Permutations , subsequence + , subset , shuffle , shuffleSeq @@ -215,6 +216,7 @@ import qualified Data.Semigroup as Semigroup import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Set (Set) +import qualified Data.Set as Set import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Text.Encoding as Text @@ -1679,6 +1681,19 @@ subsequence :: MonadGen m => [a] -> m [a] subsequence xs = shrink Shrink.list $ filterM (const bool_) xs +-- | Generates a random subset of a set. +-- +-- /This shrinks towards the empty set./ +-- +subset :: MonadGen m => Set a -> m (Set a) +-- Set.fromDistinctAscList has an unchecked precondition that the list +-- must be strictly ascending. This precondition is satisfied because +-- Set.toAscList produces a strictly ascending list, and the 'subsequence' +-- generator only removes elements from the list; it never adds or +-- rearranges elements, so the strictly ascending property is undisturbed. +subset = + fmap Set.fromDistinctAscList . subsequence . Set.toAscList + -- | Generates a random permutation of a list. -- -- /This shrinks towards the order of the list being identical to the input/