这可以通过归纳看出。
假设,xs == []
。两个表达式都是正确的,因为两者都 yield error
。
认为,xs == [y]
maximum([y]++map(x+)[y]) == -- by definition of map
== maximum([y]++[x+y])
-- by definition of ++
== maximum([y,x+y])
-- by definition of maximum
== foldr1 max [y,x+y]
-- by definition of foldr1
== max y (foldr1 max [x+y])
-- by definition of foldr1
== max y (x+y)
-- by definition of foldr1 and maximum [y]
== max (maximum [y]) (x+maximum [y])
接下来,我们需要一个交换性证明maximum
:maximum (xs++(y:ys)) == max y (maximum (xs++ys))
- 你会注意到这是需要的,如果你跳过这个证明并直接去证明maximum (y:ys ++ map(x+)(y:ys))
- 一步需要(x+y)
从列表的中间移动ys++(x+y):map(x+)ys
。
假设xs==[]
:
maximum ([]++(y:ys)) == maximum (y:ys)
-- by definition of foldr1 and maximum
== max y (maximum ys)
== max y (maximum ([]++ys))
假设xs==x:xx
:
maximum(x:xx++(y:ys)) == maximum (x:(xx++(y:ys)))
-- by definition of foldr1 and maximum
== max x (maximum (xx++(y:ys)))
-- by induction
== max x (max y (maximum (xx++ys)))
-- by commutativity of max, max a (max b c) == max b (max a c)
== max y (max x (maximum (xx++ys)))
-- by definition of foldr1 and maximum
== max y (maximum (x:(xx++ys)))
-- by definition of ++
== max y (maximum ((x:xx) ++ ys))
好的,现在回到证明最初的陈述。现在,假设xs == y:ys
maximum (y:ys ++ map(x+)(y:ys)) ==
-- by definition of map
== maximum(y:ys ++ (x+y):map(x+)ys)
-- by definition of foldr1 and maximum
== max y (maximum(ys ++ (x+y):map(x+)ys)
-- by commutativity of maximum
== max y (max (x+y) (maximum (ys++map(x+)ys)))
-- by induction, (maximum (ys++map(x+)ys)) == max (maximum ys) (x+maximum ys))
== max y (max (x+y)
(max (maximum ys) (x+maximum ys)))
-- by commutativity of max (ie max a (max b c) == max b (max a c))
== max y (max (maximum ys)
(max (x+y) (x+maximum ys)))
-- by associativity of max (is max a (max b c) == max (max a b) c)
== max (max y (maximum ys))
(max (x+y) (x+maximum ys)))
-- by definition of max, max (x+y) (x+z) == x+(max y z)
== max (max y (maximum ys))
(x + max y (maximum ys)))
-- by definition of foldr1 and maximum
== max (maximum (y:ys)) (x + maximum (y:ys))
由于您还询问了归纳以及如何通过归纳来证明某件事,所以这里还有更多。
你可以看到一些步骤是“根据定义”的——通过查看函数的编写方式,我们知道它们是正确的。例如,maximum = foldr1 max
对于foldr1 f (x:xs) = f x $ foldr1 f xs
非空xs
. max y z
其他一些东西的定义不太清楚——max
没有显示的定义;然而,可以通过归纳证明max (x+y)(x+z) == x+max y z
. 在这里,我们将从 的定义开始max 0 y == y
,然后如何解决max
更大的问题x
。(那么您还需要以类似的方式涵盖负面案例x
。y
)
例如,自然数是零和自然数的任何后继。你看,这里我们没有定义任何比较,什么都没有。因此,加法、减法、最大值等的性质源于函数的定义:
data Nat = Z | S Nat -- zero and any successor of a natural number
(+) :: Nat -> Nat -> Nat -- addition is...
Z + x = x -- adding zero is neutral
(S x) + y = S (x + y) -- recursive definition of (1+x)+y = 1+(x+y)
-- here unwittingly we introduced associativity of addition:
-- (x+y)+z=x+(y+z)
-- so, let's see the simplest case:
-- x == Z
-- (Z+y)+z == -- by definition, Z+y=y -- see the first line of (+)
-- == y+z
-- == Z+(y+z) -- by definition, Z+(y+z)=(y+z)
--
-- ok, now try x == S m
-- (S m + y) + z == -- by definition, (S m)+y=S(m+y) -- see the second line of(+)
-- == S (m+y) + z
-- == S ((m+y)+z) -- see the second line of (+)
-- - S (m+y) + z = S ((m+y)+z)
-- == S (m+(y+z)) -- by induction, the simpler
-- case of (m+y)+z=m+(y+z)
-- is known to be true
-- == (S m)+(y+z) -- by definition, see second line of (+)
-- proven
然后,因为我们还没有 Nats 的比较,所以必须以max
特定的方式定义。
max :: Nat -> Nat -> Nat
max Z y = y -- we know Z is not the max
max x Z = x -- and the other way around, too
-- this inadvertently introduced commutativity of max already
max (S x) (S y) = S (max x y) -- this inadvertently introduces the law
-- that max (x+y) (x+z) == x + (max y z)
假设,我们要证明后者。认为x == Z
max (Z+y) (Z+z) == -- by definition of (+)
== max y z
== Z + (max y z) -- by definition of (+)
好的,现在假设x == S m
max ((S m) + y) ((S m)+z) == -- by definition of (+)
== max (S(m+y)) (S(m+z))
-- by definition of max
== S (max (m+y) (m+z))
-- by induction
== S (m+(max y z))
-- by definition of (+)
== (S m)+(max y z)
所以,你看,了解定义很重要,证明最简单的情况很重要,并且在稍微复杂的情况下使用简单情况的证明也很重要。