以归纳方式证明两个函数定义的相等性

时间:2016-01-25 13:38:14

标签: haskell induction equational-reasoning

如何使用

进行归纳以建立语句moll n = doll n
moll 0 = 1                               --(m.1)
moll n = moll ( n-1) + n                 --(m.2)

doll n = sol 0 n                         --(d.1)
 where
  sol acc 0 = acc +1                     --(d.2)
  sol acc n = sol ( acc + n) (n-1) -- ?    (d.2)

我试图证明n = 0的基本情况

doll 0 = (d.2) = 1 = (m.1) = moll 0 , which is correct.

现在为n+1,显示

moll 2n = doll (n + 1)

=> doll (n + 1) = (d.2) = soll (acc + n + 1) n

但现在呢?我怎样才能进一步简化它?

1 个答案:

答案 0 :(得分:1)

您的n+1步骤中出现了错误。我怀疑这是因为你是Haskell及其优先规则的新手。

moll (n+1)不是,当你写moll 2n时 - 我假设你的意思是moll (2*n),因为moll 2n是一个haskell语法错误。< / p>

无论如何,moll (n+1)实际上是moll n + n + 1,或者添加了额外的括号只是为了明确:

(moll n) + (n + 1)

也就是说,您将moll应用于n,然后将n + 1添加到该结果中。

从这里你应该能够应用归纳假设并继续前进。

更明确地说,因为你似乎仍然遇到麻烦:

moll (n+1) == (moll n) + (n + 1)       (by m.2)
           == (doll n) + (n + 1)       (by induction hypot.)
           == (sol 0 n) + (n + 1)      (by d.1)

现在,作为一个引理:

sol x n == (sol 0 n) + x

这可以通过n的归纳来证明。 n等于0显然是正确的。

对于引理的诱导步骤:

sol x (n+1) == (sol (x + (n+1)) n)       (By d.2, for (n+1) > 0)
            == (sol 0 n) + (x + (n+1))   (By the induction hypot.)
            == (sol 0 n) + (n+1) + x     (This is just math; re-arranging)
            == ((sol 0 n) + (n+1)) + x
            == (sol (n+1) n) + x         (By the induction hypot. again)
            == (sol 0 (n+1)) + x         (By d.2 again)

我第二次使用归纳假设似乎有点奇怪,但请记住归纳假设说:

 sol x n == (sol 0 n) + x

适用于所有x 。因此,我可以将其应用于添加到(sol 0 n)的任何内容,包括n+1

现在,回到主要证据,使用我们的引理:

moll (n+1) == (sol 0 n) + (n + 1)      (we had this before)
           == sol (n+1) n              (by our lemma)
           == sol 0 (n+1)              (by d.2)
           == doll (n+1)               (by d.1)