1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Generate arbitrary terms using frequency & sized bounds.

This commit is contained in:
Rob Rix 2015-12-11 14:38:57 -05:00
parent 046c3d1813
commit 2764c34b8d

View File

@ -32,7 +32,12 @@ instance (Eq a, Eq f, Arbitrary a, Arbitrary f) => Arbitrary (Syntax a f) where
shrinkSyntax (Keyed k) = Keyed . Map.fromList <$> (List.subsequences (Map.toList k) >>= shrink)
instance (Eq a, Eq annotation, Arbitrary a, Arbitrary annotation) => Arbitrary (ArbitraryTerm a annotation) where
arbitrary = arbitraryBounded 4
arbitrary = sized boundedTerm
where boundedTerm n = ArbitraryTerm <$> ((,) <$> arbitrary <*> boundedSyntax n)
boundedSyntax 0 = liftM Leaf arbitrary
boundedSyntax n = frequency
[ (1, liftM Leaf arbitrary),
(4, liftM Indexed $ listOf $ boundedTerm $ n - 1) ]
shrink term@(ArbitraryTerm (annotation, syntax)) = (++) (subterms term) $ filter (/= term) $ ArbitraryTerm <$> ((,) <$> shrink annotation <*> shrinkSyntax syntax)
where shrinkSyntax (Leaf a) = Leaf <$> shrink a
shrinkSyntax (Indexed i) = Indexed <$> (List.subsequences i >>= recursivelyShrink)