TAPL《类型和程序设计语言》第三章笔记

1,377 阅读1分钟

为什么没有第二章的笔记呢?因为第二章全章都是在科普数学知识。

第二章 数学预备知识

翻译半天,我发现看这个课件 就可以了

  • 集合 Sets, 关联 Relations, and 函数 Functions
  • 有序集合 Ordered Sets
  • 序列 Sequences
  • 归纳法 Induction

第三章 算术表达式(无类型)

介绍

这一章用到的编程语言只有一些简单的语法:

  • 布尔值 truefalse
  • 条件表达式 if ... then ... else ...
  • 数字 0
  • 算术操作符 next(表示加一) 和 prev(表示减一)
  • 判断操作符 iszero

即:

term ::=                               
        true                       
        false
        if term then term else term
        0
        next term
        prev term
        iszero term

在第八章之前,term 跟 expression 同义,就是表达式的意思。看看下面表达式你能不能理解:

if flase then 0 else 1;
> 1
iszero (prev (next 0));
> true
next(next(next(0)));
> 3

语法 Syntax

上面的 term ::= 表达式定义了这门语言的语法,但现在介绍另一种定义语法的方式。terms 的集合就是这样一个集合:

  1. {true,false,0}T\{true, false, 0\} \subseteq T
  2. if t1Tt_1 \in T,then {next t1,prev t1,iszero t1}T\{\text{next } t_1, \text{prev } t_1, \text{iszero } t1\} \subseteq T
  3. if t1Tt_1 \in T, t2Tt_2 \in T, t3Tt_3 \in T, then if t1t_1 then t2t_2 else t3t_3 T\in T

上面的规则可以缩写成:

trueTtrue \in TfalseTfalse \in T0T0 \in T
t1Tnext t1T\frac{t_1 \in T}{\text{next }t_1 \in T}t1Tprev t1T\frac{t_1 \in T}{\text{prev }t_1 \in T}t1Tiszero t1T\frac{t_1 \in T}{\text{iszero }t_1 \in T}
t1T  t2T  t3Tif t1 then t2 else t3T\frac{t_1 \in T\; t_2 \in T\;t_3 \in T}{\text{if }t_1 \text{ then }t_2\text{ else }t_3 \in T}

上面的规则还可以写成:

S

求值

image.png

上图的意思是:

  • v 求值最终得到 v
  • 如果 t1t_1 求值得到 true,t2t_2 求值得到 v2v_2,则 if t1t_1 then t2t_2 else t3t_3 求值得到 v2v_2
  • 以此类推

完。