[英]To prove equality of two function definitions inductively
How do I do the induction to establish the statement moll n = doll n
, with 我如何进行归纳以建立语句moll n = doll n
,with
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)
I tried to prove the base case for n = 0 我试图证明n = 0的基本情况
doll 0 = (d.2) = 1 = (m.1) = moll 0 , which is correct.
Now for n+1
, show that 现在为n+1
,显示出来
moll 2n = doll (n + 1)
=> doll (n + 1) = (d.2) = soll (acc + n + 1) n
But what now? 但现在呢? How can I simplify it any further? 我怎样才能进一步简化它?
You've got a mistake in your n+1
step. 你的n+1
步中有一个错误。 I suspect this is because you're new to Haskell and its precedence rules. 我怀疑这是因为你是Haskell及其优先规则的新手。
moll (n+1)
is not, as you write moll 2n
- I'm assuming that by that you mean moll (2*n)
, since moll 2n
is a haskell syntax error. moll (n+1)
不是,因为你写了moll 2n
- 我假设你的意思是moll (2*n)
,因为moll 2n
是一个haskell语法错误。
In any case, moll (n+1)
is in fact moll n + n + 1
, or, with extra parentheses added just to be explicit: 在任何情况下, moll (n+1)
实际上是moll n + n + 1
,或者,加上额外的括号只是为了明确:
(moll n) + (n + 1)
That is, you apply moll
to n
and then you add n + 1
to the result of that. 也就是说,你将moll
应用于n
,然后在结果中添加n + 1
。
From here you should be able to apply the induction hypothesis and go forward. 从这里你应该能够应用归纳假设并继续前进。
More explicitly, since you seem to still be having trouble: 更明确地说,因为你似乎仍然遇到麻烦:
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)
Now, as a lemma: 现在,作为一个引理:
sol x n == (sol 0 n) + x
This can be proved by induction on n
. 这可以通过n
的归纳来证明。 It's obviously true for n
equal to 0. n
等于0显然是正确的。
For the lemma's induction step: 对于引理的归纳步骤:
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)
That second time I used the induction hypothesis may seem a bit odd, but remember that the induction hypothesis says: 我第二次使用归纳假设似乎有点奇怪,但请记住归纳假设说:
sol x n == (sol 0 n) + x
For all x
. 对于所有x
。 Therefore, I can apply it to anything added to (sol 0 n)
, including n+1
. 因此,我可以将它应用于添加到(sol 0 n)
任何内容,包括n+1
。
Now, back to the main proof, using our lemma: 现在,回到主要证据,使用我们的引理:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.