本篇前置:

基础信息

  • image
  • 书名:Concepts, Techniques, and Models of Computer Programming
  • ISBN:978-0-262-22069-9
  • 2.1 Defining practical programming languages
  • Incentive:本学期课程有一门“约束式编程”,是一种非常有意思的paradigm. 想弄明白其在各种programming paradigm全景中的位置。

2 Declarative Computation Model

  • computational model:形式系统,其中句子在抽象的某个机器上的运行效果
  • programming techniques and design principles:利用computational model的一些模式、方法
  • reasoning techniques:推理出正确性!

Intuitively, we will say that a reasonable model is one that can be used to solve many problems, that has straightforward and practical reasoning techniques, and that can be implemented efficiently

declarative:包含了functional和logic
且能很好并行(并发)

2.1 Defining practical programming languages

  • syntax (“grammar”) and semantics (“meaning”)

是分析所有paradigm的基础!

syntax

  • syntax: what are the legal programs,不关心实际上做什么
    image
  • characters经过tokenizer得到sequence of tokens,再经过parser得到树
    之前链表那里的(非平衡)树结构就是一个例子
  • EBNF:例如<int>::=<digit>{<digit>},用形式化的方式表示一个语言(字符串集合)
    具体地,是上下文无关语言(参考任意理论计算机科学课本)
  • image
    除了语言,还需要一些约束(比如先声明变量才能使用)

The grammar can be used both to verify that a statement is legal and to generate statements.

  • CFG, CSG, 歧义参见理论计算机科学课本
    消歧义的实际手段:定义优先级(precedence)和结合顺序(associativity)
    举例:C++的运算符优先级与顺序
    image
    来自docs.microsoft.com
    这“关联性”看起来翻译很机械……
  • syntax notation:没有“否定”。只要任何一条说合法,那就合法。用...省略可能性
    例如<statement> ::= skip | <expression> `=` <expression> | ...
    表示<statement>可能是skip,或者给变量赋值,或者……
    image
    更多例子。可以看到{}使用,{}+使用(至少一个,不能0个),以及complete definition的例子

semantics

semantics要能处理复杂任务,又要能数学上简单,方便推理正确性和复杂度,怎么办?
令kernel language是实际语言的抽象。kernel language容易分析,概念最少,有形式化的语义。需要把实际语言翻译成kernel language(linguistic abstraction或语法糖syntactic sugar)

The kernel language approach is used throughout the book. Each computation model has its kernel language, which builds on its predecessor by adding one new concept. The first kernel language, which is presented in this chapter, is called the declarative kernel language. Many other kernel languages are presented later on in the book

常见的semantics

  • operational:实际一步一步执行。应用最广
  • axiomatic:用公理对执行前后的状态断言。贴近stateful思想
  • denotational:“函数”思想
  • logical:使用logic theory,对declarative and relational computation models很适用
  • 数学和计算机关心的往往很不一样

Throughout the book, we give an informal semantics for every new language construct and we often reason informally about programs. These informal presentations are always based on the operational semantics.

  • linguistic abstraction例子:用条件语句构造循环语句
    • Lisp macros:语言中直接自己写abstraction
    • 有时可能有未定义行为:如{F1 {F2 X} {F3 Y}},有些编程语言中没有定义函数执行顺序
  • Syntactic sugar:不做本质抽象,只是提升效率和可读性。比如else L in ... end代替else local L in ... end end

实际设计中,提出abstraction,再尝试在语言中实现abstraction看是否好,最后再真正加入
三种翻译:kernel language对人简单。foundational approach(比如\(\lambda\)演算,图灵机)很“理论”,要素少,很“数学”。abstract machine方便计算机执行
interpreter(解释器):一种语言写就,用来读取另一种语言的语句并执行相应语义
本书不使用解释器:不方便分析复杂度(解释器可能导致复杂度量级变化),且不方便把基本概念解耦出来

总结和问答练习

  1. Q: 如果有人说“我不想学声明式,我只想学命令式”,可以看本书时跳过第2章吗?
    A: 不能。声明式是命令式(stateful/imperative)的基础(从kernel language可以看出)。且本章语法、语义、kernel language相关的思想方法后面一直要用。
    再引用一遍

The kernel language approach is used throughout the book. Each computation model has its kernel language, which builds on its predecessor by adding one new concept. The first kernel language, which is presented in this chapter, is called the declarative kernel language. Many other kernel languages are presented later on in the book

  1. Q: 虚拟机的原始含义是Java虚拟机的那个意思吗?还是我们用Vagrant,VirtualBox之类的在Windows上跑Linux的意思?
    A: image