haskell-notebooks/contravariant.ipynb

59 lines
1.3 KiB
Text
Raw Normal View History

{
"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
}