Models for Computer Generated Parody Tony C. Smith Department of Computer Science , University of Calgary , Calgary T2N 1N4 , Canada Email : tcs@waikato.ac.NZ; phone : +64 -LRB- 7 -RRB- 838 ? 4453 ; fax : +64 -LRB- 7 -RRB- 838 ? 4155 Abstract This paper outlines two approaches to the construction of computer systems that generate prose in the style of a given author . The first involves using intuitive notions of stylistic trademarks to construct a grammar that characterizes a particular author?in this case , Ernest Hemingway . The second uses statistical methods for inferring a grammar from samples of an author 's work?in this instance , Thomas Hardy . The relative merits of each approach are discussed , and text generated from the resulting grammars is assessed in terms of its parodic quality . Further to its esoteric interest , a discussion of parody generation as a useful technique for measuring the success of grammatical inferencing systems is included , along with suggestions for its practical application in areas of language modeling and text compression . August , 1993 1 Introduction Computer programs that generate prose and poetry may represent the last bastions of Artificial Intelligence research still seeking to pass the notorious Turing Test . That is , these programs attempt to generate text with surface forms that do not reveal their computational source . The objective of parody generating systems need not be expressed quite so strongly . They seek simply to generate surface forms reminiscent of the style of a particular author . As a consequence , the success of a parody generating system tends to be measured in terms of the level of mimicry achieved in its output . A spectrum of design methodologies has emerged implicitly from research in parody generation . At one end of this spectrum are programs with underlying models designed to offer psychologically plausible accounts of an author 's effective grammar . At the other end are those that employ any means possible to generate surface forms of sufficient parodic quality . This paper outlines two computer programs that generate parody?one from either end of the design spectrum . The first is based on a manually constructed grammar built from an intuitive assessment of Ernest Hemingway 's stylistic trademarks . The second infers a grammar for Thomas Hardy through statistical analyses of a large sample text?in this case Far From The Madding Crowd . Though the two approaches adopt highly disparate techniques to arrive at a final grammar , many of the intermediate objectives and internal components of each system are the same . For example , each must 1 . derive an appropriate vocabulary , 2 . construct a suitable set of phrase production rules , and 3 . develop probability distributions for the selection of vocabulary items and phrase constructs in the generation stage . This paper describes how these components are achieved within the constraints of the two methodologies . For continuity , we describe each system in its entirety before attempting to evaluate their respective merits . The first system relies on basic intuitions about authorship style , whereas the second is based on principled pattern inference . Some background material on grammar induction is included before discussion of the second system . Finally , the quality of parody demonstrated in the resulting generated texts is analyzed , along with some suggestions for improvement . Some practical applications for parody generation in other areas of computer science are touched upon . 2 Hemingway?the prescriptive model Few authors have been parodied to the same extent as Ernest Hemingway . His pervasive run-on sentence , unlikely juxtaposition , and imaginative exposition of the insipid have made him a ready target for mimicry . In 1978 , an advertising man named Paul Keye noted Hemingway 's attachment to Harry 's Bar in Venice and recognized the potential promotional value of running an Imitation Hemingway Competition for the Century City Harry 's Bar & American Grill in California . Of the thousands of submissions received by the competition every year , forty-five were published in the 1989 publication ? The Best of Bad Hemingway ? -LSB- Pli89 -RSB- . Even a modest familiarity with Hemingway 's work allows one to appreciate within these parodies the notable characteristics of his infamous style . Prompted by the entries to this competition , an attempt was made to characterize Hemingway 's style for the purpose of generating parodies by a computer program . Much of the task of ascertaining Hemingwayesque attributes had already been accomplished by the authors of the entries to the Imitation Hemingway Competition . A model of the author 's style was gleaned from a quick survey of characteristic similarities present in these so-called ? bad Hemingway ? parodies . 2.1 The vocabulary Barring the most frequent word sequence ? Harry 's Bar & American Grill ? present in every entry to the Imitation Hemingway contest as part of the submission requirements , most of the commonly adopted words in the parodies seemed to play on Hemingway 's distinctive topics . Specific proper nouns like Kilimanjaro and Madrid were frequently included , perhaps as a reflection of Hemingway 's cosmopolitan vernacular . Other nouns , like fisherman , cafe , matador and aficionado also appeared with notable frequency . The pervasive , often sublime , pathos of Hemingway may explain the repeated use of beggar , martini and death , along with the conspicuous presence of verbs that express argument , deception or conspiracy . The third-person , omniscient narrative used by Hemingway offers a possible explanation for the frequent use of introspective verbs like thought , believed , knew , and assumed . These , and similar terms , were gleaned from the sample parodies to construct a final vocabulary that consists of 96 words , which can be roughly tagged to 31 nouns , 19 verbs , 14 prepositions , 10 adjectives , 8 determiners , 4 adverbs , 4 auxiliary verbs , 3 pronouns , and 3 conjunctions . 2.2 The production rules Much of the vocabulary arose incidentally from the selection of certain key phrases found within the Hemingway parodies . A few explicit expressions , like the snows of Kilimanjaroand in a well lighted room , were lifted from the sample texts and used as the basis for assembling the entire grammar . Subsequent additions to the vocabulary arose from efforts to tie these isolated phrases together into wellformed expressions . Complete noun phrases were constructed to express caricature persona , and general formulations were derived for restrictive and relative clauses to support complex sentence structures . The grammar , shown in its entirety in Figure 1 , was thus constructed piecemeal from instantiated Generalized Phrase Structures -LSB- GKPS85 , Jac84 -RSB- . That is , prepositional phrases are expressed in fixed surface forms , as are substructures of noun and verb phrase constructs . However , the form and content of every generatable sentence is determined by random instantiation of a general propositionalform . Each sentence begins with a subject phrase -LRB- SubP -RRB- under an optional restrictive clause -LRB- ResC -RRB- . The subject is followed by a past participle auxiliary connective and verb phrase -LRB- AuxC -RRB- , or by a past tense verb and relative clause -LRB- RelC -RRB- , or by some compound phrase combination of AuxC and RelC -LRB- CompP -RRB- . A ResC is comprised of the word who and either RelC or a short form of AuxC -LRB- SAuxC -RRB- . The number preceding each token in the production rules indicates the probability with which that construct is to be selected for generation purposes . The symbol + indicates string concatenation while | | indicates disjunction . Braces are used to clarify precedence relations . 2.3 The parody The grammar uses a pseudo-random number generator to produce a piece of text between 4 and 13 sentences in length . The limitations placed on text length were made arbitrarily and do not reflect a limitation of the grammar . Random numbers are further used to select phrase substructures in accordance with their associated probability distribution . These probabilities were arrived at primarily through trial and error with the objective of giving a sufficient illusion of free form composition in the resulting text . Figure 2 shows a sample text generated from the grammar in Figure 1 . Some capitalization and minor formatting has been added for readability . Without any semantic component to guide the generation process the output generally lacks any sort of consistent focus of attention . Even so , the wellformed expressions and limited selection for subject and object allow the reader to endow the text with a marginal amount of coherence . 3 Hardy?the descriptive model O'Donnell -LSB- O'D70 -RSB- and others -LSB- Bri63 , Ell62 -RSB- have often used statistical methods in efforts to identify authors of unknown texts , or to challenge traditional authorship claims . Vocabulary size , sentence length , distribution of sentence complexity , and other ? stylistic variables ? are adopted as a metric to test , say , whether the works attributed to Thackeray and Dickens were penned by the same author or , similarly , whether Shakespeare and Marlowe are one and the same . These techniques can be adapted to construct a probability based grammar for a particular author . Vocabulary and phrase structures are gathered from a statistical analysis on a large number of sample expressions . This ? authentic ? grammar and associated probability distribution can , at least in principle , be used to generate parody text . This section describes an attempt at such a system for Thomas Hardy using Far From The Madding Crowd as the source of sample expressions . A small amount of background material on grammar induction is useful before the details of the approach are presented . Sent - > 1 SubP + -LCB- 1/3 ResC | | 2/3 Null -RCB- + -LCB- 1/5 AuxC | | 1/5 RelC | | 3/5 CompP -RCB- SubP - > -LCB- 2/7 the old man | | 1/7 the fisherman | | 1/7 only he | | 2/7 he | | 1/7 no one but he -RCB- ResC - > 1 who + -LCB- 1/2 SAuxC | | 1/2 RelC -RCB- CompP - > -LCB- 1/3 AuxC + and + RelC -RCB- | | -LCB- 1/3 RelC + and + AuxC -RCB- | | -LCB- 1/3 RelC + and + RelC -RCB- AuxC - > 1 SAuxC + -LCB- 1/2 ExtP | | 1/2 Null -RCB- SAuxC - > -LCB- 2/5 had | | 1/5 should have | | 2/5 had not -RCB- + -LCB- 1/10 cheated | | 1/10 sat beside | | 1/10 waited for | | 1/10 seen | | 1/10 argued with | | 1/10 tried to fool | | 1/10 ignored | | 1/10 mentioned something about | | 1/10 joined up with | | 1/10 told him about -RCB- + -LCB- 1/7 the racetrack aficionado | | 1/7 the matador 's friend | | 1/7 his martini | | 1/7 the old beggar from madrid | | 1/7 death | | 1/7 the waiter | | 1/7 the american girl -RCB- RelC - > -LCB- 1/6 thought | | 1/6 knew | | 1/6 was certain | | 1/6 felt | | 1/6 believed | | 1/6 assumed -RCB- + 1 that + -LCB- 1/8 the man with the patch over one eye | | 1/8 the parrot | | 1/8 most of the other fishermen | | 1/8 the locals | | 1/8 the small dog with three legs | | 1/8 everyone | | 1/8 the bullfighter | | 1/8 the woman -RCB- ExtP - > -LCB- 1/12 without letting on | | 1/12 in a well lighted room | | 1/12 at the corner table | | 1/12 for many years | | 1/12 for nothing | | 1/12 with a certain understanding | | 1/12 again | | 1/12 in the cafe | | 1/12 on kilimanjaro | | 1/12 while fast asleep | | 1/12 by the sea | | 1/12 he had heard about -RCB- Figure 1 : The prescriptive Hemingway grammar . No one but he who believed that everyone should have joined up with the american girl on the snows of Kilimanjaro believed that most of the other fishermen had argued with the waiter and should have mentioned something about the matador 's friend . The old man was certain that the parrot should not have joined up with the waiter in the cafe and felt that most of the other fishermen should not have cheated the racetrack aficionado in a well lighted room . No one but he felt that the woman had not argued with death by the sea and believed that the man with the patch over one eye had not tried to fool his martini . He was certain that everyone had mentioned something about his martini while still asleep . The old man who had seen the american girl knew that the locals should not have told him about the matador 's friend while still asleep and had not joined up with the racetrack aficionado at the corner table . He thought that most of the other fishermen should have sat beside his martini and felt that the bullfighter should not have waited for the racetrack aficionado . No one but he believed that the small dog with three legs had not sat beside the american girl with a certain understanding and should have seen the american girl . Only he who was certain that the man with the patch over one eye should have seen the old beggar from Madrid in a well lighted room thought that the bullfighter should have mentioned something about his martini . The fisherman should have told him about the waiter and thought that the man with the patch over one eye had not joined up with the matador 's friend at the corner table . The end . Figure 2 : Parody Hemingway produced from grammar in Figure 1 . 3.1 Grammatical inference The construction of a grammar as a generalization of regularities present in sample expressions is known as grammatical inference , and has been an important ongoing area of research in computer science -LSB- BP87 , CL82 , PCF77 , PC78 -RSB- . Though natural languages allow many levels of abstraction -LRB- e.g. syntax , morphology , semantics , phonetics , etc. -RRB- , grammatical inference by automata focuses on the discovery of an acceptable description for the syntax of a language based on a finite set of sample strings constructed from a finite vocabulary -LSB- Fel70 , Ang80 -RSB- . Despite their inherent limitations , it is generally accepted that context-free grammars are adequate enough models for generalizations or hypotheses about the syntax of natural languages -LSB- Mac82 -RSB- . Syntactic wellformedness is -LRB- minimally -RRB- the property of a linear arrangement of lexical categories -LRB- like nouns and verbs -RRB- instantiated in conformance with established linguistic features -LRB- like tense and number agreement -RRB- such that the expression is deemed grammatical by speakers of the language in question -LSB- OD87 -RSB- . More plainly , given the sentence ? The cowboy saddled his horse ? , the words cowboy and horse may exchange places without affecting the wellformedness of the sentence . This is true because the words cowboy and horse are of the same grammatical category -LRB- i.e. they are both nouns -RRB- and the underly - ing structure of the sentence is expressed -LRB- at least -RRB- in terms of such categories . From this we conclude that it is inherently more useful -LRB- in English -RRB- to study occurrences of the sequence ? determiner-adjectivenoun ? than it is to study occurrences of the sequence ? the-spotted-dog ? . We refine our notion of the goal of the induction process to be the discovery of an acceptable contextfree grammar based on a finite set of sample strings constructed from a finite vocabulary of lexical categories . 3.2 The vocabulary Cultivating a vocabulary and associated lexical frequencies from any machine readable text is an essentially trivial task . However , pattern analysis on plain text is generally unfruitful andgrammars derived from such tend to degenerate into an unprincipled description of random word sequences . More useful patterns can be brought to the surface by assigning each word of the sample text to an appropriate grammatical category -LRB- i.e. tagging -RRB- and thereafter forming generalizations about sequences of category symbols . Many techniques exist for tagging lexical items -LSB- Kup89 , Mer91 , GR71 -RSB- . The approach adopted here is the closed-class vocabulary method described by Smith and Witten -LSB- SW93 -RSB- , which infers lexical categories for all novel words as proximity relations to I sha n't mind the crimson from the first candle . The direction were a latter and can now speak to the correct of Smallbury ballet superiority if she is never a hated tea . Here she resolved to grave wonder Everdene . His man son upon saintly sky on had straightway what she wanted as any habit . It is in some twenty fancy why you have which were plated in this Coggan . That mistress against making an bed . I added towards a nation with husband as the flaming Cain passed and radiant the epic content . Done and he believe them she being the field . It believe what their attitude were bringing . Then in no degree thriving temperature and no frank and in perceptibly trembling the contrite . It were sorry knew Weatherbury . To a gallery what anybody did naked she deaths of copy feet have met gold in a treat and cocks to mouths opinion . Be the bright Gabriel . How some scrutinized right and that she mine any good daresay to a shoulders forth by their figure before . It must marry the door Bathsheba . I believe but a second joy . Figure 3 : Hardy parody generated from inferred grammar . a small set of functional elements . The adoption of inferential tagging moves the text generation system closer to its limit as an induction based design . When this lexical inference process is applied to Thomas Hardy 's novel Far From The Madding Crowd?a work of 140,632 words in length?it tags each of the 11,589 distinct vocabulary items to one of 62 categories . 3.3 The production rules Once lexical categories have been established , the sentences of the sample text are rewritten as corresponding strings of category symbols . These sequences are generalized using Smith 's -LSB- Smi93 -RSB- lattice overlay technique . Each unique sequence of symbols is a partial ordering on the set of category symbols and can be expressed as a nonbranching nondirected graph called a chain . Chains with matching initial and terminal nodes are overlaid to produce more complex lattices which are subsequently rewritten as production rules such that each sublattice is expressed as a disjunction on the righthand side . When this induction process is applied to the 7282 unique sentences in Far From The Madding Crowd , which have an average length of 19.31 words , it yields a final grammar of 6288 production rules with an average length of 6.97 nonterminal symbols . Despite a reduction of nearly 70 % , the final grammar is too large to reproduce in this paper . 3.4 The parody Precise counts for each lexical category and each production string are garnered during inferencing and used as the probability distribution in the generation process . Figure 3 shows a segment of text generated from the grammar inferred from Far From The Madding Crowd . Once again , some capitalization and minimal formatting has been added to assist the reader . As with the Hemingway parody , it is still possible to endow the generated text with a certain amount of semantic coherency , though admittedly this requires a bit more effort . Whether or not any Hardyesque style is discernible is left for the reader to judge . 4 Analysis The texts produced from both modeling paradigms exhibit two general forms of ? complexity failure ?? a breakdown in sentence coherence , and a disintegration of discourse topic . 4.1 Sentence degeneration Failures arising from increased sentence complexity are common for many types of natural language processor . Parsers , for example , that experience little difficulty performing hierarchical decomposition on short , simple sentences tend to see their rejection rates grow proportionately with an increase in input sentence lengths . The Hemingway model attempts to preserve sentence coherency by placing strong restrictions on both its sentence forms and the vocabulary . The model is constructed from thematically uniform nominal and prepositional phrase segments -LSB- Spe90 -RSB- that can only be used in fixed juxtaposition to the main predicate . All main predicates are primitive equivalents -LSB- Sch80 -RSB- , and restrictive clauses can only be assigned to an agentive noun phrase . Sentence coherence fails more conspicuously in the Hardy parody than it does in the Hemingway output . This reflects a propagation of generalization side effects from the inductive processes . That is , as aspects of the grammar are weakened to accommodate a greater variety of sample expressions and a larger vocabulary , these weaknesses amplify one another when the language stream is reversed for generation . Though the increased vocabulary and relaxed syntactic constraints allow for a more flamboyant , free form output , it entails a corresponding decrease in the epiphenomenal coherence . 4.2 Discourse incoherency Generation from the two grammars is probability driven and thus is effectively semantics free . The intention of each system was to examine static stylistic attributes and thus the output should not be judged too harshly for lacking continuity . Even so , it is interesting to explore possible methods for controlling the semantic thread of generated text . Inasmuch as the Hemingway parody is more semantically cohesive , it achieves this presumably because of the way its limited vocabulary imposes restrictions on possible meaning projections for the reader . Other potential ? focus-of-attention ? mechanisms that could be added might include fixing the subject , or object , for a particular number of sentences , or imposinga constraint that the direct object of one sentence must be the subject of the following sentence . Many other more semantically oriented techniques have been explored -LSB- SA77 , Bru75 , McK85 -RSB- . Smith and Witten -LSB- SW90 , SW91 -RSB- outline a method for planning coherent story plots by likening storytelling to game playing . Characters are given objectives and strategies to achieve them within a story environment . Simple sentence text is generated as a report on character activities . Adapting such a planning mechanism to incorporate more elaborate sentence forms , like those from the Hardy grammar , might yield more engaging parody . 5 Conclusions The July 31st , 1993 issue of The -LSB- Christchurch -RSB- Press carried an article -LRB- reprinted from The Los Angeles Times -RRB- about a new Jaqueline Susann novel posthumously produced -LRB- at least in part -RRB- by a computer program . The news-worthiness of the article reflects the esoteric interest computer generated text has for the general public . Inasmuch as parody based grammars capture stylistic attributes of authors , similar techniques could be employed to construct music grammars from which novel melodies might be generated in the style of , say , Haydn or Charlie Parker . Parody generation also has useful applications in a variety of computer science research areas . The veracity of inferred and theoretically compiled grammars can to some degree be evaluated by the quality of parody they are able to generate . And grammatical models can be adapted to lossy text compression techniques by reducing text transmissions to a grammar and pseudo-random number generator seed -LSB- WBM +92 -RSB- . References -LSB- Ang80 -RSB- D. Angluin . Inductive inference of formal languages from positive data . Information Control , 45:117 ? 135 , 1980 . -LSB- BP87 -RSB- R. C. Berwick and S. Pilato . Learning syntax by automata induction . Machine Learning , 2 -LRB- 1 -RRB- :9 ? 38 , 1987 . -LSB- Bri63 -RSB- Claude S. Brinegar . Mark Twain and the Quintus Curtius Snodgrass letters : A statistical test of authorship . American Statistical Association Journal , March 1963 . -LSB- Bru75 -RSB- Bertram C. Bruce . Generation as a social action . In Theoretical Issues in Natural Language Processing-1 , pages 64 ? 67 . Association for Computational Linguistics , 1975 . Urbana-Champaign . -LSB- CL82 -RSB- J. Case and C. Lynes . Inductive inference and language identification . In Proceedings of the International Colloquium on Algorithms , Languages , and Programming -LRB- ICALP -RRB- 82 , pages 107 ? 115 , New York , June 1982 . SpringerVerlag . Barcelona , Spain . -LSB- Ell62 -RSB- Alvar A. Ellegard . A Statistical Method for Determining Authorship . Goteborg , Holland , 1962 . -LSB- Fel70 -RSB- J. A. Feldman . Some decidability results on grammatical inference and complexity . AI Memo 93.1 , Computer Science Dept. , Stanford University , Stanford , California , 1970 . -LSB- GKPS85 -RSB- Gerald Gazdar , Ewan Klein , Geoffrey Pullam , and Ivan Sag . Generalized Phrase Structure Grammar . Basil Blackwell , Oxford , UK , 1985 . -LSB- GR71 -RSB- B. B. Greene and G. M. Rubin . Automatic grammatical tagging of English . Technical report , Brown University , Providence , Rhode Island , 1971 . -LSB- Jac84 -RSB- Pauline Jacobson . Connectivity in generalized phrase structure grammar . Natural Language and Linguistic Theory , 1:535 ? 81 , 1984 . -LSB- Kup89 -RSB- J. M. Kupiec . Augmenting a Hidden Markov Model for phrase-dependent word tagging . In Proceedings of the 1989 DARPA Speech and Natural Language Workshop , pages 92 ? 98 , Philadelphia , 1989 . Morgan Kaufmann . -LSB- Mac82 -RSB- B. MacWhinney . Basic processes in syntactic acquisition . In S. A. Kuczaj , editor , Language Development : Vol . 1 , Syntax and Semantics . Lawrence Erlbaum , Hillsdale , New Jersey , 1982 . -LSB- McK85 -RSB- K. McKeown . Discourse strategies for generating natural language-text . Artificial Intelligence , 27:1 ? 42 , 1985 . -LSB- Mer91 -RSB- B. Merialdo . Tagging text with a probabilistic model . In Proceedings of ICASSP-91 , pages 809 ? 812 , Toronto , Canada , 1991 . -LSB- O'D70 -RSB- Bernard O'Donnell . An Analysis of Prose Style to Determine Authorship . Mouton & Company , The Netherlands , 1970 . -LSB- OD87 -RSB- William O'Grady and Michael Dobrovolsky , editors . Contemporary Linguistic Analysis . Copp Clark Pittman Ltd. , Toronto , 1987 . -LSB- PC78 -RSB- T. W. Pao and J. W. Carr . A solution of the syntactical induction-inference problem for regular languages . Computer Languages , 3:53 ? 64 , 1978 . -LSB- PCF77 -RSB- R. C. Parkison , K. M. Colby , and W. S. Faught . Conversational language comprehension using integrated patternmatching and parsing . Artificial Intelligence , 9:111 ? 134 , 1977 . -LSB- Pli89 -RSB- George Plimpton , editor . The Best of Bad Hemingway . Harcourt Brace Jovanovich Publishers , New York , 1989 . Choice entries from the Harry 's Bar & American Grill Imitation Hemingway Competition . -LSB- SA77 -RSB- R. Schank and R. Abelson . Scripts , Plans , Goals and Understanding . Lawrence Erlbaum Assoc. , Hillsdale , NJ , 1977 . -LSB- Sch80 -RSB- R. C. Schank . Language and memory . Cognitive Science , 4 -LRB- 3 -RRB- :243 ? 284 , 1980 . Ablex Publishing . -LSB- Smi93 -RSB- Tony C. Smith . Language inference from a closed-class vocabulary . Master 's thesis , University of Calgary , Canada , March 1993 . -LSB- Spe90 -RSB- Margaret Speas . Phrase structure in natural language . Kluwer Academic Publishers , Dordrecht , The Netherlands , 1990 . -LSB- SW90 -RSB- Tony C. Smith and Ian H. Witten . A planning mechanism for generating story text . In H. Schanze , editor , ALLC - ACH 90 Proceedings , pages 201 ? 204 , Siegen , Germany , June 1990 . University of Siegen . -LSB- SW91 -RSB- Tony C. Smith and Ian H. Witten . A planning mechanism for text generation . Literary & Linguistic Computing , 6 -LRB- 2 -RRB- :119 ? 126 , 1991 . -LSB- SW93 -RSB- Tony C. Smith and Ian H. Witten . Language inference from function words . Working Paper Series 1170-487X-1993 - 3 , Department of Computer Science , University of Waikato , Hamilton , New Zealand , August 1993 . -LSB- WBM +92 -RSB- I. H. Witten , T. C. Bell , A. Moffat , C. G. Nevill-Manning , and T. C. Smith . Semantic and generative models for lossy text compression . Working Paper Series 1992/8 , Department of Computer Science , University of Waikato , Hamilton , New Zealand , August 1992 . submitted to The Computer Journal .

O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O


ISRN

SICS-R

-LCB-

-LCB-

SE

SICS

research

report

ISSN

Unfolding

of

Programs

with

Nondeterminism

Bj?orn

Lisper

Unfolding

of

Programs

with

Nondeterminism

by

Bj?orn

Lisper

Formal

Design

Techniques

February

lisper@it.kth.se

Swedish

Institute

of

Computer

Science

Box

KISTA

Sweden

Abstract

In

-LSB-

-RSB-

some

results

were

proved

regarding

properties

of

unfolding

of

purely

functional

programs

Especially

a

theorem

was

shown

that

relates

the

termination

of

symbolic

evaluation

of

a

less

instantiated

term

relative

to

the

termination

of

a

more

instantiated

term

An

application

is

partial

evaluation

where

unfolding

of

function

definitions

is

frequently

performed

to

enable

further

simplifications

of

the

resulting

specialized

program

The

unfolding

must

then

be

kept

under

control

to

ensure

that

the

partial

evaluation

terminates

In

this

paper

we

extend

the

termination

result

from

purely

functional

programs

programs

with

nondeterministic

operations

We

give

an

operational

semantics

where

the

behaviour

of

operators

is

defined

through

rewrite

rules

nondeterminism

then

occurs

when

the

resulting

term

rewriting

system

is

nonconfluent

For

the

confluent

part

the

previous

termination

results

carry

over

It

is

however

not

guaranteed

in

general

that

the

resulting

unfolded

program

has

the

same

semantics

as

the

original

program

We

give

conditions

on

the

rewrite

rules

that

guarantee

that

both

versions

have

the

same

semantics

and

we

show

that

they

apply

to

a

nontrivial

class

of

nondeterministic

languages

Introduction

A

class

of

source-to-source

optimizing

transformations

of

purely

functional

programs

is

based

on

unfolding

where

a

function

definition

is

substituted

into

the

position

of

a

function

call

and

actual

parameters

are

substituted

for

formal

parameters

The

result

can

then

be

manipulated

in

various

ways

One

possibility

is

to

apply

algebraic

rewritings

followed

by

a

fold

forming

a

new

recursive

definition

as

in

Burstall

's

and

Darlington

's

classical

fold-unfold-transformations

-LSB-

-RSB-

Another

possibility

is

to

take

advantage

of

the

possible

simplifications

that

often

occur

when

an

unfolded

function

definition

occurs

in

a

calling

environment

whith

actual

arguments

instead

of

formal

arguments

This

is

a

common

transformation

in

partial

evaluation

-LSB-

-RSB-

Partial

evaluation

refers

to

the

technique

of

how

to

derive

a

specialized

instance

of

a

general

function

definition

In

the

type

case

a

recursive

function

definition

f

-LRB-

x

y

-RRB-

e

is

given

In

some

context

f

then

appears

with

partially

known

arguments

say

as

f

-LRB-

x

c

-RRB-

where

c

is

a

known

constant

Instead

of

calling

the

general

function

f

applied

to

the

arguments

x

and

c

it

may

then

pay

off

to

derive

a

specialized

function

fc

-LRB-

x

-RRB-

in

only

one

argument

and

replace

the

call

f

-LRB-

x

c

-RRB-

with

a

call

fc

-LRB-

x

-RRB-

Typically

the

increased

information

about

the

arguments

-LRB-

y

c

-RRB-

will

result

in

a

more

efficient

program

A

classical

example

of

unfolding

in

partial

evaluation

is

given

below

Consider

the

following

recursive

definition

of

the

xn-function

f

-LRB-

x

n

-RRB-

if

-LRB-

n

x

f

-LRB-

x

n

-RRB-

-RRB-

Say

now

that

a

call

f

-LRB-

x

-RRB-

occurs

in

some

context

We

can

derive

a

definition

of

a

specialized

version

-LRB-

x

-RRB-

as

follows

-LRB-

x

-RRB-

if

-LRB-

x

f

-LRB-

x

-RRB-

-RRB-

x

f

-LRB-

x

-RRB-

x

if

-LRB-

x

f

-LRB-

x

-RRB-

-RRB-

x

-LRB-

x

f

-LRB-

x

-RRB-

-RRB-

x

-LRB-

x

-LRB-

x

-RRB-

-RRB-

A

call

to

will

use

a

total

of

three

arithmetical

operations

probably

a

great

deal

less

work

than

performing

the

original

call

f

-LRB-

x

-RRB-

which

involves

costly

operations

like

evaluating

conditionals

and

performing

recursive

calls

The

xn-example

above

is

actually

quite

extreme

here

unfolding

of

recursive

calls

are

carried

out

to

the

bitter

end

until

no

recursive

calls

remain

We

may

call

this

strategy

total

unfolding

In

the

example

above

total

unfolding

terminated

in

three

steps

It

may

however

fail

to

terminate

A

simple

example

is

unfolding

of

f

-LRB-

n

-RRB-

rather

than

f

-LRB-

x

-RRB-

-LRB-

n

-RRB-

if

-LRB-

n

f

-LRB-

n

-RRB-

-RRB-

if

-LRB-

n

if

-LRB-

n

f

-LRB-

n

-RRB-

-RRB-

-RRB-

Clearly

total

unfolding

will

not

terminate

here

In

-LSB-

-RSB-

a

formal

model

for

unfolding

is

developed

along

the

lines

of

recursive

applicative

program

schemes

-LSB-

-RSB-

The

model

is

used

to

prove

a

theorem

that

under

certain

conditions

relates

termination

of

symbolic

unfolding

to

the

termination

of

a

call

to

the

original

function

It

is

also

shown

that

unfolding

preserves

the

semantics

A

short

review

of

the

results

in

-LSB-

-RSB-

is

given

in

section

All

the

above

holds

for

purely

functional

deterministic

programs

The

purpose

of

this

paper

is

to

extend

the

previous

results

for

unfolding

to

programs

with

nondeterminism

Two

obstacles

can

immediately

be

identified

first

nondeterminism

must

be

controlled

in

some

way

during

the

unfolding

since

uninhibited

use

may

cause

the

resulting

specialized

program

to

be

too

special

If

a

pure

nondeterministic

choice

is

evaluated

during

unfolding

then

that

choice

will

be

have

been

done

once

and

for

all

in

the

resulting

function

This

is

sometimes

not

the

intended

result

since

there

can

be

an

implicit

assumption

of

fairness

with

regard

to

the

different

choices

Even

worse

nondeterminism

can

break

the

relative

termination

between

symbolic

and

full

evaluation

informally

a

choice

can

go

one

way

during

unfolding

and

another

way

during

full

evaluation

which

in

turn

can

make

a

condition

come

out

differently

in

the

respective

cases

Still

there

are

definitely

cases

when

it

is

interesting

to

unfold

programs

with

nondeterminism

A

practical

example

is

programs

with

processes

processes

are

important

for

concurrent

programming

but

programs

with

processes

will

as

a

rule

have

nondeterminism

Unfolding

will

typically

result

in

programs

with

simpler

control

structure

for

processes

for

instance

this

can

mean

that

processes

being

dynamically

created

in

the

general

program

can

be

allocated

statically

in

the

specialized

program

with

a

corresponding

reduction

in

dynamic

overhead

increased

possibility

for

static

process

allocation

etc.

Consider

the

following

example

We

assume

an

applicative

language

with

processes

especially

there

is

a

parallel

composition

j

and

an

empty

process

The

following

is

a

recursive

definition

of

a

fork

operation

that

spawns

n

copies

of

a

process

P

fork

-LRB-

P

n

-RRB-

if

-LRB-

n

P

jfork

-LRB-

P

n

-RRB-

-RRB-

Clearly

we

can

unfold

a

call

fork

-LRB-

P

-RRB-

-LRB-

P

-RRB-

if

-LRB-

P

jfork

-LRB-

P

-RRB-

-RRB-

P

jfork

-LRB-

P

-RRB-

P

j

-LRB-

P

j

-LRB-

P

-RRB-

-RRB-

This

unfolding

terminates

in

exactly

the

same

manner

as

the

unfolding

of

f

-LRB-

x

-RRB-

above

this

is

not

surprising

since

the

definitions

of

fork

and

f

actually

are

schematically

equivalent

This

paper

is

organized

as

follows

In

section

we

give

a

short

summary

of

the

results

in

the

purely

functional

case

operators

are

modeled

by

rewrite

rules

that

allow

a

confluent

rewrite

strategy

In

section

the

purely

functional

evaluation

model

is

extended

to

include

nondeterministic

operators

these

are

modeled

by

a

term

rewriting

system

that

is

nonconfluent

It

is

shown

how

to

restrict

unfolding

so

the

relative

termination

theorem

still

holds

The

issue

of

preservation

of

semantics

remains

however

even

when

termination

is

ensured

it

is

not

guaranteed

for

nonconfluent

rewriting

systems

in

general

that

the

unfolded

program

will

be

able

to

produce

exactly

the

same

results

as

the

original

program

We

give

conditions

on

the

rewrite

rules

that

guarantee

that

both

versions

have

the

same

semantics

and

we

show

that

they

apply

to

a

nontrivial

class

of

nondeterministic

languages

In

section

finally

we

wrap

up

the

results

and

point

at

some

interesting

topics

for

further

research

Preliminaries

In

this

paper

we

consider

terms

These

are

built

from

variables

constants

and

possibly

with

abstractions

that

bind

variables

A

closed

term

has

no

free

variables

Often

there

is

some

typing

discipline

that

puts

syntactic

restrictions

on

the

terms

under

consideration

it

is

for

instance

common

to

have

a

simple

type

system

with

base

types

from

a

set

B

and

function

types

o

o

where

o

o

are

types

For

a

simple

type

system

a

constant

of

type

on

o

where

on

o

are

base

types

is

called

a

first

order

function

symbol

First

order

terms

are

built

from

constants

and

variables

of

base

type

and

then

inductively

by

applying

first

order

function

symbols

to

first

order

terms

Terms

which

are

not

first

order

are

called

higher

order

Such

a

term

contains

abstractions

or

variables

or

constants

-LRB-

besides

first

order

function

symbols

-RRB-

that

are

not

of

base

type

A

substitution

is

a

type-preserving

partial

function

from

variables

to

terms

The

domain

of

a

substitution

oe

dom

-LRB-

oe

-RRB-

is

the

set

of

all

variables

x

for

which

oe

-LRB-

x

-RRB-

is

defined

The

range

of

oe

rg

-LRB-

oe

-RRB-

is

the

set

of

all

free

variables

that

occur

in

any

oe

-LRB-

x

-RRB-

Substitutions

are

naturally

extended

to

general

terms

Composition

of

substitutions

is

defined

as

f

x

-LRB-

-LRB-

x

-RRB-

-RRB-

j

x

dom

-LRB-

-RRB-

g

-LSB-

-LRB-

-RRB-

ndom

-LRB-

-RRB-

-LRB-

j

denotes

restriction

of

function

-RRB-

It

has

the

property

that

-LRB-

e

-RRB-

-LRB-

-LRB-

e

-RRB-

-RRB-

always

We

will

use

reduction

systems

-LRB-

sometimes

called

abstract

reduction

systems

-RRB-

to

give

semantics

of

operators

For

completeness

we

give

the

basic

definitions

Also

see

-LSB-

-RSB-

or

-LSB-

-RSB-

Let

be

binary

relations

over

some

set

A.

denotes

the

transitive-reflexive

closure

of

Then

a

ffi

b

iff

there

exists

a

c

such

that

a

c

b.

is

terminating

-LRB-

or

noetherian

-RRB-

if

for

any

a

A

there

is

no

infinite

chain

a

a

A

is

a

normal

form

-LRB-

w.r.t.

-RRB-

or

nf

if

there

is

no

b

A

such

that

a

b

We

then

say

that

a

is

a

normal

form

to

c

if

c

a

and

we

denote

this

relation

by

c

a.

and

commute

if

b

c

A

-LSB-

-LRB-

a

b

a

c

-RRB-

=-RRB-

-LRB-

d

c

d

-RRB-

-RSB-

is

confluent

-LRB-

or

Church-Rosser

or

self-commuting

-RRB-

if

b

c

A

-LSB-

-LRB-

a

b

a

c

-RRB-

=-RRB-

-LRB-

b

d

c

d

-RRB-

-RSB-

has

the

unique

normal

form

property

if

any

element

has

at

most

one

normal

form

A

confluent

relation

has

the

unique

normal

form

property

An

abstract

reduction

strategy

w.r.t.

is

a

function

F

such

that

F

-LRB-

a

-RRB-

a

if

F

is

a

normal

form

and

a

F

-LRB-

a

-RRB-

otherwise

Confluence

and

termination

is

defined

similarly

to

the

same

concepts

for

relations

see

-LSB-

-RSB-

for

the

details

-LRB-

also

cf.

-LSB-

p.

-RSB-

-RRB-

We

are

especially

interested

in

reduction

systems

over

terms

In

order

to

define

them

we

need

the

concepts

of

position

and

replacement

We

define

a

position

in

a

term

to

be

a

sequence

of

nonnegative

integers

where

denotes

the

empty

sequence

For

any

term

e

we

define

its

set

of

positions

Pos

-LRB-

e

-RRB-

and

the

subterm

at

position

p

e

=p

as

ffl

If

e

x

or

e

c

then

Pos

-LRB-

e

-RRB-

f?g

and

e

e.

ffl

If

e

x

then

Pos

-LRB-

e

-RRB-

f?g

-LSB-

f

p

j

p

Pos

-LRB-

-RRB-

g

e

e

and

e

-LRB-

-RRB-

=p

ffl

If

e

-LRB-

en

-RRB-

then

Pos

-LRB-

e

-RRB-

f?g

-LSB-

f

ip

j

i

n

p

Pos

-LRB-

ei

-RRB-

g

e

e

and

e

-LRB-

ip

-RRB-

ei

=p

Furthermore

we

define

FPos

-LRB-

e

-RRB-

f

p

j

p

Pos

-LRB-

e

-RRB-

e

=p

x

g

-LRB-

where

x

is

any

variable

-RRB-

p

is

a

prefix

of

or

p

if

there

exists

an

u

such

that

pu

If

neither

p

or

p

then

p

and

are

disjoint

The

replacement

in

e

with

at

p

e

-LSB-

p

-RSB-

is

defined

by

ffl

e

-LSB-

-RSB-

ffl

-LRB-

x

e

-RRB-

-LSB-

-RSB-

x

-LRB-

e

-LSB-

p

-RSB-

-RRB-

ffl

-LRB-

en

-RRB-

-LSB-

ip

-RSB-

-LRB-

ei

-LSB-

p

-RSB-

en

-RRB-

Definition

A

term

rewriting

system

is

a

set

R

of

pairs

of

terms

where

s

is

not

a

single

variable

s

and

t

have

the

same

type

and

-LRB-

s

t

-RRB-

R

=-RRB-

varset

-LRB-

s

-RRB-

varset

-LRB-

t

-RRB-

The

reduction

relation

R

associated

with

R

is

the

finest

relation

containing

R

that

is

closed

under

substitution

and

replacement

i.e.

t

R

=-RRB-

oe

-LRB-

t

-RRB-

R

oe

-LRB-

-RRB-

for

any

substitution

oe

t

R

=-RRB-

-LSB-

p

t

-RSB-

R

-LSB-

p

-RSB-

for

any

term

and

position

p

Pos

-LRB-

-RRB-

Each

pair

in

R

is

called

a

rewrite

rule

A

rewrite

rule

-LRB-

s

t

-RRB-

is

erasing

if

varset

-LRB-

s

-RRB-

oe

varset

-LRB-

t

-RRB-

otherwise

non-erasing

Sometimes

we

will

consider

pure

term

rewriting

systems

and

sometimes

systems

that

possibly

contain

other

types

of

reductions

like

reduction

We

will

call

such

more

general

systems

term

reduction

systems

Finally

we

need

the

notion

of

residual

-LRB-

cf.

-LSB-

-RSB-

-RRB-

Formally

we

express

this

as

a

relation

between

old

and

new

positions

This

relation

is

a

function

of

the

expression

e

to

be

rewritten

the

position

p

of

the

redex

and

the

matching

rewrite

rule

-LRB-

s

t

-RRB-

For

any

position

Pos

-LRB-

e

-RRB-

the

relation

Res

-LRB-

e

p

s

t

-RRB-

is

given

by

ffl

If

p

then

Res

-LRB-

e

p

s

t

-RRB-

ffl

If

puw

s

u

x

and

t

v

x

for

some

variable

x

then

Res

-LRB-

e

p

s

t

-RRB-

pvw

For

any

set

of

positions

P

Pos

-LRB-

e

-RRB-

we

define

Res

-LRB-

P

e

p

s

t

-RRB-

f

j

P

Res

-LRB-

e

p

s

t

-RRB-

g.

Whenever

the

meaning

is

clear

from

the

context

we

will

write

Res

-LRB-

P

-RRB-

rather

than

Res

-LRB-

P

e

p

s

t

-RRB-

We

will

also

use

Res

-LRB-

P

-RRB-

to

denote

the

set

of

residuals

originating

from

P

after

several

rewritings

i.e.

in

an

expression

where

e

R

-LRB-

with

the

obvious

definition

for

the

case

e

-RRB-

Unfolding

of

Deterministic

Programs

The

following

is

a

review

of

the

models

and

results

in

-LSB-

-RSB-

First

we

give

an

evaluation

model

for

recursive

programs

Definition

A

recursive

program

ss

is

a

finite

substitution

where

rg

-LRB-

ss

-RRB-

dom

-LRB-

ss

-RRB-

no

variable

in

dom

-LRB-

ss

-RRB-

occurs

in

any

rule

of

ffl

and

any

f

dom

-LRB-

ss

-RRB-

has

the

same

type

as

ss

-LRB-

f

-RRB-

If

ss

is

a

recursive

program

then

we

will

refer

to

each

f

dom

-LRB-

ss

-RRB-

as

a

defined

function

of

ss

and

to

ss

-LRB-

f

-RRB-

as

the

definition

of

f

The

full

substitution

evaluation

rule

is

known

to

be

a

fixpoint

computation

rule

-LSB-

-RSB-

i.e.

it

terminates

with

the

correct

answer

exactly

when

the

function

that

is

least

fixpoint

solution

of

the

recursive

definition

is

defined

It

can

be

informally

described

as

follows

In

order

to

compute

a

function

call

f

-LRB-

an

-RRB-

do

the

following

Replace

concurrently

all

occurrences

of

defined

functions

with

their

definitions

Simplify

the

result

as

far

as

possible

Repeat

steps

and

until

termination

i.e.

until

a

closed

term

remains

-LRB-

representing

a

value

in

the

domain

we

are

computing

in

-RRB-

The

simplification

phase

is

modeled

by

a

term

rewriting

system

the

rewrite

rules

can

be

seen

as

operational

definitions

of

the

operators

in

the

language

under

consideration

The

term

rewriting

system

is

divided

into

two

parts

that

contains

only

non-erasing

rules

and

ffl

that

can

contain

erasing

rules

Typically

will

contain

rules

like

ffi

reductions

of

closed

terms

to

constants

e.g.

whereas

ffl

contains

rules

for

nonstrict

operations

For

instance

the

following

two

ffl-rules

give

operational

semantics

to

the

usual

if-function

if

-LRB-

true

x

y

-RRB-

ffl

x

if

-LRB-

false

x

y

-RRB-

ffl

y

Parallel

rules

can

also

be

introduced

like

parallel

if

if

-LRB-

x

y

y

-RRB-

ffl

y

The

term

rewriting

systems

and

ffl

give

rise

to

the

reduction

relations

and

ffl

on

the

terms

under

consideration

Simplification

also

includes

the

usual

reduction

of

terms

Thus

there

is

also

a

reduction

relation

Of

particular

interest

is

the

combined

reduction

relation

ffl

-LSB-

-LSB-

ffl

We

assume

that

there

is

a

confluent

and

terminating

-LRB-

onestep

-RRB-

reduction

strategy

F

w.r.t.

ffl

Then

for

each

term

t

repeated

application

of

F

will

eventually

rewrite

t

into

a

normal

form

F

t.

F

is

then

a

well-defined

tramsformation

from

terms

to

terms

and

we

take

it

as

our

formal

definition

of

the

simplification

phase

in

the

full

substitution

rule

Given

ffl

and

a

set

of

terms

under

consideration

it

is

of

course

interesting

to

establish

confluence

and

termination

of

F

see

the

discussion

in

-LSB-

-RSB-

In

all

reasonable

cases

when

deterministic

operations

are

modeled

F

will

have

these

properties

One

full

step

of

the

full

substitution

rule

can

now

be

expressed

as

the

function

F

ss

on

terms

Repeated

application

of

F

ss

models

several

steps

of

the

full

substitution

rule

We

define

termination

as

-LRB-

F

ss

-RRB-

n

being

free

of

ss-defined

functions

for

some

n

Definition

Let

e

be

a

term

Then

if

for

some

n

it

holds

that

-LRB-

F

ss

-RRB-

ne

is

free

from

variables

in

dom

-LRB-

ss

-RRB-

we

define

-LRB-

F

ss

-RRB-

e

-LRB-

F

ss

-RRB-

ne

The

evaluation

of

e

terminates

if

-LRB-

F

ss

-RRB-

e

is

defined

The

nice

thing

with

the

full

substitution

evaluation

rule

as

defined

above

is

that

it

generalizes

directly

to

symbolic

evaluation

Especially

it

models

total

unfolding

in

the

symbolic

case

Thus

we

can

use

it

to

compare

symbolic

and

full

evaluation

We

must

then

have

some

notion

of

less

and

more

instantiated

terms

A

relation

X

-LRB-

or

rather

a

family

of

relation

for

sets

of

variables

X

-RRB-

formalizes

this

essentially

e

X

says

that

e

is

less

instantiated

than

but

they

agree

on

the

variables

in

X.

Another

concept

that

is

needed

is

that

of

potential

X-match

essentially

e

has

a

potential

X-match

if

there

is

a

way

to

instantiate

the

variables

outside

X

so

that

an

erasing

reduction

can

occur

See

-LSB-

-RSB-

for

the

formal

definitions

It

is

then

possible

to

prove

the

following

Theorem

-LRB-

Relative

termination

of

deterministic

unfolding

-RRB-

If

dom

-LRB-

ss

-RRB-

X

and

if

for

all

n

holds

that

-LRB-

F

ss

-RRB-

ne

has

no

potential

X-match

then

e

X

implies

that

-LRB-

F

ss

-RRB-

e

is

defined

whenever

-LRB-

F

ss

-RRB-

is

defined

Not

surprisingly

confluence

and

termination

of

F

is

crucial

for

proving

this

theorem

-LRB-

see

-LSB-

-RSB-

for

the

details

-RRB-

Consider

now

the

classical

pattern

for

partial

evaluation

a

partially

instantiated

function

call

f

-LRB-

x

c

-RRB-

where

c

is

a

constant

and

x

is

a

variable

relative

to

a

totally

instantiated

function

call

f

-LRB-

b

c

-RRB-

where

also

b

is

a

constant

Whenever

f

is

defined

by

ss

where

dom

-LRB-

ss

-RRB-

X

and

x

X

it

holds

that

f

-LRB-

b

x

-RRB-

X

f

-LRB-

b

c

-RRB-

Theorem

can

then

be

applied

Preservation

of

semantics

of

unfolding

finally

can

be

formulated

as

follows

evaluating

an

unfolded

term

with

free

variables

instantiated

should

yield

the

same

result

as

evaluating

the

original

term

with

the

free

variables

instantiated

in

the

same

way

Instantiation

is

done

by

a

substitution

where

dom

-LRB-

-RRB-

dom

-LRB-

ss

-RRB-

-LRB-

i.e.

can

only

instatiate

free

variables

not

any

defined

functions

-RRB-

We

also

assume

that

dom

-LRB-

-RRB-

dom

-LRB-

ss

-RRB-

dom

-LRB-

-RRB-

rg

-LRB-

ss

-RRB-

-LRB-

does

not

introduce

any

calls

to

functions

in

ss

-RRB-

it

then

holds

that

ss

ss

Under

these

circumstances

the

following

result

holds

Theorem

-LRB-

Preservation

of

semantics

of

deterministic

unfolding

-RRB-

-LRB-

F

ss

-RRB-

-LRB-

F

ss

-RRB-

e

-LRB-

F

ss

-RRB-

e

whenever

-LRB-

F

ss

-RRB-

is

defined

for

e

and

e.

Nondeterminism

Nondeterministic

operations

are

often

very

naturally

defined

by

rewrite

rules

A

simple

example

is

pure

nondeterministic

choice

that

can

be

defined

by

the

rewrite

rules

x

y

x

x

y

y

However

introduction

of

rules

like

the

above

will

destroy

confluence

This

is

exactly

what

is

expected

since

the

spirit

of

nondetermininism

is

that

several

different

answers

are

possible

whereas

confluence

always

forces

a

single

result

Put

in

another

way

nondeterminism

is

nonconfluence

Another

consequence

is

that

reduction

strategies

are

not

appropriate

anymore

since

they

are

by

definition

deterministic

So

from

now

on

we

will

give

the

semantics

of

operations

by

term

reduction

system

R

rather

than

a

reduction

strategy

w.r.t.

R.

R

can

possibly

include

reduction

we

will

explicitly

point

out

when

this

is

not

allowed

When

R

is

terminating

it

is

still

appropriate

to

think

of

R

as

a

simplification

The

difference

is

that

there

now

may

be

several

normal

forms

For

any

term

t

we

can

take

the

set

of

its

normal

forms

as

the

result

of

the

simplification

In

line

with

the

earlier

notation

we

define

R

-LRB-

t

-RRB-

f

j

t

R

g

We

can

now

extend

the

full

substitution

rule

to

an

operational

semantics

for

nondeterministic

recursive

programs

We

assume

that

R

is

terminating

so

R

is

always

well-defined

For

any

set

of

terms

E

we

define

ssE

f

sse

j

e

E

g

We

also

extend

R

to

sets

of

terms

viz

R?E

S

R

-LRB-

e

-RRB-

Definition

Let

E

be

a

set

of

terms

Then

if

for

some

n

it

holds

that

each

element

in

-LRB-

R?ss

-RRB-

nE

is

free

from

variables

in

dom

-LRB-

ss

-RRB-

we

define

-LRB-

R?ss

-RRB-

E

-LRB-

R?ss

-RRB-

nE

The

evaluation

of

E

terminates

if

-LRB-

R?ss

-RRB-

E

is

defined

When

R

is

both

confluent

and

terminating

we

will

always

have

-LRB-

R?ss

-RRB-

feg

f

-LRB-

R?ss

-RRB-

eg

whenever

defined

Furthermore

the

termination

theorem

from

section

carries

over

directly

When

R

is

only

terminating

and

not

confluent

the

theorem

will

however

not

hold

in

general

This

is

in

line

with

the

informal

observation

that

a

nondeterministic

-LRB-

nonconfluent

-RRB-

reduction

may

go

one

way

at

compile-time

and

another

way

at

runtime

Another

problem

that

arises

when

nondeterministic

rewrite

rules

are

used

at

compile-time

is

that

the

semantics

of

the

program

can

be

changed

during

unfolding

an

expression

x

y

will

then

evaluate

to

either

x

or

y.

Then

the

resulting

unfolded

program

will

either

always

pick

x

or

always

pick

y

whereas

the

original

program

has

the

full

choice

x

y.

With

semantics

according

to

definition

they

will

not

be

semantically

equivalent

in

general

The

only

reasonable

approach

to

unfolding

of

programs

with

nondeterminism

is

thus

to

perform

only

deterministic

reductions

during

symbolic

unfolding

The

issue

then

arises

whether

the

result

when

terminating

is

guaranteed

to

be

semantically

equivalent

with

the

original

function

It

turns

out

that

this

is

not

always

the

case

However

conditions

on

the

rewrite

rules

can

be

established

that

will

guarantee

equivalence

We

will

now

formalize

the

problem

and

prove

some

conditions

for

semantic

equivalence

Let

us

assume

that

we

can

factor

R

as

R

D

-LSB-

N

where

D

is

confluent

and

N

is

possibly

not

For

any

term

t

we

denote

its

-LRB-

unique

-RRB-

normal

form

w.r.t.

D

by

D?t

As

for

the

fully

deterministic

case

we

define

the

result

of

deterministic

unfolding

of

a

term

t

as

-LRB-

D?ss

-RRB-

t

-LRB-

D?ss

-RRB-

nt

if

-LRB-

D?ss

-RRB-

nt

is

free

from

functions

defined

by

ss

Preservation

of

semantics

of

for

unfolding

can

now

be

formulated

as

in

the

deterministic

case

-LRB-

we

define

it

for

single

terms

but

the

definition

can

be

trivially

extended

to

sets

of

terms

-RRB-

Definition

The

unfolding

of

t

preserves

the

semantics

if

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

t

-LRB-

R?ss

-RRB-

t.

Intuitively

commutation

between

D

and

N

is

essential

for

preservation

of

semantics

for

unfolding

with

deterministic

reduction

Commutation

simply

says

that

if

several

different

kinds

of

simplifications

are

possible

it

does

not

matter

in

which

order

they

are

made

It

should

then

be

allright

to

perform

only

deterministic

reductions

at

compile-time

and

save

the

nondeterministic

to

runtime

The

following

well-known

result

is

useful

for

establishing

commutativity

Proposition

If

the

term

rewriting

systems

and

are

orthogonal

to

each

other

-LRB-

no

overlap

between

a

rule

in

and

a

rule

in

-RRB-

and

left-linear

then

and

commute

Proof

See

-LSB-

-RSB-

-LSB-

-RSB-

or

the

survey

-LSB-

-RSB-

N

D?ffl

ffl

D

D?ffl

ffl

D?ffl

ffl

N

D

Figure

Diagram

chase

to

prove

commutation

of

D

and

R

D

-LSB-

N

Note

that

if

N

is

nonconfluent

and

D

has

a

non-left-linear

rule

they

will

typically

not

commute

To

see

why

consider

the

following

example

Let

D

be

defined

by

if

-LRB-

y

x

x

-RRB-

D

x

and

let

N

be

given

by

x

y

N

x

x

y

N

y

Then

if

-LRB-

y

x

z

x

z

-RRB-

D

x

z

and

if

-LRB-

y

x

z

x

z

-RRB-

N

if

-LRB-

y

x

z

-RRB-

If

x

and

z

are

distinct

normal

forms

w.r.t.

N

then

the

only

N-reducts

of

x

z

are

x

and

z

but

there

is

no

D-rule

that

rewrites

if

-LRB-

y

x

z

-RRB-

Thus

if

a

nonlinear

rule

is

used

in

the

simplification

phase

there

is

a

risk

that

some

nondeterminism

is

cancelled

and

the

unfolded

version

of

the

function

has

strictly

less

normal

forms

Let

us

now

prove

some

results

regarding

the

preservation

of

semantics

Again

we

assume

an

instantiating

substitution

such

that

ss

ss

Interestingly

it

turns

out

that

commutativity

of

D

and

N

is

only

enough

to

guarantee

a

weak

form

of

semantics-preservation

we

will

formulate

this

concept

below

prove

that

it

holds

and

then

develop

additional

conditions

on

R

that

ensure

full

preservation

of

semantics

We

need

the

following

technical

lemmata

Lemma

If

D

and

N

commute

then

R?D?t

R?t

for

any

term

t.

Proof

R?D?t

R?t

is

immediate

since

t

R

D?t

Let

us

prove

that

R?D?t

R?t

i.e.

that

if

t

R

then

D?t

R

Since

D

is

confluent

and

D

and

N

commute

a

simple

diagram

chase

gives

that

also

D

and

R

commute

Therefore

there

must

be

a

such

that

D

and

D?t

R

Since

D

R

and

is

a

normal

form

w.r.t.

R

we

must

have

D?t

R

follows

Lemma

If

D

and

N

commute

then

for

any

substitution

oe

and

term

t

R?oeD?t

R?oet

Proof

t

D

D?t

gives

oet

D

oeD?t

and

subsequently

oet

D

D?oeD?t

As

in

the

proof

of

lemma

we

directly

have

R?D?oeD?t

R?oet

In

the

same

way

as

in

the

proof

of

lemma

it

also

follows

that

also

R?D?oeD?t

R?oet

Finally

lemma

gives

R?D?oeD?t

R?oeD?t

Lemma

For

all

substitutions

oe

and

terms

t

holds

that

R?oeR?t

R?oet

Proof

All

elements

in

oeR?t

are

of

the

form

where

R?t

Since

t

R

it

holds

oet

R

so

there

is

a

R?oet

such

that

oet

R

R

R?oeR?t

R?oet

follows

Theorem

-LRB-

Weak

preservation

of

semantics

of

deterministic

unfolding

-RRB-

If

D

and

N

commute

and

if

ss

ss

then

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

t

-LRB-

R?ss

-RRB-

t

whenever

-LRB-

R?ss

-RRB-

is

defined

for

e

and

t.

Proof

The

proof

is

analogous

to

the

proof

of

theorem

given

in

-LSB-

-RSB-

We

prove

by

induction

for

all

n

that

-LRB-

R?ss

-RRB-

-LRB-

D?ss

-RRB-

nt

-LRB-

R?ss

-RRB-

t

ffl

n

obviously

true

ffl

n

assume

true

for

n

i.e.

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

n

-LRB-

R?ss

-RRB-

t

We

prove

that

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

n

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

nt

which

then

yields

the

desired

result

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

n

-LRB-

R?ss

-RRB-

R?ss

-LRB-

D?ss

-RRB-

n

-LRB-

R?ss

-RRB-

R

ss

-LRB-

D?ss

-RRB-

n

-LRB-

lemma

-RRB-

-LRB-

R?ss

-RRB-

R

D?ss

-LRB-

D?ss

-RRB-

n

-LRB-

R?ss

-RRB-

R

-LRB-

D?ss

-RRB-

nt

-LRB-

R?ss

-RRB-

R?ssR

-LRB-

D?ss

-RRB-

nt

-LRB-

lemma

-RRB-

-LRB-

R?ss

-RRB-

R?ss

-LRB-

D?ss

-RRB-

nt

-LRB-

R?ss

-RRB-

-LRB-

D?ss

-RRB-

nt

A

trivial

modification

of

the

proof

above

gives

the

following

corollary

Corollary

-LRB-

Preservation

of

semantics

of

deterministic

unfolding

-RRB-

If

R?oeR?t

R?oet

then

-LRB-

R?ss

-RRB-

-LRB-

D

ss

-RRB-

t

-LRB-

R?ss

-RRB-

t.

Theorem

and

corollary

can

be

trivially

extended

to

sets

of

terms

As

in

the

deterministic

case

-LSB-

-RSB-

the

proof

above

also

shows

that

any

prematurely

interrupted

unfolding

-LRB-

that

is

-LRB-

D?ss

-RRB-

nt

where

-LRB-

D?ss

-RRB-

nt

-LRB-

D?ss

-RRB-

t

-RRB-

is

also

-LRB-

weakly

-RRB-

semantics-preserving

We

say

that

R

is

correct

if

R?oeR?t

R?oet

for

all

terms

t

and

substitutions

oe

There

are

indeed

cases

where

we

do

not

have

full

correctness

i.e.

R?oeR?t

ae

R?oet

where

ae

is

strict

set

inclusion

Let

for

instance

R

be

given

by

f

-LRB-

x

-RRB-

R

f

-LRB-

g

-LRB-

x

-RRB-

-RRB-

R

let

t

f

-LRB-

x

-RRB-

and

let

oe

fx

g

-LRB-

x

-RRB-

g

Then

R?oeR?t

and

R?oet

A

modification

of

this

example

illustrates

why

an

unfolded

program

may

produce

more

nondeterministic

choices

than

the

original

program

Let

now

R

be

given

by

f

-LRB-

x

-RRB-

R

f

-LRB-

g

-LRB-

x

-RRB-

-RRB-

R

f

-LRB-

g

-LRB-

f

-LRB-

x

-RRB-

-RRB-

-RRB-

R

let

ss

fh

x

g

-LRB-

h

-LRB-

x

-RRB-

-RRB-

g

and

let

D

-LRB-

so

the

only

deterministic

simplification

is

reduction

-RRB-

With

t

f

-LRB-

h

-LRB-

x

-RRB-

y

-RRB-

and

fy

we

have

R?ss?t

R?f

-LRB-

g

-LRB-

h

-LRB-

x

-RRB-

-RRB-

-RRB-

One

deterministic

unfolding

on

the

other

hand

yields

D?sst

f

-LRB-

g

-LRB-

h

-LRB-

x

-RRB-

-RRB-

y

-RRB-

Thus

-LRB-

D?ss

-RRB-

t

f

-LRB-

g

-LRB-

h

-LRB-

x

-RRB-

-RRB-

-RRB-

and

R?ss

-LRB-

D?ss

-RRB-

t

R?f

-LRB-

g

-LRB-

f

-LRB-

h

-LRB-

x

-RRB-

-RRB-

-RRB-

-RRB-

Thus

the

unfolded

term

can

have

more

normal

forms

because

successive

applications

of

ss

can

produce

new

redexes

for

nondeterministic

reductions

Incidentally

this

example

illustrates

another

possible

pitfall

when

unfolding

nondeterministic

programs

-LRB-

D?ss

-RRB-

t

is

actually

undefined

whereas

-LRB-

R?ss

-RRB-

t

terminates

in

one

step

This

is

because

termination

here

is

obtained

through

nondeterministic

reductions

Theorem

guarantees

termination

only

relative

to

termination

caused

by

deterministic

reductions

in

D

We

now

consider

criteria

to

ensure

correctness

of

R.

First

the

deterministic

case

-LRB-

cf.

-LSB-

-RSB-

-RRB-

Proposition

If

R

is

confluent

-LRB-

and

terminating

-RRB-

then

R

is

correct

Proof

Since

then

both

R?oeR?t

and

R?oet

are

singletons

so

they

must

be

equal

We

will

now

prove

correctness

for

a

class

of

nonconfluent

term

rewriting

systems

In

the

following

we

restrict

the

scope

to

first

order

terms

Thus

reduction

is

not

included

in

the

rewrite

rules

A

slight

modification

of

the

evaluation

model

is

then

necessary

instead

of

modeling

first

order

function

definitions

with

-LRB-

higher

order

-RRB-

substitutions

they

can

be

directly

modeled

by

rewrite

rules

For

instance

a

function

definition

expressed

as

ff

xn

eg

will

be

expressed

as

the

rewrite

rule

f

-LRB-

xn

-RRB-

e

This

is

actually

more

in

accordance

with

the

tradition

for

recursive

applicative

program

schemes

see

for

instance

-LSB-

-RSB-

or

-LSB-

-RSB-

All

results

so

far

carry

over

directly

Definition

A

rewrite

rule

-LRB-

s

t

-RRB-

is

flat

if

s

is

of

the

form

f

-LRB-

xn

-RRB-

where

xn

are

variables

Examples

of

flat

rules

are

the

ones

defining

nondeterministic

choice

x

y

x

x

y

y

parallel

if

-LRB-

a

flat

rule

need

not

be

left-linear

-RRB-

if

-LRB-

x

y

y

-RRB-

y

and

any

function

definition

f

-LRB-

xn

-RRB-

e

can

be

seen

as

a

flat

rule

of

the

form

f

-LRB-

xn

-RRB-

e

The

usual

if-then-else-rules

if

-LRB-

true

x

y

-RRB-

x

if

-LRB-

false

x

y

-RRB-

y

are

however

not

flat

since

they

contain

the

constants

true

false

in

positions

where

a

flat

rule

must

have

variables

Theorem

If

R

has

only

flat

rules

then

R

is

correct

on

first

order

terms

Proof

The

proof

follows

by

a

kind

of

parallel

moves

lemma

The

important

observation

is

that

if

a

flat

rule

matches

a

position

in

Pos

-LRB-

t

-RRB-

in

a

term

oet

then

the

rule

must

also

match

the

same

position

in

t.

Using

this

fact

we

will

prove

that

for

every

such

that

oet

R

there

is

a

v

such

that

t

R

v

and

a

such

that

oev

R

and

R

It

then

follows

that

any

normal

form

to

oet

is

also

a

nomal

form

to

some

v

in

oeR?t

The

proof

is

by

induction

over

the

length

n

of

the

derivation

oet

n

We

will

use

a

somewhat

stronger

induction

hypothesis

that

all

sequences

of

reductions

from

taking

place

in

positions

in

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

will

eventually

result

in

a

term

such

that

oev

R

for

some

normal

form

v

to

t

We

write

-LRB-

pn

-RRB-

to

state

that

a

sequence

of

reductions

successively

taking

place

in

pn

takes

into

We

also

write

pR

if

a

reduction

at

p

takes

into

-LRB-

Not

to

be

confused

with

nR

-RRB-

ffl

n

Obviously

true

since

t

R

v

implies

oet

R

oev

where

all

redexes

are

in

the

original

positions

i.e.

in

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

ffl

n

assume

true

for

n

Then

if

oet

nR

the

induction

hypothesis

is

true

for

any

such

that

oet

n

R

R

t.

Pick

some

Then

pR

for

some

position

p

There

are

two

cases

p

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

Then

for

any

and

any

sequence

-LRB-

pn

-RRB-

of

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

redexes

such

that

-LRB-

pn

-RRB-

there

are

m

disjoint

positions

in

that

are

residuals

of

p

in

They

will

all

be

redexes

w.r.t.

the

original

reduction

taking

place

at

p

It

follows

that

there

is

a

term

such

that

mR

and

-LRB-

pn

-RRB-

Since

the

reduction

at

p

did

not

affect

any

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

redex

the

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

sequences

from

are

the

same

as

those

from

-LRB-

This

is

a

parallel

move

-RRB-

p

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

then

for

any

and

sequence

-LRB-

pn

-RRB-

of

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

redexes

such

that

-LRB-

pn

-RRB-

we

have

-LRB-

pn

-RRB-

Thus

all

possible

sequences

-LRB-

pn

-RRB-

of

Res

-LRB-

Pos

-LRB-

t

-RRB-

-RRB-

reductions

from

must

end

in

some

with

oev

R

-LRB-

This

is

a

one

step

closer

move

-RRB-

Corollary

C

defined

by

x

y

C

x

x

y

C

y

is

correct

-LRB-

Nondeterministic

choice

-RRB-

From

the

proof

of

theorem

it

is

easy

to

see

why

the

restriction

to

first

order

terms

is

important

It

is

crucial

for

the

correctness

that

if

a

flat

rule

matches

a

term

oee

then

it

also

matches

e

But

if

higher

order

terms

are

allowed

then

there

R

-LRB-

pn

-RRB-

fflffl

-LRB-

pn

-RRB-

fflffl

R

m

R

MMMMMMMMMMM

-LRB-

pn

-RRB-

fflffl

-LRB-

pn

-RRB-

xxqqqqqqqqqqq

Figure

A

parallel

move

and

a

one

step

closer

move

are

terms

t

x

-LRB-

tn

-RRB-

where

x

is

a

free

-LRB-

higher

order

-RRB-

variable

Consider

a

flat

rule

-LRB-

s

-RRB-

with

s

f

-LRB-

xn

-RRB-

-LRB-

s

-RRB-

now

matches

fx

fgt

f

-LRB-

tn

-RRB-

but

not

t.

Let

us

now

turn

to

the

the

question

of

compositionality

if

and

are

correct

when

will

-LSB-

be

correct

Apparently

not

always

as

shown

by

the

following

simple

counterexample

Let

given

by

f

-LRB-

x

-RRB-

R

and

let

be

given

by

f

-LRB-

g

-LRB-

x

-RRB-

-RRB-

R

Then

both

and

are

correct

since

each

one

is

by

itself

confluent

-LRB-

left-linear

and

non-overlapping

see

-LSB-

-RSB-

-RRB-

But

as

we

saw

before

-LSB-

is

not

correct

Lemma

If

for

two

term

rewriting

systems

and

holds

that

ffl

and

are

left-linear

and

non-overlapping

ffl

for

all

rules

-LRB-

s

-RRB-

and

-LRB-

q

-RRB-

contains

no

function

symbol

in

q

and

ffl

is

flat

then

-LSB-

ffi

For

any

u

there

is

a

-LRB-

-LSB-

-RRB-

nf

v

such

that

u

v.

Proof

To

prove

first

consider

three

terms

e

where

e

For

the

rules

-LRB-

q

-RRB-

q

is

of

the

form

f

-LRB-

xn

-RRB-

No

reduction

in

can

introduce

any

occurrence

of

f

thus

any

position

of

a

in

is

a

residual

of

a

position

p

in

e

where

e

=p

is

of

the

form

f

-LRB-

en

-RRB-

But

then

e

=p

is

a

Since

and

are

left-linear

and

non-overlapping

the

parallel

moves

lemma

can

be

applied

the

origins

in

e

of

in

can

be

reduced

first

followed

by

reductions

of

the

residuals

from

e

of

to

produce

It

follows

that

e

Now

let

t

-LSB-

Then

there

are

terms

un

such

that

t

un

A

repeated

application

of

the

result

above

now

yields

t

ffi

To

prove

consider

any

u.

Consider

any

v

such

that

u

v

Then

any

in

v

must

be

a

residual

of

some

in

u

But

u

is

a

thus

v

is

also

a

and

thus

a

-LRB-

-LSB-

-RRB-

nf

oet

oeu

oev

ffl

ffl

oe

ffl

ffl

ffl

ffl

ffl

ffl

ffl

ffl

oe

ffl

ffl

ffl

ffl

ffl

ffl

oe

Figure

Illustration

of

the

proof

of

theorem

Theorem

If

for

two

term

rewriting

systems

and

holds

that

ffl

is

confluent

ffl

is

correct

ffl

and

commute

ffl

-LSB-

ffi

and

ffl

For

any

u

there

is

a

-LRB-

-LSB-

-RRB-

nf

v

such

that

u

v

then

-LSB-

is

correct

Proof

Consider

some

term

t

and

any

u

where

t

u

Then

there

is

a

-LRB-

-LSB-

-RRB-

nf

v

such

that

u

v

We

thus

have

t

-LSB-

v

We

also

have

oet

oeu

oev

Consider

now

any

where

oet

-LSB-

Then

there

is

a

such

that

oet

Since

is

correct

there

is

a

such

that

and

oeu

We

thus

have

oeu

and

oeu

oev

and

commute

thus

there

is

a

such

that

and

oev

Now

we

have

the

situation

that

i.e.

-LSB-

and

Confluence

of

and

commutation

with

yields

commutation

with

-LSB-

thus

there

is

a

such

that

-LSB-

and

oev

-LRB-

or

oev

-LSB-

-RRB-

The

existence

of

such

an

element

proves

the

correctness

of

-LSB-

Corollary

If

for

two

term

rewriting

systems

and

holds

that

ffl

is

confluent

ffl

is

flat

ffl

and

are

left-linear

and

non-overlapping

and

ffl

for

all

rules

-LRB-

s

-RRB-

and

-LRB-

q

-RRB-

contains

no

function

symbol

in

q

then

-LSB-

is

correct

for

first

order

terms

Example

From

the

above

we

can

deduce

that

any

first

order

pure

functional

language

where

no

operators

are

defined

by

non-left-linear

rules

can

be

extended

with

nondeterministic

operations

defined

by

flat

rules

in

a

way

that

makes

it

possible

to

perform

semantics-preserving

unfolding

Especially

nondeterministic

choice

can

be

added

Conclusion

and

Further

Research

We

have

extended

results

regarding

unfolding

of

purely

functional

programs

to

programs

with

nondeterminism

The

reduction

system

that

gives

semantics

to

the

operators

must

be

factored

into

a

confluent

and

a

possibly

nonconfluent

part

where

the

confluent

part

then

gives

semantics

to

the

deterministic

operators

and

the

nonconfluent

part

gives

semantics

to

the

nondeterministic

operators

Under

certain

conditions

on

the

reduction

system

correct

unfolding

can

be

done

with

regard

to

the

confluent

part

Termination

results

for

unfolding

of

purely

functional

programs

then

carry

over

directly

It

should

be

observed

however

that

for

programs

whose

termination

properties

depend

on

reduction

of

nonconfluent

rules

-LRB-

execution

of

nondeterministic

operators

-RRB-

unfolding

where

simplification

is

restricted

to

confluent

rules

can

not

be

expected

to

terminate

even

when

the

termination

criteria

says

so

We

pay

certain

attention

to

the

issue

of

semantics-preservation

of

unfolding

The

semantics

of

two

nondeterministic

programs

are

considered

equivalent

if

the

sets

of

possible

results

are

equal

We

restrict

the

scope

to

programs

where

all

possible

executions

terminate

Under

these

conditions

we

give

some

general

conditions

on

the

reduction

system

that

ensures

that

unfolding

preserves

the

semantics

There

are

two

conditions

first

the

confluent

and

nonconfluent

part

of

the

system

must

commute

Second

the

whole

reduction

system

must

be

correct

which

essentially

means

that

substitution

can

not

create

any

new

nonconfluent

redexes

in

a

term

through

overlapping

beteen

the

term

and

the

substitution

The

conditions

are

shown

to

be

met

by

two

kinds

of

systems

confluent

systems

in

general

and

flat

term

rewriting

systems

on

first

order

terms

The

latter

can

be

nonconfluent

pure

nondeterministic

choice

for

instance

can

be

defined

by

flat

rules

We

also

give

conditions

for

when

a

confluent

term

rewriting

system

and

a

flat

term

rewriting

system

can

be

combined

into

a

system

that

is

correct

These

conditions

essentially

boil

down

to

orthogonality

and

that

the

confluent

part

is

not

allowed

to

introduce

any

nondeterministic

operators

If

the

reduction

system

is

not

correct

then

it

seems

like

optimizing

program

transformations

if

they

are

to

preserve

the

semantics

must

be

restricted

to

parts

of

expressions

that

are

known

never

to

affect

any

nondeterministic

choice

of

any

kind

Therefore

establishing

correctness

is

important

from

an

implementation

point

of

view

The

concept

of

semantics-preservation

can

be

formulated

for

transformations

on

expressions

in

general

Thus

the

possible

preservation

of

semantics

can

be

assessed

for

other

source-to-source

optimizing

transformations

than

unfolding

If

terms

with

explicit

substitutions

-LSB-

-RSB-

are

introduced

for

instance

then

transformations

that

yield

sharing

of

subexpressions

can

be

expressed

as

a

term

rewrite

system

with

non-left-linear

rules

E.g.

the

non-left-linear

rule

f

-LRB-

x

x

-RRB-

f

-LRB-

y

y

-RRB-

-LSB-

-LSB-

y

x

-RSB-

-RSB-

replaces

common

arguments

to

f

with

a

single

copy

-LRB-

Here

-LSB-

-LSB-

y

x

-RSB-

-RSB-

is

an

explicit

representation

of

a

substitution

where

y

is

replaced

by

x

This

should

be

considered

a

binding

of

y

so

y

is

bound

in

f

-LRB-

y

y

-RRB-

-LSB-

-LSB-

y

x

-RSB-

-RSB-

and

no

free

variables

are

thus

introduced

-RRB-

It

then

immediately

follows

that

sharing

of

subexpressions

can

give

incorrect

results

since

these

rules

will

in

general

not

commute

with

the

nonconfluent

rewrite

rules

We

would

like

to

point

out

the

relationship

with

previous

work

on

operational

semantics

for

nondeterministic

programs

-LSB-

-RSB-

Proposition

in

-LSB-

-RSB-

is

very

much

the

same

as

our

corollary

in

the

special

case

when

a

deterministic

language

is

extended

with

pure

nondeterministic

choice

On

the

other

hand

their

result

is

with

regard

to

a

more

developed

semantics

that

takes

infinite

computations

into

account

Boudol

-LSB-

-RSB-

points

out

that

in

for

computations

defined

by

term

rewriting

systems

in

general

the

full

substitution

evaluation

rule

is

not

correct

i.e.

other

evaluation

orders

can

sometimes

produce

results

that

the

full

substitution

evaluation

rule

can

not

This

is

related

to

our

observation

that

symbolic

unfolding

of

a

program

sometimes

can

increase

the

number

of

possible

outcomes

Conversely

we

conjecture

that

our

correctness

conditions

on

term

reduction

systems

are

sufficient

to

guarantee

correctness

-LRB-

in

the

sense

of

Boudol

-RRB-

of

the

full

substitution

evaluation

rule

It

would

be

practically

very

interesting

to

extend

the

results

here

to

programs

with

processes

In

order

to

do

this

several

hurdles

must

be

overcome

however

For

instance

the

operational

semantics

must

be

extended

to

deal

both

with

infinite

computations

and

with

infinitely

many

possible

outcomes

It

is

also

not

clear

at

all

that

simple

reduction

systems

on

terms

as

considered

here

will

suffice

to

give

semantics

to

a

nontrivial

class

of

process

languages

Possibly

other

notions

of

rewriting

-LRB-

e.g.

conditional

or

modulo

equivalence

relations

-RRB-

can

be

useful

and

it

would

then

be

interesting

to

extend

the

results

here

to

such

systems

Again

this

is

a

prime

subject

for

future

research

Acknowledgements

This

research

was

supported

by

the

ESPRIT

BRA

project

CONFER

project

no.

References

-LSB-

-RSB-

M.

Abadi

L.

Cardelli

P.-L

Curien

and

J.-J

L?evy

Explicit

substitutions

In

Proc

Seventh

Annual

ACM

Symposium

on

Principles

of

Programming

Languages

pages

-LCB-

San

Francisco

Jan.

-LSB-

-RSB-

A.

Arnold

P.

Naudin

and

M.

Nivat

On

semantics

of

nondeterministic

recursive

program

schemes

In

M.

Nivat

and

J.

C.

Reynolds

editors

Algebraic

Methods

in

Semantics

chapter

pages

-LCB-

Cambridge

University

Press

-LSB-

-RSB-

H.

P.

Barendregt

The

Lambda

Calculus

-LCB-

Its

Syntax

and

Semantics

volume

of

Studies

in

Logic

and

the

Foundations

of

Mathematics

North-Holland

Amsterdam

-LSB-

-RSB-

G.

Berry

and

J.-J

L?evy

Minimal

and

optimal

computations

of

recursive

programs

J.

Assoc.

Comput

Mach.

-LRB-

-RRB-

-LCB-

Jan.

-LSB-

-RSB-

G.

Boudol

Computational

semantics

of

term

rewriting

systems

In

M.

Nivat

and

J.

C.

Reynolds

editors

Algebraic

Methods

in

Semantics

chapter

pages

-LCB-

Cambridge

University

Press

-LSB-

-RSB-

R.

M.

Burstall

and

J.

Darlington

A

transformation

system

for

developing

recursive

programs

J.

Assoc.

Comput

Mach.

-LRB-

-RRB-

-LCB-

Jan.

-LSB-

-RSB-

J.

M.

Cadiou

Recursive

Definitions

of

Partial

Functions

and

their

Computation

PhD

thesis

Stanford

University

Stanford

California

-LSB-

-RSB-

B.

Courcelle

Recursive

applicative

program

schemes

In

J.

van

Leeuwen

editor

Handbook

of

Theoretical

Computer

Science

chapter

pages

-LCB-

Elsevier

Science

Publishers

B.

V.

-LSB-

-RSB-

J.

Darlington

and

R.

M.

Burstall

A

system

which

automatically

improves

programs

Acta

Inform.

-LRB-

-RRB-

-LCB-

-LSB-

-RSB-

G.

Huet

Confluent

reductions

Abstract

properties

and

applications

to

term

rewriting

systems

J.

Assoc.

Comput

Mach.

-LRB-

-RRB-

-LCB-

Oct.

-LSB-

-RSB-

N.

D.

Jones

C.

K.

Gomard

and

P.

Sestoft

Partial

Evaluation

and

Automatic

Program

Generation

Prentice

Hall

Hertfordshire

UK

-LSB-

-RSB-

J.

W.

Klop

Term

rewriting

systems

In

S.

Abramsky

D.

M.

Gabbay

and

T.

S.

E.

Maibaum

editors

Handbook

of

Logic

in

Computer

Science

vol

chapter

pages

-LCB-

Oxford

University

Press

Oxford

-LSB-

-RSB-

B.

Lisper

Total

unfolding

Theory

and

applications

Accepted

for

publication

in

Journal

of

Functional

Programming

-LSB-

-RSB-

J.-C

Raoult

and

J.

Vuillemin

Operational

and

semantic

equivalence

between

recursive

programs

J.

Assoc.

Comput

Mach.

-LRB-

-RRB-

-LCB-

Oct.

-LSB-

-RSB-

B.

K.

Rosen

Tree-manipulating

systems

and

Church-Rosser

theorems

J.

Assoc.

Comput

Mach.

-LRB-

-RRB-

-LCB-

Jan.

nondeterminism
recursive programs
term rewriting
unfolding