A Special-Purpose Language for Picture-Drawing
Total Page:16
File Type:pdf, Size:1020Kb
The following paper was originally published in the Proceedings of the Conference on Domain-Specific Languages Santa Barbara, California, October 1997 A Special-Purpose Language for Picture-Drawing Samuel N. Kamin and David Hyatt University of Illinois, Urbana-Champaign For more information about USENIX Association contact: 1. Phone: 510 528-8649 2. FAX: 510 548-5738 3. Email: [email protected] 4. WWW URL:http://www.usenix.org A Sp ecial-Purp ose Language for Picture-Drawing y Samuel N. Kamin David Hyatt Computer Science Department University of Il linois at Urbana-Champaign Urbana, Il linois 61801 fs-kamin,[email protected] Abstract supp orted in programming languages. This supp ort means allowing the creation of \ rst-class" values of eachtyp e, that is, values not sub ject to arbitrary Special purpose languages are typical ly characterized restrictions based on the typ e. It also means pro- by a type of primitive data and domain-speci c oper- viding op erations appropriate to those typ es in a ations on this data. One approach to special purpose concise, non-bureaucratic form. language design is to embed the data and operations In our view, this approach to language design is p er- of the language within an existing functional lan- guage. The data can be de ned using the typecon- fectly suited to the design of sp ecial-purp ose lan- structions provided by the functional language, and guages. These languages are usually characterized the special purpose language then inherits al l of the byatyp e of primitive data sp eci c to a problem domain, and op erations on those data. These data features of the more general language. In this paper can b e incorp orated into a language having the typ e we outline a domain-speci c language, FPIC, for the representation of two-dimensional pictures. The constructions just mentioned. In fact, they can b e primitive data and operations are de ned in ML. We incorp orated into an existing functional language; outline the operations provided by the language, il- the typ e constructions will apply to the new data, lustrate the power of the language with examples, and the entire language will then b ecome a sp ecial- purp ose language, with its many other features in- and discuss the design process. cluded \for free." The principal weakness of many sp ecial-purp ose languages is that, b eyond a concise and natural 1 Intro duction syntax, and ecient implementation, for the val- ues and op erations sp eci c to the domain, over- FPIC is a sp ecial-purp ose language for drawing sim- arching language structure is weak. This weak- ple pictures. It was built by de ning typ es and ness would b e very signi cantly mitigated if sp ecial- functions in the functional language Standard ML purp ose languages were routinely designed|or at [6]. This metho d of construction is easy and results least prototyp ed|in the waywehave outlined. in a language with many useful features. In addi- This pap er is a case study of the design of FPIC ac- tion to b eing concise for small examples, FPIC is cording to this philosophy.We describ e the pro cess powerful enough to allow the programming of large by whichwe decided what the primitive data were programs and program libraries, an area in which and how they should b ehave, then describ e the lan- many sp ecial-purp ose languages are weak. guage itself with numerous examples. Our emphasis Functional programming has b een characterized throughout is on the advantages obtained byhaving in manyways. Our view is that it repre- the functional language sup erstructure of Standard sents an approach to language design. This ap- ML as part of FPIC. proach holds that some mathematical constructs| pro ducts, functions, disjoint unions, and others| are fundamental in computing and should b e well Partially supp orted by NSF Grant CCR 96{19655. y Current address: Netscap e Communications Corp., Mountain View, CA, hyatt@netscap e.com. 2 Simple FPIC Examples PIC [4] is a language for drawing simple pictures, such as trees and blo ck diagrams. It has primi- tives for drawing b oxes, circles, and other shap es, val nose = dtriangle; with or without lab els, and for drawing lines and circle 2.5 arrows b etween them. It also has a facility for nam- seq nose at 2.0,2.0 ing p oints on pictures, to b e used, for example, as seq nose rotate ~90.0 scale 0.7 at 1.2,2.7 the endp oints of lines and arrows. These constructs seq nose rotate 90.0 scale 0.7 at 3.1,2.7 seq doval scaleXY 0.5,0.3 at 1.7,0.7; are provided in a concise syntax, with a simple lan- guage structure including lo ops added on. The seq op eration simply places pictures on top of FPIC was inspired by PIC. Our goal was to demon- one another, without moving them either rightor strate that we could b ene t by following the lan- down. The expression pic at point draws the picture guage design philosophy outlined in the intro duc- pic with its reference p oint the lower-left corner at tion. That is, by using essentially the same primi- point. tive data typ es and op erations as in PIC, but em- An imp ortant feature of PIC, whichwehave b edding them in a functional language, we could obtain a far more p owerful language than PIC and adopted in FPIC, is the ability to name p oints in do so at a far lower cost than if we had built the a picture and subsequently refer to them. The com- language from scratch. pass p oints|s for south, ne for northeast, and so on, plus c for center|are automatically de ned for In this section, we present a few examples to show every picture. This allows us to eliminate some of the principal primitive op erations of FPIC, making the guesswork in the previous example: only minimal use of the programming features of Standard ML. Section 5 gives many more examples, emphasizing the utility of the features of ML in com- val face = circle 2.5; bination with the FPIC primitives. val facecenter = face pt "c"; val lefteye = nose rotate ~90.0 scale 0.7; The most basic primitives are those for drawing sim- val righteye = nose rotate 90.0 scale 0.7; 1 val mouth = doval scaleXY 0.5,0.3; ple shap es and placing them next to one another: face seq nose centeredAt facecenter seq lefteye centeredAt facecenter -- 1.0,~0.7 seq righteye centeredAt facecenter ++ 1.0,0.7 seq mouth centeredAt facecenter -- 0.0,1.5; Named p oints can b e added to a picture. A third Hello! way to pro duce the same \pumpkin face" is to draw the face and name the lo cations of the facial fea- box 1.0 2.0 hseq circle 1.5 vseq tures: label "\\Huge Hello!" oval 2.0 1.0; val face = hseq and vseq represent the op erations of plac- let val f = circle 2.5 ing pictures next to one another, either horizontally val facecenter=fpt"c" or vertically. Note that backslashes inside quotes in namePts f must b e doubled. ["nosepos", facecenter, "lefteyepos", facecenter -- 0.9,~0.8, Pictures can b e moved and otherwise transformed in "righteyepos", facecenter ++ 0.9,0.8, "mouthpos", facecenter -- 0.0,1.5] various ways. In this example, we use ML's name end; de nition facility in the rst line. dtriangle is a face seq nose centeredAt face pt "nosepos" \default triangle;" similarly for doval and dcircle seq lefteye centeredAt face pt "lefteyepos" and dbox later in the pap er. seq righteye centeredAt face pt "righteyepos" seq mouth centeredAt face pt "mouthpos"; 1 App endix A gives a concise overview of ML syntax. Ap- p endix B lists all the FPIC primitives used in the examples in Pictures can b e named as well as p oints. This is this pap er, indicating the typ es of their arguments, whether or not they are in x, and what they return. useful when a numberofpoints on a picture may b e of interest. For example, cell is a \cons cell" of PIC to help answer what we consider the most consisting of twoboxes vertically stacked; for future crucial question in the design pro cess: what do es reference, the individual b oxes are named car and each piece of syntax mean? In ordinary program- cdr, resp ectively: ming terms, syntactic phrases have some intuitive op erational meaning; \circle 2.0" means \drawa circle with radius 2.0." A lesson from denotational semantics is that phrases can b e given precise mean- ings as values of well-de ned sets. It is this notion of \meaning" that we found useful in designing FPIC. We wish to give the reader some idea of the thought pro cess wewent through. val cell = namePic dbox "car" We will call the set in which the meaning of \circle vseq namePic dbox "cdr"; 2.0" resides Picture. The question is, what exactly is in this set? What is a picture? The most obvious In addition to optionally b eing named, subpictures answer is that it is some concrete representation of are automatically numb ered. p nthpic i is the ith a picture, for example, an array of pixels or a list subpicture in p.