Proceedings of the FREENIX Track: 2002 USENIX Annual Technical Conference
Total Page:16
File Type:pdf, Size:1020Kb
USENIX Association Proceedings of the FREENIX Track: 2002 USENIX Annual Technical Conference Monterey, California, USA June 10-15, 2002 THE ADVANCED COMPUTING SYSTEMS ASSOCIATION © 2002 by The USENIX Association All Rights Reserved For more information about the USENIX Association: Phone: 1 510 528 8649 FAX: 1 510 548 5738 Email: [email protected] WWW: http://www.usenix.org Rights to individual papers remain with the author or the author's employer. Permission is granted for noncommercial reproduction of the work for educational or research purposes. This copyright notice must be included in the reproduced paper. USENIX acknowledges all trademarks herein. Biglook: a Widget Library for the Scheme Programming Language Erick Gallesio Manuel Serrano Universite´ de Nice – Sophia Antipolis Inria Sophia Antipolis 950 route des Colles, B.P. 145 2004 route des Lucioles – B.P. 93 F-06903 Sophia Antipolis, Cedex F-06902 Sophia-Antipolis, Cedex EÖickºGaÐÐe×iÓ@ÙÒiceºfÖ ÅaÒÙeкËeÖÖaÒÓ@iÒÖiaºfÖ Abstract Biglook is an Object Oriented Scheme library for constructing GUIs. It uses classes of a CLOS- like object layer to represent widgets and Scheme closures to handle events. Combining functional and object-oriented programming styles yields an original application programming interface that advocates a strict separation between the implementation of the graphical interfaces and the user-associated commands, enabling compact source code. The Biglook implementation separates the Scheme programming interface and the na- Figure 1: Two Biglook applications: a code tive back-end. This permits different ports browser on the left, “dock” applications on the for Biglook. The current version uses GTK· right and Swing graphical toolkits, while the pre- vious release used Tk. It is available at: hØØÔ:»»kaÓÐiÒºÙÒiceºfÖ»BigÐÓÓk . shots of Biglook applications: i) kbÖÓÛ×e on the left and ii) the Biglook dock applications, ala` NextStep, on the right. These Biglook applica- tions are used on a daily basis. Introduction By contrast to previous work, no attempt has been made to make that library familiar to pro- We have studied the problem of construct- grammers used to imperative or purely object ori- ing GUI in functional languages by designing a ented programming style. On the contrary, our li- widget library for the Scheme programming lan- brary introduces an original application program- guage, called Biglook. In this paper we focus on ming interface (API) that benefits from the high how to apply the functional style to GUIspro- level constructions of the extended Scheme im- gramming. plementation named Bigloo [25] which is open source and freely available since 1992. The main Biglook’s primary use is to implement graph- Bigloo component is an optimizing compiler that ical applications (e.g., ÜÐÓad, editors ala` delivers small and efficient applications for the Emacs, browser ala` Netscape, programming UnixTM operating system. Bigloo is able to pro- tools such as kbÖÓÛ×e, our graphical Scheme duce native code (via C) and JVM bytecode. Cur- code browser). Figure 1 presents two screen rently Biglook uses GTK· [20] associated with the Bigloo C back-end and Swing [29] with the 1 Bigloo Bigloo JVM back-end. The previous release of Biglook [12] used Tk [19]. Bigloo is an open implementation of the Bigloo implements an object layer inspired Scheme programming language. From the be- by CLOS [1]. It is a class-based model where ginning, the aim was to propose a realistic and methods override generic functions and are not pragmatic alternative to the strict Scheme [17]. declared inside classes as in Smalltalk [13], Bigloo does not implement “all” of Scheme; for O’Caml [22] or Java [14]. example, the execution of tail-recursion may al- locate memory. On the other hand, Bigloo imple- Biglook is implemented as a wrapping layer ments numerous extensions: support for lexical on top of native widget libraries (that we name and syntactic analysis, pattern matching, an ex- henceforth the back-end). This software architec- ception mechanism, a foreign interface, an object ture saves the effort of implementing low-level layer and a module language. In this Section, we constructions (pixel switching, clipping, event present Bigloo’s modules and its object model; handling and so on) allowing to focus on the that is, class declarations and generic functions. Scheme implementation of new features. Virtual slots, which are heavily used in Biglook, are presented in Section 2. When designing the Biglook API,wealways had to decide which model to choose: the func- tional model or the object model. We think that 1.1 Modules these two models are not contradictory but com- plementary. For instance, if the widget hierarchy naturally fits a class hierarchy, user call-backs are Bigloo modules have two basic purposes: one naturally implemented by the means of Scheme is to allow separate compilation and the second is closures. to increase the number of errors that can be de- tected by the compiler. Bigloo modules are sim- In Section 1 we briefly present the Bigloo sys- ple and they have been designed with the concern tem emphasizing its module system and its object of an easy implementation. layer. This section is required for readers unfa- miliar with the CLOS model and it also serves as A module is a compilation unit for Bigloo. It an introduction to virtual slots. Virtual slots are is represented by one or more files and has the a new Bigloo construction that is required in or- following syntax: der to separate the Biglook API made of classes and the native back-end. They are presented in ´ÑÓdÙÐe module-name · £ ´iÑÔÓÖØ Section 2. In Section 3 we present the Biglook import µ · £ µ library. We start showing a simple Biglook ap- ´eÜÔÓÖØ export · £ µ plication and its associated source code. Then, ´×ØaØic static · £ µ ´ÐibÖaÖÝ static we detail the Biglook programming principles µ (widgets creation, event handling, etc.) motivat- opt-body ing our design orientations by programming lan- guage considerations. In this section we are con- Import clauses are used to import bindings in the ducted to compare the functional programming module. In order to import, one just needs to state style and the object oriented one. In Section 4 we the identifier to be imported and its source mod- present the Biglook implementation. At last, in ule. Note that a shorthand exists to import all the Section 5 we present a comparison with related bindings of a module. work. Export and static clauses play a close role. They point out to the compiler that the module implements some bindings and distinguish those that can be used within other modules (they are exported) and those that cannot (they are static). These clauses do not contain identifiers but proto- types. It is then possible to export variables (mu- ÑÓdÙÐe¹ÔÓiÒØ× table bindings) or functions (read-only bindings). ´ÑÓdÙÐe ´cÐa×× ÔÓiÒØ Static clauses are optional (the bindings of a mod- ´eÜÔÓÖØ ´ÔÓiÒعiÒiص ´defaÙÐØ ¼º¼µµ ule which are not referenced in a clause are, by ´Ü::dÓÙbÐe ´defaÙÐØ ¼º¼µµµ default, static). ´Ý::dÓÙbÐe ´cÐa×× ÔÓiÒع¿d::ÔÓiÒØ ´defaÙÐØ ¼º¼µµµµµ Library clauses enable programs to use Bigloo ´Þ::dÓÙbÐe ÔÓiÒعiÒiØ libraries. A Bigloo library is merely a collec- ´defiÒe ´´cÓÙÒØ ¼µµ tion of pre-compiled modules. Using a library is ´ÐeØ ´Óbj::ÔÓiÒص equivalent to importing all the modules compos- ´ÐaÑbda cÓÙÒØ ´· ½ cÓÙÒصµ ing the library. Detailed information on Bigloo ´×eØ! ´ÔÖiÒØ "# Óf ÔÓiÒØ×: " cÓÙÒصµµµ modules may be found in the two papers [25, 26]. 1.2.2 Instances 1.2 Object layer When declaring a class cÐa, Bigloo automat- ically generates the predicate cÐa?, an allo- cÐa cator iÒ×ØaÒØiaØe:: , an instance cloner In this paper we assume a CLOS-like object cÐa cÐa¹Ü dÙÔÐicaØe:: , accessors (e.g., for model with single inheritance and single dis- cÐa¹Ü¹×eØ! aslotÜ), modifiers (e.g., for a patch. The object layer implemented in Bigloo slot Ü) and an abbreviated special access form, is a restricted version of CLOS [1] inspired, to a cÐa ÛiØh¹acce××:: , to allow accessing and great extent, by MEROON [21]. writing slots simply by using their name. Here is how to allocate and access an instance of ÔÓiÒع¿d : ´ÐeØ ´´Ô ´iÒ×ØaÒØiaØe::ÔÓiÒع¿d ¹¿º4µ 1.2.1 Class declarations ´Ý ´Ü ½º¼µµµµ ;; The initialization value of a slot can be omitted ;; from the arguments list if it has a default value; ÔÓiÒع¿d Classes can be declared static or exported. It ;; this is the case for the slot Þ of class . Ô ´Ü Ý Þµ is then possible to make a declaration accessible ´ÛiØh¹acce××::ÔÓiÒع¿d ´· ´×ÕÖ Üµ ´×ÕÖ Ýµ ´×ÕÖ Þµµµµµ from another module or to limit its scope to one ´×ÕÖØ module. The abbreviated syntax of a class decla- ÛiØh¹acce×× ration is: The iÒ×ØaÒØiaØe and special forms are implemented by the means of macros that statically resolve the keyword parameters :: µ ´cÐa×× class-id super-class-id opt-init opt-slots Ý Þ (such as Ü, and ). For instance, the above ex- ample is expanded into: A class can inherit from a single super class. ´´Ô ´Ñake¹ÔÓiÒع¿d ½º¼ ¹¿º4 ¼º¼µµµ Classes with no specified super class inherit from ´ÐeØ ´×ÕÖØ ´· ´×ÕÖ ´ÔÓiÒع¿d¹Ü Ôµµ ÓbjecØ ´ÔÓiÒع¿d¹Ý Ôµµ the class. The type associated with a sub- ´×ÕÖ ´ÔÓiÒع¿d¹Þ Ôµµµµµ class is a subtype of the type of the super class. ´×ÕÖ A class may be provided with an initialization As we can see, since macros are expanded at function (opt-init) that is automatically called compile-time, there is no run-time penalty asso- each time an instance of class-id is created. Ini- ciated with keyword parameters. tialization functions accept one argument, the created instance.