繁体   English   中英

证明流的平等

[英]Proving equality of streams

我有一个数据类型

data N a = N a [N a]

玫瑰树和应用实例

instance Applicative N where
 pure a = N a (repeat (pure a))
 (N f xs) <*> (N a ys) = N (f a) (zipWith (<*>) xs ys)

并需要证明适用法律。 然而, 纯粹创造无限深,无限分枝的树木。 因此,例如,在证明同态定律

pure f <*> pure a = pure (f a)

我认为这证明了平等

zipWith (<*>) (repeat (pure f)) (repeat (pure a)) = repeat (pure (f a))

通过近似(或采取)引理将起作用。 然而,我的尝试导致归纳步骤中的“恶性循环”。 特别是减少

approx (n + 1) (zipWith (<*>) (repeat (pure f)) (repeat (pure a))

(pure f <*> pure a) : approx n (repeat (pure (f a)))

其中approx是近似函数。 如果没有明确的共同证据,我怎样才能证明这种平等?

我会使用展开的通用属性(因为重复和适当的不合格的拉链都展开)。 在我的博客上有一个相关的讨论。 但您可能也喜欢Ralf Hinze关于ICFP2008 (以及随后的JFP论文)的独特定点的论文。

(只是检查一下:你所有的玫瑰树都是无限宽而且无限深?我猜这些法律不会另有规定。)

以下是我认为有用并且保持在程序语法和等式推理层面的一些草图。

基本的直觉是, repeat x推理要比推理流(更糟糕的是,列表)更容易。

uncons (repeat x) = (x, repeat x)

zipWithAp xss yss = 
    let (x,xs) = uncons xss
        (y,ys) = uncons yss
    in (x <*> y) : zipWithAp xs ys

-- provide arguments to zipWithAp
zipWithAp (repeat x) (repeat y) = 
    let (x',xs) = uncons (repeat x)
        (y',ys) = uncons (repeat y)
    in (x' <*> y') : zipWithAp xs ys

-- substitute definition of uncons...
zipWithAp (repeat x) (repeat y) = 
    let (x,repeat x) = uncons (repeat x)
        (y,repeat y) = uncons (repeat y)
    in (x <*> y) : zipWithAp (repeat x) (repeat y)

-- remove now extraneous let clause
zipWithAp (repeat x) (repeat y) = (x <*> y) : zipWithAp (repeat x) (repeat y)

-- unfold identity by one step
zipWithAp (repeat x) (repeat y) = (x <*> y) : (x <*> y) : zipWithAp (repeat x) (repeat y)

-- (co-)inductive step
zipWithAp (repeat x) (repeat y) = repeat (x <*> y)

你为什么需要共同诱导? 只是归纳。

pure f <*> pure a = pure (f a)

也可以写(你需要证明左右相等)

N f [(pure f)] <*> N a [(pure a)] = N (f a) [(pure (f a))]

这允许你一次关闭一个学期。 这会给你感应。

暂无
暂无

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

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