haskell-notebooks/contravariant.ipynb
Sanchayan Maity 0fb8217770 Add contravariant notebook
Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
2020-03-21 17:53:55 +05:30

58 lines
1.3 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contravariant Functors"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"{-# LANGUAGE KindSignatures #-}\n",
"\n",
"-- (.) :: (b -> c) -> (a -> b) -> a -> c\n",
"-- contraComp :: (a -> b) -> (b -> c) -> a -> c\n",
"\n",
"-- (.) f g x = f (g x)\n",
"-- contraComp f g x = g (f x)\n",
"\n",
"-- instance Functor (a ->) where\n",
"-- fmap = (.)\n",
"\n",
"-- instance Contravariant (-> z) where\n",
"-- contramap = flip (.)\n",
"\n",
"newtype Predicate a = Predicate { getPredicate :: a -> Bool }\n",
"\n",
"newtype Op b a = Op { getOp :: a -> b }\n",
" \n",
"class Contravariant (f :: * -> *) where\n",
" contramap :: (a -> b) -> f b -> f a\n",
" \n",
"instance Contravariant (Op a) where\n",
" contramap f g = Op $ let bToa = getOp g in bToa . f"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"codemirror_mode": "ihaskell",
"file_extension": ".hs",
"name": "haskell",
"pygments_lexer": "Haskell",
"version": "8.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}