structural induction

最近在看programming languges theory相关的书,遇到了structural induction这个概念,没什么新东西,只是作为笔记记录一下。在介绍structural induction之前,先介绍一个在自然数上应用数学归纳法的例子。

定义一个集合 S S S

  • 1 ∈ S \in S S
  • 3 × t + 2 ∈ S 3 \times t + 2 \in S 3×t+2S,其中 t ∈ S t \in S tS
  • 其它自然数都不属于 S S S

证明对于任意 t ∈ S t \in S tS t t t是奇数。整个证明过程如下,

  1. 首先证明base case是奇数,1显然是奇数
  2. 证明“奇数”这个属性能够在推导出新值的过程中保持 t t t奇数, 3 × t 3 \times t 3×t是奇数,所以 3 × t + 2 3 \times t + 2 3×t+2显然也、 是奇数。

所以对于对于任意 t ∈ S t \in S tS t t t是奇数。其实上述这个归纳过程可以说是structural induction的特例,其中Help explaining Structural Induction有一个更好的例子来说明这个问题。下面分别介绍三本书中关于structural induction的例子。

Types and Programming Languages

这本书中structural induction是在介绍terms的概念时引入的,定义

DEFINITION: The set of constans appearing in a term t, written Consts(t), is defined as follows:
\qquad
C o n s t s Consts Consts(true) = {true}
C o n s t s Consts Consts(false) = {false}
C o n s t s Consts Consts(0) = {0}
C o n s t s Consts Consts(succ t1) = Consts(t1)
C o n s t s Consts Consts(pred t1) = Consts(t1)
C o n s t s Consts Consts(iszero t1) = Consts(t1)
C o n s t s Consts Consts(if t1 then t2 else t3) = Consts(t1) ∪ \cup Consts(t2) ∪ \cup Consts(t3)

DEFINITION: The size of a term t, writtern size(t), is defined as follows:
s i z e size size(true) = 1
s i z e size size(false) = 1
s i z e size size(0) = 1
s i z e size size(succ t1) = s i z e size size(t1) + 1
s i z e size size(pred t1) = s i z e size size(t1) + 1
s i z e size size(iszero t1) = s i z e size size(t1) + 1
s i z e size size(if t1 then t2 else t3) = s i z e size size(t1) + s i z e size size(t2) + s i z e size size(t3) + 1

请证明The number of distinct constans in a term t is no greater then the size of t (i.e. | C o n s t s Consts Consts(t)| ≤ \le | s i z e size size(t)|),其实这个证明和自然数证明很相似,都是分为两步,证明基础情形 P P P(t)成立,然后证明在构建term的不同case中 P P P(t)能够保持

这本书给出了在 t e r m term term上进行induction的通用形式,
P r o o f Proof Proof: By induction on t.

  • Case: t = true
    …Show P P P(true)
  • Case: t = false
    …Show P P P(false)
  • Cast: t = if t1 then t2 else t3
    …Show P P P(if t1 then t2 else t3), using P P P(t1), P P P(t2), P P P(t3)
    (And similarly for the other syntatic forms.)

我们可以看到自然数的例子是structural induciton的特殊形式,构建的case只有一种 3 × t + 2 ∈ S 3 \times t + 2 \in S 3×t+2S。但是要应用structural induciton的一个限制是整个 S e t Set Set只能由上述的构建规则构建出来。

Practical Foundations For Programming Languages

在这本书中是在介绍Abstract Syntax Trees的时候引入structural induciton概念的,因为AST是天然的树形结构,很适合使用structural induciton。这本书定义了一个只涉及到数学运算的语言,

The trees structure of ast’s provides a very useful principle of reasoning, called structural induction.
\qquad
Suppose that we wish to prove that some property P ( a ) P(a) P(a) holds for ast’s a a a of a given sort. To show this, it is enough to consider all the ways in which a a a can be generated and show that the property holds in each case under the assumption that it holds for its consituent ast’s (if any).

As an example, consider a language of arithmetic expression built from numbers, addition, and multiplication. The abstract syntax of such a language consists of a single sort E x p Exp Exp generated by these operators:

  1. An operator n u m [ n ] num[n] num[n] of sort E x p Exp Exp for each n ∈ N n \in \mathbb{N} nN
  2. Two operators, p l u s plus plus and t i m e s times times, of sort E x p Exp Exp, each with two arguments of sort E x p Exp Exp.

例如对于表达式(2 + (3 × x \times x ×x),可以表示成 p l u s ( n u m [ 2 ] ; t i m e s ( n u m [ 3 ] ; x ) ) plus(num[2]; times(num[3];x)) plus(num[2];times(num[3];x))。要证明关于这棵树上的断言 P ( a s t ) P(ast) P(ast)成立,可以由下面几个步骤组成:

  1. The propery holds for any variable x x x of sort E x p Exp Exp: Prove that P ( x ) P(x) P(x)
  2. The property holds for any number, n u m [ n ] num[n] num[n]: for every n ∈ N n \in \mathbb{N} nN, prove that P ( n u m [ n ] ) P(num[n]) P(num[n])
  3. Assuming that the propery holds for a a a1 and a a a2, prove that it holds for p l u s ( a 1 ; a 2 ) plus(a_1;a_2) plus(a1;a2) and t i m e s ( a 1 ; a 2 ) times(a_1;a_2) times(a1;a2): if P ( a 1 ) a n d P ( a 2 ) P(a_1) and P(a_2) P(a1)andP(a2), then prove P ( p l u s ( a 1 ; a 2 ) ) P(plus(a_1;a_2)) P(plus(a1;a2)) and P ( t i m e s ( a 1 ; a 2 ) ) P(times(a_1;a_2)) P(times(a1;a2))

Because these cases exhaust all possibilities for the formation of a a a, we are assumed that P ( a ) P(a) P(a) holds for any ast a a a of sort E x p Exp Exp,可以看到这里整个归纳过程遵循了和上面相同的套路。

Programming Languages and Lambda Calculi

这本书中关于structural induciton的介绍更加抽象,

structural induction applies when proving a claim about a recursively-defined set. The set is well founded, of course, only if these are atomic elements in the definition; those elements consitute the base cases for induction proofs. The self-referential elements of the definition consitute the induction cases.

证明过程大同小异,和前面的没什么区别。

Programming in Standard ML

《Practical Foundations For Programming Languages》的作者Robert Harper在CMU开设的一门课程《Programming in Standard ML》涉及到了structural induciton

The importance of induction and recursion are not limited to functions defined over the integers. Rather, the familiar concept of mathematical induction over the natural numbers is an instance of the more general notion of structural induction over values of an inductively-defined type, see https://www.cs.cmu.edu/~rwh/introsml/techniques/structur.htm.

对于ML中 l i s t list list,我们可以将其定义成如下形式:
Generalizing a bit, we may think of the type ′ a l i s t 'a list alist as inductively defined by the following clauses:

  1. n i l nil nil is a value of type ′ a   l i s t 'a \ list a list
  2. If h h h is a value of type ′ a 'a a, and t t t is a value of type ′ a 'a a list, then h : : t h::t h::t is a value of type ′ a   l i s t 'a\ list a list.
  3. Nothing else is a value of type ′ a   l i s t 'a\ list a list.
    (The third clause is sometimes called the e x t r e m a l c l a u s e extremal clause extremalclause)

按照上面的定义,我们证明关于 r e v e r s e reverse reverse(也就是SML中的 r e v rev rev方法)方法的一些性质。例如我们可以证明 r e v e r s e   l reverse \ l reverse l最终会evaluate为 l l l的反转, r e v e r s e [ 1 , 2 , 3 , 4 ] reverse [1, 2, 3, 4] reverse[1,2,3,4]的结果为 [ 4 , 3 , 2 , 1 ] [4,3,2,1] [4,3,2,1]

fun reverse nil = nil
  | reverse (h::t) = reverse t @ [h]

整个证明过程分为两步,

第一步 r e v e r s e n i l reverse nil reversenil得到 n i l nil nil,成立;

第二步,证明 r e v e r s e ( h : : t ) reverse(h::t) reverse(h::t)会得到 h : : t h::t h::t的反转,由于 r e v e r s e ( h : : t ) = r e v e r s e t @ [ h ] reverse(h::t) = reverse t @ [h] reverse(h::t)=reverset@[h],也就是 h : : t h::t h::t的反转。

可以看到structural induciton是可以完美应用到涉及 inductively-defined type 的属性证明中,例如程序语言。

Help explaining Structural Induction
Untyped Arithmetic Expressions
Structural induction
Inductive definitions and structural induction - an overview
structural induciton

Structural SVM(结构化支持向量机)是一种用于结构化数据的机器学习算法,在处理序列标注、解析树、图像分割等任务时表现出色。使用Structural SVM进行模型训练可以有效地考虑数据的结构化特征,使得模型更加准确和泛化能力更强。 编写Structural SVM代码需要先导入相应的机器学习库,比如Python中的scikit-learn或者LibSVM。然后,需要定义模型的结构化特征和约束条件,比如在序列标注任务中,可以定义标签序列的转移约束。接着,需要定义模型的损失函数,通常是结构化数据之间的差异度。最后,通过优化算法(比如梯度下降法)来训练模型,使得模型的预测能够最大程度地符合结构化数据的特征和约束条件。 Structural SVM代码的一般框架包括模型的定义、特征提取、训练和预测等步骤。在模型定义中,需要考虑数据的结构化特征和约束条件,比如在解析树任务中需要考虑树的拓扑结构和节点的特征。在特征提取中,需要将数据转换成模型可接受的输入形式,通常是向量表示。在训练中,需要根据训练数据和损失函数来调整模型的参数,使得模型能够更好地拟合数据。在预测中,可以利用训练好的模型对新的数据进行结构化预测。 总之,编写Structural SVM代码需要仔细考虑数据的结构化特征和约束条件,合理选择损失函数和优化算法,以及有效地实现模型的训练和预测过程。通过深入理解和实践,可以更好地掌握Structural SVM算法的原理和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值