Skip to content

Instantly share code, notes, and snippets.

@L7R7
Created February 17, 2021 16:46
Show Gist options
  • Select an option

  • Save L7R7/4c50a7531280cb091df966436898e928 to your computer and use it in GitHub Desktop.

Select an option

Save L7R7/4c50a7531280cb091df966436898e928 to your computer and use it in GitHub Desktop.
My attempt on solving the exercises in https://cvlad.info/posts/2020-01-22-profunctor.html
class Profunctor p where
{-# MINIMAL dimap | lmap, rmap #-}
dimap :: (c -> a) -> (b -> d) -> p a b -> p c d
dimap ca bd = rmap bd . lmap ca
lmap :: (c -> a) -> p a b -> p c b
lmap ca = dimap ca id
rmap :: (b -> c) -> p a b -> p a c
rmap bc = dimap id bc
mapInput :: (input -> out) -> (newInput -> input) -> (newInput -> out)
mapInput = (.)
instance Profunctor (->) where
rmap = (.)
lmap = flip (.)
dimap ca bd ab = bd . ab . ca
data Sum f g a b = L (f a b) | R (g a b)
instance (Profunctor f, Profunctor g) => Profunctor (Sum f g) where
lmap ca (L fab) = L (lmap ca fab)
lmap ca (R gab) = R (lmap ca gab)
rmap bc (L fab) = L (rmap bc fab)
rmap bc (R gab) = R (rmap bc gab)
dimap ca bd (L fab) = L (dimap ca bd fab)
dimap ca bd (R gab) = R (dimap ca bd gab)
newtype Product f g a b = Product (f a b, g a b)
instance (Profunctor f, Profunctor g) => Profunctor (Product f g) where
lmap ca (Product (fab, gab)) = Product (lmap ca fab, lmap ca gab)
rmap bc (Product (fab, gab)) = Product (rmap bc fab, rmap bc gab)
dimap ca bd (Product (fab, gab)) = Product (dimap ca bd fab, dimap ca bd gab)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment