Lists使用结构化的数据
数值序对
此声明可以读作:「构造数值序对的唯一一种方法,就是将构造子 pair
应用到两个 nat 类型的参数上。」
以下函数通过模式匹配,提取序对中第一个和第二个分量。
Definition fst (p : natprod) : nat :=
match p with
| pair x y ⇒ x
end.
Definition snd (p : natprod) : nat :=
match p with
| pair x y ⇒ y
end.
Compute (fst (pair 3 5)).
(* ===> 3 *)
match p with
| pair x y ⇒ x
end.
Definition snd (p : natprod) : nat :=
match p with
| pair x y ⇒ y
end.
Compute (fst (pair 3 5)).
(* ===> 3 *)
由于序对十分常用,不妨以标准的数学记法 (x,y) 取代 pair x y。
通过 Notation 向 Coq 声明该记法:
新的记法在表达式和模式匹配中均可使用。
Compute (fst (3,5)).
Definition fst' (p : natprod) : nat :=
match p with
| (x,y) ⇒ x
end.
Definition snd' (p : natprod) : nat :=
match p with
| (x,y) ⇒ y
end.
Definition swap_pair (p : natprod) : natprod :=
match p with
| (x,y) ⇒ (y,x)
end.
Definition fst' (p : natprod) : nat :=
match p with
| (x,y) ⇒ x
end.
Definition snd' (p : natprod) : nat :=
match p with
| (x,y) ⇒ y
end.
Definition swap_pair (p : natprod) : natprod :=
match p with
| (x,y) ⇒ (y,x)
end.
注意,不要将对序对进行【模式匹配|Pattern Matching】(带括号:(x, y))
与我们之前见过的【多重模式|Multiple Pattern】语法(不带括号:x, y)
混淆。上面的示例展示了如何对元素为 x 和 y 的序对进行模式匹配;
而 Basics 中 minus 的定义,则是对值 n 和 m 分别匹配:
Fixpoint minus (n m : nat) : nat :=
match n, m with
| O , _ ⇒ O
| S _ , O ⇒ n
| S n', S m' ⇒ minus n' m'
end. 这种区别很小,但仍然值得理解:它们并不相同。例如,以下定义是 【非良构|Ill-Formed】的:
(* 无法用多重模式匹配序对: *)
Definition bad_fst (p : natprod) : nat :=
match p with
| x, y ⇒ x
end.
(* 无法用序对模式匹配多个值: *)
Definition bad_minus (n m : nat) : nat :=
match n, m with
| (O , _ ) ⇒ O
| (S _ , O ) ⇒ n
| (S n', S m') ⇒ bad_minus n' m'
end.
如果我们以稍显古怪的方式陈述序对的性质,那么有时只需
reflexivity 及其内建的简化即可完成证明:
Fixpoint minus (n m : nat) : nat :=
match n, m with
| O , _ ⇒ O
| S _ , O ⇒ n
| S n', S m' ⇒ minus n' m'
end. 这种区别很小,但仍然值得理解:它们并不相同。例如,以下定义是 【非良构|Ill-Formed】的:
(* 无法用多重模式匹配序对: *)
Definition bad_fst (p : natprod) : nat :=
match p with
| x, y ⇒ x
end.
(* 无法用序对模式匹配多个值: *)
Definition bad_minus (n m : nat) : nat :=
match n, m with
| (O , _ ) ⇒ O
| (S _ , O ) ⇒ n
| (S n', S m') ⇒ bad_minus n' m'
end.
Theorem surjective_pairing' : ∀ (n m : nat),
(n,m) = (fst (n,m), snd (n,m)).
Proof.
reflexivity. Qed.
(n,m) = (fst (n,m), snd (n,m)).
Proof.
reflexivity. Qed.
但如果我们想要用一种更为自然的方式来陈述此引理的话,只用
reflexivity 还不够:
Theorem surjective_pairing_stuck : ∀ (p : natprod),
p = (fst p, snd p).
Proof.
simpl. (* 啥也没有归约! *)
Abort.
p = (fst p, snd p).
Proof.
simpl. (* 啥也没有归约! *)
Abort.
我们还需要向 Rocq 展示 p 的具体结构,这样 simpl 才能对
fst 和 snd 做模式匹配。 destruct 即可。
Theorem surjective_pairing : ∀ (p : natprod),
p = (fst p, snd p).
Proof.
intros p. destruct p as [n m]. simpl. reflexivity. Qed.
p = (fst p, snd p).
Proof.
intros p. destruct p as [n m]. simpl. reflexivity. Qed.
Theorem snd_fst_is_swap : ∀ (p : natprod),
(snd p, fst p) = swap_pair p.
Proof.
(* 在这里填写 *) Admitted.
☐
(snd p, fst p) = swap_pair p.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem fst_swap_is_snd : ∀ (p : natprod),
fst (swap_pair p) = snd p.
Proof.
(* 在这里填写 *) Admitted.
☐
fst (swap_pair p) = snd p.
Proof.
(* 在这里填写 *) Admitted.
☐
例如,这是一个三元素列表:
和序对一样,使用熟悉的编程记法来表示列表会更方便些。
以下两个声明能让我们用 :: 作为中缀的 cons 操作符,
用方括号作为构造列表的【外缀|Outfix】记法。
Notation "x :: l" := (cons x l)
(at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
(at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
我们不必完全理解这些声明,但如果你感兴趣的话,我会大致说明发生了什么。
注解 "right associativity" 告诉 Rocq 当遇到多个 ::
时如何给表达式加括号,如此一来下面三个声明做的就是同一件事:
Definition mylist1 := 1 :: (2 :: (3 :: nil)).
Definition mylist2 := 1 :: 2 :: 3 :: nil.
Definition mylist3 := [1;2;3].
Definition mylist2 := 1 :: 2 :: 3 :: nil.
Definition mylist3 := [1;2;3].
"at level 60" 告诉 Rocq 当遇到表达式和其它中缀运算符时应该如何加括号。
例如,我们已经为 plus 函数定义了 + 中缀符号,它的优先级是 50:
Notation "x + y" := (plus x y) (at level 50, left associativity). + 会比 :: 结合的更紧密,所以 1 + 2 :: [3] 会被解析成 (1 + 2) :: [3] 而非 1 + (2 :: [3])。
(当你在 .v 文件中看到「1 + (2 :: [3])」这样的记法时可能会很疑惑。
最里面那个括住了 3 的方括号,表明它是一个列表。
而外层的方括号则是用来指示「rocq doc」这部分要被显示为代码而非普通的文本;
在生成的 HTML 文件中,外层的方括号是看不到的。)
上面的第二和第三个 Notation 声明引入了标准的方括号记法来表示列表。
第三个声明的右半部分展示了在 Rocq 中声明 n 元记法的语法,
以及将它们翻译成嵌套的二元构造子序列的方法。
再次提醒,如果这些解析细节让你感到困惑,也请不用担心:
本课程中需要的所有符号都会为你定义。
接下来我们看几个用来构造和操作列表的函数。
第一个是 repeat 函数,它接受一个数字 n 和一个 count,
返回一个长度为 count、每个元素都是 n 的列表。
Notation "x + y" := (plus x y) (at level 50, left associativity). + 会比 :: 结合的更紧密,所以 1 + 2 :: [3] 会被解析成 (1 + 2) :: [3] 而非 1 + (2 :: [3])。
Repeat 重复
Fixpoint repeat (n count : nat) : natlist :=
match count with
| O ⇒ nil
| S count' ⇒ n :: (repeat n count')
end.
match count with
| O ⇒ nil
| S count' ⇒ n :: (repeat n count')
end.
Fixpoint app (l1 l2 : natlist) : natlist :=
match l1 with
| nil ⇒ l2
| h :: t ⇒ h :: (app t l2)
end.
match l1 with
| nil ⇒ l2
| h :: t ⇒ h :: (app t l2)
end.
由于下文中 app 随处可见,不妨将其定义为中缀运算符。
Notation "x ++ y" := (app x y)
(right associativity, at level 60).
Example test_app1: [1;2;3] ++ [4;5] = [1;2;3;4;5].
Proof. reflexivity. Qed.
Example test_app2: nil ++ [4;5] = [4;5].
Proof. reflexivity. Qed.
Example test_app3: [1;2;3] ++ nil = [1;2;3].
Proof. reflexivity. Qed.
(right associativity, at level 60).
Example test_app1: [1;2;3] ++ [4;5] = [1;2;3;4;5].
Proof. reflexivity. Qed.
Example test_app2: nil ++ [4;5] = [4;5].
Proof. reflexivity. Qed.
Example test_app3: [1;2;3] ++ nil = [1;2;3].
Proof. reflexivity. Qed.
表头与表尾
Definition hd (default : nat) (l : natlist) : nat :=
match l with
| nil ⇒ default
| h :: t ⇒ h
end.
Definition tl (l : natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ t
end.
Example test_hd1: hd 0 [1;2;3] = 1.
Proof. reflexivity. Qed.
Example test_hd2: hd 0 [] = 0.
Proof. reflexivity. Qed.
Example test_tl: tl [1;2;3] = [2;3].
Proof. reflexivity. Qed.
match l with
| nil ⇒ default
| h :: t ⇒ h
end.
Definition tl (l : natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ t
end.
Example test_hd1: hd 0 [1;2;3] = 1.
Proof. reflexivity. Qed.
Example test_hd2: hd 0 [] = 0.
Proof. reflexivity. Qed.
Example test_tl: tl [1;2;3] = [2;3].
Proof. reflexivity. Qed.
Exercises
练习: 2 stars,标准,特别有用 (list_funs)
完成以下 nonzeros、oddmembers 和 countoddmembers 的定义, 你可以查看测试函数来理解这些函数应该做什么。
Fixpoint nonzeros (l:natlist) : natlist
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_nonzeros:
nonzeros [0;1;0;2;3;0;0] = [1;2;3].
(* 在这里填写 *) Admitted.
Fixpoint oddmembers (l:natlist) : natlist
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_oddmembers:
oddmembers [0;1;0;2;3;0;0] = [1;3].
(* 在这里填写 *) Admitted.
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_nonzeros:
nonzeros [0;1;0;2;3;0;0] = [1;2;3].
(* 在这里填写 *) Admitted.
Fixpoint oddmembers (l:natlist) : natlist
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_oddmembers:
oddmembers [0;1;0;2;3;0;0] = [1;3].
(* 在这里填写 *) Admitted.
对于下一个问题 countoddmembers,我们给出了一个使用关键字
Definition 而非 Fixpoint 的头部。以这种方式陈述问题,
是为了鼓励你用已经定义好的函数来实现它,而非编写自己的递归定义。
Definition countoddmembers (l:natlist) : nat
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_countoddmembers1:
countoddmembers [1;0;3;1;4;5] = 4.
(* 在这里填写 *) Admitted.
Example test_countoddmembers2:
countoddmembers [0;2;4] = 0.
(* 在这里填写 *) Admitted.
Example test_countoddmembers3:
countoddmembers nil = 0.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_countoddmembers1:
countoddmembers [1;0;3;1;4;5] = 4.
(* 在这里填写 *) Admitted.
Example test_countoddmembers2:
countoddmembers [0;2;4] = 0.
(* 在这里填写 *) Admitted.
Example test_countoddmembers3:
countoddmembers nil = 0.
(* 在这里填写 *) Admitted.
☐
练习: 3 stars,高级 (alternate)
完成以下 alternate 的定义,它从两个列表中交替地取出元素并合并成 一个交错的列表。更多具体示例见后面的测试。
Fixpoint alternate (l1 l2 : natlist) : natlist
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_alternate1:
alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
(* 在这里填写 *) Admitted.
Example test_alternate2:
alternate [1] [4;5;6] = [1;4;5;6].
(* 在这里填写 *) Admitted.
Example test_alternate3:
alternate [1;2;3] [4] = [1;4;2;3].
(* 在这里填写 *) Admitted.
Example test_alternate4:
alternate [] [20;30] = [20;30].
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_alternate1:
alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
(* 在这里填写 *) Admitted.
Example test_alternate2:
alternate [1] [4;5;6] = [1;4;5;6].
(* 在这里填写 *) Admitted.
Example test_alternate3:
alternate [1;2;3] [4] = [1;4;2;3].
(* 在这里填写 *) Admitted.
Example test_alternate4:
alternate [] [20;30] = [20;30].
(* 在这里填写 *) Admitted.
☐
这些命题都能通过 reflexivity 来证明。
Example test_count1: count 1 [1;2;3;1;4;1] = 3.
(* 在这里填写 *) Admitted.
Example test_count2: count 6 [1;2;3;1;4;1] = 0.
(* 在这里填写 *) Admitted.
(* 在这里填写 *) Admitted.
Example test_count2: count 6 [1;2;3;1;4;1] = 0.
(* 在这里填写 *) Admitted.
【多重集|Multiset】sum 类似于【集合|Set】 union:
sum a b 包含 a 的所有元素以及 b 的所有元素。
(数学家通常会以稍有不同的方式在多重集上定义【并集|Union】,
即使用 max 而非 sum。这就是我们不将此操作称为 union 的原因。)
我们有意给出了一个没有为参数指定显式名称的头部。请在不修改头部的情况下,
用一个已经定义好的函数来实现 sum。
Definition sum : bag → bag → bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_sum1: count 1 (sum [1;2;3] [1;4;1]) = 3.
(* 在这里填写 *) Admitted.
Definition add (v : nat) (s : bag) : bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_add1: count 1 (add 1 [1;4;1]) = 3.
(* 在这里填写 *) Admitted.
Example test_add2: count 5 (add 1 [1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Fixpoint member (v : nat) (s : bag) : bool
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_member1: member 1 [1;4;1] = true.
(* 在这里填写 *) Admitted.
Example test_member2: member 2 [1;4;1] = false.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_sum1: count 1 (sum [1;2;3] [1;4;1]) = 3.
(* 在这里填写 *) Admitted.
Definition add (v : nat) (s : bag) : bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_add1: count 1 (add 1 [1;4;1]) = 3.
(* 在这里填写 *) Admitted.
Example test_add2: count 5 (add 1 [1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Fixpoint member (v : nat) (s : bag) : bool
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_member1: member 1 [1;4;1] = true.
(* 在这里填写 *) Admitted.
Example test_member2: member 2 [1;4;1] = false.
(* 在这里填写 *) Admitted.
☐
练习: 3 stars,标准,可选 (bag_more_functions)
你可以把下面这些与 bag 有关的函数当作额外的练习。
Fixpoint remove_one (v : nat) (s : bag) : bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_remove_one1:
count 5 (remove_one 5 [2;1;5;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_one2:
count 5 (remove_one 5 [2;1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_one3:
count 4 (remove_one 5 [2;1;4;5;1;4]) = 2.
(* 在这里填写 *) Admitted.
Example test_remove_one4:
count 5 (remove_one 5 [2;1;5;4;5;1;4]) = 1.
(* 在这里填写 *) Admitted.
Fixpoint remove_all (v:nat) (s:bag) : bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_remove_all1: count 5 (remove_all 5 [2;1;5;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_all2: count 5 (remove_all 5 [2;1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_all3: count 4 (remove_all 5 [2;1;4;5;1;4]) = 2.
(* 在这里填写 *) Admitted.
Example test_remove_all4: count 5 (remove_all 5 [2;1;5;4;5;1;4;5;1;4]) = 0.
(* 在这里填写 *) Admitted.
Fixpoint included (s1 : bag) (s2 : bag) : bool
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_included1: included [1;2] [2;1;4;1] = true.
(* 在这里填写 *) Admitted.
Example test_included2: included [1;2;2] [2;1;4;1] = false.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_remove_one1:
count 5 (remove_one 5 [2;1;5;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_one2:
count 5 (remove_one 5 [2;1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_one3:
count 4 (remove_one 5 [2;1;4;5;1;4]) = 2.
(* 在这里填写 *) Admitted.
Example test_remove_one4:
count 5 (remove_one 5 [2;1;5;4;5;1;4]) = 1.
(* 在这里填写 *) Admitted.
Fixpoint remove_all (v:nat) (s:bag) : bag
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_remove_all1: count 5 (remove_all 5 [2;1;5;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_all2: count 5 (remove_all 5 [2;1;4;1]) = 0.
(* 在这里填写 *) Admitted.
Example test_remove_all3: count 4 (remove_all 5 [2;1;4;5;1;4]) = 2.
(* 在这里填写 *) Admitted.
Example test_remove_all4: count 5 (remove_all 5 [2;1;5;4;5;1;4;5;1;4]) = 0.
(* 在这里填写 *) Admitted.
Fixpoint included (s1 : bag) (s2 : bag) : bool
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_included1: included [1;2] [2;1;4;1] = true.
(* 在这里填写 *) Admitted.
Example test_included2: included [1;2;2] [2;1;4;1] = false.
(* 在这里填写 *) Admitted.
☐
(*
Theorem add_inc_count : ...
Proof.
...
Qed.
*)
(* 不要修改下面这行: *)
Definition manual_grade_for_add_inc_count : option (nat×string) := None.
☐
Theorem add_inc_count : ...
Proof.
...
Qed.
*)
(* 不要修改下面这行: *)
Definition manual_grade_for_add_inc_count : option (nat×string) := None.
☐
...因为 [] 被代入 app 定义中的「被检项」
(即 match 要检查其值的表达式),整个 match 得以简化。
和数字一样,有时对一个列表做分类讨论(是否为空)是非常有用的。
Theorem tl_length_pred : ∀ l:natlist,
pred (length l) = length (tl l).
Proof.
intros l. destruct l as [| n l'].
- (* l = nil *)
reflexivity.
- (* l = cons n l' *)
reflexivity. Qed.
pred (length l) = length (tl l).
Proof.
intros l. destruct l as [| n l'].
- (* l = nil *)
reflexivity.
- (* l = cons n l' *)
reflexivity. Qed.
在这里 nil 的情况能够工作是因为我们定义了 tl nil = nil。
而 destruct 策略中 as 注解引入的两个名字,n 和 l',
分别对应了 cons 构造子的两个参数(正在构造的列表的头和尾)。
然而一般来说,许多关于列表的有趣定理都需要用到归纳法来证明。
接下来我们就会看到证明的方法。
(一点点说教:随着不断地深入,若你只是【阅读】证明的话,
并不会获得什么特别有用的东西。
搞清楚每一个细节非常重要。你应该在
Rocq 中单步执行这些证明并思考每一步在整个证明中的作用,
否则练习题将毫无用处。
啰嗦完毕。)
对列表进行归纳
- 首先,证明当 l 为 nil 时 P l 成立。
- 然后,证明当 l 为 cons n l' 时 P l 成立。 其中 n 是某个自然数,l' 是某个更小的列表,并假设 P l' 成立。
Theorem app_assoc : ∀ l1 l2 l3 : natlist,
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof.
intros l1 l2 l3. induction l1 as [| n l1' IHl1'].
- (* l1 = nil *)
reflexivity.
- (* l1 = cons n l1' *)
simpl. rewrite → IHl1'. reflexivity. Qed.
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof.
intros l1 l2 l3. induction l1 as [| n l1' IHl1'].
- (* l1 = nil *)
reflexivity.
- (* l1 = cons n l1' *)
simpl. rewrite → IHl1'. reflexivity. Qed.
注意,和归纳自然数时一样,此处 induction 策略的 as... 子句
为「l1 由构造子 cons 构造而来」这一情况时出现的
「更小的列表」和归纳假设取了名字。
再次强调,如果你把 Rocq 的证明当做静态的文档,那么可能不会有特别多的收获。
如果你通过交互式的 Rocq 会话来阅读证明,就能看到当前的目标和语境,
而这些状态在你阅读写下来的脚本时是不可见的。
所以一份用自然语言写成的证明(写给人看的)
需要包含更多的提示,来帮助读者理解当前的状态,
比如第二种情况下的归纳假设到底是什么。
【定理】:对所有的列表 l1, l2, 和 l3,
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3)。
Proof: By induction on l1.
在某些情况下,为了用归纳法证明一个陈述,有必要对它进行泛化。
直观地说,原因在于更一般的陈述也会产生更一般(更强)的归纳假设。
如果你在证明中卡住了,退后一步看看能否证明一个更强的陈述,可能会有所帮助。
- 首先, 假设 l1 = []。我们必须证明:
([] ++ l2) ++ l3 = [] ++ (l2 ++ l3), 这可以通过展开 ++ 的定义得到。 - 然后, 设 l1 = n::l1',有:
(l1' ++ l2) ++ l3 = l1' ++ (l2 ++ l3) (归纳假设)。我们必须证明:
((n :: l1') ++ l2) ++ l3 = (n :: l1') ++ (l2 ++ l3). 根据 ++ 的定义, 上式等价于:
n :: ((l1' ++ l2) ++ l3) = n :: (l1' ++ (l2 ++ l3)), 该式可通过我们的归纳假设立即证得。 ☐
泛化陈述
Theorem repeat_double_firsttry : ∀ c n: nat,
repeat n c ++ repeat n c = repeat n (c + c).
Proof.
intros c. induction c as [| c' IHc'].
- (* c = 0 *)
intros n. simpl. reflexivity.
- (* c = S c' *)
intros n. simpl.
(* 现在我们似乎卡住了。IH 不能用来改写 repeat n (c' + S c'):
它只适用于 repeat n (c' + c')。如果这里的 IH 更宽泛一些
(例如,如果它适用于任意第二个加数),证明就能通过。 *)
Abort.
repeat n c ++ repeat n c = repeat n (c + c).
Proof.
intros c. induction c as [| c' IHc'].
- (* c = 0 *)
intros n. simpl. reflexivity.
- (* c = S c' *)
intros n. simpl.
(* 现在我们似乎卡住了。IH 不能用来改写 repeat n (c' + S c'):
它只适用于 repeat n (c' + c')。如果这里的 IH 更宽泛一些
(例如,如果它适用于任意第二个加数),证明就能通过。 *)
Abort.
为了得到一个更一般的归纳假设,我们可以像下面这样泛化该陈述:
Theorem repeat_plus: ∀ c1 c2 n: nat,
repeat n c1 ++ repeat n c2 = repeat n (c1 + c2).
Proof.
intros c1 c2 n.
induction c1 as [| c1' IHc1'].
- simpl. reflexivity.
- simpl.
rewrite <- IHc1'.
reflexivity.
Qed.
repeat n c1 ++ repeat n c2 = repeat n (c1 + c2).
Proof.
intros c1 c2 n.
induction c1 as [| c1' IHc1'].
- simpl. reflexivity.
- simpl.
rewrite <- IHc1'.
reflexivity.
Qed.
Fixpoint rev (l:natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ rev t ++ [h]
end.
Example test_rev1: rev [1;2;3] = [3;2;1].
Proof. reflexivity. Qed.
Example test_rev2: rev nil = nil.
Proof. reflexivity. Qed.
match l with
| nil ⇒ nil
| h :: t ⇒ rev t ++ [h]
end.
Example test_rev1: rev [1;2;3] = [3;2;1].
Proof. reflexivity. Qed.
Example test_rev2: rev nil = nil.
Proof. reflexivity. Qed.
为了比目前所见的证明多一点挑战性,
我们来证明反转一个列表不会改变它的长度。
我们的首次尝试在后继这一分支上卡住了...
Theorem rev_length_firsttry : ∀ l : natlist,
length (rev l) = length l.
Proof.
intros l. induction l as [| n l' IHl'].
- (* l = nil *)
reflexivity.
- (* l = n :: l' *)
(* 这种情况比较棘手。我们从一般的化简开始。 *)
simpl.
(* 现在我们好像卡住了:目标是要证明涉及 ++ 的等式,
但是我们在语境和全局环境下并没有任何有用的等式!
通过用 IH 来重写目标,我们可以推进一点... *)
rewrite <- IHl'.
(* ...但也仅此而已。 *)
Abort.
length (rev l) = length l.
Proof.
intros l. induction l as [| n l' IHl'].
- (* l = nil *)
reflexivity.
- (* l = n :: l' *)
(* 这种情况比较棘手。我们从一般的化简开始。 *)
simpl.
(* 现在我们好像卡住了:目标是要证明涉及 ++ 的等式,
但是我们在语境和全局环境下并没有任何有用的等式!
通过用 IH 来重写目标,我们可以推进一点... *)
rewrite <- IHl'.
(* ...但也仅此而已。 *)
Abort.
推进证明的第一次尝试,是直接证明我们此刻缺少的那个陈述。
但这次尝试会失败,因为归纳假设还不够一般。
Theorem app_rev_length_S_firsttry: ∀ l n,
length (rev l ++ [n]) = S (length (rev l)).
Proof.
intros l. induction l as [| m l' IHl'].
- (* l = *)
intros n. simpl. reflexivity.
- (* l = m:: l' *)
intros n. simpl.
(* IHl' 不适用。 *)
Abort.
length (rev l ++ [n]) = S (length (rev l)).
Proof.
intros l. induction l as [| m l' IHl'].
- (* l = *)
intros n. simpl. reflexivity.
- (* l = m:: l' *)
intros n. simpl.
(* IHl' 不适用。 *)
Abort.
结果发现,上面的引理比实际需要的更特殊。我们可以强化该引理,
使其不仅适用于反转后的列表,也适用于一般列表。
Theorem app_length_S: ∀ l n,
length (l ++ [n]) = S (length l).
Proof.
intros l n. induction l as [| m l' IHl'].
- (* l = *)
simpl. reflexivity.
- (* l = m:: l' *)
simpl.
rewrite IHl'.
reflexivity.
Qed.
length (l ++ [n]) = S (length l).
Proof.
intros l n. induction l as [| m l' IHl'].
- (* l = *)
simpl. reflexivity.
- (* l = m:: l' *)
simpl.
rewrite IHl'.
reflexivity.
Qed.
现在我们可以完成原来的证明。
Theorem rev_length : ∀ l : natlist,
length (rev l) = length l.
Proof.
intros l. induction l as [| n l' IHl'].
- (* l = nil *)
reflexivity.
- (* l = cons *)
simpl.
rewrite → app_length_S.
rewrite → IHl'.
reflexivity.
Qed.
length (rev l) = length l.
Proof.
intros l. induction l as [| n l' IHl'].
- (* l = nil *)
reflexivity.
- (* l = cons *)
simpl.
rewrite → app_length_S.
rewrite → IHl'.
reflexivity.
Qed.
注意,我们上面证明的 app_length_S 引理相当狭窄,
它要求第二个列表只包含一个元素。我们可以为任意两个列表证明一个更一般的版本。
Theorem app_length : ∀ l1 l2 : natlist,
length (l1 ++ l2) = (length l1) + (length l2).
Proof.
(* 已在课上完成 *)
intros l1 l2. induction l1 as [| n l1' IHl1'].
- (* l1 = nil *)
reflexivity.
- (* l1 = cons *)
simpl. rewrite → IHl1'. reflexivity. Qed.
length (l1 ++ l2) = (length l1) + (length l2).
Proof.
(* 已在课上完成 *)
intros l1 l2. induction l1 as [| n l1' IHl1'].
- (* l1 = nil *)
reflexivity.
- (* l1 = cons *)
simpl. rewrite → IHl1'. reflexivity. Qed.
作为对比,以下是这两个定理的非形式化证明:
【定理】:对于所有的列表 l1 和 l2,
length (l1 ++ l2) = length l1 + length l2。
【证明】:对 l1 进行归纳。
【定理】: 对于所有的列表 l,length (rev l) = length l。
【证明】: 对 l 进行归纳。
这些证明的风格实在是冗长而迂腐。读多了之后,我们会发现减少细枝末节,
详述不太显然的步骤更有助于我们理解证明。
毕竟细节更容易在大脑中思考,必要时我们还可以在草稿纸上补全。
下面我们以一种更加紧凑的方式呈现之前的证明:
【定理】:对于所有 l,length (rev l) = length l。
【证明】:首先,观察到 length (l ++ [n]) = S (length l) 对一切 l 成立,
这一点可通过对 l 的归纳直接得到。当 l = n'::l' 时,通过再次对 l 使用归纳,
然后同时使用之前观察得到的性质和归纳假设即可证明。 ☐
一般而言,在不同的情况下合适的风格也会不同。
读者对这个问题的了解程度,以及当前的证明与读者熟悉的证明之间的相似度,
都会影响到这一点。对于我们现在的目的而言,最好先用更加冗长的方式。
- 首先,假设 l1 = []。我们必须证明
length ([] ++ l2) = length [] + length l2, 根据 length、++ 和 plus 的定义,上式显然可得。 - 其次,假设 l1 = n::l1',并且
length (l1' ++ l2) = length l1' + length l2. 我们必须证明
length ((n::l1') ++ l2) = length (n::l1') + length l2. 根据 length 和 ++ 的定义以及归纳假设,上式显然可得。 ☐
- 首先,假设 l = []。我们必须证明
length (rev []) = length [], 根据 length 和 rev 的定义,上式显然可得。 - 其次,假设 l = n::l',并且
length (rev l') = length l'. 我们必须证明
length (rev (n :: l')) = length (n :: l'). 根据 rev 的定义,上式来自于
length ((rev l') ++ [n]) = S (length l') 根据之前的引理,此式等同于
length (rev l') + length [n] = S (length l'). 根据归纳假设和 length 的定义,上式显然可得。 ☐
Search 搜索
或者,比如你忘记了证明加法满足交换律的定理的名字,
那么可以使用模式匹配,搜索所有涉及两个加法的等式的定理。
你会在那里看到很多结果,其中几乎全都来自标准库。为了限制结果,
你可以在某个特定「【模块|Module】」中搜索:
你也可以在搜索模式中使用变量而非「【通配符|Wildcard】」,
从而让搜索更加精确:
(变量前面的问号是必需的,用于表明它是搜索模式中的变量,
而非一个已定义且预期当前在作用域内的「【标识符|Identifier】」。)
在接下来的学习中,你要记得使用 Search,它能为你节约大量的时间!
你的 IDE 很可能有自己的功能来帮助搜索。例如,在 VSRocq 中,
你可以使用 Command-Control-K 打开一个用于执行搜索的标签页。
Theorem app_nil_r : ∀ l : natlist,
l ++ [] = l.
Proof.
(* 在这里填写 *) Admitted.
Theorem rev_app_distr: ∀ l1 l2 : natlist,
rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof.
(* 在这里填写 *) Admitted.
l ++ [] = l.
Proof.
(* 在这里填写 *) Admitted.
Theorem rev_app_distr: ∀ l1 l2 : natlist,
rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof.
(* 在这里填写 *) Admitted.
An involution is a function that is its own inverse. That is,
applying the function twice yield the original input.
【对合|Involution】函数是指自身是其逆函数的函数。
将该函数应用两次即可得到原始输入。
下面的练习有简短的解法。
如果你开始发现情况已经复杂到你无法理清的程度,
请后退一步并试着寻找更为简单的方法。
Theorem app_assoc4 : ∀ l1 l2 l3 l4 : natlist,
l1 ++ (l2 ++ (l3 ++ l4)) = ((l1 ++ l2) ++ l3) ++ l4.
Proof.
(* 在这里填写 *) Admitted.
l1 ++ (l2 ++ (l3 ++ l4)) = ((l1 ++ l2) ++ l3) ++ l4.
Proof.
(* 在这里填写 *) Admitted.
一个关于你对 nonzeros 的实现的练习:
Lemma nonzeros_app : ∀ l1 l2 : natlist,
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
(* 在这里填写 *) Admitted.
☐
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
(* 在这里填写 *) Admitted.
☐
Fixpoint eqblist (l1 l2 : natlist) : bool
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_eqblist1 :
(eqblist nil nil = true).
(* 在这里填写 *) Admitted.
Example test_eqblist2 :
eqblist [1;2;3] [1;2;3] = true.
(* 在这里填写 *) Admitted.
Example test_eqblist3 :
eqblist [1;2;3] [1;2;4] = false.
(* 在这里填写 *) Admitted.
Theorem eqblist_refl : ∀ l:natlist,
true = eqblist l l.
Proof.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_eqblist1 :
(eqblist nil nil = true).
(* 在这里填写 *) Admitted.
Example test_eqblist2 :
eqblist [1;2;3] [1;2;3] = true.
(* 在这里填写 *) Admitted.
Example test_eqblist3 :
eqblist [1;2;3] [1;2;4] = false.
(* 在这里填写 *) Admitted.
Theorem eqblist_refl : ∀ l:natlist,
true = eqblist l l.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem count_member_nonzero : ∀ (s : bag),
1 <=? (count 1 (1 :: s)) = true.
Proof.
(* 在这里填写 *) Admitted.
☐
1 <=? (count 1 (1 :: s)) = true.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem leb_n_Sn : ∀ n,
n <=? (S n) = true.
Proof.
intros n. induction n as [| n' IHn'].
- (* 0 *)
simpl. reflexivity.
- (* S n' *)
simpl. rewrite IHn'. reflexivity. Qed.
n <=? (S n) = true.
Proof.
intros n. induction n as [| n' IHn'].
- (* 0 *)
simpl. reflexivity.
- (* S n' *)
simpl. rewrite IHn'. reflexivity. Qed.
Theorem remove_does_not_increase_count: ∀ (s : bag),
(count 0 (remove_one 0 s)) <=? (count 0 s) = true.
Proof.
(* 在这里填写 *) Admitted.
☐
(count 0 (remove_one 0 s)) <=? (count 0 s) = true.
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 3 stars,标准,可选 (bag_count_sum)
写下一个用到函数 count 和 sum 的,关于袋子的有趣定理 bag_count_sum, 然后证明它。(你可能会发现该证明的难度取决于你如何定义 count!
(* 在这里填写 *)
☐
☐
练习: 3 stars,高级 (involution_injective)
Theorem involution_injective : ∀ (f : nat → nat),
(∀ n : nat, n = f (f n)) → (∀ n1 n2 : nat, f n1 = f n2 → n1 = n2).
Proof.
(* 在这里填写 *) Admitted.
☐
(∀ n : nat, n = f (f n)) → (∀ n1 n2 : nat, f n1 = f n2 → n1 = n2).
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 2 stars,高级 (rev_injective)
证明 rev 是单射。不要用归纳法证明它,那会很难。相反,请复用你在 involution_injective 中使用的同一种证明技巧。(不过:不要试图直接将那个 练习作为引理使用:类型并不相同!)
Theorem rev_injective : ∀ (l1 l2 : natlist),
rev l1 = rev l2 → l1 = l2.
Proof.
(* 在这里填写 *) Admitted.
☐
rev l1 = rev l2 → l1 = l2.
Proof.
(* 在这里填写 *) Admitted.
☐
Fixpoint nth_bad (l:natlist) (n:nat) : nat :=
match l with
| nil ⇒ 42
| a :: l' ⇒ match n with
| 0 ⇒ a
| S n' ⇒ nth_bad l' n'
end
end.
match l with
| nil ⇒ 42
| a :: l' ⇒ match n with
| 0 ⇒ a
| S n' ⇒ nth_bad l' n'
end
end.
这种方案并不好:如果 nth_bad 返回了 42,
那么不经过进一步处理的话,我们无法得知该值是否真的出现在了输入中。
(译注:我们无法判断是什么因素让它返回了 42。
它可能是列表过短时的返回值,也可能(此时列表足够长)是在列表中找到的值。)
一种更好的方式是改变 nth_bad 的返回类型,
使其包含一个错误值作为可能的结果。
我们将此类型命名为 natoption。
Inductive natoption : Type :=
| Some (n : nat)
| None.
(* 注意,我们按照 Rocq 标准库中的定义,将构造子名称 None 和 Some
首字母大写了。一般来说,构造子(以及变量)名称既可以以大写字母开头,
也可以以小写字母开头。 *)
| Some (n : nat)
| None.
(* 注意,我们按照 Rocq 标准库中的定义,将构造子名称 None 和 Some
首字母大写了。一般来说,构造子(以及变量)名称既可以以大写字母开头,
也可以以小写字母开头。 *)
然后,我们可以修改上面对 nth_bad 的定义,使其在列表过短时返回
None,而在列表足够长且 a 在 n 处时返回 Some a。
我们将这个新函数称为 nth_error,以表示它可能产生错误。
(如这里所见,归纳定义的构造子允许首字母大写。)
Fixpoint nth_error (l:natlist) (n:nat) : natoption :=
match l with
| nil ⇒ None
| a :: l' ⇒ match n with
| O ⇒ Some a
| S n' ⇒ nth_error l' n'
end
end.
Example test_nth_error1 : nth_error [4;5;6;7] 0 = Some 4.
match l with
| nil ⇒ None
| a :: l' ⇒ match n with
| O ⇒ Some a
| S n' ⇒ nth_error l' n'
end
end.
Example test_nth_error1 : nth_error [4;5;6;7] 0 = Some 4.
Proof. reflexivity. Qed.
Example test_nth_error2 : nth_error [4;5;6;7] 3 = Some 7.
Proof. reflexivity. Qed.
Example test_nth_error3 : nth_error [4;5;6;7] 9 = None.
Proof. reflexivity. Qed.
(在 HTML 版本中隐藏了这些老套的证明。若你想看它请点击小方格。)
下面的函数从 natoption 中提取出 nat,在 None 的情况下提供一个默认值。
Definition option_elim (d : nat) (o : natoption) : nat :=
match o with
| Some n' ⇒ n'
| None ⇒ d
end.
match o with
| Some n' ⇒ n'
| None ⇒ d
end.
Definition hd_error (l : natlist) : natoption
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_hd_error1 : hd_error [] = None.
(* 在这里填写 *) Admitted.
Example test_hd_error2 : hd_error [1] = Some 1.
(* 在这里填写 *) Admitted.
Example test_hd_error3 : hd_error [5;6] = Some 5.
(* 在这里填写 *) Admitted.
☐
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Example test_hd_error1 : hd_error [] = None.
(* 在这里填写 *) Admitted.
Example test_hd_error2 : hd_error [1] = Some 1.
(* 在这里填写 *) Admitted.
Example test_hd_error3 : hd_error [5;6] = Some 5.
(* 在这里填写 *) Admitted.
☐
Theorem option_elim_hd : ∀ (l:natlist) (default:nat),
hd default l = option_elim default (hd_error l).
Proof.
(* 在这里填写 *) Admitted.
☐
hd default l = option_elim default (hd_error l).
Proof.
(* 在这里填写 *) Admitted.
☐
偏映射
本质上来说,id 只是一个数。
但通过 Id 标签封装自然数来引入新的类型,
能让定义变得更加可读,同时也给了我们更多的灵活性。
我们还需要一个 id 的相等关系测试:
Module PartialMap.
Export NatList. (* 让来自 NatList 的定义可以在这里使用 *)
Inductive partial_map : Type :=
| empty
| record (i : id) (v : nat) (m : partial_map).
Export NatList. (* 让来自 NatList 的定义可以在这里使用 *)
Inductive partial_map : Type :=
| empty
| record (i : id) (v : nat) (m : partial_map).
此声明可以读作:「有两种方式可以构造一个 partial_map。
用构造子 empty 表示一个空的偏映射;
或将构造子 record 应用到一个键、一个值和一个既有的 partial_map,
来构造一个带「键-值」映射的 partial_map。」
update 函数在偏映射中覆盖给定的键以取代原值(如该键尚不存在,
则新建其记录)。
最后,find 函数按照给定的键搜索一个 partial_map。若该键无法找到,
它就返回 None;若该键与 val 相关联,则返回 Some val。
若同一个键被映到多个值,find 就会返回它遇到的第一个值。
Fixpoint find (x : id) (d : partial_map) : natoption :=
match d with
| empty ⇒ None
| record y v d' ⇒ if eqb_id x y
then Some v
else find x d'
end.
match d with
| empty ⇒ None
| record y v d' ⇒ if eqb_id x y
then Some v
else find x d'
end.
Theorem update_eq :
∀ (d : partial_map) (x : id) (v: nat),
find x (update d x v) = Some v.
Proof.
(* 在这里填写 *) Admitted.
☐
∀ (d : partial_map) (x : id) (v: nat),
find x (update d x v) = Some v.
Proof.
(* 在这里填写 *) Admitted.
☐
