在“解释语言的语言”中提到,设计一个新的语言,仅仅是使用meta-language给出其描述是不够的,我们还需要去验证一些性质。
考虑一下我们在“解释语言的语言中”给出的求值关系\(r\) ,对于表达式\(( f . ( t . f ) )\), 表达式能够通过求值关系\(\rightarrow_{r}\)得到\((f . (t . f) )\rightarrow_{r}(f . t)\)以及\((f . (t . f) )\rightarrow_{r}(t . f)\),但是我们的关注点在于通过求值关系\(\rightarrow_{r}\)得到的不同表达式的最终结果是否相同。这上面的例子中\((t . f)\)与\((f . t)\)最终都会规约到\(t\),但是单个例子显然没有说服力。这需要证明求值的一致性(Consistency of Evaluation)。
回忆一下在上一节提到的不同扩展范围的求值关系:
name | definition | intution |
---|---|---|
\(-\) | the base relation on members of an expression grammar | a single "reduction" step with no context |
\(\rightarrow_{-}\) | the compatible clojure of - with respect to expression grammar | a single step within context |
\(\twoheadrightarrow_{-}\) | the reflexive-transitive clojure of \(\rightarrow_{-}\) | multiple evaluation steps (zero or more) |
\(=_{-}\) | the symmetric-transitive clojure of \(\twoheadrightarrow_{-}\) | equate expression that produce the same result |
\(eval_{-}\) | \(=_{-}\)restricted to a range of results | complete evaluation |
接下来在这里先给出3个定理
Theorem 1: \(eval_r(B_0)=R_1\) and \(eval_r(B_1)=R_2\), then \(R_1=R_2\)
Theorem 2(Church-Rosser Theorem ): If \(M=_{r}N\) , there exists expression \(L\) such that \(M \twoheadrightarrow_{r} L\) and \(N \twoheadrightarrow_{r} L\)
Theorem 3(Diamond property of \(\twoheadrightarrow_{r}\)): If \(L \twoheadrightarrow_{r} M\) and \(L \twoheadrightarrow_{r} N\), then there exists expression \(L'\) such that \(M \twoheadrightarrow_{r} L'\) and \(N \twoheadrightarrow_{r} L'\)
定理1解释图:
定理1描述的是一个表达式通过求值函数\(eval_r(B_0)\)得到的值的一致性。
定理2解释图:
定理2描述的是两个通过求值关系\(\twoheadrightarrow_{r}\)得到的表达式最终规约结果的一致性。
证明过程:
Base case: if \(M =_{r} N\), there exists an expression \(L\) such that \(M =_{r} L\) and \(L =_{r} N\)( transitive relation)
- Case \(M \twoheadrightarrow_{r} N\)
Let \(L=N\) , the claim holds
Inductive cases:
- Case \(M =_{r} N\) because \(N =_{r} M\)
By induction, \(L\) exists for \(N =_{r} M\), that is the \(L\) we want
- Case \(M \twoheadrightarrow_{r} N\) because \(M \twoheadrightarrow_{r} L_0\) and \(L_0 \twoheadrightarrow_{r} N\)
By induction, there exists an \(L_1\) such that \(M \twoheadrightarrow_{r} L_1\) and \(L_0 \twoheadrightarrow_{r} L_1\). Also by the induction, there exists an \(L_2\) such that \(N \twoheadrightarrow_{r} L_2\) and \(L_0 \twoheadrightarrow_{r} L_2\)
then by theorem 1 and induction, there exists an \(L_3\) such that \(L_1 \twoheadrightarrow_{r} L_3\) and \(L_2 \twoheadrightarrow_{r} L_3\), the claim holds.
定理2证明图:
定理3解释图:
定理3描述的是一个表达式的不同规约路径,最终规约到一个结果的过程,由于形状是一个四边形(可能中间也会有多个中间表达式\(L_n\)),因此称其为关系\(\twoheadrightarrow_{r}\)的Diamond property。
基于上述的定理,可以得到基于关系\(\twoheadrightarrow_{r}\)拓展而来的求值关系求值一致性保证。但是在具体的语言描述中,还是需要对我们的语言描述进行验证,但这总是一个好的开始。
参考文献
[1] Matthias Fellesisen, Matthew Flatt.Programming Languages And Lambda Calculi[M]. Utah:, 2006. 21-24.