Exercise 2.51: Define the below operation
for painters. Below takes two painters as arguments. The resulting
painter, given a frame, draws with the first painter in the bottom of the frame
and with the second painter in the top. Define below in two different
ways—first by writing a procedure that is analogous to the beside
procedure given above, and again in terms of beside and suitable
rotation operations (from Exercise 2.50).
Levels of language for robust design
The picture language exercises some of the critical ideas we’ve introduced
about abstraction with procedures and data. The fundamental data abstractions,
painters, are implemented using procedural representations, which enables the
language to handle different basic drawing capabilities in a uniform way. The
means of combination satisfy the closure property, which permits us to easily
build up complex designs. Finally, all the tools for abstracting procedures
are available to us for abstracting means of combination for painters.
We have also obtained a glimpse of another crucial idea about languages and
program design. This is the approach of
stratified design, the
notion that a complex system should be structured as a sequence of levels that
are described using a sequence of languages. Each level is constructed by
combining parts that are regarded as primitive at that level, and the parts
constructed at each level are used as primitives at the next level. The
language used at each level of a stratified design has primitives, means of
combination, and means of abstraction appropriate to that level of detail.
Stratified design pervades the engineering of complex systems. For example, in
computer engineering, resistors and transistors are combined (and described
using a language of analog circuits) to produce parts such as and-gates and
or-gates, which form the primitives of a language for digital-circuit
design. These parts
are combined to build processors, bus structures, and memory systems, which are
in turn combined to form computers, using languages appropriate to computer
architecture. Computers are combined to form distributed systems, using
languages appropriate for describing network interconnections, and so on.
As a tiny example of stratification, our picture language uses primitive
elements (primitive painters) that are created using a language that specifies
points and lines to provide the lists of line segments for
segments->painter, or the shading details for a painter like
rogers. The bulk of our description of the picture language focused on
combining these primitives, using geometric combiners such as beside and
below. We also worked at a higher level, regarding beside and
below as primitives to be manipulated in a language whose operations,
such as square-of-four, capture common patterns of combining geometric
combiners.
Stratified design helps make programs
robust, that is, it makes it
likely that small changes in a specification will require correspondingly small
changes in the program. For instance, suppose we wanted to change the image
based on wave shown in Figure 2.9. We could work at the lowest
level to change the detailed appearance of the wave element; we could
work at the middle level to change the way corner-split replicates the
wave; we could work at the highest level to change how
square-limit arranges the four copies of the corner. In general, each
level of a stratified design provides a different vocabulary for expressing the
characteristics of the system, and a different kind of ability to change it.
练习 2.51:为画家定义 below 操作。below 接受两个画家作为参数。给定一个框架,所得画家用第一个画家在框架的下半部分绘图,用第二个画家在框架的上半部分绘图。用两种不同的方式定义 below——先写一个类似于前面所给 beside 过程的过程,再借助 beside 和适当的旋转操作(来自练习 2.50)加以定义。
健壮设计的语言层次
图形语言练习了我们引入的关于过程和数据抽象的若干核心思想。基本数据抽象——画家——是用过程式表示实现的,这使该语言能够以统一的方式处理各种基本绘图能力。组合的方法满足闭包性质,使我们能够轻松地构建复杂图案。最终,所有用于抽象过程的工具也都可供我们用于抽象画家的组合方法。
我们还瞥见了关于语言与程序设计的另一个关键思想,即分层设计 (stratified design) 的方法——复杂系统应当被构造为一系列层次,每一层次用一系列语言加以描述。每一层次将在该层次被视为基本元素的部件组合起来构建,而在每一层次构建出的部件又作为下一层次的基本元素。分层设计中每一层次所使用的语言,都拥有与该层次细节相适应的基本元素、组合的方法和抽象的方法。
分层设计贯穿于复杂系统的工程实践之中。例如,在计算机工程中,电阻和晶体管被组合(并用模拟电路语言加以描述),构成与门和或门等部件,它们是数字电路设计语言的基本元素。这些部件再被组合,构建出处理器、总线结构和存储系统,进而用适合计算机体系结构的语言将它们组合成计算机。计算机又被组合成分布式系统,使用适合描述网络互连的语言,如此类推。
作为分层设计的一个微小示例,我们的图形语言使用基本元素(基本画家),这些基本画家是用一种语言创建的,该语言指定点和线,以向 segments->painter 提供线段表,或为 rogers 这样的画家提供阴影细节。我们对图形语言的大部分描述集中于用几何组合子(如 beside 和 below)来组合这些基本元素。我们也在更高一层工作过,将 beside 和 below 视为基本元素,在一种以 square-of-four 等操作捕获几何组合子常见组合模式的语言中加以操纵。
分层设计有助于使程序更加健壮 (robust),也就是说,规格说明的微小改动很可能只需要程序作出相应的微小改动。例如,假设我们想改变图 2.9 中基于 wave 的图像。我们可以在最低层工作,改变 wave 元素的详细外观;可以在中间层工作,改变 corner-split 复制 wave 的方式;也可以在最高层工作,改变 square-limit 排列四份副本的方式。一般而言,分层设计的每一层次都提供了一套不同的词汇来表达系统的特性,并提供了一种不同的变更能力。