Bird Meertens Formalismhomomorphism

是个神奇的理论啊...

homomorphism(态射)是从一个 monoid(幺半群)到一个 monoid 的线性映射。

幺半群的意思是有幺元,对乘法封闭,乘法满足左右结合律

那么 homomorphism 满足

h id= id
h (x y) = h x h y
也就是线性变换应该满足的条件

 

reduce: 也就是 foldr ,把元素从右往左用运算符计算

reduce [a,b,c,d,e,f] = (a + ( b + ( c + ( d + (e + zero))))

promotion(升格引理):

h is a homomorphism from (α, , id) to (β, , id) if and only if the following holds.

h · ⊕/ = / · h

也就是说先 reduce 再 map 等价于 先 map 再 reduce

这是两个结论:

∗ · ++ = ++ · ∗ ∗

先连接再 map f 等价于把对所有的序列都 map ( map f ) 再连接

· ++ · (/)

先连接再 op 等于先 map op 再用 op 连起来

注意:map 作用完还是 list ,而 op 作用完就是一个 β 了

Identity函数是个态射:

id = ++ / · [·]

引理: 

h is a homomorphism from the list monoid if and only if there exist f and such that

h = / · f

也就是说序列上的函数是态射当且仅当它能写成这种形式。

这个时候两个幺半群是( List , ++ , [] ) (β , (*) , id (*) )

证明很妙

=> : h= h · id = ...

<= : h · ++/ = ...

使用上文的 promotion 进行推导

其他 homomorphism :

#: compute the length of a list.
# = +/ · K1

K1是把一个元素变成1

reverse = ++˜ / · [·]

Here, x˜ y = y x

all p = / · p

some p = / · p


 All apply to 

[]o 叫做all apply to

[f,g,h]o a = [ f a , g a , h a ]

特别的 []o 为这个态射的零元(作用在任意 a 上都得到 [] )

[_] = [ id ]o

只是给它加了个壳

[[ id ]o]o a =[ [ id ]o a ] = [ [ a ] ]

(加两层

看起来有点蠢,但一会就不了

Conditonal Expressons

h x = if p x then f x else g x

h = (p -> f ,g)

这个挺好理解

h · (p f, g) = (p h · f, h · g)
(p f, g) · h = (p · h f · h, g · h)
(p f, f) = f
这是它的规则,也都挺好理解

Filter (◁)

p◁ = ++/ (p -> [id]o , [])*

(p) · ++ / = ++ / · (p)

对于 List 的 List ,连起来在 filter 等价于先 filter 再连起来

(p) · f= f ∗ ·(p · f)

先 map f 再过滤 等价于先过滤,过滤的时候判断 p f 再 map f

Cross-priduct

叉积

[a, b]X[c, d, e] = [a c, b c, a d, b d, a e, b e]

[] 是叉积的零元
xX⊕ 是一个homomorphism

叉积的 promotion

f ∗ ∗ · X++ / = X++ / · f ∗ ∗∗
(/) ∗ ·X++ / = X/ · (/) ∗ ∗
有点难理解了
首先他的作用对象是 [ [ [a] ] ] , 

即 list 的 list 的 list

X++ 作用在两个 List 的 List 上,把他们各自含有的 list 用 ++ 连接其起来

X++/ 就是对 (List 的 List) 组成的序列 用叉积和++ reduce

太抽象了,来个栗子

X++/ [  [ [ a ] , [ b ] ]  ,  [ [ c , d ] , [ e ] ]  ]  

= [ [ a , c , d ] , [ a , e ] , [ b , c , d ] , [ b , e ] ] 

这么一看就清楚些了

 cross-product : takes a list of lists and returns a list of lists of elements, one from each component.

cp [[a, b], [c], [d, e]] =  [ [ , , , , , , ] ]
cp = X++ / · ([id]o)

subs [1,2,3] =  [ [ ] , [ 1 ] , [ 2 ] , [ 3 ] , [ 1 , 2 ] [ 1 , 3 ] , [ 2 , 3 ] [ 1 , 2, 3 ] ]  

subs = X++/·([ []o , [id]o ]o)*

关于层数,可以记为x++/会把三层的降为两层

[id]o加一层,[[id]o]o加两层

++/也是降一层

(all p)◁ 

(all even )◁ [[1,2,3],[2,3],[2,4]] = [[2,4]]

(all p)◁ = ++/ (X++/ · (p->[[id]o]o,[]o)*)*

怎么理解这个呢:

首先对于每个元素 1 2 3  4 这样的

如果满足条件就 变成 [ [ 2 ] ] 否则就变成 []

然后把这些东西 X++/

这样的话只要有 [] 最终结果就是 []  

否则的话就是[ [ [ 2 , 4 ] ] ]

所以还要 reduce 

Selection Operation

↑f 

associative, commutative and idempotent
满足结合律,交换律,幂等

幂等应该是说 (x ↑f y) ↑fy = x ↑f y 

selective :

x ↑f y = x or x ↑f y = y

也就是操作的结果必是 x y 之一

f(x ↑f y) = (f x) ↑ (f y)

这个操作返回较大的值

哎讲了那么一大圈,这个就是给 x ,y 返回 f 作用后较大的那一个

那么自然 f 应该要是单射(否则可能有不同的x y 对应同一 f x)

非单射的时候可以特殊定义一下返回谁

原文地址:https://www.cnblogs.com/liankewei/p/15700818.html