Is it possible to function function checkcheck function type?
I am trying to implement my own instances of functor and quickcheck them and am running into problems in typeclasses that are not instances Eq
, namely (->)
and IO
. My attempts lead to an error No instance for (Eq ...)
.
In case (->)
I ran into a similar error with Show
, that is No instance for (Show ...)
, and was able to fix it by adding an instance Show (a -> b)
as suggested in the answer. It would seem I could solve the missing instances as well by Eq
adding them in a similar way. However, this question about function equality notes that in Haskell, instantiation is Eq (a -> b)
equivalent to a halting problem and therefore not possible.
I'm not sure if instantiation is possible Eq IO a
. In case IO
I also run an error No instance for (Arbitrary ...)
.
Is there a way to quickly check the properties of a function type functor (->)
? Is there a way to do the same for a type IO
?
My code looks like this.
import Prelude hiding (Functor, fmap)
import Test.QuickCheck
import Test.QuickCheck.Function
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor IO where
fmap h f = f >>= (pure . h)
instance Functor ((->) e) where
fmap = (.)
data T a = T
prop_functorid :: (Functor f, Eq (f a)) => T (f a) -> f a -> Bool
prop_functorid T x = fmap id x == x
prop_functorcompose :: (Functor f, Eq (f c)) => T (f a) -> T b -> T c -> f a -> Fun a b -> Fun b c -> Bool
prop_functorcompose T T T x (apply -> g) (apply -> h) =
fmap (h . g) x == (fmap h . fmap g) x
instance Show (a -> b) where
show a= "function"
prop_function :: IO ()
prop_function = do
quickCheck $ prop_functorid (T :: T (String -> String))
quickCheck $ prop_functorcompose (T :: T (String -> String)) (T :: T String) (T :: T String)
prop_io :: IO ()
prop_io = do
quickCheck $ prop_functorid (T :: T (IO String))
quickCheck $ prop_functorcompose (T :: T (IO String)) (T :: T String) (T :: T String)
main = do
prop_function
prop_io
source to share
No one has answered this question yet
See similar questions:
or similar: