Biglook: a Widget Library for the Scheme Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
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 ¢¡¤£¦¥¨§ © ¢ ¦¦£¦£¦¥©¡ ¤ ¦¢© ¦¡¡ ¦£!¢¡£¦ ©¡ 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: #%$&$&')(+*,*-/.,0%1324)5 6,472&8¤9:5<;=3*>72?/10&0- . shots of Biglook applications: i) -&B,=/0¤CD¤9 on the left and ii) the Biglook dock applications, a` la 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 GUIs pro- 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 1&0&.&A ical applications (e.g., @ , editors a` la delivers small and efficient applications for the Emacs, browser a` la Netscape, programming UnixTM operating system. Bigloo is able to pro- tools such as -&B%=%0¤CD¤9 , 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, we always 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 ¢¡¤£¦¥¨§ ©¨ module-name ¡ £¦ Section 2. In Section 3 we present the Biglook import ¦ ¨¨ £¦ library. We start showing a simple Biglook ap- export ¦ ¨ ¦ static plication and its associated source code. Then, © !¨" 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- ¢¡¤£¦¥¨§ ©¨ ¡¤£¦¥¨§ ©¨ £ ¨©¨ £ table bindings) or functions (read-only bindings). ¨¨ £¦ £ ¤ Static clauses are optional (the bindings of a mod- ¥ £§! ©¨ ¥ ¦§ ©¦ ¡¢¡ ule which are not referenced in a clause are, by " ¥ £§! ©¨ ¥ ¦§ ©¦ ¡¢¡ default, static). ¨©¨ £ ¨¥ £ ¡¢¡ ! ¥ £§! ©¨ ¥ ¦§ ©¦ ¡¢¡ Library clauses enable programs to use Bigloo £ ¤ libraries. A Bigloo library is merely a collec- ¥ ©¨ ¨ ¨£§¢ tion of pre-compiled modules. Using a library is ©¨ ¡ !¥ £!" £ ¡¢¡ ¤# ¨£§¢ !$&% ¨£§¢ equivalent to importing all the modules compos- ¦ ¨ (') £* £ ' ¨£§¢ ing the library. Detailed information on Bigloo ¡ modules may be found in the two papers [25, 26]. 1.2.2 Instances 1.2 Object layer When declaring a class +-,. , Bigloo automat- / ically generates the predicate 81&. , an allo- 2¦4D¢$/.4%$2.$%9 (,( cator +-,. , an instance cloner 1/2&8¤.$/9:(,( 81&.1 In this paper we assume a CLOS-like object A6,' @ +-,0. , accessors (e.g., for %D¤9$32 model with single inheritance and single dis- 8¤1&. @ a slot @ ), modifiers (e.g., for a patch. The object layer implemented in Bigloo slot @ ) and an abbreviated special access form, ¤&.38,8¤9/D,D (,( is a restricted version of CLOS [1] inspired, to a C2$&# +4,0. , 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 ©657¦8-¥9 9:¤ : ©¨ ¨ * ¨ £ ¨¥ ¡¢¡ "<; =%9 1.2.1 Class declarations ;; The initialization value of a slot can be omitted ;; from the arguments list if it has a default value; ;; this is the case for the slot of class > ?@BADCFEHG¨I . KJ¤ L0¨ ¦ £ ¨¥ "M Classes can be declared static or exported. It ¡¢¡ FN !$ FN FN " FNO is then possible to make a declaration accessible from another module or to limit its scope to one C2¦$&#P&.38&8¤93D,D module. The abbreviated syntax of a class decla- 2¦4D¢$%.¤4%$2.¤$/9 ration is: The and special forms are implemented by the means of macros ¨©¨ ¡¢¡ that statically resolve the keyword parameters class-id super-class-id opt-init opt-slots Q R (such as @ , and ). For instance, the above ex- ample is expanded into: ¢¡*S £ ¨¥T%9O<;O A class can inherit from a single super class. ©¨ ¨ FN !$ FN £ ¨¥¦ Classes with no specified super class inherit from 0¤B¤£,9/8¢$ FN £ ¨¥¦" the class. The type associated with a sub- FN £ ¨¥* 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. 1.2.3 Generic functions and methods A slot may be typed (with the annotation ¦¥¨§¨© (,( ) and may have a default value ( A%9&;%.¤631¤$ option). Here are some possible decla- Generic function declarations are function decla- 2¦4%$ '30 24%$¤&A ?/9¤439=72&8 rations for the traditional '/0 and : rations annotated by the keyword. They ¥ ¡ ¨L £¦¥ BL £FJ £ £ ¡¢¡ KJ¤ L0¨ ¦ £ £ " can be exported, which means that they can be ¡¢¡ '©¨D' used from other modules and that methods can '"©¨D' " be added to those functions from other mod- ules. They can also be static, that is, not acces- ¥ ¡ ¨L £¦¥ BL £FJ £ £ ¨¥ ¡¢¡ KJ¤ L0¨ ¦ £ ¨¥ £ ! sible from within other modules, hich means that ¡¢¡ ¢F ¨¡ ¨L £¦¥ no other modules can add methods. The CLOS ¦©© ' ©¨D' model fits harmoniously with the traditional func- tional programming style because a function can be thought as a generic function overridden with Hereafter is an example of a call to the D#30¤C exactly one method.