Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import Control.Arrow (right, left)
  2. import Data.Void
  3. import Test.QuickCheck
  4. import Test.QuickCheck.Function
  5.  
  6. class Functor f => StrongSum f where
  7. distRight :: Either a (f b) -> f (Either a b)
  8.  
  9. instance StrongSum (Either a) where
  10. distRight = either (pure . Left) (fmap Right)
  11.  
  12. data WithInt a = WithInt Int a deriving (Eq, Show)
  13.  
  14. fromPair = uncurry WithInt
  15. toPair (WithInt a b) = (a, b)
  16.  
  17. instance Arbitrary a => Arbitrary (WithInt a) where
  18. arbitrary = fromPair <$> arbitrary
  19. shrink = fmap fromPair . shrink . toPair
  20.  
  21. instance Functor WithInt where
  22. fmap f (WithInt no x) = WithInt no (f x)
  23.  
  24. instance StrongSum WithInt where
  25. distRight (Left x) = WithInt 0 (Left x)
  26. distRight (Right (WithInt no x)) = WithInt (no + 1) (Right x)
  27.  
  28. instance Applicative WithInt where
  29. pure = fmap (either id absurd) . distRight . Left
  30. WithInt a f <*> WithInt b x = WithInt (a + b) (f x)
  31.  
  32. law2 :: Fun Int Int -> Either Int (WithInt Int) -> Property
  33. law2 (Fun _ f) x = lhs === rhs
  34. where
  35. lhs = distRight . left f $ x
  36. rhs = fmap (left f) . distRight $ x
  37.  
  38. -- | Doesn't hold
  39. --
  40. -- Not valid StrongSum, yet induced pure is still valid for the applicative (Writer Int)
  41. law3 :: WithInt Int -> Property
  42. law3 x = lhs === rhs
  43. where
  44. y = Right x :: Either Void (WithInt Int)
  45. lhs = either absurd (fmap Right) $ y
  46. rhs = distRight $ y
  47.  
  48. main :: IO ()
  49. main = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement