Last active
January 22, 2019 04:42
-
-
Save emilyhorsman/8b84eb215137eb7632b53c6e474cc906 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance Arbitrary a => Arbitrary (Rose a) where | |
arbitrary = | |
let | |
numChildren :: Gen Int | |
numChildren = (`mod` 6) . abs <$> arbitrary | |
makeRose :: Arbitrary a => Int -> Gen (Rose a) | |
makeRose n = rose $ n `div` 2 | |
branch :: Arbitrary a => Int -> Gen [Rose a] | |
branch n = | |
range >>= mapM (const (makeRose n)) | |
where | |
range = flip take [0..] <$> numChildren | |
rose :: Arbitrary a => Int -> Gen (Rose a) | |
rose 0 = MkRose <$> arbitrary <*> return [] | |
rose n = MkRose <$> arbitrary <*> branch n | |
in | |
sized rose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment