第7章 Abstract Types and Functors
- 章节名：第7章 Abstract Types and Functors
- 2012-09-21 17:39:07
Everyone accepts that large programs should be organized as hierarchical modules.Standard ML's structures and signatures meet this requirement. Structures let us package up declarations of related types, values and functions. Signatures let us specify what components a structure must contain. A modular structure makes a program easier to understand. Better still, the modules ought to serve as interchangeable parts: replacing one module by an improved version should not require changing the rest of the program. Standard ML's abstract types and functors can help us meet this objective too. A module may reveal its internal details. When the module is replaced, other parts of the program that depend upon such details will fail. ML provides several ways of declaring an abstract type and related operations, while hiding the type's representation. If structure B depends upon structure A, and we wish to replace A by another structure A` , we could edit the program text and recompile the program . That is satisfactory if A is obsolete and can be discarded. But what if A and A` are both useful,such as structures for floating point arithmetic in different precisions? ML lets us declare B to take a structure as a parameter. We can then invoke B(A) and B(A`) , possibly at the same time. A parametric structure, such as B, is called a functor.Functors let us treat A and A` as interchangeable parts. The language of modules is distinct from the core language of types and expressions.It is concerned with program organization, not with computation itself. Modules may contain types and expressions,but not the other way around. The main module constructs have counterparts in the core language: structure ~ value signature ~ type functor ~ function This analogy is a starting point to understanding, but it fails to convey the full potential of Ml modules.
mr x对本书的所有笔记 · · · · · ·
说明 · · · · · ·