diff options
author | Joe Zhao <ztuowen@gmail.com> | 2015-03-25 15:50:18 +0800 |
---|---|---|
committer | Joe Zhao <ztuowen@gmail.com> | 2015-03-25 15:50:18 +0800 |
commit | 1c35f8f304a349b7fa8c5eb6c6256d707f0987fd (patch) | |
tree | 18305734a1442ce0b381f0453a7519bcad90f3c9 /functor/h4.hs | |
parent | 22fc8bffd0ee10d4e7e4ad7f4c65b472fea4fb50 (diff) | |
download | typeclass-1c35f8f304a349b7fa8c5eb6c6256d707f0987fd.tar.gz typeclass-1c35f8f304a349b7fa8c5eb6c6256d707f0987fd.tar.bz2 typeclass-1c35f8f304a349b7fa8c5eb6c6256d707f0987fd.zip |
functors
Diffstat (limited to 'functor/h4.hs')
-rw-r--r-- | functor/h4.hs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/functor/h4.hs b/functor/h4.hs new file mode 100644 index 0000000..2e00031 --- /dev/null +++ b/functor/h4.hs @@ -0,0 +1,21 @@ +-- Haskell only has covariant functors of the following form: +-- class Functor f where +-- fmap:: (a->b) -> f a -> f b +-- Contra/Co-variant : +-- http://www.reddit.com/r/haskell/comments/1cht4v/what_does_coalgebra_mean_in_the_context_of/c9gzulr + +class Contravariant f where + contramap :: (a->b) -> f b -> f a + +data Cont a = Cont (a -> Int) + +instance Contravariant Cont where + contramap f (Cont x) = Cont (x.f) + +-- The following however is covariant +data H t = H1 t | H2 ((t -> Int) -> Int) + +instance Functor H where + fmap f (H1 x) = H1 (f x) + fmap f (H2 g) = H2 (\h -> g (h . f)) + |