]> git.lizzy.rs Git - metalua.git/blob - doc/manual/intro.tex
cf7102d1dfcd3fd32fea1bcd2dbe9790605f13d0
[metalua.git] / doc / manual / intro.tex
1 \section{Concepts}
2
3 \paragraph{Lua}
4 Lua\footnote{\url{http://www.lua.org}} is an very clean and powerful
5 language, with everything the discrimiating hacker will love: advanced
6 data structures, true function closures, coroutines (a.k.a collaborative
7 multithreading), powerful runtime introspection and metaprogramming
8 abilities, ultra-easy integration with C.
9
10 The general approach in Lua's design is to implement a small number of
11 very powerful concepts, and use them to easily offer particular
12 services. For instance, objects can be implemented through metatables
13 (which allow to customize the behavior of data structures), or through
14 function closures. It's quite easy to develop a class based system
15 with single or multiple inheritance, or a prototype based system 
16 {\`a la} Self\footnote{\url{http://research.sun.com/self}}, or
17 the kind of more advanced and baroque things that only CLOS users
18 could dream of...
19
20 Basically, Lua could be though of as Scheme, with:
21 \begin{itemize}
22 \item a conventional syntax (similar to Pascal's or Ruby's);
23 \item the associative table as basic datatype instead of the list;
24 \item no full continuations (although coroutines are actually one-shot
25   semi-continuations);
26 \item no macro system.
27 \end{itemize}
28
29 \paragraph{Metalua}
30 Metalua is an extension of Lua, which essentially addresses the lack
31 of a macro system, by providing compile-time metaprogramming (CTMP)
32 and the ability for user to extend the syntax from within Lua.
33
34 Runtime metaprogramming (RTMP) allows a program to inspect itself
35 while running: an object can thus enumerate its fields and methods,
36 their properties, maybe dump its source code; it can be modified
37 on-the-fly, by adding a method, changing its class, etc. But this
38 doesn't allow to change the shape of the language itself: you cannot
39 use this to add exceptions to a language that lacks them, nor
40 call-by-need (a.k.a. ``lazy'') evaluation to a traditional language,
41 nor continuations, nor new control structures, new operators... To do
42 this, you need to modify the compiler itself. It can be done, if you
43 have the sources of the compiler, but that's generally not worth it,
44 given the complexity of a compiler program and the portability and
45 maintenance issues that ensue.
46
47 \paragraph{Metaprogramming}
48 A compiler is essentially a system which takes sources (generally as
49 a set of ASCII files), turn them into a practical-to-play-with data
50 structure, does stuff on it, then feeds it to a bytecode or machine
51 code producer. The source and byte-code stages are bad abstraction
52 levels to do anything practical: the sensible way to represent code,
53 when you want to manipulate it with programs, is tha abstract syntax
54 tree (AST). This is the practical-to-play-with abstraction level
55 mentionned above: a tree in which each node corresponds to a control
56 structure, where the inclusion relationship is respected (e.g. if an
57 instruction I is in a loop's body B, then the node representing I
58 is a subtree of the tree representing B)...
59
60 CTMP is possible if the compiler allows its user to read, generate and
61 modify AST, and to splice these generated AST back into programs. This
62 is done by Lisp and Scheme by making the programmer write programs
63 directly in AST (hence the lot of parentheses in Lisp sources), and by
64 offering a magic instruction that executes during compilation a piece
65 of code which generates an AST, and inserts this AST into the source
66 AST: that magic couple of instructions is the macro system.
67
68 Metalua has a similar execute-and-splice-the-result magic construct;
69 the main difference is that it doesn't force the programmer to
70 directly write in AST (although he's allowed to if he finds it most
71 suitable for a specific task). However, supporting ``real language
72 syntax'' adds a couple of issues to CTMP: there is a need for
73 transformation from real syntax to AST and the other way around, as
74 well as a need for a way to extend syntax.
75
76 This manual won't try to teach Lua, there's a wealth of excellent
77 tutorials on the web for this. I highly recommand Roberto
78 Ierusalimschy's ``Programming in Lua''
79 book\footnote{Programming in Lua, 2nd edition.\\
80   Published by Lua.org, March 2006\\
81   ISBN 85-903798-2-5 Paperback, 328 pages\\
82   Distributed by Ingram and Baker \& Taylor.}, 
83 a.k.a. ``the blue PiL'', probably one of the best programming books
84 since K\&R's ``The C Language''. Suffice to say that a seasonned
85 programmer will be able to program in Lua in a couple of hours,
86 although some advanced features (coroutines, function environments,
87 function closures, metatables, runtime introspection) might take
88 longer to master if you don't already know a language supporting them.
89
90 Among resources available online, my personal favorites would be:
91 \begin{itemize}
92 \item The reference manual: \url{http://www.lua.org/manual/5.1}
93 \item The first edition of PiL, kindly put online by its author at
94   \url{http://www.lua.org/pil}
95 \item A compact reference sheet (grammar and standard libraries) by
96   Enrico Colombini: \url{http://lua-users.org/wiki/LuaShortReference}
97 \item Generally speaking, the Lua community wiki
98   (\url{http://lua-users.org/wiki}) is invaluable.
99 \item The mailing list (\url{http://www.lua.org/lua-l.html})
100   and the IRC channel (\url{irc://irc.freenode.net/#lua}) are
101   populated with a very helpful community.
102 \item You will also find a lot of useful programs and libraries for
103   Lua hosted at \url{http://luaforge.net}: various protocol parsers, 
104   bindings to 2D/3D native/portable GUI, sound, database drivers...
105 \item A compilation of the community's wisdom will eventually be
106   plubished as ``Lua Gems''; you can already check its ToC at
107   \url{http://www.lua.org/gems}
108 \end{itemize}
109
110 So, instead of including yet another Lua tutorial, this manual will
111 rather focus on the features specific to Metalua, that is mainly:
112 \begin{itemize}
113 \item The couple of syntax extensions offered by Metalua over Lua;
114 \item The two CTMP magic constructs \verb|+{...}| and \verb|-{...}|;
115 \item The libraries which support CTMP (mainly for syntax extension).
116 \end{itemize}
117
118 \paragraph{Metalua design philosophy}
119 Metalua has been designed to occupy a vacant spot in the space of
120 CTMP-enabled languages:
121 \begin{itemize}
122
123 \item Lisp offers a lot of flexibility, at the price of macro-friendly
124   syntax, rather than user-friendly. Besides the overrated problem of
125   getting used to those lots of parentheses, it's all too tempting to
126   mix macros and normal code in Lisp, in a way that doesn't visually
127   stand out; this really doesn't encourage the writing of reusable,
128   mutually compatible libraries. As a result of this extreme
129   flexibility, large scale collaboration doesn't seem to happen, and
130   Lisps lack a {\em de facto} comprehensive set of standard libs,
131   besides those included in Common Lisp's specification. Comparisons
132   have been drawn between getting Lispers to work together and herding
133   cats\ldots
134
135 \item Macro-systems bolted on existing languages (Template
136   Haskell\footnote{\url{http://www.haskell.org/th/}},
137   CamlP5\footnote{\url{http://pauillac.inria.fr/~ddr/camlp5/}},
138   MetaML\footnote{\url{http://www.cse.ogi.edu/pacsoft/projects/metaml}}\ldots)
139   tend to be hard to use: the syntax and semantics of these target languages are
140   complex, and make macro writing much harder than necessary. Moreover, for some
141   reason, most of these projects target statically typed languages: although
142   static inference type systems {\`a la} Hindley-Milner are extremely powerful
143   tools in many contexts, my intuition is that static types are more of a burden
144   than a help for many macro-friendly problems.
145
146 \item Languages built from scratch, such as
147   converge\footnote{\url{http://convergepl.org}} or
148   Logix\footnote{\url{http://http://www.livelogix.net/logix/index.html}}, have
149   to bear with the very long (often decade) maturing time required by a
150   programming language. Moreover, they lack the existing libraries and
151   developpers that come with an already succesful language.
152
153 \end{itemize}
154
155 \noindent Lua presents many features that beg for a real macro system:
156
157 \begin{itemize}
158 \item Its compact, clear, orthogonal, powerful semantics, and its
159   approach of giving powerful generic tools rather than ready-made
160   closed features to its users.
161 \item Its excellent supports for runtime metaprogramming.
162 \item Its syntax, despite (or due to its) being very readable and easy
163   to learn, is also extremely simple to parse. This means no extra
164   technology gets in the way of handling syntax (no BNF-like
165   specialized language, no byzantine rules and exceptions). Even more
166   importantly, provided that developers respect a couple of
167   common-sense rules, cohabitation of multiple syntax extensions in a
168   single project is made surprizingly easy.
169 \end{itemize}
170
171 Upon this powerful and sane base, Metalua adds CTMP with the following
172 design goals:
173
174 \begin{itemize}
175 \item Simple things should be easy and look clean: writing simple
176   macros shouldn't require an advanced knowledge of the language's
177   internals. And since we spend 95\% of our time {\em not} writing
178   macros, the syntax should be optimized for regular code rather than
179   for code generation.
180 \item Good coding practices should be encouraged. Among others,
181   separation between meta-levels must be obvious, so that it stands
182   out when something {\em interesting} is going on. Ideally, good code
183   must look clean, and messy code should look ugly.
184 \item However, the language must be an enabler, not handcuffs: it
185   should ensure that users know what they're doing, but it must
186   provide them with all the power they're willing to handle.
187 \end{itemize}
188
189 Finally, it's difficult to talk about a macro-enabled language without making
190 Lisp comparisons. Metalua borrows a lot to Scheme's love for empowering
191 minimalism, through Lua. However, in many other respects, it's closer to Common
192 Lisp: where Scheme insists on doing The Right Thing, CL and metalua assume that
193 the programmer knows better than the compiler. Therefore, when a powerful but
194 potentially dangerous feature is considered, metalua generally tries to warn the
195 user that he's entering the twilight zone, but will let him proceed. The most
196 prominent example is probably macro hygiene. Scheme pretty much constraints
197 macro writing into a term rewriting system: it allows the compiler to enforce
198 macro hygiene automatically, but is sometimes crippling when writing complex
199 macros (although it is, of cource, Turing-complete). Metalua opts to offer CL
200 style, non-hygienic macros, so that AST are regular data manipulated by regular
201 code. Hygienic safety is provided by an {\em optional} library, which makes it
202 easy but not mandatory to do hygienic macros.