简体   繁体   English

阿格达幂定理

[英]Agda Theorems for Powers

I'm trying to prove the following: 我正在尝试证明以下内容:

1-pow : ∀ {n : ℕ} → 1 pow n ≡ 1
1-pow {zero} = refl
1-pow {suc x} = {!!}  

I'm brand new to Adga and don't even really know where to start. 我是Adga的新手,甚至都不知道从哪里开始。 Any suggestions or guidance? 有什么建议或指导吗? Obviously very easy to prove on paper but I am unsure of what to tell Agda. 显然很容易在纸上证明,但我不确定该告诉阿格达什么。

I defined my pow function as follows: 我将pow函数定义如下:

_pow_ : ℕ → ℕ → ℕ
x pow zero = 1
x pow (suc zero) = x
x pow (suc y) = x * (x pow y)

When you pattern match on n in 1-pow and find out it is zero , Agda will take a look at the definition of _pow_ and check if one of the function clauses matches. 当您在1-pow中对n进行模式匹配并发现其zero ,Agda将查看_pow_的定义,并检查其中一个功能子句是否匹配。 The first one does, so it will apply that definition and 1 pow zero becomes 1 . 第一个这样做,因此它将应用该定义,并且1 pow zero变为1 1 is obviously equal to 1 , so refl will work for the proof. 1显然等于1 ,所以refl将适用于证明。

What about the case when n was suc x ? 那当nsuc x时又如何呢? Here's the problem: Agda cannot commit to the second clause (because x could be zero ) nor the third clause (because x could be suc y for some y ). 这里的问题:阿格达不能承诺第二条(因为x可能是zero ),也不是第三个子句(因为x可能是suc y一些y )。 So you have to go one step further to make sure Agda applies the definition of _pow_ : 因此,您必须进一步确保Agda应用_pow_的定义:

1-pow : ∀ {n : ℕ} → 1 pow n ≡ 1
1-pow {zero}        = refl
1-pow {suc zero}    = {!!}
1-pow {suc (suc x)} = {!!}

Let's check out what is the type of the first hole. 让我们检查一下第一个孔的类型。 Agda tells us it is 1 ≡ 1 , so we can use refl again. 阿格达(Agda)告诉我们1 ≡ 1 ,所以我们可以再次使用refl The last one is a bit trickier, we are supposed to produce something of type 1 * 1 pow (suc x) ≡ 1 . 最后一个有点棘手,我们应该产生类型1 * 1 pow (suc x) ≡ 1 Assuming your are using the standard definition of _*_ (ie recursion on the left argument and repeated addition on the left side, such as the one in the standard library), this should reduce to 1 pow (suc x) + 0 ≡ 1 . 假设您正在使用_*_的标准定义(即,左侧参数上的递归和左侧的重复加法,例如标准库中的一个),则应减小为1 pow (suc x) + 0 ≡ 1 Induction hypothesis (that is, 1-pow applied to suc x ) tells us that 1 pow (suc x) ≡ 1 . 归纳假设(即,对suc x施加1-pow )告诉我们1 pow (suc x) ≡ 1

So we are almost there, but we don't know that n + 0 ≡ n (that's because addition is defined by recursion on the left argument, so we can't simplify this expression). 所以我们几乎到了,但是我们不知道n + 0 ≡ n (这是因为加法是通过对左参数的递归定义的,因此我们无法简化该表达式)。 One option is to prove this fact, which I leave as an exercise. 一种选择是证明这一事实,我留作练习。 Here's a hint, though: you might find this function useful. 不过,这里有个提示:您可能会发现此功能很有用。

cong : ∀ {a b} {A : Set a} {B : Set b}
       (f : A → B) {x y} → x ≡ y → f x ≡ f y
cong f refl = refl

It's already part of the Relation.Binary.PropositionalEquality module, so you don't need to define it yourself. 它已经是Relation.Binary.PropositionalEquality模块的一部分,因此您无需自己定义它。

So, to recap: we know that n + 0 ≡ n and 1 pow (suc x) ≡ 1 and we need 1 pow (suc x) + 0 ≡ 1 . 因此,回顾一下:我们知道n + 0 ≡ n1 pow (suc x) ≡ 1 ,我们需要1 pow (suc x) + 0 ≡ 1 These two facts fit together quite nicely - the equality is transitive, so we should be able to merge 1 pow (suc x) + 0 ≡ 1 pow (suc x) and 1 pow (suc x) ≡ 1 into one proof and indeed, this is the case: 这两个事实非常吻合-相等是可传递的,因此我们应该能够将1 pow (suc x) + 0 ≡ 1 pow (suc x)1 pow (suc x) ≡ 1合并为一个证明,实际上,是这样的:

1-pow {suc (suc x)} = trans (+0 (1 pow suc x)) (1-pow {suc x})

And that's it! 就是这样!


Let me mention few other approaches. 让我提及其他一些方法。

The whole proof could also be done using a proof that 1 * x ≡ x , though this is hardly different from what we did before. 整个证明也可以使用证明1 * x ≡ x来完成,尽管这与我们之前所做的几乎没有什么不同。

You could simplify _pow_ to: 您可以将_pow_简化为:

_pow_ : ℕ → ℕ → ℕ
x pow zero    = 1
x pow (suc y) = x * (x pow y)

This is slightly more convenient to work with. 使用起来稍微方便些。 The proof would be changed accordingly (ie it wouldn't have the second clause of the original proof). 证明将进行相应的更改(即,它将没有原始证明的第二子句)。

And lastly, you could do this: 最后,您可以执行以下操作:

1-pow : ∀ {n : ℕ} → 1 pow n ≡ 1
1-pow {zero}        = refl
1-pow {suc zero}    = refl
1-pow {suc (suc x)} = cong (λ x → x + 0) (1-pow {suc x})

Try to figure out why that works! 尝试找出原因。 If you have any problems, let me know in the comments and I'll help you. 如果您有任何问题,请在评论中让我知道,我们将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM