BMF学习笔记

前言:这应该是下半学期(甚至可以说整个学期,因为上半期haskell感觉在铺垫)计算概论课主要想讲的内容了

Bird Meertens Formalism (BMF)

应该是一种计算理论?

是建立在半群(幺半群)上的理论

沿袭FP的思想,把算法描述成函数组合的形式(在这里可以看出FP的高妙之处,就是用函数组合的方式,使得我们的算法可以利用等式进行推导)

而怎么推导呢,就需要利用一些理论

引入了同态概念之后,又多了很多推导的等式

同态(Homomorphism):简单来说,就是在两个群之间映射,满足如果第一个群里有a\(\oplus\)b=c,那么第二个群里有f(a)\(\otimes\)f(b) = f(c)

而我们熟知的List,就是一个幺半群,对它可以衍生出很多同态函数


一些记号和约定:

f*表示map,对List里所有元素施加f

⊕/表示reduce,把List里的元素按顺序从左向右用⊕连起来计算

⊕→/e or ⊕←/e 表示foldl/foldr,把List里的元素从e开始复合从左向右/从右向左运算,e可以不给出直接算

⊕→//e or ⊕←//e表示accumulation,就是把foldl/foldr计算的每一个值都记下来,按原顺序顺塞到一个List里


一些Tips

无疑map和reduce是两个Homomorphism

而所有homo都可以表示成reduce和map的复合,证明很有意思

即:h = ⊕/ · f∗

显然
(⊕→/) · ([a]++) = ⊕→/a
(⊕←/) · (++[a]) = ⊕←/a


一些法则

map distributivity
(f · g)∗ = (f∗) · (g∗)


promotion

h是同态等价于

h · ⊕/ = ⊗/ · h∗

于是我们有下面的promotion

map && reduce promotion

f∗ · ++/ = ++/ · (f∗)∗
⊕/ · ++/ = ⊕/ · (⊕/)∗


Accumulation Lemma

(⊕→//e) = (⊕→/e) ∗ ·inits
(⊕→//) = (⊕→/) ∗ ·inits+


Horner's rule

如果两个同态 ⊗ 对 ⊕ 满足右结合律:
(a ⊕ b) ⊗ c = (a ⊗ c) ⊕ (b ⊗ c)

那么
⊕/ · ⊗/∗ ·tails = ⊙→/e
where
e = id⊗
a ⊙ b = (a ⊗ b) ⊕ e


Segment Decomposition

如果
S = ⊕/ · f ∗ ·segs
T = ⊕/ · f ∗ ·tails

并且T = h · ⊙→/e

那么S = ⊕/ · h∗ ·⊙→//e


Existence Lemma

h是同态等价于

对于任意v,w,x,y有

h v = h x ∧ h w = h y ⇒ h (v ++ w) = h (x ++ y)

必要性显然,充分性则是利用h的弱反函数(h.g.h=h)来构造出我们要的运算⊗


一些经典推导和证明

The Maximum Segment Sum (mss) Problem

inits,所有前缀的集合,inits = (++→//[]) · [·]∗

tails,所有后缀的集合,tails = (++←//[]) · [·]∗

segs = ++ / · tails ∗ ·inits

即所有非空子串

mss
= { definition of mss }
↑ / · +/ ∗ ·segs
= { definition of segs }
↑ / · +/ ∗ · ++ / · tails ∗ ·inits
= { map and reduce promotion }
↑ / · (↑ / · +/ ∗ ·tails) ∗ ·inits
= { Horner’s rule with a ⊙ b = (a + b) ↑ 0 }
↑ / · ⊙→/ 0 ∗ ·inits
= { accumulation lemma }
↑ / · ⊙→// 0


The Third Homomorphism Theorem

(就不翻译了

A function f can be described as a foldl and a foldr
h = ⊕←/ e
h = ⊗→/ e

that is,
h ([a] ++ x) = a ⊕ h x
h (x ++ [a]) = h x ⊗ a

iff there exists an associative operator ⊙ such that
h(x ++ y) = h x ⊙ h y

Proof. Let h v = h x and h w = h y. Then:
h (v ++ w)
= { h = ⊕←/ e }
⊕←/ e(v ++ w)
= { property of right-to-left reduction }
⊕←/ ⊕←/ ewv
= { h w = h y }
⊕←/ ⊕←/ eyv
= { property of right-to-left reduction }
⊕←/ e(v ++ y)
= { h = ⊕←/ e }
h (v ++ y)
= { symmetrically, since h = ⊗→/ e }
h (x ++ y)

By the Existence Lemma, h is a homomorphism.


还有一些定义和结论,就不一一列举,都可以用上面的rules推知


Fusion && Tupling

Fusion:

就是下面这个式子

f(a ⊕ r) = a ⊗ f r
f · foldr (⊕) e = foldr (⊗) (f e)

如果我们能找到⊗,让f(a ⊕ r) = a ⊗ f r,那么我们就可以直接把f施加到e上,然后用⊗串起来


Tupling:

这个有点像在递归时维护一些辅助参数

fi [] = ei
fi (a : x) = a ⊕i (f1 x, f2 x, . . . , fn x)
f = [[(e1, . . . , en),(⊕1, . . . , ⊕n)]]

那么我们有

Mutu-Tupling Lemma

[[(e1, e2, . . . , en),(⊕1, ⊕2, . . . , ⊕n)]]
= foldr (⊕) (e1, e2, . . . , en)
where a ⊕ r = (a ⊕1 r, a ⊕2 r, . . . , a ⊕n r)

Foldr-Tupling Lemma

(foldr (⊕1) e1 x, foldr (⊕2) e2 x) = foldr (⊕) (e1, e2) x
where a ⊕ (r1,r2) = (a ⊕1 r1, a ⊕2 r2)

理解起来挺容易的

posted @ 2022-12-20 22:01  deaf  阅读(285)  评论(0编辑  收藏  举报