Как исправить неоднозначность произвольного экземпляра типа списка

Учтите следующее:

import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes

data List a = Nil | Cons a (List a) deriving (Eq, Show)

instance Functor List where
    fmap _ Nil = Nil
    fmap f (Cons a l) = Cons (f a) (fmap f l)

instance Eq a => EqProp (List a) where (=-=) = eq

genList :: Arbitrary a => Gen (List a)
genList = do
    n <- choose (3 :: Int, 5)
    gen <- arbitrary
    elems <- vectorOf n gen
    return $ build elems
  where build [] = Nil
        build (e:es) = Cons e (build es)

instance Arbitrary a => Arbitrary (List a) where
    arbitrary = frequency [ (1, return Nil)
                          , (3, genList)
                          ]

main = quickBatch $ functor (Nil :: List (Int, String, Int))

Это не компилируется из-за двусмысленности в genList:

• Could not deduce (Arbitrary (Gen a))
    arising from a use of ‘arbitrary’
  from the context: Arbitrary a
    bound by the type signature for:
               genList :: forall a. Arbitrary a => Gen (List a)
    at reproducer.hs:13:1-38
• In a stmt of a 'do' block: gen <- arbitrary
  In the expression:
    do n <- choose (3 :: Int, 5)
       gen <- arbitrary
       elems <- vectorOf n gen
       return $ build elems
  In an equation for ‘genList’:
      genList
        = do n <- choose (3 :: Int, 5)
             gen <- arbitrary
             elems <- vectorOf n gen
             ....
        where
            build [] = Nil
            build (e : es) = Cons e (build es)
   |
16 |     gen <- arbitrary
   |            ^^^^^^^^^

Я знаю, что могу написать genList как genList = Cons <$> arbitrary <*> arbitrary, но мне хотелось бы знать. Как убрать двусмысленность? Разве утверждение типа в main не должно очистить его?


person Jan Synáček    schedule 04.01.2018    source источник
comment
Разве ты не хочешь просто let gen = arbitrary? (или elems <- vectorOf n arbitrary).   -  person Lee    schedule 04.01.2018
comment
@ Ли, я чувствую себя глупо. Действительно, в этом случае gen не следует разворачивать.   -  person Jan Synáček    schedule 04.01.2018


Ответы (1)