Skip to content

Instantly share code, notes, and snippets.

@folivetti
Created January 21, 2020 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folivetti/a0630e84087d7174cc69fc8c6a95bed8 to your computer and use it in GitHub Desktop.
Save folivetti/a0630e84087d7174cc69fc8c6a95bed8 to your computer and use it in GitHub Desktop.
module Main where
newtype Identity a = Id a deriving Show
data DoubleF a = DoubleF a a deriving Show
instance Functor Identity where
fmap f (Id a) = Id (f a)
instance Functor DoubleF where
fmap f (DoubleF x y) = DoubleF (f x) (f y)
-- it is isomorphic to
-- diag :: a -> (a, a)
-- diag x = (x, x)
diag :: Identity a -> DoubleF a
diag (Id x) = DoubleF x x
x :: Identity Int
x = Id 10
-- Part (a)
-- (diag . fmap f) x == (fmap f . diag) x
-- diag $ fmap f x == fmap f (diag x)
-- diag $ (f x) == fmap f (x,x)
-- (f x, f x) == (f x, f x)
main = do
print $ (diag . fmap show) x
print $ (fmap show . diag) x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment