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的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(58)Bifoldable

    Bifoldable class Bifoldable p where bifold :: Monoid m => p m m -> m bifold = bifoldMap id id ...

  7. Haskell语言学习笔记(84)Concurrent

    Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...

  8. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

  9. Haskell语言学习笔记(44)Lens(2)

    自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

随机推荐

  1. drbd脑裂问题处理

    http://blog.csdn.net/heianemo/article/details/8439813 split brain实际上是指在某种情况下,造成drbd的两个节点断开了连接,都以prim ...

  2. php 实现欧拉函数Euler

    欧拉函数ph(n)的意思是所有小于n且与n互质的个数.比如说ph(10) = 4{1,3,7,9与10互质} 代码如下: function Euler($x) { $res = $x; $now = ...

  3. FineUI 选中多行获取行ID

    http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=2506&page=1 /// <summary>       ...

  4. 使用redis防止商品超发

    redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用.redis中key的原子自增incrby和判断key不存在再写入的setnx方法,可以有效的防止超发. 下面使用两个不同的方 ...

  5. 如何在eclipse中安装angularjs插件

    1,首先,在eclipse中使用angularjs的时候,由于没有相应的提示,导致在开发的时候给我们带来了很多的不便,需要在这上面耗费一些时间.那么这时候我们都在想可不可以让eclipse也和一些前端 ...

  6. Android Drawable Mipmap Vector使用及Vector兼容

    原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...

  7. lwip调试记录

    1. lwip在调用tcp_write后不会立即发送数据,而会等到tcp_slow_tmr中再发送.如需立即发送,可以在tcp_write后调用tcp_output.lwip的这种处理方式对连续调用t ...

  8. scala学习之路一

    所谓学习,那么首先就先简单介绍一下scala吧 1.scala的介绍 Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性. Scal ...

  9. HDU 2066 一个人的旅行 (Dijkstra算法)

    一个人的旅行 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  10. java发送http请求,内容为xml格式&&传统URI类请求

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...