没有合适的资源?快使用搜索试试~ 我知道了~
Foundations of Programming Languages Lecture Notes (CMU 15-312)
3星 · 超过75%的资源 需积分: 9 12 下载量 96 浏览量
2017-03-08
16:02:29
上传
评论
收藏 2.71MB PDF 举报
温馨提示
试读
210页
Foundations of Programming Languages Lecture Notes (CMU 15-312)
资源推荐
资源详情
资源评论
Lecture Notes on Inductive Definitions
15-312: Foundations of Programming Languages
Frank Pfenning
Lecture 2
September 2, 2004
These supplementary notes review the notion of an inductive definition
and give some examples of rule induction. References to Robert Harper’s
draft book on Programming Languages: Theory and Practice are given in square
brackets, by chapter or section.
Given our general goal to define and reason about programming lan-
guages, we will have to deal with a variety of description tasks. The first is
to describe the grammar of a language. The second is to describe its static
semantics, usually via some typing rules. The third is to describe its dy-
namic semantics, often via transitions of an abstract machine. On the sur-
face, these appear like very different formalisms (grammars, typing rules,
abstract machines) but it turns out that they can all be viewed as special
cases of inductive definitions [Ch. 1]. Following standard practice, inductive
definitions will be presented via judgments and inference rules providing
evidence for judgments.
The first observation is that context-free grammars can be rewritten in
the form of inference rules [Ch. 3.2]. The basic judgment has the form
s A
where s is a string and A is a non-terminal. This should be read as the
judgment that s is a string of syntactic category A.
As a simple example we consider the language of properly matched
parentheses over the alphabet Σ = {(, )}. This language can be defined by
the grammar
M : : = ε | (M ) | M M
with the only non-terminal M. Recall that ε stands for the empty string.
Rewritten as inference rules we have:
LECTURE NOTES SEPTEMBER 2, 2004
L2.2 Inductive Definitions
ε M
m
1
s M
(s) M
m
2
s
1
M s
2
M
s
1
s
2
M
m
3
As an example, consider a deduction of ( ) ( ) M .
ε M
m
1
( ) M
m
2
ε M
m
1
( ) M
m
2
( ) ( ) M
m
3
Our interpretation of these inference rules as an inductive definition of
the judgment s M for a string s means:
s M holds if and only if there is a deduction of s M using rules (m
1
),
(m
2
), and (m
3
).
Based on this interpretation we can prove properties of strings in the syn-
tactic category M by rule induction. To apply rule induction we have to
show that the property in question is preserved by every inference rule
of the judgment s M . That is, we have to show that for each rule, if all
premises satisfy the property then the conclusion also satisfies the prop-
erty. Here is a very simple example.
Theorem 1 (Counting Parentheses)
If s M then s has the same number of left and right parentheses.
Proof: By rule induction. We consider each case in turn.
(Rule m
1
) Then s = ε.
s has 0 left and 0 right parens Since s = ε
(Rule m
2
) Then s = (s
0
).
s
0
M Subderivation
s
0
has n
0
left and right parens for some n
0
By i.h.
s has n
0
+ 1 left and right parens Since s = (s
0
)
LECTURE NOTES SEPTEMBER 2, 2004
Inductive Definitions L2.3
(Rule m
3
) Then s = s
1
s
2
.
s
1
M Subderivation
s
2
M Subderivation
s
1
has n
1
left and right parens for some n
1
By i.h.
s
2
has n
2
left and right parens for some n
2
By i.h.
s has n
1
+ n
2
left and right parens Since s = s
1
s
2
The grammar we gave, unfortunately, is ambiguous [Ch. 3.3]. For ex-
ample, there are infinitely many derivations that ε M, because
ε = ε ε = ε ε ε = · · ·
In the particular example of this grammar we would be able to avoid rewrit-
ing it if we can show that the abstract syntax tree [Ch. 4] we construct will
be the same, independently of the derivation of a particular judgment.
An alternative is to rewrite the grammar so that it defines the same
language of strings, but the derivation of any particular string is uniquely
determined. The following grammar accomplishes this:
1
L : : = ε | (L) L
One can think of L as a (possibly empty) list of parenthesized expres-
sions, terminated by the empty string. This readily translates into an in-
ductive definition via inference rules.
ε L
l
1
s
1
L s
2
L
(s
1
) s
2
L
l
2
Now there are two important questions to ask: (1) is the new grammar
really equivalent to the old one in the sense that it generates the same set of
1
An alternative solution, suggested in lecture in 2003, exemplifies the idea of a simul-
taneous inductive definition. It uses two non-terminals L and N , where the category L
corresponds to M , while N is an auxiliary non-terminal.
L : : = ε | N L
N : : = (L)
Note that the new definition arises from substituting out the definition of N in the alterna-
tion for L.
LECTURE NOTES SEPTEMBER 2, 2004
L2.4 Inductive Definitions
strings, and (2) is the new grammar really unambiguous. The latter is left
as a (non-trivial!) exercise; the first one we discuss here.
At a high level we want to show that for any string s, s M iff s L. We
break this down into two lemmas. This is because “if-and-only-if” state-
ments can rarely be proven by a single induction, but require different con-
siderations for the two directions.
We first consider the direction where we assume s M and try to show
s L. When writing out the cases we notice we need an additional lemma.
As is often the case, the presentation of the proof is therefore different from
its order of discovery. To read this proof in a more natural order, skip ahead
to Lemma 3 and pay particular attention to the last step in the case of rule
(m
3
). That step motivates the following lemma.
Lemma 2 (Concatenation)
If s
1
L and s
2
L then s
1
s
2
L.
Proof: By induction on the derivation of s
1
L. Note that induction on the
derivation on s
2
L will not work in this case!
(Rule l
1
) Then s
1
= ε.
s
2
L Assumption
s
1
s
2
L Since s
1
s
2
= ε s
2
= s
2
(Rule l
2
) Then s
1
= (s
11
) s
12
.
s
11
L Subderivation
s
12
L Subderivation
s
2
L Assumption
s
12
s
2
L By i.h.
(s
11
) s
12
s
2
L By rule (l
2
)
Now we are ready to prove the left-to-right implication.
Lemma 3
If s M then s L.
Proof: By induction on the derivation of s M.
LECTURE NOTES SEPTEMBER 2, 2004
Inductive Definitions L2.5
(Rule m
1
) Then s = ε.
s L By rule (l
1
) since s = ε
(Rule m
2
) Then s = (s
0
).
s
0
M Subderivation
s
0
L By i.h.
ε L By rule (l
1
)
(s
0
) L By rule (l
2
) and (s
0
) ε = (s
0
)
(Rule m
3
) Then s = s
1
s
2
.
s
1
M Subderivation
s
2
M Subderivation
s
1
L By i.h.
s
2
L By i.h.
s
1
s
2
L By concatenation (Lemma 2)
The right-to-left direction presents fewer problems.
Lemma 4
If s L then s M.
Proof: By rule induction on the derivation of s L. There are two cases to
consider.
(Rule l
1
) Then s = ε.
s M By rule (m
1
), since s = ε
(Rule l
2
) Then s = (s
1
) s
2
.
s
1
L Subderivation
s
2
L Subderivation
s
1
M By i.h.
(s
1
) M By rule (m
2
)
s
2
M By i.h.
(s
1
) s
2
M By rule (m
3
)
LECTURE NOTES SEPTEMBER 2, 2004
剩余209页未读,继续阅读
资源评论
- blue123dfg2017-10-12还不错的资源
绝不原创的飞龙
- 粉丝: 1w+
- 资源: 1091
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功