等式推理很有趣!如果你自己做一些证明,你会很快掌握它的诀窍。我热烈推荐ch。Graham Hutton 的Programming in Haskell中的第 13 节进行了简要介绍。
无论如何,您可以证明,对于所有等式和有限的(请参阅汤姆·埃利斯的回答) xs
,ys
并且z
,
elem z (xs ++ ys) == elem z xs || elem z ys
通过归纳列表xs
。为此,您需要使用 、 和 的定义,++
并使用关联的事实:||
elem
||
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
False || b = b
True || _ = True
elem _ [] = False
elem x (y:ys) = x == y || elem x ys
基本情况
让ys
是 一个 类型的 值Eq a => [a]
和z
一个 类型 的 值Eq a => a
; 那么我们有
elem z ([] ++ ys)
= {applying ++}
elem z ys
= {unapplying ||}
False || elem z ys
= {unapplying elem}
elem z [] || elem z ys
归纳案例
让xs
,ys
是类型的值Eq a => [a]
,和x
,z
类型的值Eq a => a
。假设(归纳假设)
elem z (xs ++ ys) == elem z xs || elem z ys
然后我们有
elem z ((x:xs) ++ ys)
= {applying ++)
elem z (x : (xs ++ ys))
= {applying elem}
z == x || elem (xs ++ ys)
= {induction hypothesis}
z == x || (elem z xs || elem z ys)
= {associativity of ||}
(z == x || elem z xs) || elem z ys
= {unapplying elem}
elem z (x:xs) || elem z ys
(QED)