【PL理论】(33) 类型系统:推导树证明 φ ⊢ e∶t | 继续定义关系:Γ ⊢ e∶t

  • 💬 写在前面:本章我们将讲解推导树证明,推导树实际上就是推理规则的应用。只要学会如何选择并应用适当的推理规则,证明就不是难事了。

目录

0x00 推导树证明 𝝓 ⊢ 𝒆 ∶ 𝒕

0x01 继续定义关系:𝚪 ⊢ 𝒆 ∶ 𝒕


0x00 推导树证明 𝝓 ⊢ 𝒆 ∶ 𝒕

对于程序 e,如果我们能画出一个推导树来证明:

\color{} \phi \vdash e:t

那么我们的类型系统接受 e,这意味着该程序没有类型错误。

推导树就是推理规则的应用,整个程序的形式为 let x = e1 in e2,因此我们应用以下推理规则: 

实例化 \color{} \Gamma 为 \color{} \phi, \color{} e_1:= 3+2,e_2:=x<2 :

左子树必须证明:\color{} \phi \vdash 3+2 :int

右子树必须证明:\color{} x \mapsto int \vdash x<7 : bool

完整的推导树如下所示,注意观察每个子树部分应用了哪些推理规则:

0x01 继续定义关系:𝚪 ⊢ 𝒆 ∶ 𝒕

下面我们继续定义关系:\color{} \phi \vdash e:t 

考虑 let f x = e1 in e2(函数定义),假设当参数 \color{} x 的类型为 \color{} t_a 时,\color{} e_1 的类型为 \color{} t_r

那么函数 \color{} f 的类型是 \color{} t_a \mapsto t_r

思考 \color{} e_1,e_2 的和函数应用:

如果 \color{} e_1 的类型为 \color{} t_a\rightarrow t_r,且参数 \color{} e_2 的类型为 \color{} t_a,则函数调用的结果为 \color{} t_r

证明对于以下程序 \color{} \phi \vdash e:t,写出其推导树:

使用以下推理规则,实例化 \color{} \Gamma 为 \color{} \phi\color{} e_1:= x<1,e_2:=f5

同时,我们知道 \color{} t_a 必须是 int,而 \color{} t_r 是 bool:

完整的推导树如下:

0x02 注意多种类型地情况

意对于某些程序 (𝒆),可能存在多种类型 (𝒕),使得 𝝓 ⊢ 𝒆 ∶ 𝒕 成立。

换句话说,类型可能不唯一确定!

let f x = x in f 为例:以下几种情况下的 𝝓 ⊢ 𝒆 ∶ 𝒕 都是可以证明,可以画出推导树的:

📌 [ 笔者 ]   王亦优
📃 [ 更新 ]   2024.6.15
❌ [ 勘误 ]   /* 暂无 */
📜 [ 声明 ]   由于作者水平有限,本文有错误和不准确之处在所难免,
              本人也很想知道这些错误,恳望读者批评指正!

📜 参考资料 

- R. Neapolitan, Foundations of Algorithms (5th ed.), Jones & Bartlett, 2015.

- T. Cormen《算法导论》(第三版),麻省理工学院出版社,2009年。

- T. Roughgarden, Algorithms Illuminated, Part 1~3, Soundlikeyourself Publishing, 2018.

- J. Kleinberg&E. Tardos, Algorithm Design, Addison Wesley, 2005.

- R. Sedgewick&K. Wayne,《算法》(第四版),Addison-Wesley,2011

- S. Dasgupta,《算法》,McGraw-Hill教育出版社,2006。

- S. Baase&A. Van Gelder, Computer Algorithms: 设计与分析简介》,Addison Wesley,2000。

- E. Horowitz,《C语言中的数据结构基础》,计算机科学出版社,1993

- S. Skiena, The Algorithm Design Manual (2nd ed.), Springer, 2008.

- A. Aho, J. Hopcroft, and J. Ullman, Design and Analysis of Algorithms, Addison-Wesley, 1974.

- M. Weiss, Data Structure and Algorithm Analysis in C (2nd ed.), Pearson, 1997.

- A. Levitin, Introduction to the Design and Analysis of Algorithms, Addison Wesley, 2003. - A. Aho, J. Hopcroft, and J. Ullman, Data Structures and Algorithms, Addison-Wesley, 1983.

- E. Horowitz, S. Sahni and S. Rajasekaran, Computer Algorithms/C++, Computer Science Press, 1997.

- R. Sedgewick, Algorithms in C: 第1-4部分(第三版),Addison-Wesley,1998

- R. Sedgewick,《C语言中的算法》。第5部分(第3版),Addison-Wesley,2002

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王平渊

喜欢的话可以支持下我的付费专栏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值