
Generic Programming y David R Musser Alexander A Stepanov Rensselaer Polytechnic Institute HewlettPackard Lab oratories Computer Science Department Software Technology Lab oratory Amos Eaton Hall Post Oce Box Troy New York Palo Alto California Abstract Generic programming centers around the idea of abstracting from concrete ef cient algorithms to obtain generic algorithms that can b e combined with dierent data representations to pro duce a wide variety of useful software For example a class of generic sorting algorithms can b e dened whichwork with nite sequences but which can b e instantiated in dierentways to pro duce algorithms working on arrays or linked lists Four kinds of abstractiondata algorithmic structural and representational are discussed with examples of their use in building an Ada library of software comp onents The main topic discussed is generic algorithms and an approachto their formal sp ecication and verication with illustration in terms of a partitioning algorithm such as is used in the quicksort algorithm It is argued that generically programmed software comp onent libraries oer imp ortantadvantages for achieving software pro ductivity and reliability This pap er was presented at the First International Joint Conference of ISSAC and AAECC Rome Italy July ISSAC stands for International Symp osium on Symb olic and Algebraic Computation and AAECC for Applied Algebra Algebraic Algorithms and Error Correcting Co des It was published in Lecture Notes in Computer Science SpringerVerlag pp y The rst authors work was sp onsored in part through a sub contract from Computational Logic Inc whichwas sp onsored in turn by the Defense Advanced Research Pro jects Agency ARPA order The views and conclusions contained in this do cument are those of the authors and should not be interpreted as representing the ocial p olicies either expressed or implied of the Defense Advanced Research Pro jects AgencytheUSGovernment or Computational Logic Inc i ii CONTENTS Contents Intro duction Classication of Abstractions Data abstractions Algorithmic abstractions Structural abstractions Representational abstractions Algorithmic Abstractions A Generic Partition Algorithm Abstract Algorithm Sp ecication and Verication Basic sp ecication of Partition Obtaining a sequence satisfying Test Correctness when Swap is an assignment op eration Correctness of the b o dy of Partition Conclusion Intro duction By generic programming we mean the denition of algorithms and data structures at an abstract or generic level thereby accomplishing many related programming tasks si multaneously The central notion is that of generic algorithms which are parameterized pro cedural schemata that are completely indep endent of the underlying data representa tion and are derived from concrete ecient algorithms The purp ose of this pap er is to convey the key ideas of generic programming fo cusing mainly on establishing a metho d ological framework that includes appropriate notions of algorithm and data abstractions and what it means to formally sp ecify and verify such abstractions We present the issues relating to generic algorithms mainly in terms of a single ex ample a Partition algorithm such as is used in quicksort but we will also allude to a large collection of examples wehavedevelop ed in Ada as part of an Ada Generic Li brary pro ject The structure of the library is designed to achievea much higher degree of mo dularity than has b een found in previous libraries by completely separating data representations from algorithmic and other data abstraction issues Some of our goals are in common with the parameterized programming approachadvo cated byJ Goguen but the most fundamental dierence is that Goguen mainly addresses meta issuesnamelyhow to manipulate theorieswhile our primary interest is in building useful theories to manipulate The notion of generic algorithms is not new but we are unaware of any similar attempt to structure a software library founded on this idea The Ada library develop ed byG Booch for example makes only very limited use of generic algorithms Boochhas develop ed an interesting taxonomy of data structures and has p opulated it with many dierent abstract data typ es but the implementations of these data typ es are for the most part built directly on Adas own data structure facilities rather than using other data abstractions in the library ie there is very little layering of the implementations Some use of generic algorithms and layering is describ ed for list and tree structure algorithms but almost as an afterthoughtinachapter on utilities In fact most work on development of abstraction facilities for the past decade or more has fo cused on data abstraction Algorithmic abstraction has received little attention even in the more recentwork on ob ject oriented programming Most work on pro cedural abstraction has b een language rather than algorithmoriented attempting to nd elegant and concise linguistic primitives our goal is to nd abstract representations of ecient algorithms As an example of algorithmic abstraction consider the task of cho osing and imple menting a sorting algorithm for linked list data structures The merge sort algorithm CLASSIFICATION OF ABSTRACTIONS can b e used and if prop erly implemented provides one of the most ecient sorting al gorithms for linked lists Ordinarily one might program this algorithm directly in terms of whatever p ointer and record eld access op erations are provided in the programming language Instead however one can abstract away a concrete representation and express the algorithm in terms of the smallest p ossible numb er of generic op erations In this case we essentially need just four op erations Next and Set Next for accessing the next cell in End for detecting the end of a list and Test a binary predicate on the data in a list Is cells For a particular representation of linked lists one then obtains the corresp onding version of a merge sorting algorithm by instantiating the generic access op erations to b e subprograms that access that representation We b elieve it is b etter whenever p ossible to give programming examples in a real language rather than using pseudolanguage as is so frequently done Although wedonot argue that Ada is p erfect for expressing the programming abstractions wehave found most useful it has b een adequate in most cases and it supp orts our goal of eciency through its compile time expansion of generics and provision for directing that subprograms b e compiled inline For numerous examples of the use of generic programming techniques in the Scheme language and a brief discussion of the relative merits of Ada and Scheme for this typ e of programming see Classication of Abstractions We discuss four classes of abstractions that wehave found useful in generic programming as shown in Table which lists a few examples of packages in our Ada Generic library Each of these Ada packages has b een written to provide generic algorithms and generic data structures that fall into the corresp onding abstraction class Data abstractions Data abstractions are data typ es and sets of op erations dened on them the usual deni tion they are abstractions mainly in that they can b e understo o d and formally sp ecied by such techniques as algebraic axioms indep endently of their actual implementation In Ada data abstractions can b e written as packages which dene a new typ e and pro ce dures and functions on that typ e Another degree of abstractness is achieved by using a generic packageinwhichthetyp e of elements b eing stored is a generic formal parameter In our librarywe program only a few such data abstractions directlythose necessary to create some fundamental data representations and dene how they are implemented Algorithmic abstractions Data Abstractions System Allo cated Singly Linked Data typ es with op erations User Allo cated Singly Linked dened on them fInstantiations of representational abstractionsg Algorithmic Abstractions Sequence Algorithms Families of data abstractions Linked List Algorithms with common algorithms Vector Algorithms Structural Abstractions Singly Linked Lists Intersections of Doubly Linked Lists algorithmic abstractions Vectors Representational Abstractions Double Ended Lists Mappings from one structural Stacks abstraction to another Output Restricted Deques Table Classication of Abstractions and Example Ada Packages in terms of Ada typ es suchasarrays records and access typ es Most other data abstrac tions are obtained bycombining existing data abstraction packages with packages from the structural or representational classes dened b elow Algorithmic abstractions These are families of data abstractions that have a set of ecient algorithms in common we refer to the algorithms themselves as generic algorithms For example in our Ada library there is a package of generic algorithms for linkedlists and a more general package of sequence algorithms whose memb ers can b e used on either linkedlist or vector repre sentations of sequences The linkedlist generic algorithms package contains dierent algorithms such as for example generic merge and sort algorithms that are instantiated in various ways to pro duce merge and sort subprograms in structural abstraction packages such as singlylinked lists and doublylinked lists We stress that the algorithms at this level are derived by abstraction from concrete ecient algorithms
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages19 Page
-
File Size-