References可变引用的类型判断
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
Set Warnings "-deprecated-syntactic-definition".
From Stdlib Require Import Strings.String.
From Stdlib Require Import Init.Nat.
From Stdlib Require Import Arith.
From Stdlib Require Import PeanoNat.
From Stdlib Require Import Lia.
From PLF Require Import Maps.
From PLF Require Import Smallstep.
From Stdlib Require Import List. Import Datatypes.
Check length.
Import Nat.
Set Warnings "-deprecated-syntactic-definition".
From Stdlib Require Import Strings.String.
From Stdlib Require Import Init.Nat.
From Stdlib Require Import Arith.
From Stdlib Require Import PeanoNat.
From Stdlib Require Import Lia.
From PLF Require Import Maps.
From PLF Require Import Smallstep.
From Stdlib Require Import List. Import Datatypes.
Check length.
Import Nat.
定义
引用上的基本操作是【分配】、【解引用】与【赋值】。
- 要分配一个引用,我们使用 ref 操作符,
为新单元提供初始值。
例如,ref 5 创建一个含值 5 的新单元, 并归约为指向该单元的引用。
- 要读出该单元的当前值,我们使用解引用操作符 !。
例如,!(ref 5) 归约为 5。
- 要改变单元中存储的值,我们使用赋值操作符。
若 r 是引用,r := 7 会把值 7 存入 r 所引用的单元。
类型
T ::= Nat
| Unit
| T → T
| Ref T
项
t ::= ... Terms
| ref t allocation
| !t dereference
| t := t assignment
| l location
Inductive tm : Type :=
(* STLC with numbers: *)
| tm_var : string → tm
| tm_app : tm → tm → tm
| tm_abs : string → ty → tm → tm
| tm_const : nat → tm
| tm_succ : tm → tm
| tm_pred : tm → tm
| tm_mult : tm → tm → tm
| tm_if0 : tm → tm → tm → tm
(* New terms: *)
| tm_unit : tm
| tm_ref : tm → tm
| tm_deref : tm → tm
| tm_assign : tm → tm → tm
| tm_loc : nat → tm.
Declare Custom Entry stlc_ty.
Declare Custom Entry stlc_tm.
Declare Scope stlc_scope.
Notation "x" := x (in custom stlc_ty at level 0, x global) : stlc_scope.
Notation "<{{ x }}>" := x (x custom stlc_ty).
Notation "( t )" := t (in custom stlc_ty at level 0, t custom stlc_ty) : stlc_scope.
Notation "S -> T" :=
(Ty_Arrow S T) (in custom stlc_ty at level 99, right associativity) : stlc_scope.
Notation "$( t )" := t (in custom stlc_ty at level 0, t constr) : stlc_scope.
Notation "$( x )" := x (in custom stlc_tm at level 0, x constr, only parsing) : stlc_scope.
Notation "x" := x (in custom stlc_tm at level 0, x constr at level 0) : stlc_scope.
Notation "<{ e }>" := e (e custom stlc_tm at level 200) : stlc_scope.
Notation "( x )" := x (in custom stlc_tm at level 0, x custom stlc_tm) : stlc_scope.
Notation "x y" :=
(tm_app x y) (in custom stlc_tm at level 10, left associativity)
: stlc_scope.
Notation "\ x : t , y" :=
(tm_abs x t y) (in custom stlc_tm at level 200, x global,
t custom stlc_ty,
y custom stlc_tm at level 200,
left associativity).
Coercion tm_var : string >-> tm.
Arguments tm_var _%_string.
Notation "'Nat'" := Ty_Nat (in custom stlc_ty at level 0).
Notation "'succ' x" := (tm_succ x) (in custom stlc_tm at level 10,
x custom stlc_tm at level 0) : stlc_scope.
Notation "'pred' x" := (tm_pred x) (in custom stlc_tm at level 10,
x custom stlc_tm at level 0) : stlc_scope.
Notation "x * y" := (tm_mult x y) (in custom stlc_tm at level 95,
right associativity) : stlc_scope.
Notation "'if0' x 'then' y 'else' z" :=
(tm_if0 x y z) (in custom stlc_tm at level 0,
x custom stlc_tm at level 0,
y custom stlc_tm at level 0,
z custom stlc_tm at level 0) : stlc_scope.
Coercion tm_const : nat >-> tm.
Notation "'Unit'" :=
(Ty_Unit) (in custom stlc_ty at level 0) : stlc_scope.
Notation "'unit'" := tm_unit (in custom stlc_tm at level 0) : stlc_scope.
Notation "'Ref' t" :=
(Ty_Ref t) (in custom stlc_ty at level 4).
Notation "'loc' x" := (tm_loc x) (in custom stlc_tm at level 2) : stlc_scope.
Notation "'ref' x" := (tm_ref x) (in custom stlc_tm at level 2) : stlc_scope.
Notation "'!' x " := (tm_deref x) (in custom stlc_tm at level 2) : stlc_scope.
Notation " e1 ':=' e2 " := (tm_assign e1 e2) (in custom stlc_tm at level 21) : stlc_scope.
Open Scope stlc_scope.
(* STLC with numbers: *)
| tm_var : string → tm
| tm_app : tm → tm → tm
| tm_abs : string → ty → tm → tm
| tm_const : nat → tm
| tm_succ : tm → tm
| tm_pred : tm → tm
| tm_mult : tm → tm → tm
| tm_if0 : tm → tm → tm → tm
(* New terms: *)
| tm_unit : tm
| tm_ref : tm → tm
| tm_deref : tm → tm
| tm_assign : tm → tm → tm
| tm_loc : nat → tm.
Declare Custom Entry stlc_ty.
Declare Custom Entry stlc_tm.
Declare Scope stlc_scope.
Notation "x" := x (in custom stlc_ty at level 0, x global) : stlc_scope.
Notation "<{{ x }}>" := x (x custom stlc_ty).
Notation "( t )" := t (in custom stlc_ty at level 0, t custom stlc_ty) : stlc_scope.
Notation "S -> T" :=
(Ty_Arrow S T) (in custom stlc_ty at level 99, right associativity) : stlc_scope.
Notation "$( t )" := t (in custom stlc_ty at level 0, t constr) : stlc_scope.
Notation "$( x )" := x (in custom stlc_tm at level 0, x constr, only parsing) : stlc_scope.
Notation "x" := x (in custom stlc_tm at level 0, x constr at level 0) : stlc_scope.
Notation "<{ e }>" := e (e custom stlc_tm at level 200) : stlc_scope.
Notation "( x )" := x (in custom stlc_tm at level 0, x custom stlc_tm) : stlc_scope.
Notation "x y" :=
(tm_app x y) (in custom stlc_tm at level 10, left associativity)
: stlc_scope.
Notation "\ x : t , y" :=
(tm_abs x t y) (in custom stlc_tm at level 200, x global,
t custom stlc_ty,
y custom stlc_tm at level 200,
left associativity).
Coercion tm_var : string >-> tm.
Arguments tm_var _%_string.
Notation "'Nat'" := Ty_Nat (in custom stlc_ty at level 0).
Notation "'succ' x" := (tm_succ x) (in custom stlc_tm at level 10,
x custom stlc_tm at level 0) : stlc_scope.
Notation "'pred' x" := (tm_pred x) (in custom stlc_tm at level 10,
x custom stlc_tm at level 0) : stlc_scope.
Notation "x * y" := (tm_mult x y) (in custom stlc_tm at level 95,
right associativity) : stlc_scope.
Notation "'if0' x 'then' y 'else' z" :=
(tm_if0 x y z) (in custom stlc_tm at level 0,
x custom stlc_tm at level 0,
y custom stlc_tm at level 0,
z custom stlc_tm at level 0) : stlc_scope.
Coercion tm_const : nat >-> tm.
Notation "'Unit'" :=
(Ty_Unit) (in custom stlc_ty at level 0) : stlc_scope.
Notation "'unit'" := tm_unit (in custom stlc_tm at level 0) : stlc_scope.
Notation "'Ref' t" :=
(Ty_Ref t) (in custom stlc_ty at level 4).
Notation "'loc' x" := (tm_loc x) (in custom stlc_tm at level 2) : stlc_scope.
Notation "'ref' x" := (tm_ref x) (in custom stlc_tm at level 2) : stlc_scope.
Notation "'!' x " := (tm_deref x) (in custom stlc_tm at level 2) : stlc_scope.
Notation " e1 ':=' e2 " := (tm_assign e1 e2) (in custom stlc_tm at level 21) : stlc_scope.
Open Scope stlc_scope.
直观地说:
在非形式化示例中,我们也会自由使用
MoreStlc 一章中发展的 STLC 扩展;
不过,为使证明简短,我们不会在这里再形式化它们。
(这样做很容易,因为那些特性与引用之间
没有什么很有趣的相互作用。)
- ref t(形式地,ref t)分配一个新引用单元,
其值为 t,并归约为新分配单元的位置;
- !t(形式地,deref t)归约为 t 所引用单元的内容;
- t1 := t2(形式地,assign t1 t2)把 t2 赋给
t1 所引用的单元;以及
- l(形式地,loc l)是指向位置 l 处单元的引用。 位置将在后文讨论。
类型判断(预览)
| Gamma |-- t1 ∈ T1 | (T_Ref) |
| Gamma |-- ref t1 ∈ Ref T1 |
| Gamma |-- t1 ∈ Ref T1 | (T_Deref) |
| Gamma |-- !t1 ∈ T1 |
| Gamma |-- t1 ∈ Ref T2 | |
| Gamma |-- t2 ∈ T2 | (T_Assign) |
| Gamma |-- t1 := t2 : Unit |
值与替换
Inductive value : tm → Prop :=
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
| v_nat : ∀ n : nat ,
value <{ n }>
| v_unit :
value <{ unit }>
| v_loc : ∀ l,
value <{ loc l }>.
Hint Constructors value : core.
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
| v_nat : ∀ n : nat ,
value <{ n }>
| v_unit :
value <{ unit }>
| v_loc : ∀ l,
value <{ loc l }>.
Hint Constructors value : core.
把替换扩展以处理新的项语法很直接:
对指针做替换不会改变它。
Reserved Notation "'[' x ':=' s ']' t"
(in custom stlc_tm at level 5, x global, s custom stlc_tm,
t custom stlc_tm at next level, right associativity).
Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
match t with
(* pure STLC *)
| tm_var y ⇒
if String.eqb x y then s else t
| <{\y:T, t1}> ⇒
if String.eqb x y then t else <{\y:T, [x:=s] t1}>
| <{t1 t2}> ⇒
<{([x:=s] t1) ([x:=s] t2)}>
(* numbers *)
| tm_const _ ⇒
t
| <{succ t1}> ⇒
<{succ [x := s] t1}>
| <{pred t1}> ⇒
<{pred [x := s] t1}>
| <{t1 × t2}> ⇒
<{ ([x := s] t1) × ([x := s] t2)}>
| <{if0 t1 then t2 else t3}> ⇒
<{if0 [x := s] t1 then [x := s] t2 else [x := s] t3}>
(* unit *)
| <{ unit }> ⇒
t
(* references *)
| <{ ref t1 }> ⇒
<{ ref ([x:=s] t1) }>
| <{ !t1 }> ⇒
<{ !([x:=s] t1) }>
| <{ t1 := t2 }> ⇒
<{ ([x:=s] t1) := ([x:=s] t2) }>
| <{ loc _ }> ⇒
t
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc_tm) : stlc_scope.
(in custom stlc_tm at level 5, x global, s custom stlc_tm,
t custom stlc_tm at next level, right associativity).
Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
match t with
(* pure STLC *)
| tm_var y ⇒
if String.eqb x y then s else t
| <{\y:T, t1}> ⇒
if String.eqb x y then t else <{\y:T, [x:=s] t1}>
| <{t1 t2}> ⇒
<{([x:=s] t1) ([x:=s] t2)}>
(* numbers *)
| tm_const _ ⇒
t
| <{succ t1}> ⇒
<{succ [x := s] t1}>
| <{pred t1}> ⇒
<{pred [x := s] t1}>
| <{t1 × t2}> ⇒
<{ ([x := s] t1) × ([x := s] t2)}>
| <{if0 t1 then t2 else t3}> ⇒
<{if0 [x := s] t1 then [x := s] t2 else [x := s] t3}>
(* unit *)
| <{ unit }> ⇒
t
(* references *)
| <{ ref t1 }> ⇒
<{ ref ([x:=s] t1) }>
| <{ !t1 }> ⇒
<{ !([x:=s] t1) }>
| <{ t1 := t2 }> ⇒
<{ ([x:=s] t1) := ([x:=s] t2) }>
| <{ loc _ }> ⇒
t
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc_tm) : stlc_scope.
副作用与串连
r:=succ(!r); !r
作为
(\x:Unit, !r) (r:=succ(!r)).
的缩写。
其效果是依次归约两个表达式并返回第二个的值。
把第一个表达式的类型限制为 Unit,
有助于类型检查器捕获一些愚蠢错误。
只有确实保证第一个值平凡时,我们才被允许丢弃它。
r:=succ(!r); r:=succ(!r); r:=succ(!r); r:=succ(!r); !r
形式地,我们引入串连作为【派生形式】 tseq,
它展开为一个抽象与一个应用。
Definition x : string := "x".
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Definition tseq t1 t2 :=
<{ (\ x : Unit, t2) t1 }>.
Notation "t1 ; t2" := (tseq t1 t2) (in custom stlc_tm at level 3) : stlc_scope.
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Definition tseq t1 t2 :=
<{ (\ x : Unit, t2) t1 }>.
Notation "t1 ; t2" := (tseq t1 t2) (in custom stlc_tm at level 3) : stlc_scope.
引用与别名
let r = ref 5 in let s = r in s := 82; (!r)+1之后,r 所引用的单元将含值 82, 而整个表达式的结果将是 83。 引用 r 与 s 被称为同一单元的【别名|Alias】。
r := 5; r := !s先把 5 赋给 r,随后立即用 s 的当前值覆盖它; 这与单次赋值
r := !s的效果完全相同,【除非】我们恰好在 r 与 s 是同一单元别名的上下文中这样做!
let r = ref 0 in let s = r in r := 5; r := !s
共享状态
let c = ref 0 in let incc = \_:Unit, (c := succ (!c); !c) in let decc = \_:Unit, (c := pred (!c); !c) in ...
对象
newcounter =
\_:Unit,
let c = ref 0 in
let incc = \_:Unit, (c := succ (!c); !c) in
let decc = \_:Unit, (c := pred (!c); !c) in
{i=incc, d=decc}
let c1 = newcounter unit in let c2 = newcounter unit in // we've allocated two separate storage cells now! let r1 = c1.i unit in let r2 = c2.i unit in r2 // yields 1, not 2!
练习: 1 star,标准,可选 (store_draw)
(在纸上)画出执行到前两个 let 已完成、 第三个即将开始时存储的内容。
(* 在这里填写 *)
☐
☐
指向复合类型的引用
equal =
fix
(\eq:Nat->Nat->Bool,
\m:Nat, \n:Nat,
if m=0 then iszero n
else if n=0 then false
else eq (pred m) (pred n))
newarray = \_:Unit, ref (\n:Nat,0)
lookup = \a:NatArray, \n:Nat, (!a) n
update = \a:NatArray, \m:Nat, \v:Nat,
let oldf = !a in
a := (\n:Nat, if equal m n then v else oldf n);
练习: 2 stars,高级,特别有用 (compact_update)
若我们更紧凑地定义 update 如下
update = \a:NatArray, \m:Nat, \v:Nat,
a := (\n:Nat, if equal m n then v else (!a) n)
行为会一样吗?
(* 在这里填写 *)
(* 不要修改下面这行: *)
Definition manual_grade_for_compact_update : option (nat×string) := None.
☐
(* 不要修改下面这行: *)
Definition manual_grade_for_compact_update : option (nat×string) := None.
☐
空引用
垃圾回收
练习: 2 stars,标准 (type_safety_violation)
说明这如何会导致类型安全被违反。
(* 在这里填写 *)
(* 不要修改下面这行: *)
Definition manual_grade_for_type_safety_violation : option (nat×string) := None.
☐
(* 不要修改下面这行: *)
Definition manual_grade_for_type_safety_violation : option (nat×string) := None.
☐
位置
存储
我们用 store_lookup n st 取存储 st
中位置 n 处引用单元的值。
注意必须给 nth 一个默认值,
以备我们试图查找过大索引。
(事实上我们从不真正这样做,
但【证明】我们不这样做需要一点工作。)
要更新存储,我们使用 replace 函数,
它替换特定索引处单元的内容。
Fixpoint replace {A:Type} (n:nat) (x:A) (l:list A) : list A :=
match l with
| nil ⇒ nil
| h :: t ⇒
match n with
| O ⇒ x :: t
| S n' ⇒ h :: replace n' x t
end
end.
match l with
| nil ⇒ nil
| h :: t ⇒
match n with
| O ⇒ x :: t
| S n' ⇒ h :: replace n' x t
end
end.
不出所料,我们还需要一些关于 replace 的技术引理;
它们的证明很直接。
Lemma replace_nil : ∀ A n (x:A),
replace n x nil = nil.
Lemma length_replace : ∀ A n x (l:list A),
length (replace n x l) = length l.
Lemma lookup_replace_eq : ∀ l t st,
l < length st →
store_lookup l (replace l t st) = t.
Lemma lookup_replace_neq : ∀ l1 l2 t st,
l1 ≠ l2 →
store_lookup l1 (replace l2 t st) = store_lookup l1 st.
replace n x nil = nil.
Lemma length_replace : ∀ A n x (l:list A),
length (replace n x l) = length l.
Proof with auto.
intros A n x l. generalize dependent n.
induction l; intros n.
destruct n...
destruct n...
simpl. rewrite IHl...
Qed.
intros A n x l. generalize dependent n.
induction l; intros n.
destruct n...
destruct n...
simpl. rewrite IHl...
Qed.
Lemma lookup_replace_eq : ∀ l t st,
l < length st →
store_lookup l (replace l t st) = t.
Proof with auto.
intros l t st.
unfold store_lookup.
generalize dependent l.
induction st as [|t' st']; intros l Hlen.
- (* st = *)
inversion Hlen.
- (* st = t' :: st' *)
destruct l; simpl...
apply IHst'. simpl in Hlen. lia.
Qed.
intros l t st.
unfold store_lookup.
generalize dependent l.
induction st as [|t' st']; intros l Hlen.
- (* st = *)
inversion Hlen.
- (* st = t' :: st' *)
destruct l; simpl...
apply IHst'. simpl in Hlen. lia.
Qed.
Lemma lookup_replace_neq : ∀ l1 l2 t st,
l1 ≠ l2 →
store_lookup l1 (replace l2 t st) = store_lookup l1 st.
Proof with auto.
unfold store_lookup.
induction l1 as [|l1']; intros l2 t st Hneq.
- (* l1 = 0 *)
destruct st.
+ (* st = *) rewrite replace_nil...
+ (* st = _ :: _ *) destruct l2... contradict Hneq...
- (* l1 = S l1' *)
destruct st as [|t2 st2].
+ (* st = *) destruct l2...
+ (* st = t2 :: st2 *)
destruct l2...
simpl; apply IHl1'...
Qed.
unfold store_lookup.
induction l1 as [|l1']; intros l2 t st Hneq.
- (* l1 = 0 *)
destruct st.
+ (* st = *) rewrite replace_nil...
+ (* st = _ :: _ *) destruct l2... contradict Hneq...
- (* l1 = S l1' *)
destruct st as [|t2 st2].
+ (* st = *) destruct l2...
+ (* st = t2 :: st2 *)
destruct l2...
simpl; apply IHl1'...
Qed.
归约
| (ST_RefValue) | |
| ref v / st --> loc |st| / st,v |
| t1 / st --> t1' / st' | (ST_Ref) |
| ref t1 / st --> ref t1' / st' |
| l < |st| | (ST_DerefLoc) |
| !(loc l) / st --> lookup l st / st |
| t1 / st --> t1' / st' | (ST_Deref) |
| !t1 / st --> !t1' / st' |
| l < |st| | (ST_Assign) |
| loc l := v / st --> unit / replace l v st |
| t1 / st --> t1' / st' | (ST_Assign1) |
| t1 := t2 / st --> t1' := t2 / st' |
| t2 / st --> t2' / st' | (ST_Assign2) |
| v1 := t2 / st --> v1 := t2' / st' |
| value v2 | (ST_AppAbs) |
| (\x:T2,t1) v2 / st --> [x:=v2]t1 / st |
| t1 / st --> t1' / st' | (ST_App1) |
| t1 t2 / st --> t1' t2 / st' |
| value v1 t2 / st --> t2' / st' | (ST_App2) |
| v1 t2 / st --> v1 t2' / st' |
| t1 / st --> t1' / st' | (ST_Deref) |
| !t1 / st --> !t1' / st' |
| l < |st| | (ST_DerefLoc) |
| !(loc l) / st --> lookup l st / st |
| t1 / st --> t1' / st' | (ST_Assign1) |
| t1 := t2 / st --> t1' := t2 / st' |
| t2 / st --> t2' / st' | (ST_Assign2) |
| v1 := t2 / st --> v1 := t2' / st' |
| l < |st| | (ST_Assign) |
| loc l := v / st --> unit / [l:=v]st |
| t1 / st --> t1' / st' | (ST_Ref) |
| ref t1 / st --> ref t1' / st' |
| (ST_RefValue) | |
| ref v / st --> loc |st| / st,v |
Inductive step : tm × store → tm × store → Prop :=
| ST_AppAbs : ∀ x T2 t1 v2 st,
value v2 →
<{ (\x : T2, t1) v2 }> / st --> <{ [x := v2] t1 }> / st
| ST_App1 : ∀ t1 t1' t2 st st',
t1 / st --> t1' / st' →
<{ t1 t2 }> / st --> <{ t1' t2 }> / st'
| ST_App2 : ∀ v1 t2 t2' st st',
value v1 →
t2 / st --> t2' / st' →
<{ v1 t2 }> / st --> <{ v1 t2' }> / st'
(* numbers *)
| ST_SuccNat : ∀ (n : nat) st,
<{ succ n }> / st --> tm_const (S n) / st
| ST_Succ : ∀ t1 t1' st st',
t1 / st --> t1' / st' →
<{ succ t1 }> / st --> <{ succ t1' }> / st'
| ST_PredNat : ∀ (n : nat) st,
<{ pred n }> / st --> tm_const (n - 1) / st
| ST_Pred : ∀ t1 t1' st st',
t1 / st --> t1' / st' →
<{ pred t1 }> / st --> <{ pred t1' }> / st'
| ST_MultNats : ∀ (n1 n2 : nat) st,
<{ n1 × n2 }> / st --> tm_const (n1 × n2) / st
| ST_Mult1 : ∀ t1 t2 t1' st st',
t1 / st --> t1' / st' →
<{ t1 × t2 }> / st --> <{ t1' × t2 }> / st'
| ST_Mult2 : ∀ v1 t2 t2' st st',
value v1 →
t2 / st --> t2' / st' →
<{ v1 × t2 }> / st --> <{ v1 × t2' }> / st'
| ST_If0 : ∀ t1 t1' t2 t3 st st',
t1 / st --> t1' / st' →
<{ if0 t1 then t2 else t3 }> / st --> <{ if0 t1' then t2 else t3 }> / st'
| ST_If0_Zero : ∀ t2 t3 st,
<{ if0 0 then t2 else t3 }> / st --> t2 / st
| ST_If0_Nonzero : ∀ n t2 t3 st,
<{ if0 $(S n) then t2 else t3 }> / st --> t3 / st
(* references *)
| ST_RefValue : ∀ v st,
value v →
<{ ref v }> / st --> <{ loc $(length st) }> / (st ++ v::nil)
| ST_Ref : ∀ t1 t1' st st',
t1 / st --> t1' / st' →
<{ ref t1 }> / st --> <{ ref t1' }> / st'
| ST_DerefLoc : ∀ st l,
l < length st →
<{ !(loc l) }> / st --> <{ $(store_lookup l st) }> / st
| ST_Deref : ∀ t1 t1' st st',
t1 / st --> t1' / st' →
<{ ! t1 }> / st --> <{ ! t1' }> / st'
| ST_Assign : ∀ v l st,
value v →
l < length st →
<{ (loc l) := v }> / st --> <{ unit }> / replace l v st
| ST_Assign1 : ∀ t1 t1' t2 st st',
t1 / st --> t1' / st' →
<{ t1 := t2 }> / st --> <{ t1' := t2 }> / st'
| ST_Assign2 : ∀ v1 t2 t2' st st',
value v1 →
t2 / st --> t2' / st' →
<{ v1 := t2 }> / st --> <{ v1 := t2' }> / st'
where "t '/' st '-->' t' '/' st'" := (step (t,st) (t',st')).
这里应指出一个略显难看之处:
在 ST_RefValue 规则中,
我们写 st ++ v::nil 而非更自然的 st ++ [v]
来扩展状态。
原因是我们为替换定义的记号使用方括号,
它与标准库的列表记号冲突。
Hint Constructors step : core.
Definition multistep := (multi step).
Notation "t '/' st '-->*' t' '/' st'" :=
(multistep (t,st) (t',st'))
(at level 40, st at level 39, t' at level 39).
Definition multistep := (multi step).
Notation "t '/' st '-->*' t' '/' st'" :=
(multistep (t,st) (t',st'))
(at level 40, st at level 39, t' at level 39).
存储类型
| Gamma |-- lookup l st : T1 | |
| Gamma |-- loc l : Ref T1 |
| Gamma; st |-- lookup l st : T1 | |
| Gamma; st |-- loc l : Ref T1 |
[\x:Nat, (!(loc 1)) x, \x:Nat, (!(loc 0)) x]
练习: 3 stars,标准 (cyclic_store)
你能找到一个项, 其归约会创建这个特定的循环存储吗?
Theorem cyclic_store:
∃ t,
t / nil -->*
<{ unit }> / (<{ \x:Nat, (!(loc 1)) x }> :: <{ \x:Nat, (!(loc 0)) x }> :: nil).
Proof.
(* 在这里填写 *) Admitted.
☐
∃ t,
t / nil -->*
<{ unit }> / (<{ \x:Nat, (!(loc 1)) x }> :: <{ \x:Nat, (!(loc 0)) x }> :: nil).
Proof.
(* 在这里填写 *) Admitted.
☐
store_Tlookup 函数取出特定索引处的类型。
假设给定描述存储 st 的存储类型 ST,
某项 t 将在其中被归约。
那么我们可以用 ST 计算 t 结果的类型,
而从不直接查看 st。
例如,若 ST 是 [Unit, Unit→Unit],
我们可立即推出 !(loc 1) 类型为 Unit→Unit。
更一般地,位置的类型规则
可用存储类型重新表述如下:
也就是说,只要 l 是有效位置,
我们就可通过在 ST 中查找来计算 l 的类型。
类型判断再次是四元关系,
但以存储【类型】而非具体存储为参数。
其余类型规则类似地用存储类型扩展。
| l < |ST| | |
| Gamma; ST |-- loc l : Ref (store_Tlookup l ST) |
类型关系
| l < |ST| | (T_Loc) |
| Gamma; ST |-- loc l : Ref (store_Tlookup l ST) |
| Gamma; ST |-- t1 : T1 | (T_Ref) |
| Gamma; ST |-- ref t1 : Ref T1 |
| Gamma; ST |-- t1 : Ref T1 | (T_Deref) |
| Gamma; ST |-- !t1 : T1 |
| Gamma; ST |-- t1 : Ref T2 | |
| Gamma; ST |-- t2 : T2 | (T_Assign) |
| Gamma; ST |-- t1 := t2 : Unit |
Inductive has_type (ST : store_ty) : context → tm → ty → Prop :=
| T_Var : ∀ Gamma x T1,
Gamma x = Some T1 →
<{ Gamma / ST |-- x \in T1 }>
| T_Abs : ∀ Gamma x T1 T2 t1,
<{ x ⊢> T2; Gamma / ST |-- t1 \in T1 }> →
<{ Gamma / ST |-- \x:T2, t1 \in T2 → T1 }>
| T_App : ∀ T1 T2 Gamma t1 t2,
<{ Gamma / ST |-- t1 \in T2 → T1 }> →
<{ Gamma / ST |-- t2 \in T2 }> →
<{ Gamma / ST |-- t1 t2 \in T1 }>
| T_Nat : ∀ Gamma (n : nat),
<{ Gamma / ST |-- n \in Nat }>
| T_Succ : ∀ Gamma t1,
<{ Gamma / ST |-- t1 \in Nat }> →
<{ Gamma / ST |-- succ t1 \in Nat }>
| T_Pred : ∀ Gamma t1,
<{ Gamma / ST |-- t1 \in Nat }> →
<{ Gamma / ST |-- pred t1 \in Nat }>
| T_Mult : ∀ Gamma t1 t2,
<{ Gamma / ST |-- t1 \in Nat }> →
<{ Gamma / ST |-- t2 \in Nat }> →
<{ Gamma / ST |-- t1 × t2 \in Nat }>
| T_If0 : ∀ Gamma t1 t2 t3 T0,
<{ Gamma / ST |-- t1 \in Nat }> →
<{ Gamma / ST |-- t2 \in T0 }> →
<{ Gamma / ST |-- t3 \in T0 }> →
<{ Gamma / ST |-- if0 t1 then t2 else t3 \in T0 }>
| T_Unit : ∀ Gamma,
<{ Gamma / ST |-- unit \in Unit }>
| T_Loc : ∀ Gamma l,
l < length ST →
<{ Gamma / ST |-- (loc l) \in Ref $(store_Tlookup l ST) }>
| T_Ref : ∀ Gamma t1 T1,
<{ Gamma / ST |-- t1 \in T1 }> →
<{ Gamma / ST |-- (ref t1) \in Ref T1 }>
| T_Deref : ∀ Gamma t1 T1,
<{ Gamma / ST |-- t1 \in Ref T1 }> →
<{ Gamma / ST |-- (! t1) \in T1 }>
| T_Assign : ∀ Gamma t1 t2 T2,
<{ Gamma / ST |-- t1 \in Ref T2 }> →
<{ Gamma / ST |-- t2 \in T2 }> →
<{ Gamma / ST |-- (t1 := t2) \in Unit }>
where "<{ Gamma '/' ST '|--' t '∈' T }>" := (has_type ST Gamma t T).
Hint Constructors has_type : core.
只有当归约使用的具体存储
符合类型检查时假设的存储类型,
这些类型规则才能准确预测归约结果。
这一前提与基础 STLC 中自由变量的情形平行。
替换引理承诺:若 Gamma |-- t : T,
则可用 Gamma 中列出的类型的值
替换 t 中的自由变量,
得到类型 T 的闭项。
根据类型保持定理,
若它产生任何结果,
将归约为类型 T 的最终结果。
下文将看到如何为存储与存储类型
形式化类似的直观想法。
但为对程序员真正书写的项做类型检查,
无需做任何棘手之事去猜存储类型。
具体位置只出现在作为归约中间结果的项中;
它们不在程序员书写的语言里。
因此,我们只需相对于【空】存储类型
对程序员的项做类型检查。
随着归约创建新位置,
只要看新分配单元中初始值的类型,
就知道如何扩展存储类型。
这一直观想法将在类型保持定理中形式化。
性质
Theorem preservation_wrong : ∀ ST T t st t' st',
<{ empty / ST |-- t \in T }> →
t / st --> t' / st' →
<{ empty / ST |-- t' \in T }>.
Abort.
<{ empty / ST |-- t \in T }> →
t / st --> t' / st' →
<{ empty / ST |-- t' \in T }>.
Abort.
若相对一组关于存储中值类型的假设做类型检查,
再对违反这些假设的存储归约,
结果将是灾难。
我们说存储 st 相对于存储类型 ST 是【良型的】,
若 st 中每个位置 l 处的项
具有 ST 中位置 l 处的类型。
由于只有闭项 从来会被存入位置(为什么?),
在空上下文中给它们定类型就足够了。
下面 store_well_typed 的定义将此形式化。
Definition store_well_typed (ST:store_ty) (st:store) :=
length ST = length st ∧
(∀ l, l < length st →
<{ empty / ST |-- $(store_lookup l st)
\in $(store_Tlookup l ST) }>).
length ST = length st ∧
(∀ l, l < length st →
<{ empty / ST |-- $(store_lookup l st)
\in $(store_Tlookup l ST) }>).
非形式地,我们用 ST |-- st 表示 store_well_typed ST st。
直观地说,存储 st 与存储类型 ST 一致,
若存储中每个值都具有存储类型所预测的类型。
唯一微妙之处在于,给存储中的值定类型时,
我们向类型关系提供的是同一个存储类型。
这使我们能够为上面见过的循环存储定类型。
练习: 3 stars,标准 (store_not_unique)
你能找到存储 st 与两个不同的存储类型 ST1、ST2, 使得 ST1 |-- st 与 ST2 |-- st 都成立吗?
(* 在这里填写 *)
Theorem store_not_unique:
∃ st, ∃ ST1, ∃ ST2,
store_well_typed ST1 st ∧
store_well_typed ST2 st ∧
ST1 ≠ ST2.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem store_not_unique:
∃ st, ∃ ST1, ∃ ST2,
store_well_typed ST1 st ∧
store_well_typed ST2 st ∧
ST1 ≠ ST2.
Proof.
(* 在这里填写 *) Admitted.
☐
Theorem preservation_lesswrong : ∀ ST T t st t' st',
<{ empty / ST |-- t \in T }> →
t / st --> t' / st' →
store_well_typed ST st →
<{ empty / ST |-- t' \in T }>.
Abort.
<{ empty / ST |-- t \in T }> →
t / st --> t' / st' →
store_well_typed ST st →
<{ empty / ST |-- t' \in T }>.
Abort.
除分配规则 ST_RefValue 外,
该陈述对所有归约规则都成立。
问题在于该规则给出的存储,
其定义域比初始存储更大。
这会证伪上述陈述的结论。
若 st' 含新位置 l 的绑定,
则 l 不可能在 ST 的定义域中。
而 t' 确实提到 l,
在 ST 下将不可定类型。
例如,项 ref 5 在空上下文与空存储类型下可定类型,
但它步进到 loc 0,
而 loc 0 在空存储类型下不可定类型。
扩展存储类型
Inductive extends : store_ty → store_ty → Prop :=
| extends_nil : ∀ ST',
extends ST' nil
| extends_cons : ∀ x ST' ST,
extends ST' ST →
extends (x::ST') (x::ST).
| extends_nil : ∀ ST',
extends ST' nil
| extends_cons : ∀ x ST' ST,
extends ST' ST →
extends (x::ST') (x::ST).
例如,含单个类型为 Nat 位置的存储类型 [Nat]
扩展了空存储类型。
我们需要一些关于扩展上下文的技术引理。
首先,在扩展存储类型中查找类型
与在原存储类型中得到相同结果:
Lemma extends_lookup : ∀ l ST ST',
l < length ST →
extends ST' ST →
store_Tlookup l ST' = store_Tlookup l ST.
l < length ST →
extends ST' ST →
store_Tlookup l ST' = store_Tlookup l ST.
Proof with auto.
intros l ST.
generalize dependent l.
induction ST as [|a ST2]; intros l ST' Hlen HST'.
- (* nil *) inversion Hlen.
- (* cons *) unfold store_Tlookup in ×.
destruct ST'.
+ (* ST' = nil *) inversion HST'.
+ (* ST' = a' :: ST'2 *)
inversion HST'; subst.
destruct l as [|l'].
× (* l = 0 *) auto.
× (* l = S l' *) simpl. apply IHST2...
simpl in Hlen; lia.
Qed.
intros l ST.
generalize dependent l.
induction ST as [|a ST2]; intros l ST' Hlen HST'.
- (* nil *) inversion Hlen.
- (* cons *) unfold store_Tlookup in ×.
destruct ST'.
+ (* ST' = nil *) inversion HST'.
+ (* ST' = a' :: ST'2 *)
inversion HST'; subst.
destruct l as [|l'].
× (* l = 0 *) auto.
× (* l = S l' *) simpl. apply IHST2...
simpl in Hlen; lia.
Qed.
其次,若 ST' 扩展 ST,
则 ST' 的长度至少与 ST 一样长。
最后,ST ++ T 扩展 ST,且 extends 是自反的。
Definition preservation_theorem := ∀ ST t t' T st st',
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
t / st --> t' / st' →
∃ ST',
extends ST' ST ∧
<{ empty / ST' |-- t' \in T }> ∧
store_well_typed ST' st'.
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
t / st --> t' / st' →
∃ ST',
extends ST' ST ∧
<{ empty / ST' |-- t' \in T }> ∧
store_well_typed ST' st'.
保持性定理仅断言存在【某个】
扩展 ST 的存储类型 ST',
使得新项 t' 相对于 ST' 是良型的。
这里 ST' 在所有旧位置上与 ST 一致,
但定理并未确切告诉我们 ST' 是什么。
直观上,ST' 要么是 ST,
要么精确地是 ST ++ T1::nil,
其中 T1 是扩展存储 st ++ v1::nil
中值 v1 的类型。
但明确陈述会使定理更复杂,却未必更有用。
上面较弱的版本已经具有正确形式。
其结论蕴含其假设,
可以反复「转曲柄」,
推出每个【归约序列】保持良型性。
与前进性性质结合,
我们得到通常的保证:
「良型程序绝不出错。」
为证明这一点,照例需要几条引理。
Lemma weakening : ∀ Gamma Gamma' ST t T,
includedin Gamma Gamma' →
<{ Gamma / ST |-- t \in T }> →
<{ Gamma' / ST |-- t \in T }>.
Proof.
intros Gamma Gamma' ST t T H Ht.
generalize dependent Gamma'.
induction Ht; eauto using includedin_update.
Qed.
Lemma weakening_empty : ∀ Gamma ST t T,
<{ empty / ST |-- t \in T }> →
<{ Gamma / ST |-- t \in T }>.
Proof.
intros Gamma ST t T.
eapply weakening.
discriminate.
Qed.
Lemma substitution_preserves_typing : ∀ Gamma ST x U t v T,
<{ x ⊢> U; Gamma / ST |-- t \in T }> →
<{ empty / ST |-- v \in U }> →
<{ Gamma / ST |-- [x:=v]t \in T }>.
includedin Gamma Gamma' →
<{ Gamma / ST |-- t \in T }> →
<{ Gamma' / ST |-- t \in T }>.
Proof.
intros Gamma Gamma' ST t T H Ht.
generalize dependent Gamma'.
induction Ht; eauto using includedin_update.
Qed.
Lemma weakening_empty : ∀ Gamma ST t T,
<{ empty / ST |-- t \in T }> →
<{ Gamma / ST |-- t \in T }>.
Proof.
intros Gamma ST t T.
eapply weakening.
discriminate.
Qed.
Lemma substitution_preserves_typing : ∀ Gamma ST x U t v T,
<{ x ⊢> U; Gamma / ST |-- t \in T }> →
<{ empty / ST |-- v \in U }> →
<{ Gamma / ST |-- [x:=v]t \in T }>.
Proof.
intros Gamma ST x U t v T Ht Hv.
generalize dependent Gamma. generalize dependent T.
induction t; intros T Gamma H;
(* in each case, we'll want to get at the derivation of H *)
inversion H; clear H; subst; simpl; eauto.
- (* var *)
rename s into y. destruct (String.eqb_spec x y); subst.
+ (* x=y *)
rewrite update_eq in H2.
injection H2 as H2; subst.
apply weakening_empty. assumption.
+ (* x<>y *)
apply T_Var. rewrite update_neq in H2; auto.
- (* abs *)
rename s into y.
destruct (String.eqb_spec x y); subst; apply T_Abs.
+ (* x=y *)
rewrite update_shadow in H5. assumption.
+ (* x<>y *)
apply IHt.
rewrite update_permute; auto.
Qed.
intros Gamma ST x U t v T Ht Hv.
generalize dependent Gamma. generalize dependent T.
induction t; intros T Gamma H;
(* in each case, we'll want to get at the derivation of H *)
inversion H; clear H; subst; simpl; eauto.
- (* var *)
rename s into y. destruct (String.eqb_spec x y); subst.
+ (* x=y *)
rewrite update_eq in H2.
injection H2 as H2; subst.
apply weakening_empty. assumption.
+ (* x<>y *)
apply T_Var. rewrite update_neq in H2; auto.
- (* abs *)
rename s into y.
destruct (String.eqb_spec x y); subst; apply T_Abs.
+ (* x=y *)
rewrite update_shadow in H5. assumption.
+ (* x<>y *)
apply IHt.
rewrite update_permute; auto.
Qed.
Lemma assign_pres_store_typing : ∀ ST st l t,
l < length st →
store_well_typed ST st →
<{ empty / ST |-- t \in $(store_Tlookup l ST) }> →
store_well_typed ST (replace l t st).
l < length st →
store_well_typed ST st →
<{ empty / ST |-- t \in $(store_Tlookup l ST) }> →
store_well_typed ST (replace l t st).
Proof with auto.
intros ST st l t Hlen HST Ht.
inversion HST; subst.
split. rewrite length_replace...
intros l' Hl'.
destruct (l' =? l) eqn: Heqll'.
- (* l' = l *)
apply eqb_eq in Heqll'; subst.
rewrite lookup_replace_eq...
- (* l' <> l *)
apply eqb_neq in Heqll'.
rewrite lookup_replace_neq...
rewrite length_replace in Hl'.
apply H0...
Qed.
intros ST st l t Hlen HST Ht.
inversion HST; subst.
split. rewrite length_replace...
intros l' Hl'.
destruct (l' =? l) eqn: Heqll'.
- (* l' = l *)
apply eqb_eq in Heqll'; subst.
rewrite lookup_replace_eq...
- (* l' <> l *)
apply eqb_neq in Heqll'.
rewrite lookup_replace_neq...
rewrite length_replace in Hl'.
apply H0...
Qed.
Lemma store_weakening : ∀ Gamma ST ST' t T,
extends ST' ST →
<{ Gamma / ST |-- t \in T }> →
<{ Gamma / ST' |-- t \in T }>.
extends ST' ST →
<{ Gamma / ST |-- t \in T }> →
<{ Gamma / ST' |-- t \in T }>.
可用 store_weakening 引理证明:
若存储相对于存储类型是良型的,
则用 t 扩展后的存储,
相对于用 t 的类型扩展后的存储类型,
仍良型。
Lemma store_well_typed_app : ∀ ST st t1 T1,
store_well_typed ST st →
<{ empty / ST |-- t1 \in T1 }> →
store_well_typed (ST ++ T1::nil) (st ++ t1::nil).
store_well_typed ST st →
<{ empty / ST |-- t1 \in T1 }> →
store_well_typed (ST ++ T1::nil) (st ++ t1::nil).
Proof with auto.
intros.
unfold store_well_typed in ×.
destruct H as [Hlen Hmatch].
rewrite app_length, add_comm. simpl.
rewrite app_length, add_comm. simpl.
split...
- (* types match. *)
intros l Hl.
unfold store_lookup, store_Tlookup.
apply le_lt_eq_dec in Hl; destruct Hl as [Hlt | Heq].
+ (* l < length st *)
apply <-Nat.succ_lt_mono in Hlt.
rewrite !app_nth1...
× apply store_weakening with ST. apply extends_app.
apply Hmatch...
× rewrite Hlen...
+ (* l = length st *)
injection Heq as Heq; subst.
rewrite app_nth2; try lia.
rewrite <- Hlen.
rewrite sub_diag. simpl.
apply store_weakening with ST...
{ apply extends_app. }
rewrite app_nth2; [|lia].
rewrite sub_diag. simpl. assumption.
Qed.
intros.
unfold store_well_typed in ×.
destruct H as [Hlen Hmatch].
rewrite app_length, add_comm. simpl.
rewrite app_length, add_comm. simpl.
split...
- (* types match. *)
intros l Hl.
unfold store_lookup, store_Tlookup.
apply le_lt_eq_dec in Hl; destruct Hl as [Hlt | Heq].
+ (* l < length st *)
apply <-Nat.succ_lt_mono in Hlt.
rewrite !app_nth1...
× apply store_weakening with ST. apply extends_app.
apply Hmatch...
× rewrite Hlen...
+ (* l = length st *)
injection Heq as Heq; subst.
rewrite app_nth2; try lia.
rewrite <- Hlen.
rewrite sub_diag. simpl.
apply store_weakening with ST...
{ apply extends_app. }
rewrite app_nth2; [|lia].
rewrite sub_diag. simpl. assumption.
Qed.
下面 终于给出保持性定理:
Theorem preservation : ∀ ST t t' T st st',
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
t / st --> t' / st' →
∃ ST',
extends ST' ST ∧
<{ empty / ST' |-- t' \in T }> ∧
store_well_typed ST' st'.
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
t / st --> t' / st' →
∃ ST',
extends ST' ST ∧
<{ empty / ST' |-- t' \in T }> ∧
store_well_typed ST' st'.
Proof with eauto using store_weakening, extends_refl.
remember empty as Gamma.
intros ST t t' T st st' Ht.
generalize dependent t'.
induction Ht; intros t' HST Hstep;
subst; try solve_by_invert; inversion Hstep; subst;
try (eauto using store_weakening, extends_refl).
(* T_App *)
- (* ST_AppAbs *) ∃ ST.
inversion Ht1; subst.
split; try split... eapply substitution_preserves_typing...
- (* ST_App1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_App2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_Succ *)
+ (* ST_Succ *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_Pred *)
+ (* ST_Pred *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Mult *)
- (* ST_Mult1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_Mult2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_If0 *)
+ (* ST_If0_1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'. split...
(* T_Ref *)
- (* ST_RefValue *)
∃ (ST ++ T1::nil).
inversion HST; subst.
split.
{ apply extends_app. }
split.
{ replace <{{ Ref T1 }}>
with <{{ Ref $(store_Tlookup (length st) (ST ++ T1::nil)) }}>.
{ apply T_Loc.
rewrite <- H. rewrite app_length, add_comm. simpl. lia. }
unfold store_Tlookup. rewrite <- H. rewrite nth_eq_last.
reflexivity. }
apply store_well_typed_app; assumption.
- (* ST_Ref *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Deref *)
- (* ST_DerefLoc *)
∃ ST. split; try split...
destruct HST as [_ Hsty].
replace T1 with (store_Tlookup l ST).
apply Hsty...
inversion Ht; subst...
- (* ST_Deref *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Assign *)
- (* ST_Assign *)
∃ ST. split; try split...
eapply assign_pres_store_typing...
inversion Ht1; subst...
- (* ST_Assign1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_Assign2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
Qed.
remember empty as Gamma.
intros ST t t' T st st' Ht.
generalize dependent t'.
induction Ht; intros t' HST Hstep;
subst; try solve_by_invert; inversion Hstep; subst;
try (eauto using store_weakening, extends_refl).
(* T_App *)
- (* ST_AppAbs *) ∃ ST.
inversion Ht1; subst.
split; try split... eapply substitution_preserves_typing...
- (* ST_App1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_App2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_Succ *)
+ (* ST_Succ *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_Pred *)
+ (* ST_Pred *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Mult *)
- (* ST_Mult1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_Mult2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* T_If0 *)
+ (* ST_If0_1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'. split...
(* T_Ref *)
- (* ST_RefValue *)
∃ (ST ++ T1::nil).
inversion HST; subst.
split.
{ apply extends_app. }
split.
{ replace <{{ Ref T1 }}>
with <{{ Ref $(store_Tlookup (length st) (ST ++ T1::nil)) }}>.
{ apply T_Loc.
rewrite <- H. rewrite app_length, add_comm. simpl. lia. }
unfold store_Tlookup. rewrite <- H. rewrite nth_eq_last.
reflexivity. }
apply store_well_typed_app; assumption.
- (* ST_Ref *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Deref *)
- (* ST_DerefLoc *)
∃ ST. split; try split...
destruct HST as [_ Hsty].
replace T1 with (store_Tlookup l ST).
apply Hsty...
inversion Ht; subst...
- (* ST_Deref *)
eapply IHHt in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
(* T_Assign *)
- (* ST_Assign *)
∃ ST. split; try split...
eapply assign_pres_store_typing...
inversion Ht1; subst...
- (* ST_Assign1 *)
eapply IHHt1 in H0...
destruct H0 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
- (* ST_Assign2 *)
eapply IHHt2 in H5...
destruct H5 as [ST' [Hext [Hty Hsty]]].
∃ ST'...
Qed.
练习: 3 stars,标准,可选 (preservation_informal)
为保持性定理写一份仔细的非形式化证明, 集中于 T_App、T_Deref、T_Assign 与 T_Ref 情形。
Theorem progress : ∀ ST t T st,
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
(value t ∨ ∃ t' st', t / st --> t' / st').
<{ empty / ST |-- t \in T }> →
store_well_typed ST st →
(value t ∨ ∃ t' st', t / st --> t' / st').
Proof with eauto.
intros ST t T st Ht HST. remember empty as Gamma.
induction Ht; subst; try solve_by_invert...
- (* T_App *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve_by_invert.
destruct IHHt2 as [Ht2p | Ht2p]...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ (\ x0 : T0, t0) t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' t2 }>, st'...
- (* T_Succ *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [ inversion Ht ].
× (* t1 is a const *)
∃ <{ $(S n) }>, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ succ t1' }>, st'...
- (* T_Pred *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht ].
× (* t1 is a const *)
∃ <{ $(n - 1) }>, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ pred t1' }>, st'...
- (* T_Mult *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht1].
destruct IHHt2 as [Ht2p | Ht2p]...
× (* t2 is a value *)
inversion Ht2p; subst; try solve [inversion Ht2].
∃ <{ $(n × n0) }>, st...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ n × t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' × t2 }>, st'...
- (* T_If0 *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht1].
destruct n.
× (* n = 0 *) ∃ t2, st...
× (* n = S n' *) ∃ t3, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ if0 t1' then t2 else t3 }>, st'...
- (* T_Ref *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ref t1'}>, st'...
- (* T_Deref *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve_by_invert.
eexists. eexists. apply ST_DerefLoc...
inversion Ht; subst. inversion HST; subst.
rewrite <- H...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ ! t1' }>, st'...
- (* T_Assign *)
right. destruct IHHt1 as [Ht1p|Ht1p]...
+ (* t1 is a value *)
destruct IHHt2 as [Ht2p|Ht2p]...
× (* t2 is a value *)
inversion Ht1p; subst; try solve_by_invert.
eexists. eexists. apply ST_Assign...
inversion HST; subst. inversion Ht1; subst.
rewrite H in H4...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ t1 := t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' := t2 }>, st'...
Qed.
intros ST t T st Ht HST. remember empty as Gamma.
induction Ht; subst; try solve_by_invert...
- (* T_App *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve_by_invert.
destruct IHHt2 as [Ht2p | Ht2p]...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ (\ x0 : T0, t0) t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' t2 }>, st'...
- (* T_Succ *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [ inversion Ht ].
× (* t1 is a const *)
∃ <{ $(S n) }>, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ succ t1' }>, st'...
- (* T_Pred *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht ].
× (* t1 is a const *)
∃ <{ $(n - 1) }>, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ pred t1' }>, st'...
- (* T_Mult *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht1].
destruct IHHt2 as [Ht2p | Ht2p]...
× (* t2 is a value *)
inversion Ht2p; subst; try solve [inversion Ht2].
∃ <{ $(n × n0) }>, st...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ n × t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' × t2 }>, st'...
- (* T_If0 *)
right. destruct IHHt1 as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve [inversion Ht1].
destruct n.
× (* n = 0 *) ∃ t2, st...
× (* n = S n' *) ∃ t3, st...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ if0 t1' then t2 else t3 }>, st'...
- (* T_Ref *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ref t1'}>, st'...
- (* T_Deref *)
right. destruct IHHt as [Ht1p | Ht1p]...
+ (* t1 is a value *)
inversion Ht1p; subst; try solve_by_invert.
eexists. eexists. apply ST_DerefLoc...
inversion Ht; subst. inversion HST; subst.
rewrite <- H...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ ! t1' }>, st'...
- (* T_Assign *)
right. destruct IHHt1 as [Ht1p|Ht1p]...
+ (* t1 is a value *)
destruct IHHt2 as [Ht2p|Ht2p]...
× (* t2 is a value *)
inversion Ht1p; subst; try solve_by_invert.
eexists. eexists. apply ST_Assign...
inversion HST; subst. inversion Ht1; subst.
rewrite H in H4...
× (* t2 steps *)
destruct Ht2p as [t2' [st' Hstep]].
∃ <{ t1 := t2' }>, st'...
+ (* t1 steps *)
destruct Ht1p as [t1' [st' Hstep]].
∃ <{ t1' := t2 }>, st'...
Qed.
引用与非终止
(\r:Ref (Unit -> Unit),
r := (\x:Unit,(!r) unit); (!r) unit)
(ref (\x:Unit,unit))
Module ExampleVariables.
Open Scope string_scope.
Definition x := "x".
Definition y := "y".
Definition r := "r".
Definition s := "s".
End ExampleVariables.
Module RefsAndNontermination.
Import ExampleVariables.
Definition loop_fun :=
<{ \x : Unit, (!r) unit }>.
Definition loop :=
<{ (\r : Ref (Unit → Unit),
(( r := loop_fun );
( (! r) unit ) ))
(ref (\x : Unit, unit)) }> .
Open Scope string_scope.
Definition x := "x".
Definition y := "y".
Definition r := "r".
Definition s := "s".
End ExampleVariables.
Module RefsAndNontermination.
Import ExampleVariables.
Definition loop_fun :=
<{ \x : Unit, (!r) unit }>.
Definition loop :=
<{ (\r : Ref (Unit → Unit),
(( r := loop_fun );
( (! r) unit ) ))
(ref (\x : Unit, unit)) }> .
该项是良型的:
Import ListNotations.
Lemma loop_typeable : ∃ T, <{ empty / [] |-- loop \in T }>.
Lemma loop_typeable : ∃ T, <{ empty / [] |-- loop \in T }>.
Proof with eauto.
eexists. unfold loop. unfold loop_fun.
eapply T_App...
eapply T_Abs...
eapply T_App...
eapply T_Abs. eapply T_App. eapply T_Deref. eapply T_Var.
rewrite update_neq; [|intros; discriminate].
rewrite update_eq. reflexivity. auto.
eapply T_Assign.
eapply T_Var. rewrite update_eq. reflexivity.
eapply T_Abs.
eapply T_App...
eapply T_Deref. eapply T_Var. reflexivity.
Qed.
eexists. unfold loop. unfold loop_fun.
eapply T_App...
eapply T_Abs...
eapply T_App...
eapply T_Abs. eapply T_App. eapply T_Deref. eapply T_Var.
rewrite update_neq; [|intros; discriminate].
rewrite update_eq. reflexivity. auto.
eapply T_Assign.
eapply T_Var. rewrite update_eq. reflexivity.
eapply T_Abs.
eapply T_App...
eapply T_Deref. eapply T_Var. reflexivity.
Qed.
为形式地证明该项发散,
我们首先定义单步归约关系的 step_closure,
记作 -->+。
它类似单步归约的自反步闭包
(我们一直写作 -->*),
只是并非自反:
t -->+ t' 意味着 t 可通过【一步或多步】
归约到达 t'。
Inductive step_closure {X:Type} (R: relation X) : X → X → Prop :=
| sc_one : ∀ (x y : X),
R x y → step_closure R x y
| sc_step : ∀ (x y z : X),
R x y →
step_closure R y z →
step_closure R x z.
Definition multistep1 := (step_closure step).
Notation "t1 '/' st '-->+' t2 '/' st'" :=
(multistep1 (t1,st) (t2,st'))
(at level 40, st at level 39, t2 at level 39).
| sc_one : ∀ (x y : X),
R x y → step_closure R x y
| sc_step : ∀ (x y z : X),
R x y →
step_closure R y z →
step_closure R x z.
Definition multistep1 := (step_closure step).
Notation "t1 '/' st '-->+' t2 '/' st'" :=
(multistep1 (t1,st) (t2,st'))
(at level 40, st at level 39, t2 at level 39).
现在,我们可以证明表达式 loop 归约为
表达式 !(loc 0) unit
与大小为一的存储 [r:=(loc 0)]loop_fun。
为方便起见,引入 normalize 策略的一个轻微变体,
称为 reduce。
它在每一步尝试用 multi_refl 解目标,
而非等到目标不能再归约。
要点正是 loop 不规范化,
旧的 normalize 策略
会永远归约它,陷入无穷循环!
Ltac print_goal := match goal with ⊢ ?x ⇒ idtac x end.
Ltac reduce :=
repeat (print_goal; eapply multi_step ;
[ (eauto 10; fail) | compute];
try solve [apply multi_refl]).
Ltac reduce :=
repeat (print_goal; eapply multi_step ;
[ (eauto 10; fail) | compute];
try solve [apply multi_refl]).
接下来,我们用 reduce 证明
loop 从空存储步进到 !(loc 0) unit。
Lemma loop_steps_to_loop_fun :
loop / nil -->*
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil.
Proof.
unfold loop.
reduce.
Qed.
loop / nil -->*
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil.
Proof.
unfold loop.
reduce.
Qed.
最后,我们证明后一表达式
在两步内归约为它自己!
Lemma loop_fun_step_self :
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil -->+
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil.
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil -->+
<{ (! (loc 0)) unit }> / cons <{ [r := loc 0] loop_fun }> nil.
练习: 4 stars,标准 (factorial_ref)
用上述思想在带引用的 STLC 中实现阶乘函数。 (无需形式化证明它确实表现得像阶乘。 只需取消下面示例的注释, 确保它作用于实参 4 时给出正确结果即可。)
Definition factorial : tm
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Lemma factorial_type : <{ empty / nil |-- factorial \in (Nat → Nat) }>.
Proof with eauto.
(* 在这里填写 *) Admitted.
(* 将本行替换成 ":= 【你的定义】 ." *). Admitted.
Lemma factorial_type : <{ empty / nil |-- factorial \in (Nat → Nat) }>.
Proof with eauto.
(* 在这里填写 *) Admitted.
若你的定义正确,
你应能直接取消下面示例的注释;
证明用 reduce 策略应能完全自动完成。
(*
Lemma factorial_4 : exists st,
<{ factorial 4 }> / nil -->* tm_const 24 / st.
Proof.
eexists. unfold factorial. reduce.
Qed.
*)
☐
Lemma factorial_4 : exists st,
<{ factorial 4 }> / nil -->* tm_const 24 / st.
Proof.
eexists. unfold factorial. reduce.
Qed.
*)
☐
