haskell - Testing type classes with Quickcheck, variable number of parameters -
haskell - Testing type classes with Quickcheck, variable number of parameters -
i have ring type class looks this:
class ring addid :: addinverse :: -> mulid :: add together :: -> -> mul :: -> ->
for class have several instances, e.g.
instance ring matrix ... instance ring integer ... instance ring modulo ...
in order test these instances, have next quickcheck tests:
prop_addid :: (ring a, eq a, arbitrary a) => -> bool prop_addinv :: (ring a, eq a, arbitrary a) => -> bool prop_mulid :: (ring a, eq a, arbitrary a) => -> bool prop_addcommutative :: (ring a, eq a, arbitrary a) => -> -> bool prop_addassociative :: (ring a, eq a, arbitrary a) => -> -> -> bool prop_mulassociative :: (ring a, eq a, arbitrary a) => -> -> -> bool prop_distributive :: (ring a, eq a, arbitrary a) => -> -> -> bool
i'm unsure how run these testcases class instances. found solution here leads following:
forallrings :: (forall a. (ring a, arbitrary a, eq a) => -> bool) -> [io ()] forallrings x = [ quickcheck (x :: matrix -> bool) , quickcheck (x :: integer -> bool) , quickcheck (x :: modulo -> bool) ] forallrings2 :: (forall a. (ring a, arbitrary a, eq a) => -> -> bool) -> [io ()] forallrings2 x = [ quickcheck (x :: matrix -> matrix -> bool) , quickcheck (x :: integer -> integer -> bool) , quickcheck (x :: modulo -> modulo -> bool) ] forallrings3 :: (forall a. (ring a, arbitrary a, eq a) => -> -> -> bool) -> [io ()] forallrings3 x = [ quickcheck (x :: matrix -> matrix -> matrix -> bool) , quickcheck (x :: integer -> integer -> integer -> bool) , quickcheck (x :: modulo -> modulo -> modulo -> bool) ] ringtests :: io () ringtests = sequence_ $ forallrings propaddid ++ forallrings prop_addinv ++ forallrings prop_mulid ++ forallrings2 prop_addcommutative ++ forallrings3 prop_addassociative ++ forallrings3 prop_mulassociative ++ forallrings3 prop_distributive
i unsatisfied solution. find forallringsx functions little ugly , repetetive. reason tests have different number of parameters. there improve way (i.e. 1 less boiler plate code) test instances?
i think typeclass function way go here. 1 simple way add together fellow member class ring
ringtests :: (eq a, arbitrary a) => proxy -> io result
. proxy argument necessary caller can specify type test: ringtests (proxy :: proxy matrix)
.
ringtests
can have default implementation should easy write; may need scopedtypevariables
type variable in scope.
you can of course of study set test function in separate class, 1 time again default implementation. you'd have write instance ringtests matrix where
, forth, no implementation necessary.
haskell typeclass quickcheck
Comments
Post a Comment