阅读时间约 20 分钟

「SF-PLF」6 Types

Programming Language Foundations - Type Systems

Posted by Hux on March 6, 2019
SF (软件基础) PLF (编程语言基础) Coq 笔记

This chapter:

  • typing relation – 定型关系
  • type preservation and progress (i.e. soundness proof) – 类型保留,可进性

Typed Arithmetic Expressions (Toy Typed Language)

The toy lang from SmallStep is too “safe” to demonstrate any runtime (or dynamic) type errors. – 运行时类型错误 So that’s add some operations (common church numeral ones), and bool type.

…same teaching order as TAPL. In PLT, we went directly to STLC.

Syntax

t ::= tru | fls | test t then t else t | zro | scc t | prd t | iszro t
Inductive tm : Type :=
  | tru : tm
  | fls : tm
  | test : tm  tm  tm  tm
  | zro : tm
  | scc : tm  tm
  | prd : tm  tm
  | iszro : tm  tm.

(* object language has its own bool / nat , 这里不使用 Coq (meta-language) 比较 pure 一些? *)
Inductive bvalue : tm  Prop :=
  | bv_tru : bvalue tru
  | bv_fls : bvalue fls.
Inductive nvalue : tm  Prop :=
  | nv_zro : nvalue zro
  | nv_scc : t, nvalue t  nvalue (scc t).  (** 注意这里 nv_scc 是描述所有 [scc t] 是 nvalue 的一个 constructor / tag **)

(* [value?] predicate *)
Definition value (t : tm) := bvalue t  nvalue t.

Automation

Hint are added to database to help with auto. More details on auto. eapply. eauto. were mentioned in lf/Auto.

Hint Constructors bvalue nvalue.
Hint Unfold value.
Hint Unfold update.

S.O.S

Small-step operational semantics… can be made formally in Coq code:

Reserved Notation "t1 '-->' t2" (at level 40).
Inductive step : tm  tm  Prop :=
  | ST_TestTru : t1 t2,
      (test tru t1 t2) --> t1
  ...

“is stuck” vs. “can get stuck” 卡住的项 vs. 将会卡住的项

Noticed that the small-step semantics doesn’t care about if some term would eventually get stuck.

Normal Forms and Values

因为这个语言有 stuck 的情况,所以 value != normal form (terms cannot make progress) possible_results_of_reduction = value | stuck

Notation step_normal_form := (normal_form step).
Definition stuck (t : tm) : Prop :=
  step_normal_form t  ¬value t.

Slide Q&A 1

  1. Yes
  2. No scc zro is a value
  3. No is a value

Typing

Inductive ty : Type :=
  | Bool : ty
  | Nat : ty.

Noticed that it’s just a non-dependently-typed ADT, but : ty is written explcitly here…they are the same!

Typing Relations

okay the funny thing… it make sense to use here since : has been used by Coq. but this notation is actually represented as \in. We suddenly switch to LaTex mode…

Reserved Notation "'|-' t '\in' T" (at level 40).

Noticed the generic T here. In PLT we sometimes treat them as “magic” meta variable, here we need to make the T explcit (we are in the meta-language).

⊢ t1 ∈ Bool    ⊢ t2 ∈ T    ⊢ t3 ∈ T	
----------------------------------  (T_Test)
  ⊢ test t1 then t2 else t3 ∈ T
| T_Test : t1 t2 t3 T,     (** <--- explicit ∀ T **)
        t1  Bool 
        t2  T 
        t3  T 
        test t1 t2 t3  T
Example has_type_1 :
   test fls zro (scc zro)  Nat.
Proof.
  apply T_Test.      (** <--- we already know [T] from the return type [Nat] **)
    - apply T_Fls.   (** ⊢ _ ∈ Bool **)
    - apply T_Zro.   (** ⊢ _ ∈ Nat  **)
    - apply T_Scc.   (** ⊢ _ ∈ Nat  **)
       + apply T_Zro.
Qed.

(Since we’ve included all the constructors of the typing relation in the hint database, the auto tactic can actually find this proof automatically.)

typing relation is a conservative (or static) approximation

类型关系是一个保守的(或静态的)近似

Example has_type_not :
  ¬(  test fls zro tru  Bool ).
Proof.
  intros Contra. solve_by_inverts 2. Qed.   (** 2-depth inversions **)

Lemma Canonical Forms 典范形式

As PLT.

Progress (可进性)

Theorem progress : t T,
   t  T 
  value t  t', t --> t'.

Progress vs Strong Progress? Progress require the “well-typeness”!

Induction on typing relation.

Slide Q&A

  • partial function yes
  • total function no
    • thinking as our inference rules.
    • we could construct some terms that no inference rules can apply and get stucked.

Type Preservation (维型性)

Theorem preservation : t t' T,
   t  T    (** HT **)
  t --> t'   (** HE **)
   t'  T.   (** HT' **)

按 PLT 的思路 Induction on HT,需要 inversion HE 去枚举所有情况拿到 t’ 之后证明 HT’ 按 PFPL 的思路 Inudction on HE, 只需 inversion HT,因为 HT 是按 reduction 相反方向定义的!

  • reduction 方向,AST top-down e.g. (+ 5 5) —–> 10
  • typing 方向,AST bottom-up e.g. - ..:N —– - (+ 5 5):N
Proof with eauto.
  intros t t' T HT HE.
  generalize dependent T.
  induction HE; intros T HT;
    inversion HT; subst...
  apply nvalue_in_nat...  (** 除了 ST_PrdScc 全部 inversion 解决... **)
Qed.

The preservation theorem is often called subject reduction, – 主语化简 想象 term 是主语,仅仅 term 在化简,而谓语宾语不变

one might wonder whether the opposity property — subject expansion — also holds. – 主语拓张 No, 我们可以很容易从 (test tru zro fls) 证明出 |- fls \in Nat. – 停机问题 (undecidable)

Type Soundness (Type Safety)

a well-typed term never get stuck.

Definition multistep := (multi step).  (** <--- from SmallStep **)
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).

Corollary soundness : t t' T,
   t  T 
  t -->* t' 
  ~(stuck t').
Proof.
  intros t t' T HT P. induction P; intros [R S].
  destruct (progress x T HT); auto.
  apply IHP. apply (preservation x y T HT H).
  unfold stuck. split; auto. Qed.

Induction on -->*, the multi-step derivation. (i.e. the reflexive-transtive closure)

Noticed that in PLT, we explcitly write out what is “non-stuck”. But here is ~(stuck t') thus the proof becomes:

R : step_normal_form x    (** normal form **)
S : ~ value x             (** and not value **)
=======================
False                     (** prove this is False **)

The proof is weird tho.



Testimonials

What readers say

先看读者反馈,再直接在当前页面继续讨论。公共留言需要 Waline 服务端;配置后访客只填昵称即可发布。

这类长文如果结构清楚,我会一路读到底。这里最好的地方是把概念、公式和代码示例放在同一篇里。

L
Lin 算法读者

数据库和工程文档的风格很实用,截图、SQL 和说明都能直接拿去复盘项目。

M
Mia 工程笔记党

强化学习相关文章密度很高,但排版如果更清楚,回看体验会更好。这个新版方向是对的。

R
Ryo 深夜学习者

我更喜欢能快速扫到标签、修改时间和文章重点的首页,现在这种卡片视图会比纯列表更容易选读。

C
Chen 知识整理控

代码块只要语言标识和层级做好,技术博客的专业感会立刻上来。

A
Ava 前端同行

评论区不用社交账号强绑定会更愿意留言,尤其是这种偏学习记录的网站。

N
Noah 匿名访客

Quick Identity

Pick a preset and leave a note

留言方式:先选择一个预设身份,再在下方输入评论。当前若显示“需要配置 Waline”,说明站点还缺少可写评论后端。

当前未选择预设身份

Live Discussion Waline 配置完成后,真实评论会加载在下方,移动端和主题切换会同步处理。
WALINE
Loading comments…