Debian Codegraph @

docsrc/xlisp/xlisp-doc/tutorials/environment.htm

raw
1 <html><head>
2
3 <title>Environment</title>
4
5 <style type="text/css">
6 .example {
7 color: #000000;
8 background-color: #F5F5F5;
9 padding: 8px;
10 border: #808080;
11 border-style: solid;
12 border-width: 1px;
13 width:auto;
14 }
15 .button {
16 color: #000000;
17 background-color: #F5F5F5;
18 padding-top: 1px;
19 padding-bottom: 1px;
20 padding-left: 4px;
21 padding-right: 8px;
22 border: #808080;
23 border-style: solid;
24 border-width: 1px;
25 white-space: pre;
26 }
27 .box {
28 color: #000000;
29 padding-top: 4px;
30 padding-bottom: 4px;
31 padding-left: 16px;
32 padding-right: 16px;
33 border: #808080;
34 border-style: solid;
35 border-width: 1px;
36 }
37 </style>
38
39 </head>
40
41 <body>
42
43 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
44 <a href="../manual/contents.htm">Contents</a> |
45 <a href="tutorials.htm">Tutorials</a> |
46 <a href="../examples/examples.htm">Examples</a> |
47 <a href="../reference/reference-index.htm">Reference</a>
48
49 <hr>
50
51 <h1>Environment</h1>
52
53 <hr>
54
55 <a name="global-and-lexical-binding"></a>
56
57 <hr>
58
59 <h2>Global and Lexical Binding</h2>
60
61 <hr>
62
63 <p>From the XLISP perspective, there are two kinds of bindings:</p>
64
65 <ol>
66
67 <li><p>Global bindings are bindings to symbols in the *obarray*.</p></li>
68
69 <li><p>Lexical bindings are bindings in a local association list</p></li>
70
71 </ol>
72
73 <p>There is a third kind of binding, 'dynamical binding', used by progv.</p>
74
75 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
76
77 <a name="lexical-scope"></a>
78
79 <hr>
80
81 <h2>Lexical Scope</h2>
82
83 <hr>
84
85 <p>Have you ever wondered why this doesn't work:</p>
86
87 <pre class="example">
88 (defun <font color="#0000CC">print-x</font> ()
89 (print x)) <font color="#008844">; unbound variable X</font>
90
91 (let ((x 'hello))
92 (print-x))
93 <font color="#AA0000">error: unbound variable - X</font>
94 </pre>
95
96 <p>The answer is twofold:</p>
97
98 <ol>
99
100 <li><p>The '<nobr>print-x</nobr>' function is defined at the global
101 <nobr>top-level</nobr>, where no lexical environment exists.</p></li>
102
103 <li><p>The '<nobr>print-x</nobr>' function is called inside a
104 <a href="../reference/let.htm">let</a> form, where a lexical variable
105 binding for 'x' exists, but '<nobr>print-x</nobr>' is evaluated at the
106 global <nobr>top-level</nobr>, where it was defined, so
107 '<nobr>print-x</nobr>' cannot see the lexical
108 <a href="../reference/let.htm">let</a> binding <nobr>of 'x'</nobr> and
109 signals an '<nobr>unbound variable</nobr>' error.</p></li>
110
111 </ol>
112
113 <p>Here is a version that seems to work:</p>
114
115 <pre class="example">
116 (let ((x 'hello))
117
118 (defun <font color="#0000CC">print-x</font> ()
119 (print x))
120
121 (print-x))
122 HELLO
123 </pre>
124
125 <ol>
126
127 <li><p>The '<nobr>print-x</nobr>' function is defined inside a
128 <a href="../reference/let.htm">let</a> form.</p></li>
129
130 <li><p>The '<nobr>print-x</nobr>' function is called inside the same
131 <a href="../reference/let.htm">let</a> form as where it was defined, so
132 '<nobr>print-x</nobr>' prints the lexical
133 <a href="../reference/let.htm">let</a> binding
134 <nobr>of 'x'</nobr>.</p></li>
135
136 </ol>
137
138 <p>But here again a version that does not behave as wanted:</p>
139
140 <pre class="example">
141 (let ((x 'lexical))
142 (defun <font color="#0000CC">print-x</font> ()
143 (print x)))
144
145 (let ((x 'hello))
146 (print-x))
147 LEXICAL
148 </pre>
149
150 <ol>
151
152 <li><p>The '<nobr>print-x</nobr>' function is defined inside a
153 <a href="../reference/let.htm">let</a> form.</p></li>
154
155 <li><p>The '<nobr>print-x</nobr>' function is called inside a different
156 <a href="../reference/let.htm">let</a> form as where it was defined, so
157 '<nobr>print-x</nobr>' prints the lexical
158 <a href="../reference/let.htm">let</a> binding <nobr>of 'x'</nobr> from
159 the place where it was defined.</p></li>
160
161 </ol>
162
163 <p>Somehow it seems to be important where a function was defined.</p>
164
165 <a name="closures"></a>
166
167 <hr>
168
169 <h2>Closures</h2>
170
171 <hr>
172
173 <p>Here a Lisp function, defined inside a
174 <a href="../reference/let.htm">let</a> form:</p>
175
176 <pre class="example">
177 (let ((a 'A) (b 'B) (c 'C))
178
179 (defun <font color="#0000CC">print-abc</font> ()
180 (format t <font color="#880000">";; a = ~s, b = ~s, c = ~s~%"</font> a b c))
181
182 ) <font color="#008844">; end of LET</font>
183 </pre>
184
185 <p>Now '<nobr>print-abc</nobr>' is called outside the
186 <a href="../reference/let.htm">let</a> form:</p>
187
188 <pre class="example">
189 &gt; (print-abc)
190 ;; a = A, b = B, c = C
191 NIL
192 </pre>
193
194 <p>The lexical <a href="../reference/let.htm">let</a> variables 'a', 'b',
195 and 'c' have become a permanent part of the '<nobr>print-abc</nobr>'
196 function.</p>
197
198 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
199
200 <a name="lisp-objects"></a>
201
202 <hr>
203
204 <h2>Lisp Objects</h2>
205
206 <hr>
207
208 <p>The following examples are based on <nobr>Chapter 13</nobr> of 'Paradigms
209 of Artificial Intelligence Programming' by Peter Norvig. <nobr>The
210 code</nobr> has been ported from <nobr>Common Lisp</nobr> to XLISP, all
211 examples have been tested with <nobr>Nyquist 3.03</nobr> in <nobr>December
212 2010</nobr>.</p>
213
214 <p>The function '<nobr>new-account</nobr>' creates account objects, which
215 are implemented as closures encapsulating three variables 'name', 'balance',
216 and '<nobr>interest-rate</nobr>'. <nobr> An account</nobr> object also
217 encapsulates functions to handle five messages ':withdraw', ':deposit',
218 ':balance', ':name', and ':interest', to which the object can respond:</p>
219
220 <pre class="example">
221 (defun <font color="#0000CC">new-account</font> (name &amp;optional (balance 0.0) (interest-rate 0.06))
222 #'(lambda (message)
223 (case message
224 (:withdraw #'(lambda (amount)
225 (if (&lt;= amount balance)
226 (setq balance (- balance amount))
227 'insufficient-funds)))
228 (:deposit #'(lambda (amount)
229 (setq balance (+ balance amount))))
230 (:balance #'(lambda () balance))
231 (:name #'(lambda () name))
232 (:interest #'(lambda ()
233 (setq balance (+ balance (* interest-rate balance))))))))
234 </pre>
235
236 <p>An account object can only do one thing, receive a message and return the
237 appropriate function to execute that message. This function is called the
238 'method' that implements the message.</p>
239
240 <p>The function '<nobr>get-method</nobr>' finds the method that implements a
241 message for a given object:</p>
242
243 <pre class="example">
244 (defun <font color="#0000CC">get-method</font> (object message)
245 (funcall object message))
246 </pre>
247
248 <p>The function '<nobr>send-message</nobr>' gets the method and applies it
249 to a list of arguments:</p>
250
251 <pre class="example">
252 (defun <font color="#0000CC">send-message</font> (object message &amp;rest args)
253 (apply (get-method object message) args))
254 </pre>
255
256 <p>Here are some examples how it works:</p>
257
258 <pre class="example">
259 &gt; (setq a1 (new-account "My Name" 1000.0))
260 #&lt;Closure...&gt;
261
262 &gt; (send-message a1 :name)
263 "My Name"
264
265 &gt; (send-message a1 :balance)
266 1000.0
267
268 &gt; (send-message a1 :withdraw 500.0)
269 500
270
271 &gt; (send-message a1 :deposit 123.45)
272 623.45
273
274 &gt; (send-message a1 :balance)
275 623.45
276 </pre>
277
278 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
279
280 <a name="generic-functions"></a>
281
282 <hr>
283
284 <h2>Generic Functions</h2>
285
286 <hr>
287
288 <p>The '<nobr>send-message</nobr>' syntax is awkward, as it is different
289 from normal Lisp function calling syntax, and it doesn't fit in with the
290 other Lisp tools.</p>
291
292 <p>For example if we want <nobr>to say</nobr>:</p>
293
294 <pre class="example">
295 (mapcar :balance accounts)
296 </pre>
297
298 <p>with '<nobr>send-message</nobr>' we would have to write:</p>
299
300 <pre class="example">
301 (mapcar #'(lambda (acc)
302 (send-message acc :balance))
303 accounts)
304 </pre>
305
306 <p>We could fix this problem by defining a generic function 'withdraw' like
307 this:</p>
308
309 <pre class="example">
310 (defun <font color="#0000CC">withdraw</font> (object &amp;rest args)
311 (apply (get-method object :withdraw) args))
312 </pre>
313
314 <p>Now we can write:</p>
315
316 <pre class="example">
317 (withdraw account x)
318 </pre>
319
320 <p>instead of:</p>
321
322 <pre class="example">
323 (send-message account :withdraw x)
324 </pre>
325
326 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
327
328 <a name="classes"></a>
329
330 <hr>
331
332 <h2>Classes</h2>
333
334 <hr>
335
336 <p>The macro '<nobr>define-class</nobr>' defines a class with its associated
337 message handling methods. <nobr>It also</nobr> defines a generic function
338 for each message. Finally, it allows the programmer to make a distinction
339 between instance variables, associated with each object, and class
340 variables, associated with a class and shared by all members of the
341 class.</p>
342
343 <pre class="example">
344 (defmacro <font color="#0000CC">define-class</font> (class ivars cvars &amp;rest methods)
345 `(let ,cvars
346 (mapcar #'ensure-generic-function ',(mapcar #'first methods))
347 (defun ,class ,ivars
348 #'(lambda (message)
349 (case message
350 ,@(mapcar #'make-clause methods))))))
351 </pre>
352
353 <p>The '<nobr>make-clause</nobr>' function translates a message from
354 '<nobr>define-class</nobr>' into a
355 <a href="../reference/case.htm">case</a> clause.</p>
356
357 <pre class="example">
358 (defun <font color="#0000CC">make-clause</font> (clause)
359 `(,(car clause) #'(lambda ,(cadr clause) ,@(cddr clause))))
360 </pre>
361
362 <p>The '<nobr>ensure-generic-function</nobr>' function defines a dispatch
363 function for a message, unless it already has been defined <nobr>as
364 one</nobr>:</p>
365
366 <pre class="example">
367 (defun <font color="#0000CC">ensure-generic-function</font> (message)
368 (unless (generic-function-p message)
369 (let ((fn #'(lambda (object &amp;rest args)
370 (apply (get-method object message) args))))
371 (setf (symbol-function message) fn)
372 (putprop message fn 'generic-function))))
373 </pre>
374
375 <p>The '<nobr>generic-function-p</nobr>' function tests if a function has
376 been defined as a generic function:</p>
377
378 <pre class="example">
379 (defun <font color="#0000CC">generic-function-p</font> (name)
380 (and (fboundp name)
381 (eq (get name 'generic-function) (symbol-function name))))
382 </pre>
383
384 <p>Now we can define the 'account' class with '<nobr>define-class</nobr>'.
385 We make '<nobr>interest-rate</nobr>' a class variable, shared by all
386 accounts:</p>
387
388 <pre class="example">
389 (define-class <font color="#0066CC">account</font> (name &amp;optional (balance 0.0)) ((interest-rate 0.06))
390 (withdraw (amount)
391 (if (&lt;= amount balance)
392 (setq balance (- balance amount))
393 'insufficient-funds))
394 (deposit (amount)
395 (setq balance (+ balance amount)))
396 (balance ()
397 balance)
398 (name ()
399 name)
400 (interest ()
401 (setq balance (+ balance (* interest-rate balance)))))
402 </pre>
403
404 <p>Macroexpansion:</p>
405
406 <pre class="example">
407 (let ((interest-rate 0.06))
408 (mapcar (function ensure-generic-function)
409 (quote (withdraw deposit balance name interest)))
410 (defun account (name &amp;optional (balance 0))
411 (function (lambda (message)
412 (case message
413 (withdraw (function (lambda (amount)
414 (if (&lt;= amount balance)
415 (setq balance (- balance amount))
416 (quote insufficient-funds)))))
417 (deposit (function (lambda (amount)
418 (setq balance (+ balance amount)))))
419 (balance (function (lambda nil balance)))
420 (name (function (lambda nil name)))
421 (interest (function (lambda nil
422 (setq balance (+ balance (* interest-rate balance)))))))))))
423 </pre>
424
425 <p>Here is how it works:</p>
426
427 <pre class="example">
428 &gt; (setq a2 (account "my-name" 2000.0)
429 #&lt;Closure...&gt;
430
431 &gt; (balance a2)
432 2000
433
434 &gt; (deposit a2 42.0)
435 2042
436
437 &gt; (interest a2)
438 2164.52
439 </pre>
440
441 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
442
443 <a name="delegation"></a>
444
445 <hr>
446
447 <h2>Delegation</h2>
448
449 <hr>
450
451 <p>Here is a '<nobr>password-account</nobr>' class with two
452 message clauses:</p>
453
454 <pre class="example">
455 (define-class <font color="#0066CC">password-account</font> (password acc) ()
456 (change-password (pass new-pass)
457 (if (equal pass password)
458 (setq password new-pass)
459 'wrong-password))
460 (t (pass &amp;rest args)
461 (if (equal pass password)
462 (if args
463 (apply message (cons acc args))
464 (funcall message acc))
465 'wrong-password)))
466 </pre>
467
468 <p>The definition of '<nobr>password-account</nobr>' depends on some
469 internal details of '<nobr>define-class</nobr>'. <nobr>It uses</nobr> 't' as
470 a <nobr>catch-all</nobr> clause to <a href="../reference/case.htm">case</a>
471 and uses the dispatch variable 'message'. Usually it is not a good idea to
472 rely on details of other code, so this will be changed below.</p>
473
474 <p>Here is how '<nobr>password-account</nobr>' works:</p>
475
476 <pre class="example">
477 &gt; (setq a3 (password-account "secret" a2))
478 #&lt;Closure...&gt;
479
480 &gt; (balance a3 "secret")
481 2164.52
482
483 &gt; (withdraw a3 "guess" 2000.0)
484 WRONG-PASSWORD
485
486 &gt; (withdraw a3 "secret" 2000.0)
487 164.52
488 </pre>
489
490 <p>Here is a '<nobr>limited-account</nobr>' class, where only a limited
491 amount of money can be withdrawn at any time:</p>
492
493 <pre class="example">
494 (define-class <font color="#0066CC">limited-account</font> (limit acc) ()
495 (withdraw (amount)
496 (if (&lt;= amount limit)
497 (withdraw acc amount)
498 'over-limit))
499 (t (&amp;rest args)
500 (if args
501 (apply message (cons acc args))
502 (funcall message acc))))
503 </pre>
504
505 <p>The 'withdraw' message is redefined to check if the account limit is
506 exceeded, while the 't' clause passes all other messages unchanged:</p>
507
508 <pre class="example">
509 &gt; (setq a4 (password-account "pass"
510 (limited-account 100.0
511 (account "limited" 500.0)))
512 #&lt;Closure...&gt;
513
514
515
516
517
518 </pre>
519
520 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
521
522 <hr>
523
524 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
525 <a href="../manual/contents.htm">Contents</a> |
526 <a href="tutorials.htm">Tutorials</a> |
527 <a href="../examples/examples.htm">Examples</a> |
528 <a href="../reference/reference-index.htm">Reference</a>
529
530 </body></html>