Skip to content

程序语言理论笔记

Updated: at 15:06

Chpt 1. Set Theory

幂集, Power Set: 集合AA的所有子集构成的集合,记作P(A)P(A)2A2^A

外延性公理, Axiom of Extensionality: 两个集合包含相同的元素则它们相等

AB(x(xAxB)A=B).\forall A\forall B(\forall x(x\in A\leftrightarrow x\in B)\rightarrow A=B).

构造公理:

Ax(xA).\exist A\forall x(x\notin A). uvAx(xA(x=ux=v)\forall u\forall v\exist A\forall x(x\in A \leftrightarrow(x=u\lor x=v) ABx(xBC(CAxC))\forall A\exist B \forall x(x\in B \leftrightarrow \exist C(C\in A\land x\in C)) ABx(xBxA)\forall A\exist B\forall x(x\in B\leftrightarrow x\subseteq A)

偏序, Partial Order: 对于一个关系RR和一个集合XX,如果满足:

<X,R>\left<X, R\right>是一个偏序关系。

Chpt 2. Operational Semantics

IMP语言的操作语义

  1. 状态(State)
    • 状态是一个函数 σ:LocN\sigma :Loc\rightarrow N
    • 状态的集合表示为\sum
    • 直观理解:状态指定了各个位置(变量)所持有的值
  2. 目标
    • 构建一个关系 R(Com×)×R\subseteq(Com \times \sum)\times \sum,使得 (c,σ)′ 表示在初始状态 σ 下执行命令 c,最终得到状态σ\sigma'
  3. 算术表达式的配置(Configurations for Aexp)
    • 配置是一个对<a,σ>\left<a, \sigma\right>,其中aAexpa\in Aexpσ\sigma \in \sum
    • 目标:定义一个关系<a,σ>n\left<a, \sigma\right>\rightarrow n,表示在状态σ\sigma下,算术表达式aa被求值为整数nn.

IMP的命令语义

a,σnX:=a,σσ[Xn]\frac{\langle a, \sigma \rangle \Rightarrow n}{\langle X := a, \sigma \rangle \Rightarrow \sigma[X \mapsto n]} c1,σσc2,σσc1;c2,σσ\frac{\langle c_1, \sigma \rangle \Rightarrow \sigma'' \quad \langle c_2, \sigma'' \rangle \Rightarrow \sigma'}{\langle c_1; c_2, \sigma \rangle \Rightarrow \sigma'} b,σtruec1,σσif b then c1 else c2,σσb,σfalsec2,σσif b then c1 else c2,σσ\frac{\langle b, \sigma \rangle \Rightarrow true \quad \langle c_1, \sigma \rangle \Rightarrow \sigma'}{\langle if \ b \ then \ c_1 \ else \ c_2, \sigma \rangle \Rightarrow \sigma'}\\ \frac{\langle b, \sigma \rangle \Rightarrow false \quad \langle c_2, \sigma \rangle \Rightarrow \sigma'}{\langle if \ b \ then \ c_1 \ else \ c_2, \sigma \rangle \Rightarrow \sigma'} b,σfalsewhile b do c,σσb,σtruec,σσwhile b do c,σσwhile b do c,σσ\frac{\langle b, \sigma \rangle \Rightarrow false}{\langle while \ b \ do \ c, \sigma \rangle \Rightarrow \sigma}\\ \frac{\langle b, \sigma \rangle \Rightarrow true \quad \langle c, \sigma \rangle \Rightarrow \sigma'' \quad \langle while \ b \ do \ c, \sigma'' \rangle \Rightarrow \sigma'}{\langle while \ b \ do \ c, \sigma \rangle \Rightarrow \sigma'}

大步语义:如上,描述程序从初始状态到最终状态的转变,不关注中间过程;<c,σ>σ\left<c, \sigma\right>\rightarrow\sigma'

小步语义:描述one-step的执行变化,比如执行了一次加法或赋值;<c,σ>1<c,σ>\left<c, \sigma\right>\rightarrow_1\left<c', \sigma'\right>

Chpt 3. Some Principles of Induction

数学归纳法:

[P(0)&nN.(P(n)P(n+1))]nN.P(n)\left[ P(0) \& \forall n\in \mathbb{N}. (P(n)\rightarrow P(n+1))\right] \rightarrow \forall n\in \mathbb{N}. P(n)

良基归纳 Well-Founded Induction:

The relation \prec is well-founded if:

即不允许无限下降链,所以\leq不是良基的,因为可以出现无限下降链(全等于)。

结构归纳 Structural Induction:

  1. 基始步(Base Case) :证明性质对于基本结构(即最小或最简单的结构)成立。
  2. 归纳步(Inductive Step) :假设性质对某些子结构成立,然后证明性质对包含这些子结构的复合结构也成立。

Chpt 4. Inductive Definitions

前提/结论前提/结论

Axiom Instances: /y\empty/y

Non-axiom Rule Instances: {x1,...,xn}/y\left\{x_1, ..., x_n\right\}/y

dRyd\vdash_Ry:推导dd在规则集RR下可以导出结论yy

(/y)Ry if (/y)R(\emptyset / y) \vdash_R y \text{ if } (\emptyset / y) \in R ({d1,,dn}/y)y if ({x1,,xn}/y)R  and  d1Rx1,,dnRxn;(\{d_1, \ldots, d_n\} / y) \vdash y \text{ if } (\{x_1, \ldots, x_n\} / y) \in R ~~\text{and}~~ d_1 \vdash_R x_1, \ldots, d_n \vdash_R x_n;

Rule Induction:

xIR.P(x)  iff   (X/y)R.[(XIR  &  xX.P(x))P(y)]\begin{align*}&\forall x\in I_R. P(x) ~~\text{iff}~~~ \forall (X/y)\in R. [(X\subseteq I_R ~~\&~~ \forall x\in X.P(x))\rightarrow P(y)]\end{align*}

就是若XXyyXX都满足PPyy也满足

Closedness:

(X/y)R.(xQyQ).\forall (X/y)\in R. (x\subseteq Q\rightarrow y\in Q).

Special Rule Induction: 证明一个谓词PPIRI_R的一个子集AA上成立

(X/y)R.[(XIR & yA & (xXA.  Q(x)))Q(y)]\forall (X/y)\in R. [(X\subseteq I_R ~\&~ y\in A~\&~ (\forall x\in X \cap A.~~Q(x)))\rightarrow Q(y)]

证明过程例子:YY: a location (program variable)

c,σ,σ[(Yloc(c) & c,σσ)σ(Y)=σ(Y)]\forall c, \sigma, \sigma' \cdot [(Y \notin \text{loc}(c) \ \& \ \langle c, \sigma \rangle \to \sigma') \Rightarrow \sigma(Y) = \sigma'(Y)]

证:

  1. 定义集合AA
A:={c,σσYloc(c)}A:= \{ \langle c, \sigma \rangle \to \sigma' \mid Y \notin \text{loc}(c) \}
  1. 定义谓词QQ
Q(c,σ,σ):=σ(Y)=σ(Y)Q(c, \sigma, \sigma') := \sigma(Y) = \sigma'(Y)
  1. 验证条件:
c,σσA.Q(c,σ,σ)\forall \langle c, \sigma \rangle \to \sigma' \in A . Q(c, \sigma, \sigma')
  1. 利用闭合性:
(X/y)R.[(XIR & yA & (xXAQ(x)))Q(y)]\forall (X/y) \in R. [(X \subseteq I_R \ \& \ y \in A \ \& \ (\forall x \in X \cap A \cdot Q(x))) \Rightarrow Q(y)]

Chpt 5. The Denotational Semantics of IMP

Equivalence over Commands:

c0c1,   iff σ,σ,(<c0,σ>σ<c1,σ>σ)iff {(σ,σ)<c0,σ>σ}={(σ,σ)<c1,σ>σ}\begin{align*}c_0\sim c_1,~~~ &\text{iff}~\forall \sigma, \sigma', (\left<c_0, \sigma\right>\rightarrow \sigma'\leftrightarrow \left<c_1, \sigma\right>\rightarrow\sigma')\\&\text{iff}~\{(\sigma, \sigma')|\left<c_0, \sigma\right>\rightarrow \sigma'\}=\{(\sigma, \sigma')|\left<c_1, \sigma\right>\rightarrow \sigma'\}\end{align*}

从这个视角来看,commands都是一些偏函数,展示了从一些状态转移到另一些状态的过程。Denotational Semantics指称语义就是研究程序对应的数学对象,通过这些数学对象的性质研究程序的性质。

完备偏序集 Complete Partial Orders CPOs:

nωdn:={dnnω}={d0,d1,...,dn,...}\bigsqcup_{n\in\omega}d_n:=\sqcup\{d_n|n\in\omega\}=\sqcup\{d_0, d_1, ..., d_n, ...\}

单调函数 Monotonic Functions:

With(D,D), (E,E)(D, \sqsubseteq_D),~(E, \sqsubseteq_E),a functionf: DEf:~D\rightarrow E is monotonic if

d,dD. [dDdf(d)Ef(d)]\forall d, d'\in D.~[d\sqsubseteq_Dd'\rightarrow f(d)\sqsubseteq_E f(d')]

连续函数Continous Functions:

A function f: DEf:~D\rightarrow E is continuous if

nωf(dn)=f(nωdn)\bigsqcup_{n\in \omega}f(d_n)=f\left(\bigsqcup_{n\in\omega}d_n\right)

不动点Fixed Points:

An element dDd\in D is:

Suppose

Then

Knaster-Tarski不动点 Knaster-Tarski Fixed-Point Theorem:

Chpt 5. The Axiomatic Semantics of IMP

霍尔三元组 Hoare Triples:

{P}c{Q}\{P\}c\{Q\}

PP是前条件,cc是命令,QQ是后条件。

Assn定义一组逻辑断言,描述和验证程序的行为。状态σ\sigma满足一个断言AA记作σA\sigma\models A,当且仅当AA中所有的XXσ(X)\sigma(X)替代之后AA仍然为真。A[a/i]A[a/i]就是把程序AA中的ii全部替换成aa

σ{A}c{B} iff (σA    C[x](σ)B)\sigma\models'\{A\}c\{B\}~\text{iff}~(\sigma\models'A\implies C[x](\sigma)\models'B)

如果AA满足,cc能停止,则停止后BB一定满足


Previous Post
I-LLM: Efficient Integer-Only Inference for Fully-Quantized Low-Bit Large Language Models
Next Post
The Minimum Equivalent DNF Problem and Shortest Implicants