haskell - 反转折叠

标签 haskell fold

假设我们认为以下是一个好主意:

data Fold x y = Fold {start :: y, step :: x -> y -> y}

fold :: Fold x y -> [x] -> y

在该方案下,length等功能或 sum可以通过调用fold来实现使用适当的 Fold对象作为论据。

现在,假设您想做一些巧妙的优化技巧。特别是,假设你想写
unFold :: ([x] -> y) -> Fold x y

统治RULES 应该相对容易。 pragma 使得 fold . unFold = id .但有趣的问题是……我们真的可以实现unFold吗? ?

显然你可以使用 RULES应用任意代码转换,无论它们是否保留代码的原始含义。但是你真的能写一个unFold吗?实际执行其类型签名所暗示的实现?

最佳答案

不,这是不可能的。证明:让

f :: [()] -> Bool
f[] = False
f[()] = False
f _ = True

首先我们必须,对于 f' = unFold f , 有 start f' = False ,因为当折叠空列表时,我们直接得到起始值。那么我们必须要求 step f' () False = False实现fold f' [()] = False .但是当现在评估 fold f' [(),()] ,我们只会接到一个电话 step f' () False ,我们必须将其定义为 False , 导致 fold f' [(),()] ≡ False , 而 f[(),()] ≡ True .所以不存在unFold f满足fold $ unFold f ≡ f . □

关于haskell - 反转折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12950063/

相关文章:

arrays - 使用减少(到 :_:) to filter adjacent equal elements

recursion - Foldr 与 Foldl(或 Foldl')的含义

Haskell:Where 与 Let

haskell - 为什么这个 Functor 实例不正确?

c++ - 如何 fold STL 容器?

python - 如何使用 Pandas Timestamp 折叠参数?

haskell - foldl' 和 foldr' 的默认定义看起来很奇怪

regex - Haskell 正则表达式性能

regex - Haskell:正则表达式和 Data.Text

haskell - 为什么 putStrLn 每次迭代都会变慢?