Graph Types � Which Allow Common Shap Es

Graph Types � Which Allow Common Shap Es

Graph Typ es y z Nils Klarlund Michael I Schwartzbach fklarlundmisgdaimiaaudk Aarhus University Department of Computer Science Ny Munkegade DK Arhus Denmark Abstract Recursive data structures are abstractions of simple records and p ointers They imp ose a shape invariant which is veried at compile time and exploited to automatically generate co de for building copy ing comparing and traversing values without loss of eciency How ever such values are always tree shap ed which is a ma jor obstacle to practical use We prop ose a notion of graph types which allow common shap es such as doublylinked lists or threaded trees to b e expressed concisely and eciently We dene regular languages of routing expressions to sp ecify relative addresses of extra p ointers in a canonical spanning tree An ecient algorithm for computing such addresses is develop ed We employ a secondorder monadic logic to decide wellformedness of graph typ e sp ecications This logic can also b e used for automated reasoning ab out p ointer structures This pap er will also b e presented at POPL references should cite the pro ceedings y The author is supp orted by a fellowship from the Danish Research Counsil z The author is partially supp orted by the Danish Research Council DART Pro ject Intro duction Recursive data types are abstractions of structures built from simple records and pointers The values of a recursive data typ e form a set of p ointer structures that all ob ey a common shape invariant The advantage of this approach is twofold validity of the invariant can b e statically veried at compiletime which contributes to the correctness of programs and the invariant can b e exploited to automatically generate co de for such tasks as copying comparing and traversing values Recursive data typ es originate from the seventies and have b ecome ubiq uitous in mo dern typ ed functional languages such as ML and Miranda but they may also b e employed in Pascallike imp erative languages Their b enets are substantial but they also imp ose limitations in particular the values of recursive data typ es will always b e tree shaped In this pap er we present a natural generalization graph types which allows a large variety of graph shaped values including doublychained cyclic lists leaftoro ot linked trees leaflinked trees and threaded trees The key idea is to allow only graphs with a backbone which is a canonical spanning tree All extra edges must dep end functionally on this backb one The extra edges are sp ecied by a language of regular routing expressions which give relative addresses within the backb one We show that construc tion of such graph valuesalong with all relevant manipulationscan hap p en eciently in linear time We intro duce a decidable monadic logic of graph typ es which allows automatic derivation of some constant time op erations such as concatenation of doublylinked lists There have b een other attempts to describ e graphshap ed values Our prop osal however allows exact de scriptions of a more general class of typ es and it do es so using an intuitive notation that is very close to existing concepts in programming languages This summary is kept in an informal explanatory style Formal deni tions and algorithms are included in the app endix Data Typ es For this presentation a recursive data typ e D is a sp ecial kind of tree grammar The nonterminals are called types There is a distinguished main typ e which in examples is always the one mentioned rst the others are merely auxiliary A production T v a T a T 1 1 n n of D where T and the T s are typ es declares a variant v of typ e T containing i data elds named a a we say that the pro duction declares a type 1 n variant T v For each typ e the p ossible variants must b e mutually distinct thus T v uniquely determines the pro duction Moreover for each typ e variant the data elds must b e mutually distinct The values of a data typ e are essentially the derivation trees of the un derlying contextfree grammar starting with the main typ e They are imple mented as p ointer trees but the programmer will never directly manipulate these p ointers Each no de of such a p ointer tree is an instance of a variant of a typ e A formal denition of the values of a data typ e is given in section A of the app endix As a simple example consider the following data typ e which sp ecies a typ e of simple integer lists L nonemptyhead Int tail L empty We can think of the typ e Int as b eing a data typ e sp ecied as Int j j j We allow implicit variants as a form of syntactic sugar If the sets of data elds are distinct for all variants then the explicit variants are not needed we may think of the variant names as b eing a concatenation of the eld names Thus we may instead write L head Int tail L Programming with Data Typ es When a data typ e has b een sp ecied it gives rise to a numb er of op erations in the programming language First of all there is a language for denoting constant values For the ab ove lists one may write down Lhead tail head tail head tail for the list of typ e L with elements and If x is a variable containing a value of typ e L then xtailtailhead sp ecies the address of a subtree in this case of typ e Int In a functional language this would always denote the corresp onding value in an imp erative language there is the usual distinction b etween l and r values The comparison x y is always dened for two values of typ e L If x is a value of typ e L then the b o olean expression isxv yields true exactly when x is of variant v In an imp erative language the value assignment x y is present p ossibly accompanied by the swap x y which exchanges two subtrees without copying Values of data typ es are traversed by recursive functions or pro cedures Thus explicit p ointers are never used There is no intrinsic loss of eciency in this approach Constants can b e built copied compared and traversed in optimal linear time and addresses are accessed in constant time Thus if one really wants treeshap ed values then only advantages are to b e seen Shortcomings of Data Typ es The main drawback of data typ es is the limited shap es of values that they allow For the ab ove simple lists values always lo ok as follows an empty record is pictured as a ground symb ol However it is a common optimization to want an extra p ointer to gain con stant time access to the last element of the list Thus the values should instead have the following shap e These are not trees and hence cannot b e sp ecied by data typ es Until now there has b een no solution to this problem The only p ossibility has b een to revert to the often p erilous use of explicit p ointers Graph Typ es We intro duce the notion of graph types which form a conceptually simple extension of data typ es They allow graph shap ed values while retaining the eciency and ease of use There are two key insights to our solution while b eing graphs the values all have a backbone which is a canonical spanning tree and the remaining edges are all functionally determined by this backb one Many but not all sets of graphs t this mold we give examples of b oth kinds A graph type extends a data typ e by having routing elds as well as data elds Pro ductions now lo ok like T v a T a T R i i j j Here a is a normal data eld but a is a routing eld It is distinguished by i j having an asso ciated routing expression R A graph typ e has an underlying data typ e which is obtained by removing the routing elds The backb ones of the graph typ e values are simply the values of this data typ e Routing expressions describ e relative addresses within the backb one The complete graph typ e value is obtained by using the routing expressions to evaluate the destinations of the routing elds Routing expressions are regular expressions over a language of directives which describ e navigation within a backb one Directives include move up to the parent from a sp ecic child or a move down to a sp ecic child a and verify a prop erty of the current no de where prop erties include this is the ro ot this is a leaf and this is a sp ecic variant of a sp ecic typ e T or T v A routing expression denes the destination indicated by the corresp onding routing eld if its regular language contains precisely one sequence of successful directives leading to a no de in the tree A graph typ e is wel lformed if every routing expression always denes a unique destination Section A of the app endix gives formal denitions of these concepts To make a convincing case for this new mechanism we need to demon strate the following facts many useful families of structures can b e easily sp ecied values can b e manipulated at runtime similarly to values of data typ es and without loss of eciency and wellformedness of graph typ e sp ecications can b e decided at compile time Examples We now show that many common p ointer structures have simple sp ecica tions as graph typ es The examples are all wellformed which can b e easily seen in each case In pictures of values we use the convention that p ointers from data elds are solid whereas those from routing elds are dashed The ro ot of the underlying spanning tree or backb one is indicated by a solid p ointer with no origin The list with a p ointer to the last element lo oks like H rst L last Lrst tail L head Int tail L A typical value is rst tail tail tail last The routing expression rst tail for the last eld contains the follow ing directives move down along the rst p ointer rst

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us