逻辑:In_example_2
Logic: In_example_2
这是书中的代码:
Example In_example_2 :
forall n, In n [2; 4] ->
exists n', n = 2 * n'.
Proof.
(* WORKED IN CLASS *)
simpl.
intros n [H | [H | []]].
- exists 1. rewrite <- H. reflexivity.
- exists 2. rewrite <- H. reflexivity.
Qed.
simpl.
后In
转化为3个元素的析取:
============================
forall n : nat, 2 = n \/ 4 = n \/ False -> exists n' : nat, n = n' + (n' + 0)
但是我完全不明白怎么读这个:
intros n [H | [H | []]].
它产生了这个:
n : nat
H : 2 = n
============================
exists n' : nat, n = n' + (n' + 0)
subgoal 2 (ID 229) is:
exists n' : nat, n = n' + (n' + 0)
我的理解:
- 它将
forall
中的 n 放入上下文中。
- 它将 3 个元素的析取拆分为 2 个子目标,忽略 False:
- 根据拆分次数创建了 2 个子目标。
底部还有提示:
(** (Notice the use of the empty pattern to discharge the last case
_en passant_.) *)
En passant (French: [ɑ̃ paˈsɑ̃], lit. in passing) is a move in
chess. It is a special pawn capture that can only occur immediately
after a pawn makes a move of two squares from its starting square, and
when it could have been captured by an enemy pawn had it advanced only
one square.
看着这个:
intros n [H | [H | []]].
谁能给我解释一下:
- 是否应该使用这种形式的命令来销毁forall?这个任务还有别的吗?
- 如何用英语阅读这个命令?
- 为什么
[H | []]
被放到另外一对括号里?
- 如何
[]
告诉 coq 忽略 False 语句?
- 一般什么时候应该使用这个命令?
intros n [H | [H | []]]
表格是 shorthand for
intros n H. destruct H as [H | [H | []]].
你可以进一步重写证明
intros n H. destruct H as [H2 | H4F].
- (* H2 : 2 = n *)
exists 1. rewrite <- H2. reflexivity.
- (* H4F : 4 = n \/ False *)
destruct H4F as [H4 | HF].
+ (* H4 : 4 = n *)
exists 2. rewrite <- H4. reflexivity.
+ (* HF : False *)
destruct HF. (* No more subgoals here *)
这两个证明在逻辑上是等价的,但第一个更短(并且更容易阅读,一旦你习惯了语法)。
一般来说,策略destruct x as [...]
采用表达式x
,并为每个可以用来生成x
的构造函数生成一个子目标。构造函数的参数按照模式[...]
命名,不同构造函数对应的部分用竖线分隔。
A \/ B \/ C
形式的命题应读作 A \/ (B \/ C)
。因此,当你在上面的扩展形式中第一次调用 destruct
时,你会得到两种情况:when for when A
成立,另一种 for when B \/ C
成立。您需要再次调用 destruct
来分析内部或,这就是您在原始表达式中嵌套括号的原因。至于最后一个分支,False
被定义为一个没有构造函数的命题,所以一旦你破坏了一个假设HF : False
,证明就完成了。
(这里,“en passant”is equivalent to the English "in passing",大致意思是"incidentally"。指的是我们把最后一个case作为a对假设或假设进行案例分析的副产品。)
这是书中的代码:
Example In_example_2 :
forall n, In n [2; 4] ->
exists n', n = 2 * n'.
Proof.
(* WORKED IN CLASS *)
simpl.
intros n [H | [H | []]].
- exists 1. rewrite <- H. reflexivity.
- exists 2. rewrite <- H. reflexivity.
Qed.
simpl.
后In
转化为3个元素的析取:
============================
forall n : nat, 2 = n \/ 4 = n \/ False -> exists n' : nat, n = n' + (n' + 0)
但是我完全不明白怎么读这个:
intros n [H | [H | []]].
它产生了这个:
n : nat
H : 2 = n
============================
exists n' : nat, n = n' + (n' + 0)
subgoal 2 (ID 229) is:
exists n' : nat, n = n' + (n' + 0)
我的理解:
- 它将
forall
中的 n 放入上下文中。 - 它将 3 个元素的析取拆分为 2 个子目标,忽略 False:
- 根据拆分次数创建了 2 个子目标。
底部还有提示:
(** (Notice the use of the empty pattern to discharge the last case
_en passant_.) *)
En passant (French: [ɑ̃ paˈsɑ̃], lit. in passing) is a move in chess. It is a special pawn capture that can only occur immediately after a pawn makes a move of two squares from its starting square, and when it could have been captured by an enemy pawn had it advanced only one square.
看着这个:
intros n [H | [H | []]].
谁能给我解释一下:
- 是否应该使用这种形式的命令来销毁forall?这个任务还有别的吗?
- 如何用英语阅读这个命令?
- 为什么
[H | []]
被放到另外一对括号里? - 如何
[]
告诉 coq 忽略 False 语句? - 一般什么时候应该使用这个命令?
intros n [H | [H | []]]
表格是 shorthand for
intros n H. destruct H as [H | [H | []]].
你可以进一步重写证明
intros n H. destruct H as [H2 | H4F].
- (* H2 : 2 = n *)
exists 1. rewrite <- H2. reflexivity.
- (* H4F : 4 = n \/ False *)
destruct H4F as [H4 | HF].
+ (* H4 : 4 = n *)
exists 2. rewrite <- H4. reflexivity.
+ (* HF : False *)
destruct HF. (* No more subgoals here *)
这两个证明在逻辑上是等价的,但第一个更短(并且更容易阅读,一旦你习惯了语法)。
一般来说,策略destruct x as [...]
采用表达式x
,并为每个可以用来生成x
的构造函数生成一个子目标。构造函数的参数按照模式[...]
命名,不同构造函数对应的部分用竖线分隔。
A \/ B \/ C
形式的命题应读作 A \/ (B \/ C)
。因此,当你在上面的扩展形式中第一次调用 destruct
时,你会得到两种情况:when for when A
成立,另一种 for when B \/ C
成立。您需要再次调用 destruct
来分析内部或,这就是您在原始表达式中嵌套括号的原因。至于最后一个分支,False
被定义为一个没有构造函数的命题,所以一旦你破坏了一个假设HF : False
,证明就完成了。
(这里,“en passant”is equivalent to the English "in passing",大致意思是"incidentally"。指的是我们把最后一个case作为a对假设或假设进行案例分析的副产品。)