以下代码如何执行所需的功能?

时间:2015-02-21 08:56:06

标签: functional-programming coq theorem-proving

Lemma odd_pred2n: forall n : nat, Even.odd n -> {p : nat | n = pred    (Div2.double p)}.

Lemma even_2n : forall n, even n -> {p : nat | n = double p}.

Lemma even_odd_exists_dec:forall n, {p : nat | n = Div2.double p} + {p : nat | n = pred (Div2.double p)}.
Proof. 
 intro n.
 destruct (Even.even_odd_dec n) as [H_parity|H_parity];
 [ left; apply (Div2.even_2n _ H_parity)
 | right; apply (odd_pred2n _ H_parity)].
Defined.

Definition nat_to_Z_i (n:nat) := 
  match even_odd_exists_dec n with
  | inl s => let (k, _) := s in Z_of_nat k
  | inr s => let (k, _) := s in Zopp (Z_of_nat k)
  end.

此定义如何定义从natZ的注射?什么是inlinr以及let在这做什么?

1 个答案:

答案 0 :(得分:3)

odd_pred2n采用自然的证明它是奇怪的,并且返回一半(向下舍入)这个奇怪的自然的继承者,并且还证明了double的前身是原始的奇数自然。

even_2n采用自然的方式证明它是均匀的,并且返回一半甚至是自然的,并证明双重是原始的甚至是自然的。

even_odd_exists_dec决定一个自然是偶数还是奇数,返回一半或一半的继承者,并证明double或double的前身是原始的。

nat_to_Z_i执行以下地图。

0 -> 0
1 -> - 1
2 -> 1
3 -> - 2
4 -> 2
5 -> - 3
6 -> 3
...

inlinrsum的构造函数。

Print sum.

如果s : {x : T | P x},则s = (x, H) x : TH : P xlet (x, y) := z in f x y相当于:

 
match z with
| (x, y) => f x y
end.

match even_odd_exists_dec n with
| inl s => let (k, _) := s in Z_of_nat k
| inr s => let (k, _) := s in Zopp (Z_of_nat k)
end.

相当于:

match even_odd_exists_dec n with
| inl (k, _) => Z_of_nat k
| inr (k, _) => Zopp (Z_of_nat k)
end.