简体   繁体   English

我怎样才能说服阿格达我的职能具有一定的价值?

[英]How can I convince Agda that my function has a certain value?

I'm used to writing human proofs in mathematics, but I'm very new to writing Agda. 我曾经在数学上写过人类证明,但是我对写Agda很陌生。 The following is a toy example of something that I can't figure out how to prove with Agda. 以下是一个玩具示例,说明我无法弄清楚如何使用Agda进行证明。

Informally, I want to write a function f which takes a natural number x and a pair of naturals. 非正式地,我想编写一个函数f,该函数采用一个自然数x和一对自然数。 If the first element in the pair equals x, return the second element of the pair. 如果该对中的第一个元素等于x,则返回该对中的第二个元素。 Otherwise, return 0. 否则,返回0。

Here are my definitions for natural number equality: 这是我对自然数相等的定义:

data N : Set where
  zero : N
  s : N → N

data _≡_ {X : Set} : X → X → Set where
  refl : (x : X) → (x ≡ x)

data _≢_ : N → N → Set where
  <   : {n : N} → (zero ≢ (s n))
  >   : {n : N} → ((s n) ≢ zero)
  rec : {n m : N} → (n ≢ m) → ((s n) ≢ (s m))

data _=?_ (n m : N) : Set where
  true  : (n ≡ m) → (n =? m)
  false : (n ≢ m) → (n =? m)

equal? : (n m : N) → (n =? m)
equal? zero zero = true (refl zero)
equal? zero (s _) = false <
equal? (s _) zero = false >
equal? (s n) (s m) with (equal? n m)
... | (true (refl a)) = (true (refl (s a)))
... | (false p) = (false (rec p))

and here is the function. 这是功能。

data Npair : Set where
  pair : (n m : N) → Npair

f : N → Npair → N
f a (pair b c) with equal? a b
... | (true (refl _)) = c
... | (false _) = zero

I cannot prove 我不能证明

lemma : (x y : N) → (y ≡ (f x (pair x y)))

because when I try to introduce the refl constructor in the definition, it complains that 因为当我尝试在定义中引入refl构造函数时,它抱怨

y != f x (pair x y) | equal? x x of type N

What do I have to change in order to prove this lemma? 为了证明这一引理,我必须更改什么?

In lemma , you need to pattern match on equal? xx lemma ,您需要在equal? xx上进行模式匹配equal? xx equal? xx , because f matches on that as well, and you can't reason about f 's output until you do the same match. equal? xx ,因为f与此匹配,并且只有进行相同的匹配,您才能推断f的输出。 However, you get two cases for equal? xx 但是,您得到equal? xx两种情况equal? xx equal? xx : equal? xx

lemma : (x y : N) → (y ≡ (f x (pair x y)))
lemma x y with equal? x x 
... | true (refl _) = refl _
... | false _       = ?

Of this, the second case is not possible. 其中,第二种情况是不可能的。 To rule it out, you need to prove ∀ n → equal? nn ≡ true (refl _) 为了排除它,您需要证明∀ n → equal? nn ≡ true (refl _) ∀ n → equal? nn ≡ true (refl _) : ∀ n → equal? nn ≡ true (refl _)

equal?-true : ∀ n → equal? n n ≡ true (refl _)
equal?-true zero = refl _
equal?-true (s n) with equal? n n | equal?-true n
... | true (refl _) | q = refl _
... | false x       | ()

lemma : (x y : N) → (y ≡ (f x (pair x y)))
lemma x y with equal? x x | equal?-true x
... | true (refl _) | _ = refl _
... | false _       | ()

However, you don't need to do extra work if you define inequality just as the negation of equality, because then x ≢ x immediately implies . 但是,如果您将不等式定义为等式的否定,则不需要做额外的工作,因为x ≢ x立即意味着

data ⊥ : Set where

⊥-elim : ⊥ → {A : Set} → A
⊥-elim ()

_≢_ = λ {A : Set}(x y : A) → x ≡ y → ⊥

data _=?_ (n m : N) : Set where
  true  : (n ≡ m) → (n =? m)
  false : (n ≢ m) → (n =? m)

equal? : ∀ n m → n =? m
equal? zero zero   = true (refl zero)
equal? zero (s m)  = false (λ ())
equal? (s n) zero  = false (λ ())
equal? (s n) (s m) with equal? n m
... | true  (refl _) = true (refl _)
... | false p        = false λ {(refl _) → p (refl n)}

data Npair : Set where
  pair : (n m : N) → Npair

f : N → Npair → N
f a (pair b c) with equal? a b
... | (true (refl _)) = c
... | (false _)       = zero

lemma : (x y : N) → (y ≡ (f x (pair x y)))
lemma x y with equal? x x
... | true (refl .x) = refl y
... | false p        = ⊥-elim (p (refl _))

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

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