Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Last active January 22, 2019 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyhorsman/8b84eb215137eb7632b53c6e474cc906 to your computer and use it in GitHub Desktop.
Save emilyhorsman/8b84eb215137eb7632b53c6e474cc906 to your computer and use it in GitHub Desktop.
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