Respectful Type Converters
Total Page:16
File Type:pdf, Size:1020Kb
Resp ectful Type Converters Jeannette M Wing John Ockerbloom May CMUCS School of Computer Science Carnegie Mellon University Pittsburgh PA Submitted to IEEE Transactions on Software Engineering Abstract In converting an ob ject of one type to another we exp ect some of the original ob jects b ehavior to remain the same and some to change How can we state the relationship b etween the original ob ject and converted ob ject to characterize what information is preserved and what is lost after the conversion takes place We answer this question by introducing the new relation respects and say that a type converter function C A B respects a type T We formally dene respects in terms of the Liskov and Wing b ehavioral notion of subtyping types A and B are subtypes of T We explain in detail the applicability of resp ectful type converters in the context of the Typed Ob ject Mo del TOM Conversion Service built at Carnegie Mellon and used on a daily basis throughout the world We also briey discuss how our respects relation addresses a similar question in two other contexts type evolution and interoperability This research is sp onsored in part by the Defense Advanced Research Pro jects Agency and the Wright Lab oratory Aeronautical Systems Center Air Force Materiel Command USAF F and Rome Lab oratory Air Force Materiel Command USAF under agreement number F and in part by the National Science Foundation under Grant No CCR The US Government is authorized to repro duce and distribute reprints for Governmental purp oses notwithstanding any copyright annotation thereon The views and conclusions contained herein are those of the authors and should not b e interpreted as necessarily representing the ocial p olicies or endorsements either expressed or implied of the Defense Advanced Research Pro jects Agency Rome Lab oratory or the US Government Keywords type converters ob ject rep ository distributed ob jects subtype ob jectoriented design abstraction function sp ecications semantics Larch type evolution interoperability Motivation The tremendous growth of the Internet and the World Wide Web gives millions of p eople access to vast quantities of data While users may b e able to retrieve data easily they may not b e able to interpret or display retrieved data intelligibly For example when retrieving a Microsoft Word do cument without a Microsoft Word program the user will b e unable to read edit display or print it In general the type of the retrieved data may b e unknown to the retrieving site Users and programs cop e with this problem by converting data from one type to another eg from the unknown type to one known by the lo cal user or program Thus to view the Word do cument we could convert it to ASCI I text or HTML and then view it through our favorite text editor or browser A picture in an unfamiliar Windows bitmap type could b e converted into a more familiar GIF image type A mail message with incomprehensible MIME attachments could b e converted from an unreadable MIMEenco ded type to a text image or audio type that the recipient could examine directly In general we apply type converters on data ob jects transforming an ob ject of one type to an ob ject of a dierent type What Information Do Type Converters Preserve In converting ob jects of one type to another we exp ect there to b e some relationship b etween the original ob ject and the converted one In what way are they similar The reason to apply a converter in the rst place is that we exp ect some things ab out the original ob ject to change in a way that we are willing to forgo a but we also exp ect some things to stay the same For example supp ose we convert a L T X le to an HTML E a le We may care to ensure that the raw textual contents of the original L T X do cument are preserved but E not the formatting commands since they do not contribute to the meaning of the do cument itself here the preserved information is the underlying semantics of the text contained in the do cument Alternatively if a we convert a L T X le to a tableofcontents do cument we may care to ensure that the number order and E titles of chapters and sections in the original do cument are preserved but not the bulk of the text here the preserved information is primarily the do cuments structure The question we address in this pap er is How can we characterize what information is preserved by a type converter Our answer is given in terms of the b ehavior of some type T Informally we say a converter C A B respects type T if the original ob ject of type A and the converted ob ject of type B have the same b ehavior when b oth ob jects are viewed as a type T ob ject That is from T s viewp oint the A and B ob jects lo ok the same If the converter resp ects a type then it preserves that types observable b ehavior This pap er formalizes this novel notion of respectful type converters Our particular formalization of respects exploits the subtype relationship that holds among types of ob jects The Liskov and Wing notion of b ehavioral subtyping conveniently characterizes semantic dierences b etween types If S is a subtype of T users of T ob jects cannot p erceive when ob jects of type S are substi tuted for T ob jects Intuitively if C resp ects type T an ancestor of b oth A and B in the subtype hierarchy then T captures the b ehavioral information preserved by C This pap er sp ells out how to determine whether a given ancestor T in a type hierarchy is resp ected by a converter C A B Figure In general A and B need not b e subtypes of each other in practice they are often siblings or cousins in a given type hierarchy Also in general T is not necessarily the least common ancestor of A and B Here is an example of why T is not just any ancestor of A and B Supp ose that the PNG image and map type Figure that sp ecies the colors of the pixels in GIF image types are b oth subtypes of a pixel a rectangular region GIF images are limited to distinct colors PNG images are not Assuming the pixel map type also do es not have a color limit then a general converter from PNG images to GIF images would not resp ect the pixel map type it is p ossible to use pixel maps interface to distinguish a PNG image with thousands of colors from its conversion to a GIF image with at most colors On the other hand map is in turn a subtype of a more generic bitmap type that simply records whether a graphical supp ose pixel map are considered set if element is set or clear see Figure Supp ose further that elements in a pixel they are not black and clear if they are black As long as the PNG to GIF converter do es not change any nonblack color to black or black to nonblack and otherwise preserves the pixel layout there is no way for the bitmap interface to distinguish the PNG image from the GIF image that results from the conversion Here then the PNG to GIF converter resp ects the bitmap type T ... ... AB C: A B Figure Do es Converter C Resp ect Type T bitmap (set or clear) pixel_map (1000's of colors) PNG GIF (1000's of colors) (256 colors) C: PNG GIF Figure The PNG to GIF converter resp ects bitmap but not pixel map Although we explore the idea of resp ectful type converters primarily in the context of le and do cument converters type converters show up in other contexts Most programming languages have builtin type converters dened on primitive types for example asciiinteger charstring and stringarraychar The real world is continually faced with painful costly yet seemingly simple conversions the US Postal System converted vedigit zip co des to vefourdigit zip co des Bell Atlantic recently added a new area co de necessitating the conversion of a large p ortion of phone numbers in Western Pennsylvania from the area co de to and payroll pro cessing centers routinely need to convert large databases of employee records whenever extra elds are added to the relevant database schema Roadmap to Rest of Paper In this pap er we formally characterize the notion of when a converter respects a type We rst review in Section how we sp ecify types and determine when one type is a subtype of another b orrowing directly from Liskov and Wings b ehavioral notion of subtyping In Section we exploit this notion of subtyping to dene the respects relation b etween a converter and a type Armed with these formal denitions in Section we show how they are used in practice in the application of the Typed Ob ject Mo del TOM Conversion Service which we built at Carnegie Mellon and use on a daily basis In Section we discuss a further extension of respects that enables us to relate the implementation of types to their sp ecications sp ecically the abstraction function used to show the correctness of the implementation of abstract types ts neatly into the way we dene respects In Section we briey discuss two other applications of our respects relation type evolution and reusability in the context of interoperability Section gives a status rep ort of the TOM Conversion Service and puts it in a broader setting given TOMs environment We close with a discussion of future work Behavioral Subtyping The programming language community has come up with many denitions of the subtype relation The goal is to determine when this assignment x T E is legal in the presence of subtyping Once the assignment has o ccurred x will b e used according to its apparent type T with the exp ectation that if the program p erforms correctly when the actual type of xs ob ject is T it will also work correctly if the actual type of the ob ject denoted by x is a subtype