正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology allowed the user to cross over the barrier between those structures to get at the underlying implementation. what happens if we decide to make those barriers much stronger, thereby shielding the people who use the abstractions from the underlying representation? Slide 5.4.3 Elements of a data abstraction This then leads us to the notion of a rigorous data abstraction For example, for pairs here is what we expect in such an 1, constructor abstraction.We'll have a constructor for gluing pieces together,2. Acces s / g)b, Af)s amy'trpe and it will have a contract between the types of the inputs and 盘上凡 the type of the data structure constructed We'll have selectors or accessors for getting back out the pieces that are glued together pai?: arraytype→b。aea And most importantly, we will have a contract between the constructor and the selectors, so that whatever we glue together Say nothing about implementatie with the constructor we can get back out using the appropriate 6. Concrete Representation Implementation selector Notice that this contract says nothing about how the abstraction is implemented, only that its behavior is as documented We will have some standard operations on the abstraction, typically predicates for identifying an instance of the data abstraction All of this is separated from the actual implementation by what we call an abstraction barrier Think of this as a wall that separates the use of an abstraction from the implementation of an abstraction. This means that a user of the abstraction can freely write procedures to manipulate the structures, just rely ing on the constructor and selectors, without any knowledge of how the structure is actually made Rational numbers as an example Slide 5.4.4 A rational number is a ratio n/d Now, let's drive this point home with a more extended example, I separation of the details of implementation of an abstraction o and remember that the point we are trying to drive home is th a/b/d =(ac)(bd from the use of that abstraction. We are going to place this solid barrier between the implementation and use of an abstraction and we are going to see why having that barrier makes it much easier for us to create useful sy The example we are going to consider is that of rational numbers and simple arithmetic operations on rationals. I'll 6001 SICP rational number is just a ratio of two integers, a numerator over a denominator Associated with rationals are certain operations, for example we can add two rationals together, or multiply two rationals together, using the rules shown in the slide Notice that we are really cheating here, and in fact I have put the operation on the rationals in red to distinguish that these are operations on rationals, as opposed to operations on integers. In fact, the rules for operations on rationals really decompose into simpler operations on integers. The operations on the right hand side are just operations on normal integers, followed by the creation of a rational (which is what the division sign denotes here) Now, let's see how we can use the ideas of data abstractions to build a system for manipulating rational numbers6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. allowed the user to cross over the barrier between those structures to get at the underlying implementation. What happens if we decide to make those barriers much stronger, thereby shielding the people who use the abstractions from the underlying representation? Slide 5.4.3 This then leads us to the notion of a rigorous data abstraction. For example, for pairs here is what we expect in such an abstraction. We'll have a constructor for gluing pieces together, and it will have a contract between the types of the inputs and the type of the data structure constructed. We'll have selectors or accessors for getting back out the pieces that are glued together. And most importantly, we will have a contract between the constructor and the selectors, so that whatever we glue together with the constructor we can get back out using the appropriate selector. Notice that this contract says nothing about how the abstraction is implemented, only that its behavior is as documented. We will have some standard operations on the abstraction, typically predicates for identifying an instance of the data abstraction. All of this is separated from the actual implementation by what we call an abstraction barrier. Think of this as a wall that separates the use of an abstraction from the implementation of an abstraction. This means that a user of the abstraction can freely write procedures to manipulate the structures, just relying on the constructor and selectors, without any knowledge of how the structure is actually made. Slide 5.4.4 Now, let's drive this point home with a more extended example, and remember that the point we are trying to drive home is the separation of the details of implementation of an abstraction from the use of that abstraction. We are going to place this solid barrier between the implementation and use of an abstraction, and we are going to see why having that barrier makes it much easier for us to create useful systems. The example we are going to consider is that of rational numbers and simple arithmetic operations on rationals. I'll remind you that a rational number is just a ratio of two integers, a numerator over a denominator. Associated with rationals are certain operations, for example we can add two rationals together, or multiply two rationals together, using the rules shown in the slide. Notice that we are really cheating here, and in fact I have put the operation on the rationals in red to distinguish that these are operations on rationals, as opposed to operations on integers. In fact, the rules for operations on rationals really decompose into simpler operations on integers. The operations on the right hand side are just operations on normal integers, followed by the creation of a rational (which is what the division sign denotes here). Now, let's see how we can use the ideas of data abstractions to build a system for manipulating rational numbers
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有