Haskell语言学习笔记(35)Contravariant
contravariant 模块
contravariant 模块需要安装
$ cabal install contravariant
contravariant-1.4
Prelude> :m +Data.Functor.Contravariant
Prelude Data.Functor.Contravariant>
Contravariant Functor(逆变函子)
class Contravariant f where
contramap :: (a -> b) -> f b -> f a
(>$) :: b -> f b -> f a
(>$) = contramap . const
Functor(函子)类型类是协变的,因此它可以被看做 Covariant Functor (协变函子)的简写。
Functor是协变,是因为它改变的是输出端,相对于函数定义来说是正向的(positive)。
与此相对,Contravariant 类型类是逆变的,它是 Contravariant Functor (逆变函子)的简写。
Contravariant是逆变,是因为它改变的是输入端,相对于函数定义来说是反向的(negative)。
Contravariant 的法则
1. contramap id = id
2. contramap f . contramap g = contramap (g . f)
Predicate(谓词)是个Contravariant
newtype Predicate a = Predicate { getPredicate :: a -> Bool }
instance Contravariant Predicate where
contramap f g = Predicate $ getPredicate g . f
Predicate(谓词 )类型封装了一个 a -> Bool 类型的函数。
证明 Predicate 符合 Contravariant 的法则
1. contramap id = id
contramap id p
= Predicate $ getPredicate p . id
= Predicate $ getPredicate p
= p = id p
2. contramap f . contramap g = contramap (g . f)
(contramap f . contramap g) p
= contramap f (contramap g p)
= contramap f (Predicate $ getPredicate p . g)
= Predicate $ getPredicate (Predicate $ getPredicate p . g) . f
= Predicate $ (getPredicate p . g) . f
= Predicate $ getPredicate p . g . f
contramap (g . f) p
= Predicate $ getPredicate p . (g . f)
= Predicate $ getPredicate p . g . f
import Data.Functor.Contravariant
greaterThanThree :: Predicate Int
greaterThanThree = Predicate (> 3)
lengthGTThree :: Predicate [a]
lengthGTThree = contramap length greaterThanThree
englishGTThree :: Predicate Int
englishGTThree = contramap english lengthGTThree
english :: Int -> String
english 1 = "one"
english 2 = "two"
english 3 = "three"
english 4 = "four"
english 5 = "five"
english 6 = "six"
english 7 = "seven"
english 8 = "eight"
english 9 = "nine"
english 10 = "ten"
main :: IO ()
main = print $ filter (getPredicate englishGTThree) [1..10]
-- [3,4,5,7,8,9]
Comparison a 是个Contravariant
newtype Comparison a = Comparison { getComparison :: a -> a -> Ordering }
instance Contravariant Comparison where
contramap f g = Comparison $ on (getComparison g) f
Comparison a(同类比较)类型封装了一个 a -> a -> Ordering 类型的函数(比如 compare 函数)。
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
(*) `on` f = \x y -> f x * f y
证明 Comparison a 符合 Contravariant 的法则
1. contramap id = id
contramap id c
= Comparison $ on (getComparison c) id
= Comparison $ \x y -> (getComparison c) (id x) (id y)
= Comparison $ \x y -> (getComparison c) x y
= Comparison $ getComparison c
= c = id c
2. contramap f . contramap g = contramap (g . f)
(contramap f . contramap g) c
= contramap f (contramap g c)
= contramap f (Comparison $ on (getComparison c) g)
= contramap f (Comparison $ on (getComparison c) g)
= Comparison $ on (getComparison (Comparison $ on (getComparison c) g)) f
= Comparison $ on (on (getComparison c) g) f
= Comparison $ on (\x y -> (getComparison c) (g x) (g y)) f
= Comparison $ \x y -> (\x y -> (getComparison c) (g x) (g y)) (f x) (f y)
= Comparison $ \x y -> (getComparison c) (g (f x)) (g (f y))
contramap (g . f) c
= Comparison $ on (getPredicate c) (g . f)
= Comparison $ \x y (getPredicate c) ((g . f) x) ((g . f) y)
= Comparison $ \x y -> (getComparison c) (g (f x)) (g (f y))
Prelude Data.Functor.Contravariant Data.List> sortBy (getComparison $ contramap length $ Comparison compare) ["Groovy","Java","Scala"]
["Java","Scala","Groovy"]
Prelude Data.List Data.Function> sortBy (compare `on` length) ["Groovy","Java","Scala"]
["Java","Scala","Groovy"]
Prelude Data.List> sortOn length ["Groovy","Java","Scala"]
["Java","Scala","Groovy"]
Const a 是个Contravariant
newtype Const a b = Const { getConst :: a }
instance Contravariant (Const a) where
contramap _ (Const a) = Const a
Const a b 封装了一个值 a。
证明 Const a 符合 Contravariant 的法则
1. contramap id = id
contramap id (Const a) = Const a = id (Const a)
2. contramap f . contramap g = contramap (g . f)
(contramap f . contramap g) (Const a)
= contramap f (contramap g (Const a))
= contramap f (Const a)
= Const a
= contramap (g . f) (Const a)
正向与反向
a -- positive position
a -> Bool -- negative position
(a -> Bool) -> Bool -- positive position
((a -> Bool) -> Bool) -> Bool -- negative position
a -> Bool -> Bool = a -> (Bool -> Bool) -- negative position
参考链接
What is a contravariant functor?
Haskell语言学习笔记(35)Contravariant的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
- Haskell语言学习笔记(38)Lens(1)
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...
- Haskell语言学习笔记(92)HXT
HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...
随机推荐
- linux 下git使用教程
#添加所有新增文件 git add . #提交所有修改,包括删除,添加,修改 git add -A git add --all #查看状态 git status #添加一个文件 git add rea ...
- Microsoft Dynamics CRM 2011 中 SQL 语句总结
一.查询用户和团队 插入的类型:OwnerIdType 可以为用户或团队: 1.团队: select top 1 ObjectTypeCode from metadataschema.entity w ...
- 1、zookeeper集群安装
前提准备3台centos7.0虚拟机 c7003:192.168.70.103 c7004:192.168.70.104 c7005:192.168.70.105 并在三台虚拟机上配置hosts为 1 ...
- C# OracleBulkCopy 批量插入oracle数据库的方法
只有安装了oracle 11G客户端的机器上才可以用,要用到ODP.NET组件中的oracleDataAccess.DLL,命名空间引用为Oracle.DataAccess.Client; 引用:Or ...
- JS URL 使用base64加密与解密和MD5解密
JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...
- R语言学习——欧拉计划(3)Largest prime factor 求最大质因数
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...
- 开发需求:每个表dump出部分数据
mysqldump 每张数据表统一导出的N个记录数 需求: 在开发过程中,需要将生产的数据导出到本地开发环境,希望可以每个表都导出部分数据.由于生产数据表都是上千万或亿级别,直接从服务器导出数据会影响 ...
- 解决npm下载包失败的问题
在我朝,用npm直接从官方的镜像下载包,经常会出现网络超时下载失败的问题,具体原因大家都懂,我就不说了. 不过,这些都无法阻挡我们对知识的渴望,一下提供几种我在工作中的解决办法,希望能帮助你. 1.安 ...
- TensorFlow利用A3C算法训练智能体玩CartPole游戏
本教程讲解如何使用深度强化学习训练一个可以在 CartPole 游戏中获胜的模型.研究人员使用 tf.keras.OpenAI 训练了一个使用「异步优势动作评价」(Asynchronous Advan ...
- bash下. : () {} [] [[]] (())的解释
bash下有很多像{}.[]等一些符号命令,下面是我对一些常用的符号命令的学习笔记,若有错误或纰漏望各位兄弟指正. 一..(source).(点)与source命令一样,从文件中读取并执行命令,无论该 ...