Add contravariant notebook
Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
This commit is contained in:
parent
cadd5d2406
commit
0fb8217770
1 changed files with 58 additions and 0 deletions
58
contravariant.ipynb
Normal file
58
contravariant.ipynb
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
Loading…
Reference in a new issue