简体   繁体   English

foldr1的融合法?

[英]Fusion law for foldr1?

For foldr we have the fusion law : if f is strict, fa = b , and 对于foldr我们有融合定律 :如果f是严格的, fa = b ,和

f (gxy) = hx (fy) for all x, y , then f . foldr ga = foldr hb f (gxy) = hx (fy)表示所有x, y ,然后是f . foldr ga = foldr hb f . foldr ga = foldr hb . f . foldr ga = foldr hb

How can one discover/derive a similar law for foldr1 ? 如何为foldr1发现/推导出类似的法则? (It clearly can't even take the same form - consider the case when both sides act on [x] .) (它显然甚至不能采用相同的形式 - 考虑双方对[x]采取行动的情况。)

You can use free theorems to derive statements like the fusion law. 您可以使用自由定理来推导融合定律等语句。 The Automatic generation of free theorems does this work for you, it automatically derives the following statement if you enter foldr1 or the type (a -> a -> a) -> [a] -> a . 自由生成自由定理这对你foldr1如果输入foldr1或类型(a -> a -> a) -> [a] -> a ,它会自动导出以下语句。

If f strict and f (pxy) = q (fx) (fy)) for all x and y you have f (foldr1 pz) = foldr1 q (map fz)) . 如果f strict和f (pxy) = q (fx) (fy))对于所有xy你有f (foldr1 pz) = foldr1 q (map fz)) That is, in contrast to you statement about foldr you get an additional map f on the right hand side. 也就是说,与你关于foldr陈述相反,你会在右侧获得一个额外的map f

Also note that the free theorem for foldr is slightly more general than your fusion law and, therefore, looks quite similar to the law for foldr1 . 还要注意, foldr的自由定理比你的融合定律略foldr1 ,因此看起来与foldr1的定律非常相似。 Namely you have for strict functions g and f if g (pxy) = q (fx) (gy)) for all x and y then g (foldr pzv) = foldr q (gz) (map fv)) . 即,对于所有xy g (foldr pzv) = foldr q (gz) (map fv)) g (pxy) = q (fx) (gy))对于所有xy你具有严格函数gf ,然后g (foldr pzv) = foldr q (gz) (map fv))

I don't know if there's going to be anything satisfying for foldr1 . 我不知道是否会有任何令人满意的foldr1 [I think] It's just defined as [我认为]它只是被定义为

foldr1 f (x:xs) = foldr f x xs

let's first expand what you have above to work on the entire list, 让我们首先扩展您的上述内容,以便在整个列表中工作,

f (foldr g x xs) = foldr h (f x) xs

for foldr1, you could say, 对于foldr1,你可以说,

f (foldr1 g xs) = f (foldr g x xs)
= foldr h (f x) xs

to recondense into foldr1, you can create some imaginary function that maps f to the left element, for a result of, 为了重新融入foldr1,你可以创建一些虚构的函数,将f映射到左边的元素,结果如下:

f . foldr1 g = foldr1 h (mapfst f) where
    mapfst (x:xs) = f x : xs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM