Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Given the Free Monad, and my Eq, Show, and Functor instances, I attempted to verify the first Functor law using QuickCheck:

data Free f a = Var a
 | Node (f (Free f a)) 

I defined the following Eq and Show instances (credit to duplode duplode for helping me out on the Eq instance:

instance (Eq (f (Free f a)), Eq a) => Eq (Free f a) where
 (==) (Var x) (Var y) = x == y
 (==) (Node fu1) (Node fu2) = fu1 == fu2
 (==) _ _ = False
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
 show (Var x) = "Var " ++ (show x)
 show (Node x) = "Node " ++ (show x)

Then, I implemented a Functor instance:

instance Functor f => Functor (Free f) where
 fmap g (Var x) = Var (g x)
 fmap g (Node x) = Node $ fmap (\y -> fmap g y) x

And now the QuickCheck work:

instance Arbitrary (Free Maybe Int) where
 arbitrary = do
 x <- arbitrary :: Gen Int
 y <- arbitrary :: Gen Int
 elements [Var x, Var y, Node (Nothing), Node (Just (Var y))] 
--fmap id = id
functor_id_law :: Free Maybe Int -> Bool
functor_id_law x = (fmap id x) == (id x)

Finally, run it in QuickCheck:

ghci> quickCheck functor_id_law 
+++ OK, passed 100 tests.

However, I haven't included other Functor's, such as [], etc. Nor have I used other types, i.e. Char, String, etc.

What's a more rigorous approach to verifying that my definition of the Free Monad's Functor instance obeys the first Functor Law?

Given the Free Monad, and my Eq, Show, and Functor instances, I attempted to verify the first Functor law using QuickCheck:

data Free f a = Var a
 | Node (f (Free f a)) 

I defined the following Eq and Show instances (credit to duplode for helping me out on the Eq instance:

instance (Eq (f (Free f a)), Eq a) => Eq (Free f a) where
 (==) (Var x) (Var y) = x == y
 (==) (Node fu1) (Node fu2) = fu1 == fu2
 (==) _ _ = False
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
 show (Var x) = "Var " ++ (show x)
 show (Node x) = "Node " ++ (show x)

Then, I implemented a Functor instance:

instance Functor f => Functor (Free f) where
 fmap g (Var x) = Var (g x)
 fmap g (Node x) = Node $ fmap (\y -> fmap g y) x

And now the QuickCheck work:

instance Arbitrary (Free Maybe Int) where
 arbitrary = do
 x <- arbitrary :: Gen Int
 y <- arbitrary :: Gen Int
 elements [Var x, Var y, Node (Nothing), Node (Just (Var y))] 
--fmap id = id
functor_id_law :: Free Maybe Int -> Bool
functor_id_law x = (fmap id x) == (id x)

Finally, run it in QuickCheck:

ghci> quickCheck functor_id_law 
+++ OK, passed 100 tests.

However, I haven't included other Functor's, such as [], etc. Nor have I used other types, i.e. Char, String, etc.

What's a more rigorous approach to verifying that my definition of the Free Monad's Functor instance obeys the first Functor Law?

Given the Free Monad, and my Eq, Show, and Functor instances, I attempted to verify the first Functor law using QuickCheck:

data Free f a = Var a
 | Node (f (Free f a)) 

I defined the following Eq and Show instances (credit to duplode for helping me out on the Eq instance:

instance (Eq (f (Free f a)), Eq a) => Eq (Free f a) where
 (==) (Var x) (Var y) = x == y
 (==) (Node fu1) (Node fu2) = fu1 == fu2
 (==) _ _ = False
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
 show (Var x) = "Var " ++ (show x)
 show (Node x) = "Node " ++ (show x)

Then, I implemented a Functor instance:

instance Functor f => Functor (Free f) where
 fmap g (Var x) = Var (g x)
 fmap g (Node x) = Node $ fmap (\y -> fmap g y) x

And now the QuickCheck work:

instance Arbitrary (Free Maybe Int) where
 arbitrary = do
 x <- arbitrary :: Gen Int
 y <- arbitrary :: Gen Int
 elements [Var x, Var y, Node (Nothing), Node (Just (Var y))] 
--fmap id = id
functor_id_law :: Free Maybe Int -> Bool
functor_id_law x = (fmap id x) == (id x)

Finally, run it in QuickCheck:

ghci> quickCheck functor_id_law 
+++ OK, passed 100 tests.

However, I haven't included other Functor's, such as [], etc. Nor have I used other types, i.e. Char, String, etc.

What's a more rigorous approach to verifying that my definition of the Free Monad's Functor instance obeys the first Functor Law?

Source Link
Kevin Meredith
  • 3.2k
  • 28
  • 52

Using QuickCheck to Verify Free Monad's Functor Instance

Given the Free Monad, and my Eq, Show, and Functor instances, I attempted to verify the first Functor law using QuickCheck:

data Free f a = Var a
 | Node (f (Free f a)) 

I defined the following Eq and Show instances (credit to duplode for helping me out on the Eq instance:

instance (Eq (f (Free f a)), Eq a) => Eq (Free f a) where
 (==) (Var x) (Var y) = x == y
 (==) (Node fu1) (Node fu2) = fu1 == fu2
 (==) _ _ = False
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
 show (Var x) = "Var " ++ (show x)
 show (Node x) = "Node " ++ (show x)

Then, I implemented a Functor instance:

instance Functor f => Functor (Free f) where
 fmap g (Var x) = Var (g x)
 fmap g (Node x) = Node $ fmap (\y -> fmap g y) x

And now the QuickCheck work:

instance Arbitrary (Free Maybe Int) where
 arbitrary = do
 x <- arbitrary :: Gen Int
 y <- arbitrary :: Gen Int
 elements [Var x, Var y, Node (Nothing), Node (Just (Var y))] 
--fmap id = id
functor_id_law :: Free Maybe Int -> Bool
functor_id_law x = (fmap id x) == (id x)

Finally, run it in QuickCheck:

ghci> quickCheck functor_id_law 
+++ OK, passed 100 tests.

However, I haven't included other Functor's, such as [], etc. Nor have I used other types, i.e. Char, String, etc.

What's a more rigorous approach to verifying that my definition of the Free Monad's Functor instance obeys the first Functor Law?

lang-hs

AltStyle によって変換されたページ (->オリジナル) /