Haskell - 如何将最大值(xs ++ map(x +)xs)转换为max(最大xs)(x +最大xs)

时间:2014-12-02 23:21:37

标签: haskell equational-reasoning

“在功能上与Haskell一起思考”中的一个练习是使用融合法使程序更有效。我在尝试复制答案时遇到了一些麻烦。

计算的一部分要求您通过等式推理将maximum (xs ++ map (x+) xs)转换为max (maximum xs) (x + maximum xs)

maximum被定义为foldr1 max,因为我不知道围绕foldr1的许多规则,即使是将foldr1 max (xs ++ map (x+) xs)转换为{{1}的第一部分,我仍然坚持所以这是我想要了解的第一件事。

一旦我们过去了,下一部分似乎更难,即将max (foldr1 max xs) (foldr1 max (map (x+) xs))转换为foldr1 max (map (x+) xs)。直觉上它是有道理的;如果你发现一堆数字的最大值都添加了'x',那么就像在添加'x'之前找到所有数字的最大值并在结果中添加'x'一样。

我在第二阶段找到的唯一帮助我的是this stack overflow answer 答案基本上给你(如果你假设p = q),没有像你通常用等式推理看到的那样容易理解的步骤。

那么请有人告诉我进行转换的步骤吗?

2 个答案:

答案 0 :(得分:2)

草图:

maximum (xs ++ map (x+) xs)
foldr1 max (xs ++ map (x+) xs)
foldr max (foldr1 max xs) (map (x+) xs)
foldr max (maximum xs) (map (x+) xs)
max (maximum xs) (foldr1 max (map (x+) xs))
max (maximum xs) (x + foldr1 max xs)
max (maximum xs) (x + maximum xs)

使用(可能有较弱的假设,我选择了有效的方法)

0. xs, ys are nonempty
1. f is commutative and associative
A. foldr1 f (xs ++ ys) == foldr f (foldr1 f xs) ys
B. foldr f s xs = f s (foldr1 f xs)
C. foldr1 f (map g xs) = g (foldr1 f xs)
  if f (g x) (g y) == g (f x y)

引理的证明遵循归纳法。 首先,让我们记住我们的定义:

foldr k z  []    = z
foldr k z (x:xs) = k x (foldr k z xs)

foldr1 k [z]    = z
foldr1 k (x:xs) = k x (foldr1 k xs)

map k [] = []
map k (x:xs) = k x : map k xs

然后,对于A:

To show: foldr1 f (xs ++ ys) == foldr f (foldr1 f xs) ys
Induction on xs.
Base case: xs = [x]
    foldr1 f ([x] ++ ys) == f x (foldr1 f ys)
    Induction on ys: ys = [y]
        == f x (foldr f [y]) = f x y = f y x 
        == foldr f x [y] = foldr f (foldr1 f [x]) [y]
    Step. ys = (y:yy)
        == f x (foldr1 f (y:yy)) 
        == f x (f y (foldr1 f yy))  -> associativity and commutativity
        == f y (f x (foldr1 f yy))  -> induction assumption
        == f y (foldr f x yy)
        == foldr f (foldr1 f [x]) (y:yy)
Step: xs = (x:xx)
    foldr1 f (x:xx ++ ys) = f x (foldr1 f (xx ++ yy)) 
        == f x (fold f (foldr1 f xx) yy)
        == fold f (foldr1 f xx) (x:yy)
    Induction on ys.
    Base case ys = [y]
        == f x (f y (foldr1 f xx)) == f y (f x (foldr1 f (x:xx)) 
        == fold f (foldr1 f xs) [y]
    Step. ys = (y:yy)
        == f x (f y (fold f (foldr1 f xx) yy) -> associativity, commutativity
        == f y (f x (fold f (foldr1 f xx) yy) -> induction assumption
        == f y (fold f (foldr1 f (x:xx) yy)
        == fold f (foldr1 f xs) ys

依此类推,亲自试试。

答案 1 :(得分:2)

这可以通过归纳来看出。

假设xs == []。两个表达式都是正确的,因为两者都产生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])

接下来,我们需要maximummaximum (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 maxfoldr1 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。 (那么您还需要以类似的方式覆盖否定xy的案例。)

例如,自然数为零,是自然数的任何后继数。你知道,在这里我们没有定义任何比较,没有。因此,加法,减法,最大值等属性来自函数定义

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)

所以,你知道,重要的是要知道定义,这对于证明最简单的情况很重要,对于在更复杂的情况下使用证据来处理更简单的情况很重要。