MoreStlc扩展简单类型 Lambda-演算
Set Warnings "-notation-overridden,-parsing".
From PLF Require Import Maps.
From PLF Require Import Types.
From PLF Require Import Smallstep.
From PLF Require Import Stlc.
From PLF Require Import Maps.
From PLF Require Import Types.
From PLF Require Import Smallstep.
From PLF Require Import Stlc.
STLC 的简单扩展
数值
Let 绑定
t ::= 项
| ... (其他项同前)
| let x=t1 in t2 let-绑定
| t1 --> t1' | (ST_Let1) |
| let x=t1 in t2 --> let x=t1' in t2 |
| (ST_LetValue) | |
| let x=v1 in t2 --> [x:=v1]t2 |
| Gamma |-- t1 ∈ T1 x⊢>T1; Gamma |-- t2 ∈ T2 | (T_Let) |
| Gamma |-- let x=t1 in t2 ∈ T2 |
二元组
\x : Nat*Nat,
let sum = x.fst + x.snd in
let diff = x.fst - x.snd in
(sum,diff)
t ::= 项
| ...
| (t1,t2) 二元组
| t0.fst 第一投影
| t0.snd 第二投影
v ::= 值
| ...
| (v1,v2) 二元组值
T ::= 类型
| ...
| T1 * T2 积类型
| t1 --> t1' | (ST_Pair1) |
| (t1,t2) --> (t1',t2) |
| t2 --> t2' | (ST_Pair2) |
| (v1,t2) --> (v1,t2') |
| t0 --> t0' | (ST_Fst1) |
| t0.fst --> t0'.fst |
| (ST_FstPair) | |
| (v1,v2).fst --> v1 |
| t0 --> t0' | (ST_Snd1) |
| t0.snd --> t0'.snd |
| (ST_SndPair) | |
| (v1,v2).snd --> v2 |
| Gamma |-- t1 ∈ T1 Gamma |-- t2 ∈ T2 | (T_Pair) |
| Gamma |-- (t1,t2) ∈ T1*T2 |
| Gamma |-- t0 ∈ T1*T2 | (T_Fst) |
| Gamma |-- t0.fst ∈ T1 |
| Gamma |-- t0 ∈ T1*T2 | (T_Snd) |
| Gamma |-- t0.snd ∈ T2 |
单元素类型
t ::= 项
| ... (其他项同前)
| unit unit
v ::= 值
| ...
| unit unit 值
T ::= 类型
| ...
| Unit 单元素类型
定型:
| (T_Unit) | |
| Gamma |-- unit ∈ Unit |
不是!例如 (\x:Unit,x) unit 也是类型 unit 的一个【项】。
和类型
Nat + Bool
我们通过给分类型的元素打上【标记|Tagging】来创建这些类型的元素。
例如,若 n 是 Nat,则 inl n 是 Nat+Bool 的元素;
类似地,若 b 是 Bool,则 inr b 是 Nat+Bool 的元素。
标记 inl 与 inr 的名字来自把它们看作函数
inl ∈ Nat -> Nat + Bool
inr ∈ Bool -> Nat + Bool
它们把 Nat 或 Bool 的元素「注入」到和类型 Nat+Bool
的左、右分量中。(但注意:在我们的形式化中并不真把它们当函数:
inl 与 inr 是关键字,inl t 与 inr t 是基本语法形式,
而非函数应用。)
div ∈ Nat -> Nat -> (Nat + Unit)
div =
\x:Nat, \y:Nat,
if iszero y then
inr unit
else
inl ...
上面的类型 Nat + Unit 实际上同 Rocq 中的 option nat
同构,因此很容易写出互相转换的函数。
getNat ∈ Nat+Bool -> Nat
getNat =
\x:Nat+Bool,
case x of
inl n => n
| inr b => if b then 1 else 0
t ::= 项
| ...
| inl T2 t1 标记(左)
| inr T1 t2 标记(右)
| case t0 of 分类讨论
inl x1 => t1
| inr x2 => t2
v ::= 值
| ...
| inl T2 v1 带标记的值(左)
| inr T1 v2 带标记的值(右)
T ::= 类型
| ...
| T1 + T2 和类型
| t1 --> t1' | (ST_Inl) |
| inl T2 t1 --> inl T2 t1' |
| t2 --> t2' | (ST_Inr) |
| inr T1 t2 --> inr T1 t2' |
| t0 --> t0' | (ST_Case) |
| case t0 of inl x1 => t1 | inr x2 => t2 --> | |
| case t0' of inl x1 => t1 | inr x2 => t2 |
| (ST_CaseInl) | |
| case (inl T2 v1) of inl x1 => t1 | inr x2 => t2 | |
| --> [x1:=v1]t1 |
| (ST_CaseInr) | |
| case (inr T1 v2) of inl x1 => t1 | inr x2 => t2 | |
| --> [x2:=v2]t2 |
| Gamma |-- t1 ∈ T1 | (T_Inl) |
| Gamma |-- inl T2 t1 ∈ T1 + T2 |
| Gamma |-- t2 ∈ T2 | (T_Inr) |
| Gamma |-- inr T1 t2 ∈ T1 + T2 |
| Gamma |-- t0 ∈ T1+T2 | |
| x1⊢>T1; Gamma |-- t1 ∈ T3 | |
| x2⊢>T2; Gamma |-- t2 ∈ T3 | (T_Case) |
| Gamma |-- case t0 of inl x1 => t1 | inr x2 => t2 ∈ T3 |
列表
\x:List Nat,
case x of
nil => 0
| a::x' => case x' of
nil => a
| b::x'' => a+b
t ::= 项
| ...
| nil T 空列表
| t1 :: t2 cons
| case t1 of 分类讨论
nil => t2
| xh::xt => t3
v ::= 值
| ...
| nil T nil 值
| v1 :: v2 cons 值
T ::= 类型
| ...
| List T T 的列表
| t1 --> t1' | (ST_Cons1) |
| t1 :: t2 --> t1' :: t2 |
| t2 --> t2' | (ST_Cons2) |
| v1 :: t2 --> v1 :: t2' |
| t1 --> t1' | (ST_Lcase1) |
| (case t1 of nil => t2 | xh::xt => t3) --> | |
| (case t1' of nil => t2 | xh::xt => t3) |
| (ST_LcaseNil) | |
| (case nil T1 of nil => t2 | xh::xt => t3) | |
| --> t2 |
| (ST_LcaseCons) | |
| (case (vh::vt) of nil => t2 | xh::xt => t3) | |
| --> [xh:=vh][xt:=vt]t3 |
| (T_Nil) | |
| Gamma |-- nil T1 ∈ List T1 |
| Gamma |-- t1 ∈ T1 Gamma |-- t2 ∈ List T1 | (T_Cons) |
| Gamma |-- t1 :: t2 ∈ List T1 |
| Gamma |-- t1 ∈ List T1 | |
| Gamma |-- t2 ∈ T2 | |
| (xh⊢>T1; xt⊢>List T1; Gamma) |-- t3 ∈ T2 | (T_Lcase) |
| Gamma |-- (case t1 of nil => t2 | xh::xt => t3) ∈ T2 |
一般递归
let fact = \x:Nat,
if x=0 then 1 else x * (fact (pred x))) in
fact 3.
注意该绑定的右侧提到了 fact,即被绑定的变量本身;
按照我们上面定义 let 的方式,这是不允许的。
fact = \x:Nat,
if x=0 then 1 else x * (fact (pred x)))
我们将写:
fact =
fix
(\f:Nat->Nat,
\x:Nat,
if x=0 then 1 else x * (f (pred x)))
- 在 fact 定义的右侧,把对 fact 的递归引用
替换为新变量 f。
- 在最前面添加一个绑定 f 的抽象,并带适当的类型注解。
(由于我们用 f 代替类型为 Nat→Nat 的 fact,
应当要求 f 也具有相同类型。)
新抽象的类型是 (Nat→Nat) → (Nat→Nat)。
- 对这个抽象应用 fix。这个应用的类型为 Nat→Nat。
- 把这一切用作 fact 的普通 let 绑定的右侧。
t ::= 项
| ...
| fix t1 不动点算子
归约:
| t1 --> t1' | (ST_Fix1) |
| fix t1 --> fix t1' |
| (ST_FixAbs) | |
| fix (\xf:T1.t1) --> [xf:=fix (\xf:T1.t1)] t1 |
| Gamma |-- t1 ∈ T1->T1 | (T_Fix) |
| Gamma |-- fix t1 ∈ T1 |
F = (\f. \x. if x=0 then 1 else x * (f (pred x)))
(为简洁起见,省略类型注解)。
fix F 3
--> ST_FixAbs + ST_App1
(\x. if x=0 then 1 else x * (fix F (pred x))) 3
--> ST_AppAbs
if 3=0 then 1 else 3 * (fix F (pred 3))
--> ST_If0_Nonzero
3 * (fix F (pred 3))
--> ST_FixAbs + ST_Mult2 + ST_App1
3 * ((\x. if x=0 then 1 else x * (fix F (pred x))) (pred 3))
--> ST_PredNat + ST_Mult2 + ST_App2
3 * ((\x. if x=0 then 1 else x * (fix F (pred x))) 2)
--> ST_AppAbs + ST_Mult2
3 * (if 2=0 then 1 else 2 * (fix F (pred 2)))
--> ST_If0_Nonzero + ST_Mult2
3 * (2 * (fix F (pred 2)))
--> ST_FixAbs + 2 x ST_Mult2 + ST_App1
3 * (2 * ((\x. if x=0 then 1 else x * (fix F (pred x))) (pred 2)))
--> ST_PredNat + 2 x ST_Mult2 + ST_App2
3 * (2 * ((\x. if x=0 then 1 else x * (fix F (pred x))) 1))
--> ST_AppAbs + 2 x ST_Mult2
3 * (2 * (if 1=0 then 1 else 1 * (fix F (pred 1))))
--> ST_If0_Nonzero + 2 x ST_Mult2
3 * (2 * (1 * (fix F (pred 1))))
--> ST_FixAbs + 3 x ST_Mult2 + ST_App1
3 * (2 * (1 * ((\x. if x=0 then 1 else x * (fix F (pred x))) (pred 1))))
--> ST_PredNat + 3 x ST_Mult2 + ST_App2
3 * (2 * (1 * ((\x. if x=0 then 1 else x * (fix F (pred x))) 0)))
--> ST_AppAbs + 3 x ST_Mult2
3 * (2 * (1 * (if 0=0 then 1 else 0 * (fix F (pred 0)))))
--> ST_If0Zero + 3 x ST_Mult2
3 * (2 * (1 * 1))
--> ST_MultNats + 2 x ST_Mult2
3 * (2 * 1)
--> ST_MultNats + ST_Mult2
3 * 2
--> ST_MultNats
6
练习: 1 star,标准,可选 (halve_fix)
把下列非形式化的递归定义翻译成使用 fix 的形式:
halve =
\x:Nat,
if x=0 then 0
else if (pred x)=0 then 0
else 1 + (halve (pred (pred x)))
(* 在这里填写 *)☐
练习: 1 star,标准,可选 (fact_steps)
写出项 fact 1 归约到正规形所经过的步骤序列 (假定算术运算有通常的归约规则)。☐
fix (\x:T,x) 由 T_Fix 与 T_Abs,该项具有类型 T。 由 ST_FixAbs,它一遍又一遍地归约为自身。 因此它是 T 的一个【不停机元素|Diverging Element】。
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))
最后,这是一个用 fix 定义一对递归函数的示例
(说明规则 T_Fix 中的类型 T1 不必是函数类型):
let evenodd =
fix
(\eo: ((Nat -> Nat) * (Nat -> Nat)),
(\n:Nat, if0 n then 1 else (eo.snd (pred n)),
\n:Nat, if0 n then 0 else (eo.fst (pred n)))) in
let even = evenodd.fst in
let odd = evenodd.snd in
(even 3, even 4)}
记录
t ::= 项
| ...
| {i1=t1, ..., in=tn} 记录
| t0.i 投影
v ::= 值
| ...
| {i1=v1, ..., in=vn} 记录值
T ::= 类型
| ...
| {i1:T1, ..., in:Tn} 记录类型
| ti --> ti' | (ST_Rcd) |
| {i1=v1, ..., im=vm, in=ti , ...} | |
| --> {i1=v1, ..., im=vm, in=ti', ...} |
| t0 --> t0' | (ST_Proj1) |
| t0.i --> t0'.i |
| (ST_ProjRcd) | |
| {..., i=vi, ...}.i --> vi |
| Gamma |-- t1 ∈ T1 ... Gamma |-- tn ∈ Tn | (T_Rcd) |
| Gamma |-- {i1=t1, ..., in=tn} ∈ {i1:T1, ..., in:Tn} |
| Gamma |-- t0 ∈ {..., i:Ti, ...} | (T_Proj) |
| Gamma |-- t0.i ∈ Ti |
- 我们可以直接形式化语法形式与推理规则,
尽可能贴近上面给出的形式。
这在概念上很直接,大概也是我们在构建真正的编译器时想做的
(特别是,它允许我们以程序员易懂的形式打印错误信息)。
但规则的形式化版本不会很漂亮或好用,
因为上面所有的 ... 都必须替换为显式的量化或内包式。
出于这个原因,记录没有包括在本章末尾的扩展练习中。
(在这里非形式地讨论记录仍然有用。
当我们讲到 Sub 一章时,
它将有助于激发向类型系统添加【子类型|Subtyping】的动机。)
- 另外,我们可以寻找更顺畅的方式来呈现记录。
例如用二元表示:一个构造子表示空记录,
另一个构造子为已有记录添加单个字段,
而非一次性构造整个记录的单一整体构造子。
如果我们主要关心研究带记录演算的【元理论|Metatheory】,
这是正确的做法,因为它带来干净优雅的定义与证明。
Records 一章展示了如何做到这一点。
- 最后,如果愿意,我们可以完全避免形式化记录, 规定记录记法只是涉及二元组与积类型的 更复杂表达式的非形式简写。 我们在下一节简述这一途径。
编码记录(选读)
{} ----> unit
{t1, t2, ..., tn} ----> (t1, trest)
where {t2, ..., tn} ----> trest
类似地,我们可以用嵌套积类型编码元组类型:
{} ----> Unit
{T1, T2, ..., Tn} ----> T1 * TRest
where {T2, ..., Tn} ----> TRest
从元组投影一个字段的操作,
可以用一串第二投影后跟一个第一投影来编码:
t.0 ----> t.fst
t.(n+1) ----> (t.snd).n
接下来,假设记录标签上存在某种全序,
使我们可以把每个标签与唯一的自然数关联。
这个数称为标签的【位置|Position】。
例如,我们可以这样分配位置:
LABEL POSITION
a 0
b 1
c 2
... ...
bar 1395
... ...
foo 4460
... ...
我们按位置对字段排序,并用这些位置把记录值编码为元组
(即嵌套二元组)。例如:
{a=5,b=6} ----> {5,6}
{a=5,c=7} ----> {5,unit,7}
{c=7,a=5} ----> {5,unit,7}
{c=5,b=3} ----> {unit,3,5}
{f=8,c=5,a=7} ----> {7,unit,5,unit,unit,8}
{f=8,c=5} ----> {unit,unit,5,unit,unit,8}
每个字段出现在与其标签关联的位置上。
元组的大小由位置最高的标签决定。
我们用 unit 填充未使用的位置。
{a:Nat,b:Nat} ----> {Nat,Nat}
{c:Nat,a:Nat} ----> {Nat,Unit,Nat}
{f:Nat,c:Nat} ----> {Unit,Unit,Nat,Unit,Unit,Nat}
最后,记录投影编码为从适当位置对元组投影:
t.l ----> t.(position of l)
不难验证,记录「直接」呈现的全部定型规则
都被这一编码所证实。
(归约规则「几乎被证实」,并非完全,
因为编码会重排字段。)
变体(选读)
在这一系列练习中,你将形式化本章描述的一些扩展。
我们提供了项与类型语法的必要补充,
并包含了一些示例,你可以用它们测试定义是否按预期工作。
你将补全其余定义,并相应地扩展所有证明。
作为开始,我们提供了下列实现:
你需要完成的实现有:
好的策略是一次做一个扩展(先二元组,再 let,再 fix)。
分几遍完成,而非试图一遍做完全部三个。
对每个定义或证明,先仔细阅读为你提供的部分。
高层次直觉可参考 Stlc 一章的正文。
细节机制可参考嵌入的注释。
- 数值
- 和
- 列表
- 单元
- 二元组
- let(涉及绑定)
- fix
Inductive ty : Type :=
| Ty_Arrow : ty → ty → ty
| Ty_Nat : ty
| Ty_Sum : ty → ty → ty
| Ty_List : ty → ty
| Ty_Unit : ty
| Ty_Prod : ty → ty → ty.
Inductive tm : Type :=
(* 纯 STLC *)
| 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
(* 和 *)
| tm_inl : ty → tm → tm
| tm_inr : ty → tm → tm
| tm_case : tm → string → tm → string → tm → tm
(* 即 case t0 of inl x1 ⇒ t1 | inr x2 ⇒ t2 *)
(* 列表 *)
| tm_nil : ty → tm
| tm_cons : tm → tm → tm
| tm_lcase : tm → tm → string → string → tm → tm
(* 即 case t1 of | nil ⇒ t2 | x::y ⇒ t3 *)
(* 单元 *)
| tm_unit : tm
(* 你将要处理以下扩展: *)
(* 二元组 *)
| tm_pair : tm → tm → tm
| tm_fst : tm → tm
| tm_snd : tm → tm
(* let *)
| tm_let : string → tm → tm → tm
(* 即 let x = t1 in t2 *)
(* fix *)
| tm_fix : tm → tm.
| Ty_Arrow : ty → ty → ty
| Ty_Nat : ty
| Ty_Sum : ty → ty → ty
| Ty_List : ty → ty
| Ty_Unit : ty
| Ty_Prod : ty → ty → ty.
Inductive tm : Type :=
(* 纯 STLC *)
| 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
(* 和 *)
| tm_inl : ty → tm → tm
| tm_inr : ty → tm → tm
| tm_case : tm → string → tm → string → tm → tm
(* 即 case t0 of inl x1 ⇒ t1 | inr x2 ⇒ t2 *)
(* 列表 *)
| tm_nil : ty → tm
| tm_cons : tm → tm → tm
| tm_lcase : tm → tm → string → string → tm → tm
(* 即 case t1 of | nil ⇒ t2 | x::y ⇒ t3 *)
(* 单元 *)
| tm_unit : tm
(* 你将要处理以下扩展: *)
(* 二元组 *)
| tm_pair : tm → tm → tm
| tm_fst : tm → tm
| tm_snd : tm → tm
(* let *)
| tm_let : string → tm → tm → tm
(* 即 let x = t1 in t2 *)
(* fix *)
| tm_fix : tm → tm.
注意,为简洁起见,我们省略了布尔值,
改为提供单一的 if0 形式,把零测试与条件结合在一起。
也就是,不写
if x = 0 then ... else ...
而写成:
if0 x then ... else ...
Definition w : string := "w".
Definition x : string := "x".
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Delimit Scope stlc_scope with stlc.
Open 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 global) : stlc_scope.
Notation "'_'" := _ (in custom stlc_tm at level 0) : stlc_scope.
Notation "'$' x" := x (in custom stlc_tm at level 0, x constr at level 0) : stlc_scope.
Notation "'$(' x ')'" := x (in custom stlc_tm at level 0, x constr, only parsing) : 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 "S + T" :=
(Ty_Sum S T) (in custom stlc_ty at level 3, left associativity).
Notation "'inl' T t" := (tm_inl T t) (in custom stlc_tm at level 10,
T custom stlc_ty,
t custom stlc_tm at level 0).
Notation "'inr' T t" := (tm_inr T t) (in custom stlc_tm at level 10,
T custom stlc_ty,
t custom stlc_tm at level 0).
Notation "'case' t0 'of' '|' 'inl' x1 '=>' t1 '|' 'inr' x2 '=>' t2" :=
(tm_case t0 x1 t1 x2 t2) (in custom stlc_tm at level 200,
t0 custom stlc_tm at level 200,
x1 global,
t1 custom stlc_tm at level 200,
x2 global,
t2 custom stlc_tm at level 200,
left associativity).
Notation "X * Y" :=
(Ty_Prod X Y) (in custom stlc_ty at level 2, X custom stlc_ty, Y custom stlc_ty at level 0) : stlc_scope.
Notation "( x ',' y )" := (tm_pair x y) (in custom stlc_tm at level 0,
x custom stlc_tm,
y custom stlc_tm) : stlc_scope.
Notation "t '.fst'" := (tm_fst t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "t '.snd'" := (tm_snd t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "'List' T" :=
(Ty_List T) (in custom stlc_ty at level 4) : stlc_scope.
Notation "'List' T" := (Ty_List T) (at level 0) : stlc_scope.
Notation "'nil' T" := (tm_nil T) (in custom stlc_tm at level 0, T custom stlc_ty) : stlc_scope.
Notation "h '::' t" := (tm_cons h t) (in custom stlc_tm at level 2, right associativity) : stlc_scope.
Notation "'case' t1 'of' '|' 'nil' '=>' t2 '|' x '::' y '=>' t3" :=
(tm_lcase t1 t2 x y t3) (in custom stlc_tm at level 200,
t1 custom stlc_tm at level 200,
t2 custom stlc_tm at level 0,
x global,
y global,
t3 custom stlc_tm at level 0,
left associativity) : stlc_scope.
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 "'let' x '=' t1 'in' t2" :=
(tm_let x t1 t2) (in custom stlc_tm at level 200) : stlc_scope.
Notation "'fix' t" := (tm_fix t) (in custom stlc_tm at level 200) : stlc_scope.
Definition x : string := "x".
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Delimit Scope stlc_scope with stlc.
Open 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 global) : stlc_scope.
Notation "'_'" := _ (in custom stlc_tm at level 0) : stlc_scope.
Notation "'$' x" := x (in custom stlc_tm at level 0, x constr at level 0) : stlc_scope.
Notation "'$(' x ')'" := x (in custom stlc_tm at level 0, x constr, only parsing) : 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 "S + T" :=
(Ty_Sum S T) (in custom stlc_ty at level 3, left associativity).
Notation "'inl' T t" := (tm_inl T t) (in custom stlc_tm at level 10,
T custom stlc_ty,
t custom stlc_tm at level 0).
Notation "'inr' T t" := (tm_inr T t) (in custom stlc_tm at level 10,
T custom stlc_ty,
t custom stlc_tm at level 0).
Notation "'case' t0 'of' '|' 'inl' x1 '=>' t1 '|' 'inr' x2 '=>' t2" :=
(tm_case t0 x1 t1 x2 t2) (in custom stlc_tm at level 200,
t0 custom stlc_tm at level 200,
x1 global,
t1 custom stlc_tm at level 200,
x2 global,
t2 custom stlc_tm at level 200,
left associativity).
Notation "X * Y" :=
(Ty_Prod X Y) (in custom stlc_ty at level 2, X custom stlc_ty, Y custom stlc_ty at level 0) : stlc_scope.
Notation "( x ',' y )" := (tm_pair x y) (in custom stlc_tm at level 0,
x custom stlc_tm,
y custom stlc_tm) : stlc_scope.
Notation "t '.fst'" := (tm_fst t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "t '.snd'" := (tm_snd t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "'List' T" :=
(Ty_List T) (in custom stlc_ty at level 4) : stlc_scope.
Notation "'List' T" := (Ty_List T) (at level 0) : stlc_scope.
Notation "'nil' T" := (tm_nil T) (in custom stlc_tm at level 0, T custom stlc_ty) : stlc_scope.
Notation "h '::' t" := (tm_cons h t) (in custom stlc_tm at level 2, right associativity) : stlc_scope.
Notation "'case' t1 'of' '|' 'nil' '=>' t2 '|' x '::' y '=>' t3" :=
(tm_lcase t1 t2 x y t3) (in custom stlc_tm at level 200,
t1 custom stlc_tm at level 200,
t2 custom stlc_tm at level 0,
x global,
y global,
t3 custom stlc_tm at level 0,
left associativity) : stlc_scope.
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 "'let' x '=' t1 'in' t2" :=
(tm_let x t1 t2) (in custom stlc_tm at level 200) : stlc_scope.
Notation "'fix' t" := (tm_fix t) (in custom stlc_tm at level 200) : stlc_scope.
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).
t custom stlc_tm at next level, right associativity).
Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
match t with
(* 纯 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)}>
(* 数值 *)
| 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}>
(* 和 *)
| <{inl T2 t1}> ⇒
<{inl T2 ( [x:=s] t1) }>
| <{inr T1 t2}> ⇒
<{inr T1 ( [x:=s] t2) }>
| <{case t0 of | inl y1 ⇒ t1 | inr y2 ⇒ t2}> ⇒
<{case ([x:=s] t0) of
| inl y1 ⇒ $(if String.eqb x y1 then t1 else <{ [x:=s] t1 }> )
| inr y2 ⇒ $(if String.eqb x y2 then t2 else <{ [x:=s] t2 }> ) }>
(* 列表 *)
| <{nil T}> ⇒
t
| <{t1 :: t2}> ⇒
<{ ([x:=s] t1) :: [x:=s] t2 }>
| <{case t1 of | nil ⇒ t2 | y1 :: y2 ⇒ t3}> ⇒
<{case ( [x:=s] t1 ) of
| nil ⇒ [x:=s] t2
| y1 :: y2 ⇒
$(if String.eqb x y1 then
t3
else if String.eqb x y2 then t3
else <{ [x:=s] t3 }>) }>
(* 单元 *)
| <{unit}> ⇒ <{unit}>
(* 完成下列情形。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
| _ ⇒ t (* ……并在完成练习后删除此行 *)
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc_tm) : stlc_scope.
(* 用 reflexivity 确认下列测试成立: *)
Example substeg1 :
<{ [z:=$0] (let w = z in z) }> = <{ let w = $0 in $0 }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
Example substeg2 :
<{ [z:=$0] (let w = z in w) }> = <{ let w = $0 in w }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
Example substeg3 :
<{ [z:=$0] (let y = succ $0 in z) }> = <{ let y = succ $0 in $0 }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
☐
match t with
(* 纯 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)}>
(* 数值 *)
| 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}>
(* 和 *)
| <{inl T2 t1}> ⇒
<{inl T2 ( [x:=s] t1) }>
| <{inr T1 t2}> ⇒
<{inr T1 ( [x:=s] t2) }>
| <{case t0 of | inl y1 ⇒ t1 | inr y2 ⇒ t2}> ⇒
<{case ([x:=s] t0) of
| inl y1 ⇒ $(if String.eqb x y1 then t1 else <{ [x:=s] t1 }> )
| inr y2 ⇒ $(if String.eqb x y2 then t2 else <{ [x:=s] t2 }> ) }>
(* 列表 *)
| <{nil T}> ⇒
t
| <{t1 :: t2}> ⇒
<{ ([x:=s] t1) :: [x:=s] t2 }>
| <{case t1 of | nil ⇒ t2 | y1 :: y2 ⇒ t3}> ⇒
<{case ( [x:=s] t1 ) of
| nil ⇒ [x:=s] t2
| y1 :: y2 ⇒
$(if String.eqb x y1 then
t3
else if String.eqb x y2 then t3
else <{ [x:=s] t3 }>) }>
(* 单元 *)
| <{unit}> ⇒ <{unit}>
(* 完成下列情形。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
| _ ⇒ t (* ……并在完成练习后删除此行 *)
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc_tm) : stlc_scope.
(* 用 reflexivity 确认下列测试成立: *)
Example substeg1 :
<{ [z:=$0] (let w = z in z) }> = <{ let w = $0 in $0 }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
Example substeg2 :
<{ [z:=$0] (let w = z in w) }> = <{ let w = $0 in w }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
Example substeg3 :
<{ [z:=$0] (let y = succ $0 in z) }> = <{ let y = succ $0 in $0 }>.
Proof.
(*
reflexivity.
*)
(* 在这里填写 *) Admitted.
☐
Inductive value : tm → Prop :=
(* 在纯 STLC 中,函数抽象是值: *)
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
(* 数值是值: *)
| v_nat : ∀ n : nat,
value <{n}>
(* 带标记的值也是值: *)
| v_inl : ∀ v T1,
value v →
value <{inl T1 v}>
| v_inr : ∀ v T1,
value v →
value <{inr T1 v}>
(* 列表是值当且仅当其头部与尾部都是值: *)
| v_lnil : ∀ T1, value <{nil T1}>
| v_lcons : ∀ v1 v2,
value v1 →
value v2 →
value <{v1 :: v2}>
(* 单元总是值 *)
| v_unit : value <{unit}>
(* 若两个分量都是值,则二元组是值: *)
| v_pair : ∀ v1 v2,
value v1 →
value v2 →
value <{(v1, v2)}>.
Hint Constructors value : core.
(* 在纯 STLC 中,函数抽象是值: *)
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
(* 数值是值: *)
| v_nat : ∀ n : nat,
value <{n}>
(* 带标记的值也是值: *)
| v_inl : ∀ v T1,
value v →
value <{inl T1 v}>
| v_inr : ∀ v T1,
value v →
value <{inr T1 v}>
(* 列表是值当且仅当其头部与尾部都是值: *)
| v_lnil : ∀ T1, value <{nil T1}>
| v_lcons : ∀ v1 v2,
value v1 →
value v2 →
value <{v1 :: v2}>
(* 单元总是值 *)
| v_unit : value <{unit}>
(* 若两个分量都是值,则二元组是值: *)
| v_pair : ∀ v1 v2,
value v1 →
value v2 →
value <{(v1, v2)}>.
Hint Constructors value : core.
Inductive step : tm → tm → Prop :=
(* 纯 STLC *)
| ST_AppAbs : ∀ x T2 t1 v2,
value v2 →
<{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
| ST_App1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 t2}> --> <{t1' t2}>
| ST_App2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 t2}> --> <{v1 t2'}>
(* 数值 *)
| ST_Succ : ∀ t1 t1',
t1 --> t1' →
<{succ t1}> --> <{succ t1'}>
| ST_SuccNat : ∀ n : nat,
<{succ n}> --> <{ $(S n) }>
| ST_Pred : ∀ t1 t1',
t1 --> t1' →
<{pred t1}> --> <{pred t1'}>
| ST_PredNat : ∀ n:nat,
<{pred n}> --> <{ $(n - 1) }>
| ST_Mulconsts : ∀ n1 n2 : nat,
<{n1 × n2}> --> <{ $(n1 × n2) }>
| ST_Mult1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 × t2}> --> <{t1' × t2}>
| ST_Mult2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 × t2}> --> <{v1 × t2'}>
| ST_If0 : ∀ t1 t1' t2 t3,
t1 --> t1' →
<{if0 t1 then t2 else t3}> --> <{if0 t1' then t2 else t3}>
| ST_If0_Zero : ∀ t2 t3,
<{if0 $0 then t2 else t3}> --> t2
| ST_If0_Nonzero : ∀ n t2 t3,
<{if0 $(S n) then t2 else t3}> --> t3
(* 和 *)
| ST_Inl : ∀ t1 t1' T2,
t1 --> t1' →
<{inl T2 t1}> --> <{inl T2 t1'}>
| ST_Inr : ∀ t2 t2' T1,
t2 --> t2' →
<{inr T1 t2}> --> <{inr T1 t2'}>
| ST_Case : ∀ t0 t0' x1 t1 x2 t2,
t0 --> t0' →
<{case t0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> -->
<{case t0' of | inl x1 ⇒ t1 | inr x2 ⇒ t2}>
| ST_CaseInl : ∀ v0 x1 t1 x2 t2 T2,
value v0 →
<{case inl T2 v0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> --> <{ [x1:=v0]t1 }>
| ST_CaseInr : ∀ v0 x1 t1 x2 t2 T1,
value v0 →
<{case inr T1 v0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> --> <{ [x2:=v0]t2 }>
(* 列表 *)
| ST_Cons1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 :: t2}> --> <{t1' :: t2}>
| ST_Cons2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 :: t2}> --> <{v1 :: t2'}>
| ST_Lcase1 : ∀ t1 t1' t2 x1 x2 t3,
t1 --> t1' →
<{case t1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3}> -->
<{case t1' of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>
| ST_LcaseNil : ∀ T1 t2 x1 x2 t3,
<{case nil T1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3}> --> t2
| ST_LcaseCons : ∀ v1 vl t2 x1 x2 t3,
value v1 →
value vl →
<{case v1 :: vl of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>
--> <{ [x2 := vl] ([x1 := v1] t3) }>
(* 为下列扩展添加规则。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
where "t '-->' t'" := (step t t').
☐
(* 纯 STLC *)
| ST_AppAbs : ∀ x T2 t1 v2,
value v2 →
<{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
| ST_App1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 t2}> --> <{t1' t2}>
| ST_App2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 t2}> --> <{v1 t2'}>
(* 数值 *)
| ST_Succ : ∀ t1 t1',
t1 --> t1' →
<{succ t1}> --> <{succ t1'}>
| ST_SuccNat : ∀ n : nat,
<{succ n}> --> <{ $(S n) }>
| ST_Pred : ∀ t1 t1',
t1 --> t1' →
<{pred t1}> --> <{pred t1'}>
| ST_PredNat : ∀ n:nat,
<{pred n}> --> <{ $(n - 1) }>
| ST_Mulconsts : ∀ n1 n2 : nat,
<{n1 × n2}> --> <{ $(n1 × n2) }>
| ST_Mult1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 × t2}> --> <{t1' × t2}>
| ST_Mult2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 × t2}> --> <{v1 × t2'}>
| ST_If0 : ∀ t1 t1' t2 t3,
t1 --> t1' →
<{if0 t1 then t2 else t3}> --> <{if0 t1' then t2 else t3}>
| ST_If0_Zero : ∀ t2 t3,
<{if0 $0 then t2 else t3}> --> t2
| ST_If0_Nonzero : ∀ n t2 t3,
<{if0 $(S n) then t2 else t3}> --> t3
(* 和 *)
| ST_Inl : ∀ t1 t1' T2,
t1 --> t1' →
<{inl T2 t1}> --> <{inl T2 t1'}>
| ST_Inr : ∀ t2 t2' T1,
t2 --> t2' →
<{inr T1 t2}> --> <{inr T1 t2'}>
| ST_Case : ∀ t0 t0' x1 t1 x2 t2,
t0 --> t0' →
<{case t0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> -->
<{case t0' of | inl x1 ⇒ t1 | inr x2 ⇒ t2}>
| ST_CaseInl : ∀ v0 x1 t1 x2 t2 T2,
value v0 →
<{case inl T2 v0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> --> <{ [x1:=v0]t1 }>
| ST_CaseInr : ∀ v0 x1 t1 x2 t2 T1,
value v0 →
<{case inr T1 v0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2}> --> <{ [x2:=v0]t2 }>
(* 列表 *)
| ST_Cons1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 :: t2}> --> <{t1' :: t2}>
| ST_Cons2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 :: t2}> --> <{v1 :: t2'}>
| ST_Lcase1 : ∀ t1 t1' t2 x1 x2 t3,
t1 --> t1' →
<{case t1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3}> -->
<{case t1' of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>
| ST_LcaseNil : ∀ T1 t2 x1 x2 t3,
<{case nil T1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3}> --> t2
| ST_LcaseCons : ∀ v1 vl t2 x1 x2 t3,
value v1 →
value vl →
<{case v1 :: vl of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>
--> <{ [x2 := vl] ([x1 := v1] t3) }>
(* 为下列扩展添加规则。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
where "t '-->' t'" := (step t t').
☐
Notation multistep := (multi step).
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).
Hint Constructors step : core.
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).
Hint Constructors step : core.
接下来我们定义定型规则。
它们几乎是上面所示推理规则的直接转写。
Inductive has_type : context → tm → ty → Prop :=
(* 纯 STLC *)
| T_Var : ∀ Gamma x T1,
Gamma x = Some T1 →
<{ Gamma |-- x \in T1 }>
| T_Abs : ∀ Gamma x T1 T2 t1,
<{ x ⊢> T2 ; Gamma |-- t1 \in T1 }> →
<{ Gamma |-- \x:T2, t1 \in T2 → T1 }>
| T_App : ∀ T1 T2 Gamma t1 t2,
<{ Gamma |-- t1 \in T2 → T1 }> →
<{ Gamma |-- t2 \in T2 }> →
<{ Gamma |-- t1 t2 \in T1 }>
(* 数值 *)
| T_Nat : ∀ Gamma (n : nat),
<{ Gamma |-- n \in Nat }>
| T_Succ : ∀ Gamma t,
<{ Gamma |-- t \in Nat }> →
<{ Gamma |-- succ t \in Nat }>
| T_Pred : ∀ Gamma t,
<{ Gamma |-- t \in Nat }> →
<{ Gamma |-- pred t \in Nat }>
| T_Mult : ∀ Gamma t1 t2,
<{ Gamma |-- t1 \in Nat }> →
<{ Gamma |-- t2 \in Nat }> →
<{ Gamma |-- t1 × t2 \in Nat }>
| T_If0 : ∀ Gamma t1 t2 t3 T0,
<{ Gamma |-- t1 \in Nat }> →
<{ Gamma |-- t2 \in T0 }> →
<{ Gamma |-- t3 \in T0 }> →
<{ Gamma |-- if0 t1 then t2 else t3 \in T0 }>
(* 和 *)
| T_Inl : ∀ Gamma t1 T1 T2,
<{ Gamma |-- t1 \in T1 }> →
<{ Gamma |-- (inl T2 t1) \in T1 + T2 }>
| T_Inr : ∀ Gamma t2 T1 T2,
<{ Gamma |-- t2 \in T2 }> →
<{ Gamma |-- (inr T1 t2) \in T1 + T2 }>
| T_Case : ∀ Gamma t0 x1 T1 t1 x2 T2 t2 T3,
<{ Gamma |-- t0 \in T1 + T2 }> →
<{ x1 ⊢> T1 ; Gamma |-- t1 \in T3 }> →
<{ x2 ⊢> T2 ; Gamma |-- t2 \in T3 }> →
<{ Gamma |-- case t0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2 \in T3 }>
(* 列表 *)
| T_Nil : ∀ Gamma T1,
<{ Gamma |-- nil T1 \in List T1 }>
| T_Cons : ∀ Gamma t1 t2 T1,
<{ Gamma |-- t1 \in T1 }> →
<{ Gamma |-- t2 \in List T1 }> →
<{ Gamma |-- t1 :: t2 \in List T1 }>
| T_Lcase : ∀ Gamma t1 T1 t2 x1 x2 t3 T2,
<{ Gamma |-- t1 \in List T1 }> →
<{ Gamma |-- t2 \in T2 }> →
<{ x1 ⊢> T1 ; x2 ⊢> List T1 ; Gamma |-- t3 \in T2 }> →
<{ Gamma |-- case t1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3 \in T2 }>
(* 单元 *)
| T_Unit : ∀ Gamma,
<{ Gamma |-- unit \in Unit }>
(* 为下列扩展添加规则。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
where "<{ Gamma '|--' t '∈' T }>" := (has_type Gamma t T).
☐
(* 纯 STLC *)
| T_Var : ∀ Gamma x T1,
Gamma x = Some T1 →
<{ Gamma |-- x \in T1 }>
| T_Abs : ∀ Gamma x T1 T2 t1,
<{ x ⊢> T2 ; Gamma |-- t1 \in T1 }> →
<{ Gamma |-- \x:T2, t1 \in T2 → T1 }>
| T_App : ∀ T1 T2 Gamma t1 t2,
<{ Gamma |-- t1 \in T2 → T1 }> →
<{ Gamma |-- t2 \in T2 }> →
<{ Gamma |-- t1 t2 \in T1 }>
(* 数值 *)
| T_Nat : ∀ Gamma (n : nat),
<{ Gamma |-- n \in Nat }>
| T_Succ : ∀ Gamma t,
<{ Gamma |-- t \in Nat }> →
<{ Gamma |-- succ t \in Nat }>
| T_Pred : ∀ Gamma t,
<{ Gamma |-- t \in Nat }> →
<{ Gamma |-- pred t \in Nat }>
| T_Mult : ∀ Gamma t1 t2,
<{ Gamma |-- t1 \in Nat }> →
<{ Gamma |-- t2 \in Nat }> →
<{ Gamma |-- t1 × t2 \in Nat }>
| T_If0 : ∀ Gamma t1 t2 t3 T0,
<{ Gamma |-- t1 \in Nat }> →
<{ Gamma |-- t2 \in T0 }> →
<{ Gamma |-- t3 \in T0 }> →
<{ Gamma |-- if0 t1 then t2 else t3 \in T0 }>
(* 和 *)
| T_Inl : ∀ Gamma t1 T1 T2,
<{ Gamma |-- t1 \in T1 }> →
<{ Gamma |-- (inl T2 t1) \in T1 + T2 }>
| T_Inr : ∀ Gamma t2 T1 T2,
<{ Gamma |-- t2 \in T2 }> →
<{ Gamma |-- (inr T1 t2) \in T1 + T2 }>
| T_Case : ∀ Gamma t0 x1 T1 t1 x2 T2 t2 T3,
<{ Gamma |-- t0 \in T1 + T2 }> →
<{ x1 ⊢> T1 ; Gamma |-- t1 \in T3 }> →
<{ x2 ⊢> T2 ; Gamma |-- t2 \in T3 }> →
<{ Gamma |-- case t0 of | inl x1 ⇒ t1 | inr x2 ⇒ t2 \in T3 }>
(* 列表 *)
| T_Nil : ∀ Gamma T1,
<{ Gamma |-- nil T1 \in List T1 }>
| T_Cons : ∀ Gamma t1 t2 T1,
<{ Gamma |-- t1 \in T1 }> →
<{ Gamma |-- t2 \in List T1 }> →
<{ Gamma |-- t1 :: t2 \in List T1 }>
| T_Lcase : ∀ Gamma t1 T1 t2 x1 x2 t3 T2,
<{ Gamma |-- t1 \in List T1 }> →
<{ Gamma |-- t2 \in T2 }> →
<{ x1 ⊢> T1 ; x2 ⊢> List T1 ; Gamma |-- t3 \in T2 }> →
<{ Gamma |-- case t1 of | nil ⇒ t2 | x1 :: x2 ⇒ t3 \in T2 }>
(* 单元 *)
| T_Unit : ∀ Gamma,
<{ Gamma |-- unit \in Unit }>
(* 为下列扩展添加规则。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
where "<{ Gamma '|--' t '∈' T }>" := (has_type Gamma t T).
☐
示例
练习: 5 stars,标准,可选 (STLCExtended_examples)
本节给出上文示例的形式化版本(外加若干其他示例)。
Open Scope string_scope.
Notation x := "x".
Notation y := "y".
Notation a := "a".
Notation f := "f".
Notation g := "g".
Notation l := "l".
Notation k := "k".
Notation i1 := "i1".
Notation i2 := "i2".
Notation processSum := "processSum".
Notation n := "n".
Notation eq := "eq".
Notation m := "m".
Notation evenodd := "evenodd".
Notation even := "even".
Notation odd := "odd".
Notation eo := "eo".
Notation x := "x".
Notation y := "y".
Notation a := "a".
Notation f := "f".
Notation g := "g".
Notation l := "l".
Notation k := "k".
Notation i1 := "i1".
Notation i2 := "i2".
Notation processSum := "processSum".
Notation n := "n".
Notation eq := "eq".
Notation m := "m".
Notation evenodd := "evenodd".
Notation even := "even".
Notation odd := "odd".
Notation eo := "eo".
接下来是一点 Rocq 技巧,用于自动搜索定型推导。
你不需要详细了解这部分,大致浏览一下即可。
这样,将来需要为 auto 做自定义扩展时,知道该找什么。
下面的 Hint 声明说,每当 auto 到达形如
(Gamma |-- (tm_app e1 e1) ∈ T) 的目标时,
应当考虑 eapply T_App,为中间类型 T1 留下存在性变量;
lcase 类似。该变量随后会在为 e1 与 e2
搜索定型推导的过程中被填入。
我们还包含一条提示,在求解等式目标时「更努力一点」;
这有助于自动化 T_Var 的使用(它把等式作为前提)。
Hint Extern 2 (has_type _ (tm_app _ _) _) ⇒
eapply T_App; auto : core.
Hint Extern 2 (has_type _ (tm_lcase _ _ _ _ _) _) ⇒
eapply T_Lcase; auto : core.
Hint Extern 2 (_ = _) ⇒ compute; reflexivity : core.
eapply T_App; auto : core.
Hint Extern 2 (has_type _ (tm_lcase _ _ _ _ _) _) ⇒
eapply T_Lcase; auto : core.
Hint Extern 2 (_ = _) ⇒ compute; reflexivity : core.
Definition tm_test :=
<{if0
(pred
(succ
(pred
($2 × $0))))
then $5
else $6}>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof.
unfold tm_test.
(* 这里的定型推导相当深,因此我们需要把 auto 的
最大搜索深度从默认的 5 增加到 10。 *)
auto 10.
(* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 5.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End Numtest.
Module ProdTest.
Definition tm_test :=
<{(($5,$6),$7).fst.snd}>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End ProdTest.
Definition tm_test :=
<{(($5,$6),$7).fst.snd}>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End ProdTest.
Module LetTest.
Definition tm_test :=
<{let x = (pred $6) in
(succ x)}>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto.
(* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End LetTest.
Module LetTest1.
Definition tm_test :=
<{ let z = pred $6 in
(succ z) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto.
(* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End LetTest1.
Definition tm_test :=
<{let x = (pred $6) in
(succ x)}>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto.
(* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End LetTest.
Module LetTest1.
Definition tm_test :=
<{ let z = pred $6 in
(succ z) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto.
(* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 6.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End LetTest1.
Module Sumtest1.
Definition tm_test :=
<{ case (inl Nat $5) of
| inl x ⇒ x
| inr y ⇒ y }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 5.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End Sumtest1.
Module Sumtest2.
(* let processSum =
\x:Nat+Nat.
case x of
inl n => n
inr n => tm_test0 n then 1 else 0 in
(processSum (inl Nat 5), processSum (inr Nat 5)) *)
Definition tm_test :=
<{ let processSum =
(\x:Nat + Nat,
case x of
| inl n ⇒ n
| inr n ⇒ (if0 n then $1 else $0)) in
(processSum (inl Nat $5), processSum (inr Nat $5)) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat × Nat }>.
Proof. unfold tm_test. eauto 10. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* <{ ($5, $0) }>.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End Sumtest2.
Definition tm_test :=
<{ case (inl Nat $5) of
| inl x ⇒ x
| inr y ⇒ y }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 5.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End Sumtest1.
Module Sumtest2.
(* let processSum =
\x:Nat+Nat.
case x of
inl n => n
inr n => tm_test0 n then 1 else 0 in
(processSum (inl Nat 5), processSum (inr Nat 5)) *)
Definition tm_test :=
<{ let processSum =
(\x:Nat + Nat,
case x of
| inl n ⇒ n
| inr n ⇒ (if0 n then $1 else $0)) in
(processSum (inl Nat $5), processSum (inr Nat $5)) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat × Nat }>.
Proof. unfold tm_test. eauto 10. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* <{ ($5, $0) }>.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End Sumtest2.
Module ListTest.
(* let l = cons 5 (cons 6 (nil Nat)) in
case l of
nil => 0
| x::y => x*x *)
Definition tm_test :=
<{ let l = ($5 :: $6 :: (nil Nat)) in
case l of
| nil ⇒ $0
| x :: y ⇒ (x × x) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 25.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End ListTest.
(* let l = cons 5 (cons 6 (nil Nat)) in
case l of
nil => 0
| x::y => x*x *)
Definition tm_test :=
<{ let l = ($5 :: $6 :: (nil Nat)) in
case l of
| nil ⇒ $0
| x :: y ⇒ (x × x) }>.
Example typechecks :
<{ empty |-- tm_test \in Nat }>.
Proof. unfold tm_test. eauto. (* 在这里填写 *) Admitted.
Example reduces :
tm_test -->* 25.
Proof.
(*
unfold tm_test. normalize.
*)
(* 在这里填写 *) Admitted.
End ListTest.
Module FixTest1.
Definition fact :=
<{ fix
(\f:Nat→Nat,
\a:Nat,
if0 a then $1 else (a × (f (pred a)))) }>.
Definition fact :=
<{ fix
(\f:Nat→Nat,
\a:Nat,
if0 a then $1 else (a × (f (pred a)))) }>.
(警告:你可能让 fact 通过了类型检查,但仍有某些规则是错的!)
Example typechecks :
<{ empty |-- fact \in Nat → Nat }>.
Proof. unfold fact. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ fact $4 }> -->* 24.
Proof.
(*
unfold fact. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest1.
Module FixTest2.
Definition map :=
<{ \g:Nat→Nat,
fix
(\f:(List Nat)→(List Nat),
\l:List Nat,
case l of
| nil ⇒ nil Nat
| x::l ⇒ ((g x)::(f l))) }>.
Example typechecks :
<{ empty |-- map \in
(Nat → Nat) → (List Nat) → (List Nat) }>.
Proof. unfold map. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ map (\a:Nat, succ a) ($1 :: $2 :: (nil Nat)) }>
-->* <{ $2 :: $3 :: (nil Nat) }>.
Proof.
(*
unfold map. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest2.
Module FixTest3.
Definition equal :=
<{ fix
(\eq:Nat→Nat→Nat,
\m:Nat, \n:Nat,
if0 m then (if0 n then $1 else $0)
else (if0 n
then $0
else (eq (pred m) (pred n)))) }>.
Example typechecks :
<{ empty |-- equal \in Nat → Nat → Nat }>.
Proof. unfold equal. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ equal $4 $4 }> -->* 1.
Proof.
(*
unfold equal. normalize.
*)
(* 在这里填写 *) Admitted.
(* GRADE_THEOREM 0.25: reduces *)
Example reduces2 :
<{ equal $4 $5 }> -->* 0.
Proof.
(*
unfold equal. normalize.
*)
(* 在这里填写 *) Admitted.
(* GRADE_THEOREM 0.25: reduces2 *)
End FixTest3.
Module FixTest4.
Definition eotest :=
<{ let evenodd =
fix
(\eo: (Nat → Nat) × (Nat → Nat),
(\n:Nat, if0 n then $1 else (eo.snd (pred n)),
\n:Nat, if0 n then $0 else (eo.fst (pred n)))) in
let even = evenodd.fst in
let odd = evenodd.snd in
(even $3, even $4) }>.
Example typechecks :
<{ empty |-- eotest \in Nat × Nat }>.
Proof. unfold eotest. eauto 30. (* 在这里填写 *) Admitted.
Example reduces :
eotest -->* <{ ($0, $1) }>.
Proof.
(*
unfold eotest. eauto 10. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest4.
End Examples.
☐
<{ empty |-- fact \in Nat → Nat }>.
Proof. unfold fact. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ fact $4 }> -->* 24.
Proof.
(*
unfold fact. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest1.
Module FixTest2.
Definition map :=
<{ \g:Nat→Nat,
fix
(\f:(List Nat)→(List Nat),
\l:List Nat,
case l of
| nil ⇒ nil Nat
| x::l ⇒ ((g x)::(f l))) }>.
Example typechecks :
<{ empty |-- map \in
(Nat → Nat) → (List Nat) → (List Nat) }>.
Proof. unfold map. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ map (\a:Nat, succ a) ($1 :: $2 :: (nil Nat)) }>
-->* <{ $2 :: $3 :: (nil Nat) }>.
Proof.
(*
unfold map. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest2.
Module FixTest3.
Definition equal :=
<{ fix
(\eq:Nat→Nat→Nat,
\m:Nat, \n:Nat,
if0 m then (if0 n then $1 else $0)
else (if0 n
then $0
else (eq (pred m) (pred n)))) }>.
Example typechecks :
<{ empty |-- equal \in Nat → Nat → Nat }>.
Proof. unfold equal. auto 10. (* 在这里填写 *) Admitted.
Example reduces :
<{ equal $4 $4 }> -->* 1.
Proof.
(*
unfold equal. normalize.
*)
(* 在这里填写 *) Admitted.
(* GRADE_THEOREM 0.25: reduces *)
Example reduces2 :
<{ equal $4 $5 }> -->* 0.
Proof.
(*
unfold equal. normalize.
*)
(* 在这里填写 *) Admitted.
(* GRADE_THEOREM 0.25: reduces2 *)
End FixTest3.
Module FixTest4.
Definition eotest :=
<{ let evenodd =
fix
(\eo: (Nat → Nat) × (Nat → Nat),
(\n:Nat, if0 n then $1 else (eo.snd (pred n)),
\n:Nat, if0 n then $0 else (eo.fst (pred n)))) in
let even = evenodd.fst in
let odd = evenodd.snd in
(even $3, even $4) }>.
Example typechecks :
<{ empty |-- eotest \in Nat × Nat }>.
Proof. unfold eotest. eauto 30. (* 在这里填写 *) Admitted.
Example reduces :
eotest -->* <{ ($0, $1) }>.
Proof.
(*
unfold eotest. eauto 10. normalize.
*)
(* 在这里填写 *) Admitted.
End FixTest4.
End Examples.
☐
可进性
练习: 3 stars,标准 (STLCExtended.progress)
完成 progress 的证明。
Theorem progress : ∀ t T,
<{ empty |-- t \in T }> →
value t ∨ ∃ t', t --> t'.
<{ empty |-- t \in T }> →
value t ∨ ∃ t', t --> t'.
Proof with eauto.
intros t T Ht.
remember empty as Gamma.
generalize dependent HeqGamma.
induction Ht; intros HeqGamma; subst.
- (* T_Var *)
(* 给定定型推导中的最后一条规则不可能是 T_Var,
因为 empty |-- x ∈ T 永远不会成立
(语境为空)。 *)
discriminate H.
- (* T_Abs *)
(* 若最后使用的是 T_Abs 规则,则
t = \ x0 : T2, t1,它是一个值。 *)
left...
- (* T_App *)
(* 若最后应用的规则是 T_App,则 t = t1 t2,
由规则形式可知
empty |-- t1 ∈ T1 → T2
empty |-- t2 ∈ T1
由归纳假设,t1 与 t2 各自要么是值,要么可步进。 *)
right.
destruct IHHt1; subst...
+ (* t1 是值 *)
destruct IHHt2; subst...
× (* t2 是值 *)
(* 若 t1 与 t2 都是值,则可知
t1 = \x0 : T0, t11,因为抽象是
唯一可能具有箭头类型的值。但由 ST_AppAbs,
(\x0 : T0, t11) t2 --> [x:=t2]t11。 *)
destruct H; try solve_by_invert.
∃ <{ [x0 := t2]t1 }>...
× (* t2 步进 *)
(* 若 t1 是值且 t2 --> t2',
则由 ST_App2 有 t1 t2 --> t1 t2'。 *)
destruct H0 as [t2' Hstp]. ∃ <{t1 t2'}>...
+ (* t1 步进 *)
(* 最后,若 t1 --> t1',则由 ST_App1 有
t1 t2 --> t1' t2。 *)
destruct H as [t1' Hstp]. ∃ <{t1' t2}>...
- (* T_Nat *)
left...
- (* T_Succ *)
right.
destruct IHHt...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
∃ <{ $(S n) }>...
+ (* t1 步进 *)
destruct H as [t' Hstp].
∃ <{succ t'}>...
- (* T_Pred *)
right.
destruct IHHt...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
∃ <{ $(n - 1) }>...
+ (* t1 步进 *)
destruct H as [t' Hstp].
∃ <{pred t'}>...
- (* T_Mult *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct IHHt2...
× (* t2 是值 *)
destruct H; try solve_by_invert.
destruct H0; try solve_by_invert.
∃ <{ $(n × n0) }>...
× (* t2 步进 *)
destruct H0 as [t2' Hstp].
∃ <{t1 × t2'}>...
+ (* t1 步进 *)
destruct H as [t1' Hstp].
∃ <{t1' × t2}>...
- (* T_Test0 *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
destruct n as [|n'].
× (* n1=0 *)
∃ t2...
× (* n1<>0 *)
∃ t3...
+ (* t1 步进 *)
destruct H as [t1' H0].
∃ <{if0 t1' then t2 else t3}>...
- (* T_Inl *)
destruct IHHt...
+ (* t1 步进 *)
right. destruct H as [t1' Hstp]...
(* exists (tm_inl _ t1')... *)
- (* T_Inr *)
destruct IHHt...
+ (* t1 步进 *)
right. destruct H as [t1' Hstp]...
(* exists (tm_inr _ t1')... *)
- (* T_Case *)
right.
destruct IHHt1...
+ (* t0 是值 *)
destruct H; try solve_by_invert.
× (* t0 是 inl *)
∃ <{ [x1:=v]t1 }>...
× (* t0 是 inr *)
∃ <{ [x2:=v]t2 }>...
+ (* t0 步进 *)
destruct H as [t0' Hstp].
∃ <{case t0' of | inl x1 ⇒ t1 | inr x2 ⇒ t2}>...
- (* T_Nil *)
left...
- (* T_Cons *)
destruct IHHt1...
+ (* 头部是值 *)
destruct IHHt2...
× (* 尾部步进 *)
right. destruct H0 as [t2' Hstp].
∃ <{t1 :: t2'}>...
+ (* 头部步进 *)
right. destruct H as [t1' Hstp].
∃ <{t1' :: t2}>...
- (* T_Lcase *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
× (* t1=tm_nil *)
∃ t2...
× (* t1=tm_cons v1 v2 *)
∃ <{ [x2:=v2]([x1:=v1]t3) }>...
+ (* t1 步进 *)
destruct H as [t1' Hstp].
∃ <{case t1' of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>...
- (* T_Unit *)
left...
(* 完成证明。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
(* 在这里填写 *) Admitted.
intros t T Ht.
remember empty as Gamma.
generalize dependent HeqGamma.
induction Ht; intros HeqGamma; subst.
- (* T_Var *)
(* 给定定型推导中的最后一条规则不可能是 T_Var,
因为 empty |-- x ∈ T 永远不会成立
(语境为空)。 *)
discriminate H.
- (* T_Abs *)
(* 若最后使用的是 T_Abs 规则,则
t = \ x0 : T2, t1,它是一个值。 *)
left...
- (* T_App *)
(* 若最后应用的规则是 T_App,则 t = t1 t2,
由规则形式可知
empty |-- t1 ∈ T1 → T2
empty |-- t2 ∈ T1
由归纳假设,t1 与 t2 各自要么是值,要么可步进。 *)
right.
destruct IHHt1; subst...
+ (* t1 是值 *)
destruct IHHt2; subst...
× (* t2 是值 *)
(* 若 t1 与 t2 都是值,则可知
t1 = \x0 : T0, t11,因为抽象是
唯一可能具有箭头类型的值。但由 ST_AppAbs,
(\x0 : T0, t11) t2 --> [x:=t2]t11。 *)
destruct H; try solve_by_invert.
∃ <{ [x0 := t2]t1 }>...
× (* t2 步进 *)
(* 若 t1 是值且 t2 --> t2',
则由 ST_App2 有 t1 t2 --> t1 t2'。 *)
destruct H0 as [t2' Hstp]. ∃ <{t1 t2'}>...
+ (* t1 步进 *)
(* 最后,若 t1 --> t1',则由 ST_App1 有
t1 t2 --> t1' t2。 *)
destruct H as [t1' Hstp]. ∃ <{t1' t2}>...
- (* T_Nat *)
left...
- (* T_Succ *)
right.
destruct IHHt...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
∃ <{ $(S n) }>...
+ (* t1 步进 *)
destruct H as [t' Hstp].
∃ <{succ t'}>...
- (* T_Pred *)
right.
destruct IHHt...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
∃ <{ $(n - 1) }>...
+ (* t1 步进 *)
destruct H as [t' Hstp].
∃ <{pred t'}>...
- (* T_Mult *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct IHHt2...
× (* t2 是值 *)
destruct H; try solve_by_invert.
destruct H0; try solve_by_invert.
∃ <{ $(n × n0) }>...
× (* t2 步进 *)
destruct H0 as [t2' Hstp].
∃ <{t1 × t2'}>...
+ (* t1 步进 *)
destruct H as [t1' Hstp].
∃ <{t1' × t2}>...
- (* T_Test0 *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
destruct n as [|n'].
× (* n1=0 *)
∃ t2...
× (* n1<>0 *)
∃ t3...
+ (* t1 步进 *)
destruct H as [t1' H0].
∃ <{if0 t1' then t2 else t3}>...
- (* T_Inl *)
destruct IHHt...
+ (* t1 步进 *)
right. destruct H as [t1' Hstp]...
(* exists (tm_inl _ t1')... *)
- (* T_Inr *)
destruct IHHt...
+ (* t1 步进 *)
right. destruct H as [t1' Hstp]...
(* exists (tm_inr _ t1')... *)
- (* T_Case *)
right.
destruct IHHt1...
+ (* t0 是值 *)
destruct H; try solve_by_invert.
× (* t0 是 inl *)
∃ <{ [x1:=v]t1 }>...
× (* t0 是 inr *)
∃ <{ [x2:=v]t2 }>...
+ (* t0 步进 *)
destruct H as [t0' Hstp].
∃ <{case t0' of | inl x1 ⇒ t1 | inr x2 ⇒ t2}>...
- (* T_Nil *)
left...
- (* T_Cons *)
destruct IHHt1...
+ (* 头部是值 *)
destruct IHHt2...
× (* 尾部步进 *)
right. destruct H0 as [t2' Hstp].
∃ <{t1 :: t2'}>...
+ (* 头部步进 *)
right. destruct H as [t1' Hstp].
∃ <{t1' :: t2}>...
- (* T_Lcase *)
right.
destruct IHHt1...
+ (* t1 是值 *)
destruct H; try solve_by_invert.
× (* t1=tm_nil *)
∃ t2...
× (* t1=tm_cons v1 v2 *)
∃ <{ [x2:=v2]([x1:=v1]t3) }>...
+ (* t1 步进 *)
destruct H as [t1' Hstp].
∃ <{case t1' of | nil ⇒ t2 | x1 :: x2 ⇒ t3}>...
- (* T_Unit *)
left...
(* 完成证明。 *)
(* 二元组 *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
(* 在这里填写 *) Admitted.
☐
Lemma weakening : ∀ Gamma Gamma' t T,
includedin Gamma Gamma' →
<{ Gamma |-- t \in T }> →
<{ Gamma' |-- t \in T }>.
Proof.
intros Gamma Gamma' t T H Ht.
generalize dependent Gamma'.
induction Ht; eauto 7 using includedin_update.
Qed.
Lemma weakening_empty : ∀ Gamma t T,
<{ empty |-- t \in T }> →
<{ Gamma |-- t \in T }>.
Proof.
intros Gamma t T.
eapply weakening.
discriminate.
Qed.
includedin Gamma Gamma' →
<{ Gamma |-- t \in T }> →
<{ Gamma' |-- t \in T }>.
Proof.
intros Gamma Gamma' t T H Ht.
generalize dependent Gamma'.
induction Ht; eauto 7 using includedin_update.
Qed.
Lemma weakening_empty : ∀ Gamma t T,
<{ empty |-- t \in T }> →
<{ Gamma |-- t \in T }>.
Proof.
intros Gamma t T.
eapply weakening.
discriminate.
Qed.
替换
练习: 2 stars,标准 (STLCExtended.substitution_preserves_typing)
完成 substitution_preserves_typing 的证明。
Lemma substitution_preserves_typing : ∀ Gamma x U t v T,
<{ x ⊢> U ; Gamma |-- t \in T }> →
<{ empty |-- v \in U }> →
<{ Gamma |-- [x:=v]t \in T }>.
<{ x ⊢> U ; Gamma |-- t \in T }> →
<{ empty |-- v \in U }> →
<{ Gamma |-- [x:=v]t \in T }>.
Proof with eauto.
intros Gamma x U t v T Ht Hv.
generalize dependent Gamma. generalize dependent T.
(* 证明:对项 t 归纳。除 var 与 abs 外,
多数情形直接由归纳假设得出。它们并非自动完成,
因为我们必须推理变量如何相互作用。
这些情形的证明与 STLC 中的类似。
解释请读者参见 StlcProp.v。 *)
induction t; intros T Gamma H;
(* 每种情形我们都要用到 H 的推导 *)
inversion H; clear H; subst; simpl; eauto.
- (* var *)
rename s into y. destruct (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, t into S.
destruct (eqb_spec x y); subst; apply T_Abs.
+ (* x=y *)
rewrite update_shadow in H5. assumption.
+ (* x<>y *)
apply IHt.
rewrite update_permute; auto.
- (* tm_case *)
rename s into x1, s0 into x2.
eapply T_Case...
+ (* 左分支 *)
destruct (eqb_spec x x1); subst.
× (* x = x1 *)
rewrite update_shadow in H8. assumption.
× (* x <> x1 *)
apply IHt2.
rewrite update_permute; auto.
+ (* 右分支 *)
destruct (eqb_spec x x2); subst.
× (* x = x2 *)
rewrite update_shadow in H9. assumption.
× (* x <> x2 *)
apply IHt3.
rewrite update_permute; auto.
- (* tm_lcase *)
rename s into y1, s0 into y2.
eapply T_Lcase...
destruct (eqb_spec x y1); subst.
+ (* x=y1 *)
destruct (eqb_spec y2 y1); subst.
× (* y2=y1 *)
repeat rewrite update_shadow in H9.
rewrite update_shadow.
assumption.
× rewrite update_permute in H9; [|assumption].
rewrite update_shadow in H9.
rewrite update_permute; assumption.
+ (* x<>y1 *)
destruct (eqb_spec x y2); subst.
× (* x=y2 *)
rewrite update_shadow in H9.
assumption.
× (* x<>y2 *)
apply IHt3.
rewrite (update_permute _ _ _ _ _ _ n0) in H9.
rewrite (update_permute _ _ _ _ _ _ n) in H9.
assumption.
(* 完成证明。 *)
(* 在这里填写 *) Admitted.
intros Gamma x U t v T Ht Hv.
generalize dependent Gamma. generalize dependent T.
(* 证明:对项 t 归纳。除 var 与 abs 外,
多数情形直接由归纳假设得出。它们并非自动完成,
因为我们必须推理变量如何相互作用。
这些情形的证明与 STLC 中的类似。
解释请读者参见 StlcProp.v。 *)
induction t; intros T Gamma H;
(* 每种情形我们都要用到 H 的推导 *)
inversion H; clear H; subst; simpl; eauto.
- (* var *)
rename s into y. destruct (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, t into S.
destruct (eqb_spec x y); subst; apply T_Abs.
+ (* x=y *)
rewrite update_shadow in H5. assumption.
+ (* x<>y *)
apply IHt.
rewrite update_permute; auto.
- (* tm_case *)
rename s into x1, s0 into x2.
eapply T_Case...
+ (* 左分支 *)
destruct (eqb_spec x x1); subst.
× (* x = x1 *)
rewrite update_shadow in H8. assumption.
× (* x <> x1 *)
apply IHt2.
rewrite update_permute; auto.
+ (* 右分支 *)
destruct (eqb_spec x x2); subst.
× (* x = x2 *)
rewrite update_shadow in H9. assumption.
× (* x <> x2 *)
apply IHt3.
rewrite update_permute; auto.
- (* tm_lcase *)
rename s into y1, s0 into y2.
eapply T_Lcase...
destruct (eqb_spec x y1); subst.
+ (* x=y1 *)
destruct (eqb_spec y2 y1); subst.
× (* y2=y1 *)
repeat rewrite update_shadow in H9.
rewrite update_shadow.
assumption.
× rewrite update_permute in H9; [|assumption].
rewrite update_shadow in H9.
rewrite update_permute; assumption.
+ (* x<>y1 *)
destruct (eqb_spec x y2); subst.
× (* x=y2 *)
rewrite update_shadow in H9.
assumption.
× (* x<>y2 *)
apply IHt3.
rewrite (update_permute _ _ _ _ _ _ n0) in H9.
rewrite (update_permute _ _ _ _ _ _ n) in H9.
assumption.
(* 完成证明。 *)
(* 在这里填写 *) Admitted.
☐
Theorem preservation : ∀ t t' T,
<{ empty |-- t \in T }> →
t --> t' →
<{ empty |-- t' \in T }>.
<{ empty |-- t \in T }> →
t --> t' →
<{ empty |-- t' \in T }>.
Proof with eauto.
intros t t' T HT. generalize dependent t'.
remember empty as Gamma.
(* 证明:对给定的定型推导归纳。许多情形是矛盾的
(T_Var、T_Abs)。我们只展示有趣的情形。
再次请读者参见 StlcProp.v 中的解释。 *)
induction HT;
intros t' HE; subst; inversion HE; subst...
- (* T_App *)
inversion HE; subst...
+ (* ST_AppAbs *)
apply substitution_preserves_typing with T2...
inversion HT1...
(* T_Case *)
- (* ST_CaseInl *)
inversion HT1; subst.
eapply substitution_preserves_typing...
- (* ST_CaseInr *)
inversion HT1; subst.
eapply substitution_preserves_typing...
- (* T_Lcase *)
+ (* ST_LcaseCons *)
inversion HT1; subst.
apply substitution_preserves_typing with <{{List T1}}>...
apply substitution_preserves_typing with T1...
(* 完成证明。 *)
(* fst 与 snd *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
(* 在这里填写 *) Admitted.
intros t t' T HT. generalize dependent t'.
remember empty as Gamma.
(* 证明:对给定的定型推导归纳。许多情形是矛盾的
(T_Var、T_Abs)。我们只展示有趣的情形。
再次请读者参见 StlcProp.v 中的解释。 *)
induction HT;
intros t' HE; subst; inversion HE; subst...
- (* T_App *)
inversion HE; subst...
+ (* ST_AppAbs *)
apply substitution_preserves_typing with T2...
inversion HT1...
(* T_Case *)
- (* ST_CaseInl *)
inversion HT1; subst.
eapply substitution_preserves_typing...
- (* ST_CaseInr *)
inversion HT1; subst.
eapply substitution_preserves_typing...
- (* T_Lcase *)
+ (* ST_LcaseCons *)
inversion HT1; subst.
apply substitution_preserves_typing with <{{List T1}}>...
apply substitution_preserves_typing with T1...
(* 完成证明。 *)
(* fst 与 snd *)
(* 在这里填写 *)
(* let *)
(* 在这里填写 *)
(* fix *)
(* 在这里填写 *)
(* 在这里填写 *) Admitted.
☐
