Using TEX's Language Within a Course About Functional
Total Page:16
File Type:pdf, Size:1020Kb
Jean-Michel Hufflen EUROTEX 2009 71 Using TEX’s Language within a Course about Functional Programming Abstract functions’ application, whereas imperative program- We are in charge of a teaching unit, entitled Advanced ming—the paradigm implemented within more ‘tra- Functional Programming, for 4th-year university ditional’ languages, such as C [16]—emphasises students in Computer Science. This unit is optional changes in state. Many universities include courses within the curriculum, so students attending it are about functional programming, examples being re- especially interested in programming. The main ported in [35]. Besides, such languages are some- language studied in this unit is Scheme, but an important part is devoted to general features, e.g., times taught as first programming languages, ac- lexical vs dynamic scoping, limited vs unlimited extent, cording to an approach comparable to [1, 8, 32] in call by value vs call by name or need, etc. As an the case of Scheme. alternative to other programming languages, TEX Let us remark some tools developed as part of allows us to show a language where dynamic and TEX’s galaxy have already met functional program- lexical scoping—\def vs \edef—coexist. In addition, ming: cl-bibtex [18], an extension of BIBTEX— we can show how dynamic scoping allows users to the bibliography processor [26] usually associated customise T X’s behaviour. Other commands related E with the LaTEX word processor [20]—is written us- to strategies are shown, too, e.g., \expandafter, ◦ ANSI2 OMMON ISP xındy \noexpand. More generally, TEX commands are ing C L [7]; , a multilin- related to macros in more classical programming gual index processor for documents written using languages, and we can both emphasise difficulty LaTEX [24, § 11.3] is based on COMMON LISP, too; related to macros and shown non-artificial examples. BIBTEX2HTML [4], a converter from .bib format— So TEX is not our unit’s main part, but provides used by the bibliography database files of BIBTEX— significant help to illustrate some difficult notions. 3 4 5 to HTML , is written in CAML [21]; MlBIBTEX , a re-implementation of BIBTEX focusing on multilin- Keywords gual features [11], is written in Scheme [15]; as an- Functional programming, TEX programming, lexical vs 6 dynamic scope, macros, evaluation strategies. other example, Haskell [28] has been used in [38]; last but not at least, there were proposals for de- veloping NTS7—a re-implementation of TEX—using CLOS8, an object-oriented system based on COM- Introduction MON LISP [39]. The unit we mentioned above is entitled Ad- If we consider programming in TEX [17], we have 9 to admit that this language is old-fashioned, and vanced Functional Programming , it is an optional programs are often viewed as rebuses, as shown unit for 4th-year university students in Computer in the Pearls of T X programming demonstrated at Science, part of the curriculum proposed at the E University of Franche-Comté, at the Faculty of Sci- BachoTEX conferences1. Some interesting applica- tions exemplifying this language can be found in ence and Technics, located at Besançon, in the East [19, 30], but as noticed in [5], ‘some of these pro- of France. Most of these students already know gramming tricks are ingenious and even elegant. a functional programming language: Scheme, be- However [. ] it is time for a change’. cause they attended a unit introducing this lan- guage in the 2nd academic year in Computer Sci- 10 So, at first glance, it may be strange to use some ence . Other students, who attended the first two university years at Belfort, know CAML. So this examples of TEX programming within a present- day course devoted to Functional programming. Let unit is not an initiation course, we actually go thor- us recall that this programming paradigm treats oughly into functional programming. computation as the evaluation of mathematical functions, avoiding state and mutable data as far In next section, we expose the ‘philosophy’ of our as possible. Functional programming emphasises unit. Then we summarise the features of TEX that 72 MAPS 39 Jean-Michel Hufflen (define (factorial x) (defun factorial (x) ;; Returns x! if x is a natural number, the ‘false’ "Behaves like the namesake function in Scheme ;; value otherwise. (cf. Fig. 1)." (and (integer? x) (not (negative? x)) (and (let tr-fact ((counter x) (integerp x) (not (minusp x)) (acc 1)) (labels ((tr-fact (counter acc) ;; Returns acc * counter!. ;; The labels special form of (if (zero? counter) ;; COMMON LISP introduces local acc ;; recursive functions [33, § 7.5]. (tr-fact (- counter 1) (if (zerop counter) (* acc counter)))))) acc (tr-fact (- counter 1) Figure 1. The factorial function, written using Scheme. (* acc counter))))) (tr-fact x 1)))) are useful within this unit and discuss our choice Figure 2. The factorial function in Common Lisp. about TEX. Reading this article only requires ba- sic knowledge of programming, readers who would like to go thoroughly into Scheme constructs we theoretical notions without practice would not be have used throughout our examples can refer to very useful. So, our unit’s first part is devoted to [32], very didactic. Of course, the indisputable ref- the λ-calculus’ bases [10]. Then, all the practical exercises are performed with only one language, erence about TEX commands is [17]. Scheme, most students already know. Besides, this unit ends with some advanced features of this lan- Our unit’s purpose guage: delayed evaluation, continuations, hygienic Functional programming languages have a com- macros [9]. In addition, this choice allows us to 15 mon root as the λ-calculus, a formal system de- perform a demonstration of DSSSL [13], initially 16 veloped in the 1930’s by Alonzo Church to in- designed as the stylesheet language for SGML vestigate function definition, function application, texts. These students attended a unit about XML 17 and recursion [3]. However, these programming and XSLT [36] the year before, and DSSSL—which languages are very diverse, some—e.g., the Lisp may be viewed as XSLT’s ancestor—is based on a dialects11—are dynamically typed12, some—e.g., subset of Scheme, enriched by specialised libraries. Standard ML13 [27], CAML, Haskell—are strongly When we begin to program, the language we typed14 and include a type inference mechanism: are learning is always shown as finite product. It end-users do not have to make precise the types has precise rules, precise semantics, and is consis- of the variables they use, they are inferred by the tent. According to the language used, some ap- type-checker: in practice, end-users have to con- plications may be easy or difficult to implement. ceive a program using a strongly typed approach When you put down a statement, running it often because if the type-checker does not succeed in as- results in something predictible. That hides an im- sociating a type with an expression, this expres- portant point: a language results from some impor- sion is proclaimed incorrect. As examples, Fig. 1 tant choices: does it use lexical or dynamic scop- (resp. 2) show how to program the factorial func- ing, or both? To illustrate this notion with some tion in Scheme (resp. COMMON LISP). In both cases, examples in TEX, that is the difference between the the factorial function we give can be applied to any commands \firstquestion and \secondquestion value, but returns the factorial of this value only if in Fig. 4. The former can be related to lexical scop- it is a non-negative integer, otherwise, the result is ing, because it uses the value associated with the the ‘false’ value. Fig. 3 gives the same function in \state command at definition-time and produces: Standard ML: it can only be applied to an integer, You’re happy, aint’U? as reported by the type-checker (see the line begin- ning with ‘>’). whereas the latter can be related to dynamic scop- A course explaining the general principles of ing, because it uses the value of the \state com- functional programming and an overview of some mand at run-time and yields: existing functional programming languages would You’re afraid, aint’U? be indigestible for most students, since they could difficultly get familiar with several languages, due Students difficultly perceive this notion: some to durations allowed to each unit. In addition, know that they can redefine a variable by means of Using TEX’s Language within a Course about Functional Programming EUROTEX 2009 73 fun factorial x = \def\state{happy} (* If x is a negative integer, the predefined \edef\firstquestion{You’re \state, ain’t U?\par} exception Domain is raised [27, §§ 4.5–4.7]. The \def\secondquestion{You’re \state, ain’t U?\par} internal function tr_fact is defined by means of \def\state{afraid} pattern matching [27, § 4.4]. *) Figure 4. Lexical and dynamic scope within TEX. if x < 0 then raise Domain else let fun tr_fact 0 acc = acc | tr_fact counter acc = scope with an \edef command by preventing com- tr_fact (counter - 1) mand expansion by means of \noexpand: acc * counter \edef\thirdquestion{% in tr_fact x 1 end ; You’re \noexpand\state, ain’t U?\par} > val factorial = fn : int -> int and this command \thirdquestion behaves ex- actly like \secondquestion (cf. Fig. 4). Figure 3. The factorial function in Standard ML. A second construct, useful for a point of view re- lated to conception, is \global, shown in Fig. 5, because it allows ‘global’ commands to be defined a let form in Emacs Lisp [22], but they do not re- within local environments. There is an equivalent alise that this would be impossible within lexically- method in Scheme, but not naturally: see Appendix. scoped languages such as C or Scheme.