Bugfix in example code, use SCM_NEWSMOB
[bpt/guile.git] / doc / ref / libguile-smobs.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @node Defining New Types (Smobs)
8 @section Defining New Types (Smobs)
9
10 @dfn{Smobs} are Guile's mechanism for adding new primitive types to
11 the system. The term ``smob'' was coined by Aubrey Jaffer, who says
12 it comes from ``small object'', referring to the fact that they are
13 quite limited in size: they can hold just one pointer to a larger
14 memory block plus 16 extra bits.
15
16 To define a new smob type, the programmer provides Guile with some
17 essential information about the type --- how to print it, how to
18 garbage collect it, and so on --- and Guile allocates a fresh type tag
19 for it. The programmer can then use @code{scm_c_define_gsubr} to make
20 a set of C functions visible to Scheme code that create and operate on
21 these objects.
22
23 (You can find a complete version of the example code used in this
24 section in the Guile distribution, in @file{doc/example-smob}. That
25 directory includes a makefile and a suitable @code{main} function, so
26 you can build a complete interactive Guile shell, extended with the
27 datatypes described here.)
28
29 @menu
30 * Describing a New Type::
31 * Creating Instances::
32 * Type checking::
33 * Garbage Collecting Smobs::
34 * Garbage Collecting Simple Smobs::
35 * Remembering During Operations::
36 * Double Smobs::
37 * The Complete Example::
38 @end menu
39
40 @node Describing a New Type
41 @subsection Describing a New Type
42
43 To define a new type, the programmer must write four functions to
44 manage instances of the type:
45
46 @table @code
47 @item mark
48 Guile will apply this function to each instance of the new type it
49 encounters during garbage collection. This function is responsible for
50 telling the collector about any other @code{SCM} values that the object
51 has stored. The default smob mark function does nothing.
52 @xref{Garbage Collecting Smobs}, for more details.
53
54 @item free
55 Guile will apply this function to each instance of the new type that is
56 to be deallocated. The function should release all resources held by
57 the object. This is analogous to the Java finalization method-- it is
58 invoked at an unspecified time (when garbage collection occurs) after
59 the object is dead. The default free function frees the smob data (if
60 the size of the struct passed to @code{scm_make_smob_type} is non-zero)
61 using @code{scm_gc_free}. @xref{Garbage Collecting Smobs}, for more
62 details.
63
64 @item print
65 Guile will apply this function to each instance of the new type to print
66 the value, as for @code{display} or @code{write}. The default print
67 function prints @code{#<NAME ADDRESS>} where @code{NAME} is the first
68 argument passed to @code{scm_make_smob_type}. For more information on
69 printing, see @ref{Port Data}.
70
71 @item equalp
72 If Scheme code asks the @code{equal?} function to compare two instances
73 of the same smob type, Guile calls this function. It should return
74 @code{SCM_BOOL_T} if @var{a} and @var{b} should be considered
75 @code{equal?}, or @code{SCM_BOOL_F} otherwise. If @code{equalp} is
76 @code{NULL}, @code{equal?} will assume that two instances of this type are
77 never @code{equal?} unless they are @code{eq?}.
78
79 @end table
80
81 To actually register the new smob type, call @code{scm_make_smob_type}.
82 It returns a value of type @code{scm_t_bits} which identifies the new
83 smob type.
84
85 The four special functions described above are registered by calling
86 one of @code{scm_set_smob_mark}, @code{scm_set_smob_free},
87 @code{scm_set_smob_print}, or @code{scm_set_smob_equalp}, as
88 appropriate. Each function is intended to be used at most once per
89 type, and the call should be placed immediately following the call to
90 @code{scm_make_smob_type}.
91
92 There can only be at most 256 different smob types in the system.
93 Instead of registering a huge number of smob types (for example, one
94 for each relevant C struct in your application), it is sometimes
95 better to register just one and implement a second alyer of type
96 dispatching on top of it. This second layer might use the 16 extra
97 bits for as an extended type, for example.
98
99 Here is how one might declare and register a new type representing
100 eight-bit gray-scale images:
101
102 @example
103 #include <libguile.h>
104
105 struct image @{
106 int width, height;
107 char *pixels;
108
109 /* The name of this image */
110 SCM name;
111
112 /* A function to call when this image is
113 modified, e.g., to update the screen,
114 or SCM_BOOL_F if no action necessary */
115 SCM update_func;
116 @};
117
118 static scm_t_bits image_tag;
119
120 void
121 init_image_type (void)
122 @{
123 image_tag = scm_make_smob_type ("image", sizeof (struct image));
124 scm_set_smob_mark (image_tag, mark_image);
125 scm_set_smob_free (image_tag, free_image);
126 scm_set_smob_print (image_tag, print_image);
127 @}
128 @end example
129
130
131 @node Creating Instances
132 @subsection Creating Instances
133
134 Normally, smobs can have one @emph{immediate} word of data. This word
135 stores either a pointer to an additional memory block that holds the
136 real data, or it might hold the data itself when it fits. The word is
137 large enough for a @code{SCM} value, a pointer to @code{void}, or an
138 integer that fits into a @code{size_t} or @code{ssize_t}.
139
140 You can also create smobs that have two or three immediate words, and
141 when these words suffice to store all data, it is more efficient to use
142 these super-sized smobs instead of using a normal smob plus a memory
143 block. @xref{Double Smobs}, for their discussion.
144
145 Guile provides functions for managing memory which are often helpful
146 when implementing smobs. @xref{Memory Blocks}.
147
148 To retrieve the immediate word of a smob, you use the macro
149 @code{SCM_SMOB_DATA}. It can be set with @code{SCM_SET_SMOB_DATA}.
150 The 16 extra bits can be accessed with @code{SCM_SMOB_FLAGS} and
151 @code{SCM_SET_SMOB_FLAGS}.
152
153 The two macros @code{SCM_SMOB_DATA} and @code{SCM_SET_SMOB_DATA} treat
154 the immediate word as if it were of type @code{scm_t_bits}, which is
155 an unsigned integer type large enough to hold a pointer to
156 @code{void}. Thus you can use these macros to store arbitrary
157 pointers in the smob word.
158
159 When you want to store a @code{SCM} value directly in the immediate
160 word of a smob, you should use the macros @code{SCM_SMOB_OBJECT} and
161 @code{SCM_SET_SMOB_OBJECT} to access it.
162
163 Creating a smob instance can be tricky when it consists of multiple
164 steps that allocate resources and might fail. It is recommended that
165 you go about creating a smob in the following way:
166
167 @itemize
168 @item
169 Allocate the memory block for holding the data with
170 @code{scm_gc_malloc}.
171 @item
172 Initialize it to a valid state without calling any functions that might
173 cause a non-local exits. For example, initialize pointers to NULL.
174 Also, do not store @code{SCM} values in it that must be protected.
175 Initialize these fields with @code{SCM_BOOL_F}.
176
177 A valid state is one that can be safely acted upon by the @emph{mark}
178 and @emph{free} functions of your smob type.
179 @item
180 Create the smob using @code{SCM_NEWSMOB}, passing it the initialized
181 memory block. (This step will always succeed.)
182 @item
183 Complete the initialization of the memory block by, for example,
184 allocating additional resources and making it point to them.
185 @end itemize
186
187 This procedure ensures that the smob is in a valid state as soon as it
188 exists, that all resources that are allocated for the smob are
189 properly associated with it so that they can be properly freed, and
190 that no @code{SCM} values that need to be protected are stored in it
191 while the smob does not yet competely exist and thus can not protect
192 them.
193
194 Continuing the example from above, if the global variable
195 @code{image_tag} contains a tag returned by @code{scm_make_smob_type},
196 here is how we could construct a smob whose immediate word contains a
197 pointer to a freshly allocated @code{struct image}:
198
199 @example
200 SCM
201 make_image (SCM name, SCM s_width, SCM s_height)
202 @{
203 SCM smob;
204 struct image *image;
205 int width = scm_to_int (s_width);
206 int height = scm_to_int (s_height);
207
208 /* Step 1: Allocate the memory block.
209 */
210 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
211
212 /* Step 2: Initialize it with straight code.
213 */
214 image->width = width;
215 image->height = height;
216 image->pixels = NULL;
217 image->name = SCM_BOOL_F;
218 image->update_func = SCM_BOOL_F;
219
220 /* Step 3: Create the smob.
221 */
222 SCM_NEWSMOB (smob, image_tag, image);
223
224 /* Step 4: Finish the initialization.
225 */
226 image->name = name;
227 image->pixels = scm_gc_malloc (width * height, "image pixels");
228
229 return smob;
230 @}
231 @end example
232
233 Let us look at what might happen when @code{make_image} is called.
234
235 The conversions of @var{s_width} and @var{s_height} to @code{int}s might
236 fail and signal an error, thus causing a non-local exit. This is not a
237 problem since no resources have been allocated yet that would have to be
238 freed.
239
240 The allocation of @var{image} in step 1 might fail, but this is likewise
241 no problem.
242
243 Step 2 can not exit non-locally. At the end of it, the @var{image}
244 struct is in a valid state for the @code{mark_image} and
245 @code{free_image} functions (see below).
246
247 Step 3 can not exit non-locally either. This is guaranteed by Guile.
248 After it, @var{smob} contains a valid smob that is properly initialized
249 and protected, and in turn can properly protect the Scheme values in its
250 @var{image} struct.
251
252 But before the smob is completely created, @code{SCM_NEWSMOB} might
253 cause the garbage collector to run. During this garbage collection, the
254 @code{SCM} values in the @var{image} struct would be invisible to Guile.
255 It only gets to know about them via the @code{mark_image} function, but
256 that function can not yet do its job since the smob has not been created
257 yet. Thus, it is important to not store @code{SCM} values in the
258 @var{image} struct until after the smob has been created.
259
260 Step 4, finally, might fail and cause a non-local exit. In that case,
261 the complete creation of the smob has not been successful, but it does
262 nevertheless exist in a valid state. It will eventually be freed by
263 the garbage collector, and all the resources that have been allocated
264 for it will be correctly freed by @code{free_image}.
265
266 @node Type checking
267 @subsection Type checking
268
269 Functions that operate on smobs should check that the passed
270 @code{SCM} value indeed is a suitable smob before accessing its data.
271 They can do this with @code{scm_assert_smob_type}.
272
273 For example, here is a simple function that operates on an image smob,
274 and checks the type of its argument.
275
276 @example
277 SCM
278 clear_image (SCM image_smob)
279 @{
280 int area;
281 struct image *image;
282
283 scm_assert_smob_type (image_tag, image_smob);
284
285 image = (struct image *) SCM_SMOB_DATA (image_smob);
286 area = image->width * image->height;
287 memset (image->pixels, 0, area);
288
289 /* Invoke the image's update function.
290 */
291 if (scm_is_true (image->update_func))
292 scm_call_0 (image->update_func);
293
294 scm_remember_upto_here_1 (image_smob);
295
296 return SCM_UNSPECIFIED;
297 @}
298 @end example
299
300 See @ref{Remembering During Operations} for an explanation of the call
301 to @code{scm_remember_upto_here_1}.
302
303
304 @node Garbage Collecting Smobs
305 @subsection Garbage Collecting Smobs
306
307 Once a smob has been released to the tender mercies of the Scheme
308 system, it must be prepared to survive garbage collection. Guile calls
309 the @emph{mark} and @emph{free} functions of the smob to manage this.
310
311 As described in more detail elsewhere (@pxref{Conservative GC}), every
312 object in the Scheme system has a @dfn{mark bit}, which the garbage
313 collector uses to tell live objects from dead ones. When collection
314 starts, every object's mark bit is clear. The collector traces pointers
315 through the heap, starting from objects known to be live, and sets the
316 mark bit on each object it encounters. When it can find no more
317 unmarked objects, the collector walks all objects, live and dead, frees
318 those whose mark bits are still clear, and clears the mark bit on the
319 others.
320
321 The two main portions of the collection are called the @dfn{mark phase},
322 during which the collector marks live objects, and the @dfn{sweep
323 phase}, during which the collector frees all unmarked objects.
324
325 The mark bit of a smob lives in a special memory region. When the
326 collector encounters a smob, it sets the smob's mark bit, and uses the
327 smob's type tag to find the appropriate @emph{mark} function for that
328 smob. It then calls this @emph{mark} function, passing it the smob as
329 its only argument.
330
331 The @emph{mark} function is responsible for marking any other Scheme
332 objects the smob refers to. If it does not do so, the objects' mark
333 bits will still be clear when the collector begins to sweep, and the
334 collector will free them. If this occurs, it will probably break, or at
335 least confuse, any code operating on the smob; the smob's @code{SCM}
336 values will have become dangling references.
337
338 To mark an arbitrary Scheme object, the @emph{mark} function calls
339 @code{scm_gc_mark}.
340
341 Thus, here is how we might write @code{mark_image}:
342
343 @example
344 @group
345 SCM
346 mark_image (SCM image_smob)
347 @{
348 /* Mark the image's name and update function. */
349 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
350
351 scm_gc_mark (image->name);
352 scm_gc_mark (image->update_func);
353
354 return SCM_BOOL_F;
355 @}
356 @end group
357 @end example
358
359 Note that, even though the image's @code{update_func} could be an
360 arbitrarily complex structure (representing a procedure and any values
361 enclosed in its environment), @code{scm_gc_mark} will recurse as
362 necessary to mark all its components. Because @code{scm_gc_mark} sets
363 an object's mark bit before it recurses, it is not confused by
364 circular structures.
365
366 As an optimization, the collector will mark whatever value is returned
367 by the @emph{mark} function; this helps limit depth of recursion during
368 the mark phase. Thus, the code above should really be written as:
369 @example
370 @group
371 SCM
372 mark_image (SCM image_smob)
373 @{
374 /* Mark the image's name and update function. */
375 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
376
377 scm_gc_mark (image->name);
378 return image->update_func;
379 @}
380 @end group
381 @end example
382
383
384 Finally, when the collector encounters an unmarked smob during the sweep
385 phase, it uses the smob's tag to find the appropriate @emph{free}
386 function for the smob. It then calls that function, passing it the smob
387 as its only argument.
388
389 The @emph{free} function must release any resources used by the smob.
390 However, it must not free objects managed by the collector; the
391 collector will take care of them. For historical reasons, the return
392 type of the @emph{free} function should be @code{size_t}, an unsigned
393 integral type; the @emph{free} function should always return zero.
394
395 Here is how we might write the @code{free_image} function for the image
396 smob type:
397 @example
398 size_t
399 free_image (SCM image_smob)
400 @{
401 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
402
403 scm_gc_free (image->pixels, image->width * image->height, "image pixels");
404 scm_gc_free (image, sizeof (struct image), "image");
405
406 return 0;
407 @}
408 @end example
409
410 During the sweep phase, the garbage collector will clear the mark bits
411 on all live objects. The code which implements a smob need not do this
412 itself.
413
414 There is no way for smob code to be notified when collection is
415 complete.
416
417 It is usually a good idea to minimize the amount of processing done
418 during garbage collection; keep the @emph{mark} and @emph{free}
419 functions very simple. Since collections occur at unpredictable times,
420 it is easy for any unusual activity to interfere with normal code.
421
422
423 @node Garbage Collecting Simple Smobs
424 @subsection Garbage Collecting Simple Smobs
425
426 It is often useful to define very simple smob types --- smobs which have
427 no data to mark, other than the cell itself, or smobs whose immediate
428 data word is simply an ordinary Scheme object, to be marked recursively.
429 Guile provides some functions to handle these common cases; you can use
430 this function as your smob type's @emph{mark} function, if your smob's
431 structure is simple enough.
432
433 If the smob refers to no other Scheme objects, then no action is
434 necessary; the garbage collector has already marked the smob cell
435 itself. In that case, you can use zero as your mark function.
436
437 If the smob refers to exactly one other Scheme object via its first
438 immediate word, you can use @code{scm_markcdr} as its mark function.
439 Its definition is simply:
440
441 @smallexample
442 SCM
443 scm_markcdr (SCM obj)
444 @{
445 return SCM_SMOB_OBJECT (obj);
446 @}
447 @end smallexample
448
449 @node Remembering During Operations
450 @subsection Remembering During Operations
451 @cindex Remembering
452
453 It's important that a smob is visible to the garbage collector
454 whenever its contents are being accessed. Otherwise it could be freed
455 while code is still using it.
456
457 For example, consider a procedure to convert image data to a list of
458 pixel values.
459
460 @example
461 SCM
462 image_to_list (SCM image_smob)
463 @{
464 struct image *image;
465 SCM lst;
466 int i;
467
468 scm_assert_smob_type (image_tag, image_smob);
469
470 image = (struct image *) SCM_SMOB_DATA (image_smob);
471 lst = SCM_EOL;
472 for (i = image->width * image->height - 1; i >= 0; i--)
473 lst = scm_cons (scm_from_char (image->pixels[i]), lst);
474
475 scm_remember_upto_here_1 (image_smob);
476 return lst;
477 @}
478 @end example
479
480 In the loop, only the @code{image} pointer is used and the C compiler
481 has no reason to keep the @code{image_smob} value anywhere. If
482 @code{scm_cons} results in a garbage collection, @code{image_smob} might
483 not be on the stack or anywhere else and could be freed, leaving the
484 loop accessing freed data. The use of @code{scm_remember_upto_here_1}
485 prevents this, by creating a reference to @code{image_smob} after all
486 data accesses.
487
488 There's no need to do the same for @code{lst}, since that's the return
489 value and the compiler will certainly keep it in a register or
490 somewhere throughout the routine.
491
492 The @code{clear_image} example previously shown (@pxref{Type checking})
493 also used @code{scm_remember_upto_here_1} for this reason.
494
495 It's only in quite rare circumstances that a missing
496 @code{scm_remember_upto_here_1} will bite, but when it happens the
497 consequences are serious. Fortunately the rule is simple: whenever
498 calling a Guile library function or doing something that might, ensure
499 that the @code{SCM} of a smob is referenced past all accesses to its
500 insides. Do this by adding an @code{scm_remember_upto_here_1} if
501 there are no other references.
502
503 In a multi-threaded program, the rule is the same. As far as a given
504 thread is concerned, a garbage collection still only occurs within a
505 Guile library function, not at an arbitrary time. (Guile waits for all
506 threads to reach one of its library functions, and holds them there
507 while the collector runs.)
508
509 @node Double Smobs
510 @subsection Double Smobs
511
512 Smobs are called smob because they are small: they normally have only
513 room for one @code{void*} or @code{SCM} value plus 16 bits. The
514 reason for this is that smobs are directly implemented by using the
515 low-level, two-word cells of Guile that are also used to implement
516 pairs, for example. (@pxref{Data Representation} for the details.)
517 One word of the two-word cells is used for @code{SCM_SMOB_DATA} (or
518 @code{SCM_SMOB_OBJECT}), the other contains the 16-bit type tag and
519 the 16 extra bits.
520
521 In addition to the fundamental two-word cells, Guile also has
522 four-word cells, which are appropriately called @dfn{double cells}.
523 You can use them for @dfn{double smobs} and get two more immediate
524 words of type @code{scm_t_bits}.
525
526 A double smob is created with @code{SCM_NEWSMOB2} or
527 @code{SCM_NEWSMOB3} instead of @code{SCM_NEWSMOB}. Its immediate
528 words can be retrieved as @code{scm_t_bits} with
529 @code{SCM_SMOB_DATA_2} and @code{SCM_SMOB_DATA_3} in addition to
530 @code{SCM_SMOB_DATA}. Unsurprisingly, the words can be set to
531 @code{scm_t_bits} values with @code{SCM_SET_SMOB_DATA_2} and
532 @code{SCM_SET_SMOB_DATA_3}.
533
534 Of course there are also @code{SCM_SMOB_OBJECT_2},
535 @code{SCM_SMOB_OBJECT_3}, @code{SCM_SET_SMOB_OBJECT_2}, and
536 @code{SCM_SET_SMOB_OBJECT_3}.
537
538 @node The Complete Example
539 @subsection The Complete Example
540
541 Here is the complete text of the implementation of the image datatype,
542 as presented in the sections above. We also provide a definition for
543 the smob's @emph{print} function, and make some objects and functions
544 static, to clarify exactly what the surrounding code is using.
545
546 As mentioned above, you can find this code in the Guile distribution, in
547 @file{doc/example-smob}. That directory includes a makefile and a
548 suitable @code{main} function, so you can build a complete interactive
549 Guile shell, extended with the datatypes described here.)
550
551 @example
552 /* file "image-type.c" */
553
554 #include <stdlib.h>
555 #include <libguile.h>
556
557 static scm_t_bits image_tag;
558
559 struct image @{
560 int width, height;
561 char *pixels;
562
563 /* The name of this image */
564 SCM name;
565
566 /* A function to call when this image is
567 modified, e.g., to update the screen,
568 or SCM_BOOL_F if no action necessary */
569 SCM update_func;
570 @};
571
572 static SCM
573 make_image (SCM name, SCM s_width, SCM s_height)
574 @{
575 SCM smob;
576 struct image *image;
577 int width = scm_to_int (s_width);
578 int height = scm_to_int (s_height);
579
580 /* Step 1: Allocate the memory block.
581 */
582 image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
583
584 /* Step 2: Initialize it with straight code.
585 */
586 image->width = width;
587 image->height = height;
588 image->pixels = NULL;
589 image->name = SCM_BOOL_F;
590 image->update_func = SCM_BOOL_F;
591
592 /* Step 3: Create the smob.
593 */
594 SCM_NEWSMOB (smob, image_tag, image);
595
596 /* Step 4: Finish the initialization.
597 */
598 image->name = name;
599 image->pixels = scm_gc_malloc (width * height, "image pixels");
600
601 return smob;
602 @}
603
604 SCM
605 clear_image (SCM image_smob)
606 @{
607 int area;
608 struct image *image;
609
610 scm_assert_smob_type (image_tag, image_smob);
611
612 image = (struct image *) SCM_SMOB_DATA (image_smob);
613 area = image->width * image->height;
614 memset (image->pixels, 0, area);
615
616 /* Invoke the image's update function.
617 */
618 if (scm_is_true (image->update_func))
619 scm_call_0 (image->update_func);
620
621 scm_remember_upto_here_1 (image_smob);
622
623 return SCM_UNSPECIFIED;
624 @}
625
626 static SCM
627 mark_image (SCM image_smob)
628 @{
629 /* Mark the image's name and update function. */
630 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
631
632 scm_gc_mark (image->name);
633 return image->update_func;
634 @}
635
636 static size_t
637 free_image (SCM image_smob)
638 @{
639 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
640
641 scm_gc_free (image->pixels, image->width * image->height, "image pixels");
642 scm_gc_free (image, sizeof (struct image), "image");
643
644 return 0;
645 @}
646
647 static int
648 print_image (SCM image_smob, SCM port, scm_print_state *pstate)
649 @{
650 struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
651
652 scm_puts ("#<image ", port);
653 scm_display (image->name, port);
654 scm_puts (">", port);
655
656 /* non-zero means success */
657 return 1;
658 @}
659
660 void
661 init_image_type (void)
662 @{
663 image_tag = scm_make_smob_type ("image", sizeof (struct image));
664 scm_set_smob_mark (image_tag, mark_image);
665 scm_set_smob_free (image_tag, free_image);
666 scm_set_smob_print (image_tag, print_image);
667
668 scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
669 scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
670 @}
671 @end example
672
673 Here is a sample build and interaction with the code from the
674 @file{example-smob} directory, on the author's machine:
675
676 @example
677 zwingli:example-smob$ make CC=gcc
678 gcc `guile-config compile` -c image-type.c -o image-type.o
679 gcc `guile-config compile` -c myguile.c -o myguile.o
680 gcc image-type.o myguile.o `guile-config link` -o myguile
681 zwingli:example-smob$ ./myguile
682 guile> make-image
683 #<primitive-procedure make-image>
684 guile> (define i (make-image "Whistler's Mother" 100 100))
685 guile> i
686 #<image Whistler's Mother>
687 guile> (clear-image i)
688 guile> (clear-image 4)
689 ERROR: In procedure clear-image in expression (clear-image 4):
690 ERROR: Wrong type argument in position 1: 4
691 ABORT: (wrong-type-arg)
692
693 Type "(backtrace)" to get more information.
694 guile>
695 @end example