Types类型系统
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Stdlib Require Import Arith.
From PLF Require Import Maps.
From PLF Require Import Smallstep.
Set Default Goal Selector "!".
Hint Constructors multi : core.
From Stdlib Require Import Arith.
From PLF Require Import Maps.
From PLF Require Import Smallstep.
Set Default Goal Selector "!".
Hint Constructors multi : core.
有类型算术表达式
Module TM.
Inductive tm : Type :=
| tru : tm
| fls : tm
| ite : tm → tm → tm → tm
| zro : tm
| scc : tm → tm
| prd : tm → tm
| iszro : tm → tm.
Declare Custom Entry tm.
Declare Scope tm_scope.
Notation "'true'" := true (at level 1): tm_scope.
Notation "'true'" := (tru) (in custom tm at level 0): tm_scope.
Notation "'false'" := false (at level 1): tm_scope.
Notation "'false'" := (fls) (in custom tm at level 0): tm_scope.
Notation "<{ e }>" := e (e custom tm at level 99): tm_scope.
Notation "( x )" := x (in custom tm, x at level 99): tm_scope.
Notation "x" := x (in custom tm at level 0, x constr at level 0): tm_scope.
Notation "'0'" := (zro) (in custom tm at level 0): tm_scope.
Notation "'0'" := 0 (at level 1): tm_scope.
Notation "'succ' x" := (scc x) (in custom tm at level 90, x custom tm at level 80): tm_scope.
Notation "'pred' x" := (prd x) (in custom tm at level 90, x custom tm at level 80): tm_scope.
Notation "'iszero' x" := (iszro x) (in custom tm at level 80, x custom tm at level 70): tm_scope.
Notation "'if' c 'then' t 'else' e" := (ite c t e)
(in custom tm at level 90, c custom tm at level 80,
t custom tm at level 80, e custom tm at level 80): tm_scope.
Local Open Scope tm_scope.
Inductive tm : Type :=
| tru : tm
| fls : tm
| ite : tm → tm → tm → tm
| zro : tm
| scc : tm → tm
| prd : tm → tm
| iszro : tm → tm.
Declare Custom Entry tm.
Declare Scope tm_scope.
Notation "'true'" := true (at level 1): tm_scope.
Notation "'true'" := (tru) (in custom tm at level 0): tm_scope.
Notation "'false'" := false (at level 1): tm_scope.
Notation "'false'" := (fls) (in custom tm at level 0): tm_scope.
Notation "<{ e }>" := e (e custom tm at level 99): tm_scope.
Notation "( x )" := x (in custom tm, x at level 99): tm_scope.
Notation "x" := x (in custom tm at level 0, x constr at level 0): tm_scope.
Notation "'0'" := (zro) (in custom tm at level 0): tm_scope.
Notation "'0'" := 0 (at level 1): tm_scope.
Notation "'succ' x" := (scc x) (in custom tm at level 90, x custom tm at level 80): tm_scope.
Notation "'pred' x" := (prd x) (in custom tm at level 90, x custom tm at level 80): tm_scope.
Notation "'iszero' x" := (iszro x) (in custom tm at level 80, x custom tm at level 70): tm_scope.
Notation "'if' c 'then' t 'else' e" := (ite c t e)
(in custom tm at level 90, c custom tm at level 80,
t custom tm at level 80, e custom tm at level 80): tm_scope.
Local Open Scope tm_scope.
【值|Value】是 true、false 和数值……
Inductive bvalue : tm → Prop :=
| bv_true : bvalue <{ true }>
| bv_false : bvalue <{ false }>.
Inductive nvalue : tm → Prop :=
| nv_0 : nvalue <{ 0 }>
| nv_succ : ∀ t, nvalue t → nvalue <{ succ t }>.
Definition value (t : tm) := bvalue t ∨ nvalue t.
Hint Constructors bvalue nvalue : core.
Hint Unfold value : core.
| bv_true : bvalue <{ true }>
| bv_false : bvalue <{ false }>.
Inductive nvalue : tm → Prop :=
| nv_0 : nvalue <{ 0 }>
| nv_succ : ∀ t, nvalue t → nvalue <{ succ t }>.
Definition value (t : tm) := bvalue t ∨ nvalue t.
Hint Constructors bvalue nvalue : core.
Hint Unfold value : core.
操作语义
| (ST_IfTrue) | |
| if true then t1 else t2 --> t1 |
| (ST_IfFalse) | |
| if false then t1 else t2 --> t2 |
| t1 --> t1' | (ST_If) |
| if t1 then t2 else t3 --> if t1' then t2 else t3 |
| t1 --> t1' | (ST_Succ) |
| succ t1 --> succ t1' |
| (ST_Pred0) | |
| pred 0 --> 0 |
| numeric value v | (ST_PredSucc) |
| pred (succ v) --> v |
| t1 --> t1' | (ST_Pred) |
| pred t1 --> pred t1' |
| (ST_IsZero0) | |
| iszero 0 --> true |
| numeric value v | (ST_IsZeroSucc) |
| iszero (succ v) --> false |
| t1 --> t1' | (ST_IsZero) |
| iszero t1 --> iszero t1' |
Reserved Notation "t '-->' t'" (at level 40).
Inductive step : tm → tm → Prop :=
| ST_IfTrue : ∀ t1 t2,
<{ if true then t1 else t2 }> --> t1
| ST_IfFalse : ∀ t1 t2,
<{ if false then t1 else t2 }> --> t2
| ST_If : ∀ c c' t2 t3,
c --> c' →
<{ if c then t2 else t3 }> --> <{ if c' then t2 else t3 }>
| ST_Succ : ∀ t1 t1',
t1 --> t1' →
<{ succ t1 }> --> <{ succ t1' }>
| ST_Pred0 :
<{ pred 0 }> --> <{ 0 }>
| ST_PredSucc : ∀ v,
nvalue v →
<{ pred (succ v) }> --> v
| ST_Pred : ∀ t1 t1',
t1 --> t1' →
<{ pred t1 }> --> <{ pred t1' }>
| ST_IsZero0 :
<{ iszero 0 }> --> <{ true }>
| ST_IsZeroSucc : ∀ v,
nvalue v →
<{ iszero (succ v) }> --> <{ false }>
| ST_IsZero : ∀ t1 t1',
t1 --> t1' →
<{ iszero t1 }> --> <{ iszero t1' }>
where "t '-->' t'" := (step t t').
Hint Constructors step : core.
Inductive step : tm → tm → Prop :=
| ST_IfTrue : ∀ t1 t2,
<{ if true then t1 else t2 }> --> t1
| ST_IfFalse : ∀ t1 t2,
<{ if false then t1 else t2 }> --> t2
| ST_If : ∀ c c' t2 t3,
c --> c' →
<{ if c then t2 else t3 }> --> <{ if c' then t2 else t3 }>
| ST_Succ : ∀ t1 t1',
t1 --> t1' →
<{ succ t1 }> --> <{ succ t1' }>
| ST_Pred0 :
<{ pred 0 }> --> <{ 0 }>
| ST_PredSucc : ∀ v,
nvalue v →
<{ pred (succ v) }> --> v
| ST_Pred : ∀ t1 t1',
t1 --> t1' →
<{ pred t1 }> --> <{ pred t1' }>
| ST_IsZero0 :
<{ iszero 0 }> --> <{ true }>
| ST_IsZeroSucc : ∀ v,
nvalue v →
<{ iszero (succ v) }> --> <{ false }>
| ST_IsZero : ∀ t1 t1',
t1 --> t1' →
<{ iszero t1 }> --> <{ iszero t1' }>
where "t '-->' t'" := (step t t').
Hint Constructors step : core.
ST_PredSucc 与 ST_IszeroSucc 中的 nvalue 条件
对确定性是必需的(将在下面一个可选练习中证明)。
注意 step 关系并不关心被步进的表达式在全局上
是否有意义。它只检查【下一步】归约中的操作是否作用于
正确种类的操作数。
例如,项 succ true 无法步进,但这个几乎同样明显无意义的项
succ (if true then true else true) 却可以步进(一次,然后卡住)。
succ (if true then true else true) 却可以步进(一次,然后卡住)。
正规形与值
Notation step_normal_form := (normal_form step).
Definition stuck (t : tm) : Prop :=
step_normal_form t ∧ ¬ value t.
Hint Unfold stuck : core.
Definition stuck (t : tm) : Prop :=
step_normal_form t ∧ ¬ value t.
Hint Unfold stuck : core.
练习: 3 stars,标准 (value_is_nf)
(提示:在这个证明中你会遇到一处需要使用归纳来推理
一个已知是数值的项。归纳既可以对项本身进行,
也可以对它是数值这一证据进行。两种方式都能完成证明,
但你会发现其中一种要短得多。作为练习,
请试着用两种方式各完成一遍证明。) ☐
练习: 3 stars,标准,可选 (step_deterministic)
使用 value_is_nf 来表明 step 关系也是确定的。定型
在非形式化记号中,定型关系常写作 |-- t ∈ T,
读作「t 有类型 T」。|-- 符号叫做【十字转门|Turnstile】。
下面我们将看到更丰富的定型关系。其中十字转门左侧
会写出一个或多个「语境」参数。目前,语境总是空的。
| (T_True) | |
| |-- true ∈ Bool |
| (T_False) | |
| |-- false ∈ Bool |
| |-- t1 ∈ Bool |-- t2 ∈ T |-- t3 ∈ T | (T_If) |
| |-- if t1 then t2 else t3 ∈ T |
| (T_0) | |
| |-- 0 ∈ Nat |
| |-- t1 ∈ Nat | (T_Succ) |
| |-- succ t1 ∈ Nat |
| |-- t1 ∈ Nat | (T_Pred) |
| |-- pred t1 ∈ Nat |
| |-- t1 ∈ Nat | (T_IsZero) |
| |-- iszero t1 ∈ Bool |
Inductive has_type : tm → ty → Prop :=
| T_True :
<{ |-- true \in Bool }>
| T_False :
<{ |-- false \in Bool }>
| T_If : ∀ t1 t2 t3 T,
<{ |-- t1 \in Bool }> →
<{ |-- t2 \in T }> →
<{ |-- t3 \in T }> →
<{ |-- if t1 then t2 else t3 \in T }>
| T_0 :
<{ |-- 0 \in Nat }>
| T_Succ : ∀ t1,
<{ |-- t1 \in Nat }> →
<{ |-- succ t1 \in Nat }>
| T_Pred : ∀ t1,
<{ |-- t1 \in Nat }> →
<{ |-- pred t1 \in Nat }>
| T_Iszero : ∀ t1,
<{ |-- t1 \in Nat }> →
<{ |-- iszero t1 \in Bool }>
where "<{ '|--' t '∈' T }>" := (has_type t T).
Hint Constructors has_type : core.
Example has_type_1 :
<{ |-- if false then 0 else (succ 0) \in Nat }>.
Proof.
apply T_If.
- apply T_False.
- apply T_0.
- apply T_Succ. apply T_0.
Qed.
(我们已把定型关系的所有构造子加入提示数据库。
因此 auto 策略实际上可以自动找到这个证明。)
定型关系是一个【保守】(或【静态】)的近似。
它不考虑项被归约时会发生什么。特别地,
它并不计算项的正规形的类型。
Example not_has_type :
¬ <{ |-- if false then 0 else true \in Bool }>.
Example not_has_type' :
¬ <{ |-- if iszero (succ 0) then succ false else true \in Bool }>.
¬ <{ |-- if false then 0 else true \in Bool }>.
Example not_has_type' :
¬ <{ |-- if iszero (succ 0) then succ false else true \in Bool }>.
Example succ_hastype_nat__hastype_nat : ∀ t,
<{ |-- succ t \in Nat }> →
<{ |-- t \in Nat }>.
Proof.
(* 在这里填写 *) Admitted.
☐
<{ |-- succ t \in Nat }> →
<{ |-- t \in Nat }>.
Proof.
(* 在这里填写 *) Admitted.
☐
Lemma bool_canonical : ∀ t,
<{ |-- t \in Bool }> → value t → bvalue t.
Lemma nat_canonical : ∀ t,
<{ |-- t \in Nat }> → value t → nvalue t.
<{ |-- t \in Bool }> → value t → bvalue t.
Proof.
intros t HT [Hb | Hn].
- assumption.
- destruct Hn as [ | Hs].
+ inversion HT.
+ inversion HT.
Qed.
intros t HT [Hb | Hn].
- assumption.
- destruct Hn as [ | Hs].
+ inversion HT.
+ inversion HT.
Qed.
Lemma nat_canonical : ∀ t,
<{ |-- t \in Nat }> → value t → nvalue t.
可进性
练习: 3 stars,标准 (finish_progress)
完成 progress 性质的形式化证明。
(开始前,请先理解下面练习给出的非形式化证明的各部分。
这会为你节省大量时间。)
Proof.
intros t T HT.
induction HT; auto.
(* 像 T_True 和 T_False 这样显然为值的情形
会被 auto 立即消去 *)
- (* T_If *)
right. destruct IHHT1.
+ (* t1 是值 *)
apply (bool_canonical t1 HT1) in H.
destruct H.
× ∃ t2. auto.
× ∃ t3. auto.
+ (* t1 可以步进 *)
destruct H as [t1' H1].
∃ <{ if t1' then t2 else t3 }>. auto.
(* 在这里填写 *) Admitted.
intros t T HT.
induction HT; auto.
(* 像 T_True 和 T_False 这样显然为值的情形
会被 auto 立即消去 *)
- (* T_If *)
right. destruct IHHT1.
+ (* t1 是值 *)
apply (bool_canonical t1 HT1) in H.
destruct H.
× ∃ t2. auto.
× ∃ t3. auto.
+ (* t1 可以步进 *)
destruct H as [t1' H1].
∃ <{ if t1' then t2 else t3 }>. auto.
(* 在这里填写 *) Admitted.
☐
【定理】:若 |-- t ∈ T,则 t 要么是值,
要么对某个 t' 有 t --> t'。
【证明】:对 |-- t ∈ T 的导出式作归纳。
练习: 3 stars,高级,可选 (finish_progress_informal)
完成相应的非形式化证明:- 若导出式的最后一条规则是 T_If,则 t = if t1
then t2 else t3,其中 |-- t1 ∈ Bool、|-- t2 ∈ T
且 |-- t3 ∈ T。根据归纳假设,t1 要么是值,
要么可以步进到某个 t1'。
- 若 t1 是值,则由典范形式引理以及 |-- t1 ∈ Bool
的事实可知 t1 是 bvalue;也即它要么是 true
要么是 false。若 t1 = true,则 t 由 ST_IfTrue
步进到 t2;若 t1 = false,则 t 由 ST_IfFalse
步进到 t3。无论哪种情况 t 都能步进,
这正是我们要证明的。
- 若 t1 自己可以步进,则由 ST_If,t 也可以。
- 若 t1 是值,则由典范形式引理以及 |-- t1 ∈ Bool
的事实可知 t1 是 bvalue;也即它要么是 true
要么是 false。若 t1 = true,则 t 由 ST_IfTrue
步进到 t2;若 t1 = false,则 t 由 ST_IfFalse
步进到 t3。无论哪种情况 t 都能步进,
这正是我们要证明的。
- (* 在这里填写 *)
(* 不要修改下面这行: *)
Definition manual_grade_for_finish_progress_informal : option (nat×string) := None.
☐
Definition manual_grade_for_finish_progress_informal : option (nat×string) := None.
☐
完成 preservation 性质的形式化证明。
(同样,请先确保你理解了下面练习中的非形式化证明片段。)
Proof.
intros t t' T HT HE.
generalize dependent t'.
induction HT;
(* 每种情形都需要引入一些东西…… *)
intros t' HE;
(* 而且我们可以一次处理若干不可能的情形…… *)
try solve_by_invert.
- (* T_If *) inversion HE; subst; clear HE.
+ (* ST_IFTrue *) assumption.
+ (* ST_IfFalse *) assumption.
+ (* ST_If *) apply T_If; try assumption.
apply IHHT1; assumption.
(* 在这里填写 *) Admitted.
intros t t' T HT HE.
generalize dependent t'.
induction HT;
(* 每种情形都需要引入一些东西…… *)
intros t' HE;
(* 而且我们可以一次处理若干不可能的情形…… *)
try solve_by_invert.
- (* T_If *) inversion HE; subst; clear HE.
+ (* ST_IFTrue *) assumption.
+ (* ST_IfFalse *) assumption.
+ (* ST_If *) apply T_If; try assumption.
apply IHHT1; assumption.
(* 在这里填写 *) Admitted.
☐
【定理】:若 |-- t ∈ T 且 t --> t',则 |-- t' ∈ T。
【证明】:对 |-- t ∈ T 的导出式作归纳。
练习: 3 stars,高级,可选 (finish_preservation_informal)
完成下面的非形式化证明:- 若导出式的最后一条规则是 T_If,则 t = if t1
then t2 else t3,其中 |-- t1 ∈ Bool、|-- t2 ∈ T
且 |-- t3 ∈ T。
检查小步归约关系的规则并记住 t 形如 if ..., 我们看到可能被用来证明 t --> t' 的规则只有 ST_IfTrue、ST_IfFalse 或 ST_If。
- 若最后的规则是 ST_IfTrue,则 t' = t2。
但我们知道 |-- t2 ∈ T,故证毕。
- 若最后的规则是 ST_IfFalse,则 t' = t3。
但我们知道 |-- t3 ∈ T,故证毕。
- 若最后的规则是 ST_If,则 t' = if t1' then t2
else t3,其中 t1 --> t1'。我们知道 |-- t1 ∈ Bool,
所以由归纳假设 |-- t1' ∈ Bool。于是 T_If 规则
给出 |-- if t1' then t2 else t3 ∈ T,这正是所需。
- 若最后的规则是 ST_IfTrue,则 t' = t2。
但我们知道 |-- t2 ∈ T,故证毕。
- (* 在这里填写 *)
(* 不要修改下面这行: *)
Definition manual_grade_for_finish_preservation_informal : option (nat×string) := None.
☐
Definition manual_grade_for_finish_preservation_informal : option (nat×string) := None.
☐
练习: 3 stars,标准 (preservation_alternate_proof)
现在改对【求值】导出式(而非定型导出式)作归纳, 重新证明同一性质。先仔细阅读并思考上面证明的 开头几行,确保你理解它们在做什么。 本证明的 setup 类似,但不完全相同。
Theorem preservation' : ∀ t t' T,
<{ |-- t \in T }> →
t --> t' →
<{ |-- t' \in T }>.
Proof with eauto.
(* 在这里填写 *) Admitted.
☐
<{ |-- t \in T }> →
t --> t' →
<{ |-- t' \in T }>.
Proof with eauto.
(* 在这里填写 *) Admitted.
☐
Definition multistep := (multi step).
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).
Corollary soundness : ∀ t t' T,
<{ |-- t \in T }> →
t -->* t' →
~(stuck t').
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).
Corollary soundness : ∀ t t' T,
<{ |-- t \in T }> →
t -->* t' →
~(stuck t').
额外练习
练习: 3 stars,标准,特别有用 (subject_expansion)
在看到主语归约性质之后,人们可能会好奇相反的性质 即主语【扩张】是否也成立。也就是说,是否总有: 若 t --> t' 且 |-- t' ∈ T,则 |-- t ∈ T? 若是,请证明它。若否,请给出一个反例。
Theorem subject_expansion:
(∀ t t' T, t --> t' ∧ <{ |-- t' \in T }> → <{ |-- t \in T }>)
∨
¬ (∀ t t' T, t --> t' ∧ <{ |-- t' \in T }> → <{ |-- t \in T }>).
Proof.
(* 在这里填写 *) Admitted.
☐
(∀ t t' T, t --> t' ∧ <{ |-- t' \in T }> → <{ |-- t \in T }>)
∨
¬ (∀ t t' T, t --> t' ∧ <{ |-- t' \in T }> → <{ |-- t \in T }>).
Proof.
(* 在这里填写 *) Admitted.
☐
练习: 2 stars,标准 (variation1)
假设我们向定型关系添加这条新规则:| T_SuccBool : ∀ t,
<{ |-- t \in Bool }> →
<{ |-- succ t \in Bool }> 在这条规则存在时,下列哪些性质仍然成立? 对每个性质写下「仍然成立」或「变为假」。 若某性质变为假,请给出一个反例。
- step 的确定性
(* 在这里填写 *)
- 可进性
(* 在这里填写 *)
- 保型性
(* 在这里填写 *)
练习: 2 stars,标准 (variation2)
或者,假设我们向 step 关系添加这条新规则:| ST_Funny1 : ∀ t2 t3,
(<{ if true then t2 else t3 }>) --> t3 在这条规则存在时,上述哪些性质变为假? 对每个变为假的性质,给出一个反例。 (* 在这里填写 *)
练习: 2 stars,标准,可选 (variation3)
假设我们改为添加这条规则:| ST_Funny2 : ∀ t1 t2 t2' t3,
t2 --> t2' →
(<{ if t1 then t2 else t3 }>) --> (<{ if t1 then t2' else t3 }>) 在这条规则存在时,上述哪些性质变为假? 对每个变为假的性质,给出一个反例。 (* 在这里填写 *)
☐
练习: 2 stars,标准,可选 (variation4)
假设我们改为添加这条规则:| ST_Funny3 :
(<{pred false}>) --> (<{ pred (pred false)}>) 在这条规则存在时,上述哪些性质变为假? 对每个变为假的性质,给出一个反例。 (* 在这里填写 *)
☐
练习: 2 stars,标准,可选 (variation5)
假设我们改为添加这条规则:| T_Funny4 :
|-- <{ 0 }> \in Bool 在这条规则存在时,上述哪些性质变为假? 对每个变为假的性质,给出一个反例。 (* 在这里填写 *)
☐
练习: 2 stars,标准,可选 (variation6)
假设我们改为添加这条规则:| T_Funny5 :
|-- pred 0 }> \in Bool 在这条规则存在时,上述哪些性质变为假? 对每个变为假的性质,给出一个反例。 (* 在这里填写 *)
☐
练习: 3 stars,标准,可选 (more_variations)
仿照上面的练习,自己编几道。 试着有选择地破坏某些性质:改变定义, 恰好只破坏其中一个性质,而让其余性质保持不变。
(* 在这里填写 *)
☐
☐
