Skip to content

Instantly share code, notes, and snippets.

@jrvieira
Last active January 24, 2021 21:16
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 jrvieira/d7ce271dfbfd092004a67362a9ff7a8c to your computer and use it in GitHub Desktop.
Save jrvieira/d7ce271dfbfd092004a67362a9ff7a8c to your computer and use it in GitHub Desktop.
main :: IO ()
main = do
print $ fn (co length (F (:"yz"))) 'x'
print $ fn (cn ((:"yx") <?> C (F length))) 'x'
print $ fn (cn (filter (< 0) <?> C (F length))) [-1..7]
-- predicate
print $ p (filter (< 0) <?> P null) [-1..7]
class Covariant f where -- Functor
co :: (a -> b) -> f a -> f b -- fmap
class Contravariant f where
ct :: (a -> b) -> f b -> f a
infixl 4 <?>
(<?>) :: Contravariant f => (a -> b) -> f b -> f a
(<?>) = ct
--
data F a b = F { fn :: a -> b }
newtype C b a = C { cn :: F a b }
instance Covariant (F a) where
co g (F f) = F (g . f)
instance Contravariant (C a) where
ct g (C (F f)) = C (F (f . g))
-- Predicate
data P a = P { p :: a -> Bool }
instance Contravariant P where
ct g (P f) = P (f . g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment