Haskell语言学习笔记(27)Endo, Dual, Foldable
Endo Monoid
newtype Endo a = Endo { appEndo :: a -> a }
instance Monoid (Endo a) where
mempty = Endo id
Endo f `mappend` Endo g = Endo (f . g)
Endo 是个 newtype,也就是对现有类型的封装。
Endo a 封装的是一个自反射的函数,即 a->a。通过 appEndo 字段可以取出这个函数。
Endo a 在结合时结合两个函数,因此它本质上是对函数合成运算符 (.) 的封装。
Endo a 是一个幺半群。这是因为自反射函数在函数合成时满足结合律。
Dual Monoid
newtype Dual a = Dual { getDual :: a }
instance Monoid a => Monoid (Dual a) where
mempty = Dual mempty
Dual x `mappend` Dual y = Dual (y `mappend` x)
Dual 是个 newtype,也就是对现有类型的封装。
Dual a 封装的是一个值,即 a。通过 getDual 字段可以取出这个值。
Dual a 是一个幺半群,前提是 a 是个幺半群。
Dual a 在结合时交换两个操作数的值,因此它本质上是对 flip 函数的封装。
证明 Dual a 满足结合律。
(Dual x <> Dual y) <> Dual z
= Dual (y <> x) <> Dual z
= Dual (z <> (y <> x))
Dual x <> (Dual y <> Dual z)
= Dual x <> Dual (z <> y)
= Dual ((z <> y) <> x)
由于 a 是 Monoid 类型, 满足结合律
所以 (z <> y) <> x = z <> (y <> x)
=> Dual (z <> (y <> x)) = Dual ((z <> y) <> x)
=> Dual x <> (Dual y <> Dual z) = (Dual x <> Dual y) <> Dual z
Foldable 的法则
class Foldable t where
fold :: Monoid m => t m -> m
fold = foldMap id
foldMap :: Monoid m => (a -> m) -> t a -> m
foldMap f = foldr (mappend . f) mempty
foldr :: (a -> b -> b) -> b -> t a -> b
foldr f z t = appEndo (foldMap (Endo #. f) t) z
foldl :: (b -> a -> b) -> b -> t a -> b
foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
Foldable 类型类的实例都必须符合以下法则。
1. foldr f z t = appEndo (foldMap (Endo . f) t ) z
2. foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
3. fold = foldMap id
[] 是个 Foldable
instance Foldable [] where
foldl = List.foldl
foldr = List.foldr
证明 Foldable 类型类的实例 [] 符合 Foldable 的法则
1. foldr f z t = appEndo (foldMap (Endo . f) t ) z
foldr f z t
= List.foldr f z [x1, x2, ..., xn]
= x1 `f` (x2 `f` (...(xn `f` z)...))
appEndo (foldMap (Endo . f) t ) z
= appEndo (foldr (mappend . (Endo . f)) mempty [x1, x2, ..., xn]) z
= appEndo (foldr (mappend . (Endo . f)) (Endo id) [x1, x2, ..., xn]) z
= appEndo ( (mappend . (Endo . f)) x1 ( (mappend . (Endo . f)) x2 ( ... ( (mappend . (Endo . f)) xn (Endo id) ) ... ) ) ) z
= appEndo ( (Endo . (x1 `f`)) <> ( (Endo . (x2 `f`)) <> ( ... <> ( (Endo . (f . id)) xn ) ... ) ) ) z
= appEndo ( Endo . ((x1 `f`) . (x2 `f`) . ... . (xn `f`)) ) $ z
= (x1 `f`) . (x2 `f`) . ... . (xn `f`) $ z
= x1 `f` (x2 `f` (...(xn `f` z)...))
2. foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
foldl f z t
= List.foldl f z [x1, x2, ..., xn]
= (...((z `f` x1) `f` x2)...) `f` xn
appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
= appEndo (getDual (foldr (mappend . (Dual . Endo . flip f)) mempty [x1, x2, ..., xn])) z
= appEndo (getDual (foldr (mappend . (Dual . Endo . flip f)) (Dual . Endo . id) [x1, x2, ..., xn])) z
= appEndo ( getDual ( (Dual . Endo . flip f) x1 <> ( (Dual . Endo . flip f) x2 <> ( ... <> ( (Dual . Endo . (flip f . id)) xn ) ... ) ) ) ) z
= appEndo ( getDual ( (Dual . Endo . (`f` x1)) <> ( (Dual . Endo . (`f` x2)) <> ( ... <> ( (Dual . Endo . (`f` xn)) ) ... ) ) ) ) z
= appEndo ( getDual ( (Dual . Endo . ((`f` xn) . ... . (`f` x2) . (`f` x1)) ) ) z
= (`f` xn) . ... . (`f` x2) . (`f` x1) $ z
= (...((z `f` x1) `f` x2)...) `f` xn
build 与 (++)
build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build g = g (:) []
(++) xs ys = foldr (:) ys xs
foldMap 与 concatMap
foldMap :: Monoid m => (a -> m) -> t a -> m
foldMap f = foldr (mappend . f) mempty
当 m := [b] 时 foldMap := concatMap
concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
concatMap f xs = build (\c n -> foldr (\x b -> foldr c b (f x)) n xs)
concatMap f xs
= build (\c n -> foldr (\x b -> foldr c b (f x)) n xs)
= (\c n -> foldr (\x b -> foldr c b (f x)) n xs) (:) []
= foldr (\x b -> foldr (:) b (f x)) [] xs
foldMap f xs
= foldr (mappend . f) mempty xs
= foldr ((++) . f) [] xs
= foldr (\x b -> foldr (:) b (f x)) [] xs
fold 与 concat
fold :: Monoid m => t m -> m
fold = foldMap id
当 m := [a] 时 fold := concat
concat :: Foldable t => t [a] -> [a]
concat xs = build (\c n -> foldr (\x y -> foldr c y x) n xs)
concat xs
= build (\c n -> foldr (\x y -> foldr c y x) n xs)
= (\c n -> foldr (\x y -> foldr c y x) n xs) (:) xs
= foldr (\x y -> foldr (:) y x) [] xs
fold xs
= foldMap id xs
= foldr (mappend . id) mempty xs
= foldr (++) [] xs
= foldr (\x y -> foldr (:) y x) [] xs
reverse foldl foldr
用 foldl foldr 实现 reverse
How can I write reverse by foldr efficiently in Haskell?
reverse' xs
= foldl (flip (:)) [] xs
= appEndo . getDual $ foldMap (Dual . Endo . flip (flip (:))) xs $ []
= appEndo . getDual $ foldr (mappend . Dual . Endo . (:)) mempty xs $ []
= appEndo . foldr (flip mappend . Endo . (:)) mempty $ xs $ []
= foldr (flip (.) . (:)) id xs []
= flip (foldr (flip (.) . (:)) id) [] xs
reverse' = flip (foldr (flip (.) . (:)) id) []
将 reverse' 的推导过程一般化,可以得到
foldl f
= flip (foldr (flip (.) . flip f) id)
下面证明
foldl f z xs
= foldr (flip (.) . flip f) id xs z
= foldr step id xs z
where step x g a = g (f a x)
step x g a = g (f a x)
step x g a = g ((flip f) x a)
step x g a = g . (flip f) x $ a
step x g = g . (flip f) x
step x g = (.) g ((flip f) x)
step x g = (flip (.)) ((flip f) x) g
step x = (flip (.)) ((flip f) x)
step x = flip (.) . flip f $ x
step = flip (.) . flip f
Haskell语言学习笔记(27)Endo, Dual, Foldable的更多相关文章
- 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语言学习笔记(58)Bifoldable
Bifoldable class Bifoldable p where bifold :: Monoid m => p m m -> m bifold = bifoldMap id id ...
- Haskell语言学习笔记(84)Concurrent
Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...
- 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 ...
随机推荐
- tyvj1172自然数拆分
题目:http://www.joyoi.cn/problem/tyvj-1172 非常水的完全背包.物品就是1~n这n个数. 第6行有橙色的警告:this decimal constant is un ...
- opencv中的缩放函数
图像处理里面缩放操作是比较常见的: 最近邻插值:类似简单映射的处理方式,目标图像w1,h1,原始图像w0,h0,则在目标图像上的点(x,y)的像素点实际对应原始图上(x*w0/w1,y*h0/h1)的 ...
- ios获取安装的app
http://www.iphonedevsdk.com/forum/iphone-sdk-development/22289-possible-retrieve-these-information.h ...
- mysql视图 新手的问答
☺ζั͡ޓއއއ๓º♥双٩(•(365335093) 16:03:02 创建的视图能保存多长时间,保存在哪啊 上天&宠儿(961431958) 16:08:59 数据库 上天&宠儿(9 ...
- python处理excel(二):写
代码参考自zhoujie.函数接口可参考该blog. 基本的write函数接口很简单: 新建一个excel文件 file = xlwt.Workbook() (注意这里的Workbook首字母是大写) ...
- bzoj3685 普通veb树
Description 设计数据结构支持: 1 x 若x不存在,插入x 2 x 若x存在,删除x 3 输出当前最小值,若不存在输出-1 4 输出当前最大值,若不存在输出-1 5 x ...
- Eclipse: the import java.util cannot be resolved
the import java.util cannot be resolved 导入JRE System Library. 右键项目 Build Path Configure Build Path.. ...
- cpu监控之三:mpstat命令
mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具.报告CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有CP ...
- COMMON INTERVIEW QUESTIONS
1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...
- JAVA URI URL 区别
String urlString = "http://192.168.21.77:8080/swp/mainPage?aa=11&bb%3D22"; URI uri = U ...