On the Expressive Power of Programming Languages for Generative Design
Total Page:16
File Type:pdf, Size:1020Kb
On the Expressive Power of Programming Languages for Generative Design The Case of Higher-Order Functions António Leitão1, Sara Proença2 1,2INESC-ID, Instituto Superior Técnico, Universidade de Lisboa 1,2{antonio.menezes.leitao|sara.proenca}@ist.utl.pt The expressive power of a language measures the breadth of ideas that can be described in that language and is strongly dependent on the constructs provided by the language. In the programming language area, one of the constructs that increases the expressive power is the concept of higher-order function (HOF). A HOF is a function that accepts functions as arguments and/or returns functions as results. HOF can drastically simplify the programming activity, reducing the development effort, and allowing for more adaptable programs. In this paper we explain the concept of HOFs and its use for Generative Design. We then compare the support for HOF in the most used programming languages in the GD field and we discuss the pedagogy of HOFs. Keywords: Generative Design, Higher-Order Functions, Programming Languages INTRODUCTION ming language is Turing-complete, meaning that Generative Design (GD) involves the use of algo- almost all programming languages have the same rithms that compute designs (McCormack 2004, Ter- computational power. In what regards their expres- dizis 2003). In order to take advantage of comput- sive power, however, they can be very different. A ers, these algorithms must be implemented in a pro- given programming language can be textual, visual, gramming language. There are two important con- or both but, in any case, it must be expressive enough cepts concerning programming languages: compu- to allow the description of a large range of algo- tational power, which measures the complexity of rithms. the problems that can be described using the lan- Expressiveness is intuitively used to measure guage, and expressive power, which measures the how easy it is for a programming language to im- breadth of ideas that can be described using the lan- plement complex ideas. There are studies (Felleisen, guage. The expressive power is directly related to 1991) that showed that it is possible to rank lan- the human effort needed to describe those ideas in guages according to a formal definition of expres- a given programming language. sive power that captures the intuitive meaning of It is a known fact that any non-trivial program- the term. In this ranking, a language that supports, Design Tool 1 - Volume 1 - eCAADe 32 | 257 for example, user-defined functions, (e.g., FORTRAN), summation: is more expressive than one that does not supports X10 them (e.g., BASIC). This does not mean that there is a i2 = 1 + 4 + 9 + 16 + ::: + 81 + 100 (1) problem solvable in FORTRAN that cannot be solved i=1 in BASIC. What it does mean is that there are prob- Although rarely presented as such, summation is a lems that require larger implementation efforts from HOF: it accepts a function (in the previous example, a BASIC programmer than from a FORTRAN program- disguised as the expression i2) and the limits of a mer. numeric sequence (in the example, 1 and 10), and In this paper, we claim that higher-order func- computes the sum of the applications of the func- tions (HOFs) are an important and powerful program- tion to all the elements of the sequence. Using the ming language feature for GD. HOFs increase the ex- appropriateP mathematical notation, the example be- pressiveness of a language, reducing the program- 10 ! 2 comes i=1(i i ), where it is now obvious that mer's effort and improving code reusability. the summation function accepts another function as In the next sections we describe the expressive argument. This is also visible in the formal definition power allowed by HOFs, we illustrate the advantages of the summation operation: of its use in GD, and we analyse the level of support ( provided by the programming languages currently Xb 0 a > b being used for GD. Finally, we describe a pedagogic f = P (2) f(a) + b f a ≤ b approach for teaching HOFs in the realm of GD prob- a a+1 lems. Note, in the above definition, that the parameter f is used as a function, in f(a). HIGHER-ORDER FUNCTIONS Although ubiquitous in many branches of math- A HOF is a function that accepts functions as argu- ematics, HOFs are unjustly considered a complex ments and/or computes functions as results. As an topic that, as a result, is frequently not taught prop- example, consider the derivative operator D(f), that erly and is not fully supported in a large number of ! 2 accepts a function as argument, such as x x + programming languages. However, as we will show, 3x, and computes another function as result, in this HOFs are an important tool for GD: they dramatically ! case, x 2x + 3. This fact can be written as simplify scripting (McCullough, 2006) and they are ! 2 ! D(x x + 3x) = x 2x + 3. Note that the a very convenient representation for parameterized functions that we used as argument and result of geometry (Park and Holt, 2010). Programming using the derivative operator did not have a name. These HOFs is known as higher-order programming. unnamed function are known as anonymous func- As an example, consider a balcony in a build- tions and there is an alternative notation for them: ing. During the (generative) design process, a de- λx.2x + 3. This notation was proposed by the λ- signer might be more concerned about the shape Calculus (Barendregt 1984) and, as we will see, it is of the building than about the shape of the balcony adopted by several programming languages. Note, and, as a result, he might decide to implement a sim- however, that from the point of view of the deriva- plified balcony. This means that the formalization tive operator, there is no difference between anony- of the design in a programming language includes mous functions and named ones. For example, it is a parametrized definition for the building that de- well-known that the derivative of the sine function is pends on the parametrized definition of the balcony, the cosine function, a fact that we can express using as follows: named functions, namely: D(sin) = cos. For a different example, consider a typical balcony(...) = ... 258 | eCAADe 32 - Design Tool 1 - Volume 1 building(...) = facade, etc.), functions describing sequences of ele- ... ments, etc. This representation allows us to gener- wall(...) ate not only a building that is identical to the original ... concept, but also an infinite number of variations, ex- slab(...) pressing different choices for all the function param- ... eters, including those that represent other functions. balcony(...) ... HIGHER-ORDER FUNCTIONS IN GENERA- Later, when the designer turns his attention to the balcony, he might want to experiment several differ- TIVE DESIGN ent designs. In less expressive languages, his only op- In spite of the simplicity of the idea of HOFs, a (gen- tion is to redefine the balcony definition for each ex- erative) designer still needs to think about the best periment. In more expressive languages that support strategy for its use, which is strongly dependent on HOFs, a better solution is to transform the balcony the current design goals. Returning to the previous into a parameter of the building definition, which example, it is clear that it is not enough to say that then becomes a HOF, as follows: the function that creates the balcony is a functional parameter of the function that creates the building; building(...,balcony,...) = we also need to specify the communication protocol ... between both functions, namely, which information wall(...) is expected by the balcony function and which infor- ... slab(...) mation does it return to the building function. De- ... pending on the programming language being used, balcony(...) the designer might decide to provide part or the en- ... tirety of the expected information as arguments to the function. Additional information might be pro- It is now possible to simultaneously define different vided in global variables or using other language- balcony functions expressing different balcony de- specific mechanisms. For illustration purposes, we signs and treat them as plug-ins for the function that will consider that the balcony function expects the represents the building. spatial location of a corner of the balcony and the di- Although it might seem that there is little differ- mensions of the intended balcony. We will assume ence between these two approaches, only the sec- that the building function provides this information ond approach allows the designer to easily create a each time it calls the balcony function. building containing different balcony designs. More- Regarding the design of the balcony, the de- over, both the function that represents the building signer might want to experiment different mathe- as well as the functions that represent the balconies matical functions for its shape. This means that it are now less dependent from each other and can be should be possible to define another (higher-order) reused in different contexts. function that accepts the shape function and that In this paper, we claim that, besides balconies, computes a specialized balcony function that follows many other aspects of a design can be transformed that shape. We will now assume the existence of into (functional) parameters of HOFs. We illustrate this function and we will only consider the different our claim by describing the design of a complex shape functions that the designer might want to ex- building, more specifically, the Market Hall (Boranyak periment. 2010), represented by functions that accept, as argu- One possibility is the use of a sinusoidal shape. ments, functions describing the overall shape of the To this end, the designer might define a function- building, functions for the different elements (posts, Design Tool 1 - Volume 1 - eCAADe 32 | 259 Figure 1 Balconies generated using higher-order functions.