Debian Codegraph @

doc/coresyn.texi

raw
1 @node Core syntax, Macros, Programming in Gauche, Top
2 @chapter Core syntax
3 @c NODE 基本的な構文
4
5 @c ----------------------------------------------------------------------
6 @menu
7 * Lexical structure::
8 * Literals::
9 * Making procedures::
10 * Assignments::
11 * Conditionals::
12 * Binding constructs::
13 * Grouping::
14 * Iteration::
15 * Quasiquotation::
16 * Definitions::
17 * Inclusions::
18 * Feature conditional::
19 * Modules::
20 @end menu
21
22 @node Lexical structure, Literals, Core syntax, Core syntax
23 @section Lexical structure
24 @c NODE 字句構造
25
26 @c EN
27 Gauche extends R7RS Scheme lexical parser in some ways.
28 Besides, because of historical reasons, a few of the default
29 lexical syntax may conflict R7RS specification.
30 You can set a reader mode to make it R7RS compliant.
31 @c JP
32 GaucheはR7RS Schemeのパーザをいくつかの点で拡張しています。
33 また、歴史的経緯から、デフォルトの字句構文の中に、R7RSの仕様と相反するものが
34 いくつかあります。リーダのモードを設定することで、
35 R7RS互換にすることもできます。
36 @c COMMON
37
38 @table @emph
39 @c EN
40 @item Hash-bang directives
41 Tokens beginning with @code{#!} may have special meanings to
42 the reader. R7RS defines two of such directives---@code{#!fold-case}
43 and @code{#!no-fold-case}, which switches whether symbols
44 are read in case-folding or non-case-folding mode, respectively.
45
46 @pxref{Hash-bang token} below, for all the directives
47 Gauche has.
48 @c JP
49 @item hash-bang指示子
50 @code{#!}で始まるトークンはリーダに特別な影響を与えます。
51 R7RSではそういった指示子がふたつ定義されています。
52 シンボルのリード時に大文字小文字を無視するかしないかを
53 切り替える、@code{#!fold-case}と@code{#!no-fold-case}です。
54
55 Gaucheで使える指示子の一覧については、下の@ref{Hash-bang token}を参照してください。
56 @c COMMON
57
58 @c EN
59 @item Square brackets
60 @lxindex [
61 Gauche adopts the R6RS syntax that regards @code{[]} the same
62 as @code{()}. Both kind of parentheses are equivalent,
63 but the kind of corresponding open and close parentheses must match.
64 Some seasoned Lisper may frown on them, but it
65 helps visually distinguish different roles of parentheses.
66
67 A general convention is to use @code{[]} for groupings other than
68 function and macro application. If such grouping nests, however, use @code{()}
69 for outer groupings. Examples:
70 @c JP
71 @item 角括弧
72 @lxindex [
73 Gaucheは、R6RSの@code{[]}を@code{()}と同様に扱う構文規則を採用しています。
74 どちらの括弧も全く等価ですが、それぞれの開き括弧と閉じ括弧は対応していなければなりません。
75 括弧を混ぜることに眉をひそめる熟練のLisperもいますが、
76 括弧の種類を使い分けるのは、括弧の役割を視覚的に識別するのに役に立ちます。
77
78 一般的な慣習は、関数とマクロ呼び出し以外のグルーピングには@code{[]}を使う、
79 というものです。ただしそのようなグルーピングがネストする場合は、外側には@code{()}を
80 使います。例:
81 @c COMMON
82
83 @example
84 (cond [(test1 x) (y z)]
85 [(test2 x) (s t)]
86 [else (u v)])
87
88 (let ([x (foo a b)]
89 [y (bar c d)])
90 (baz x y))
91 @end example
92
93 @c EN
94 It is purely optional, so you don't need to use them if you don't like them.
95 R7RS doesn't adopt this syntax and leaves @code{[]} for extensions,
96 so it is safe to stick to @code{()} in portable R7RS programs.
97 (If the reader is in @code{strict-r7} mode, an error is signalled
98 when @code{[]} is used. @xref{Reader lexical mode}, for the details.)
99
100 Scheme-specific modes of some editors (e.g. Quack on Emacs) allows
101 you to type just @code{)} and inserts either @code{]} or @code{)}
102 depending on which kind parenthesis it is closing. We recommend
103 using such modes if you use this convention.
104 @c JP
105 この記法は、必ず従わなければいけないというものではありません。
106 R7RSではこの構文を採用せず、@code{[]}は言語拡張用に取ってあるので、
107 ポータブルなR7RSプログラムを書く場合は@code{()}だけを使うのが良いでしょう。
108 (リーダが@code{strict-r7}モードの時は、@code{[]}が使われていたら
109 エラーが通知されます。詳しくは@ref{Reader lexical mode}参照。)
110
111 エディタのSchemeモードの中には、@code{)}をタイプするとそれがどちらの
112 種類の括弧を閉じるかに応じて自動的に@code{]}か@code{)}を選んで挿入してくれる
113 ものがあります (EmacsのQuackなど)。この記法を使うときは
114 そういったモードを活用すると良いでしょう。
115 @c COMMON
116
117 @c EN
118 @item Symbol names
119 Symbol names are case sensitive by default (@pxref{Case-sensitivity}).
120 Symbol name can begin with digits, '@code{+}' or '@code{-}', as long as
121 the entire token doesn't consist valid number syntax.
122 Other weird characters can be included in a symbol name by surrounding
123 it with '|', e.g. '@code{|this is a symbol|}'.
124 @xref{Symbols}, for details.
125 @c JP
126 @item シンボル名
127 シンボル名はデフォルトで大文字小文字を区別します (@ref{Case-sensitivity}参照)。
128 数字や'@code{+}', '@code{-}' で始まるシンボル名も、
129 トークンが有効な数値リテラルを構成しない限り許されます。
130 また、他の変な文字も '|' でシンボルを囲むことによってシンボル名に含めることが
131 できます (例:'@code{|this is a symbol|}')。
132 @ref{Symbols}も参照して下さい。
133 @c COMMON
134
135 @c EN
136 @item Numeric literals
137 Either integral part or fraction part of inexact real numbers can be omitted
138 if it is zero, i.e. @code{30.}, @code{.25}, @code{-.4} are read as
139 real numbers.
140 The number reader recognizes '@code{#}' as insignificant digits.
141 Complex numbers can be written both in the rectangular format
142 (e.g. @code{1+0.3i}) and in the polar format (e.g. @code{3.0@@1.57}).
143 Inexact real numbers include
144 the positive infinity, the negative infinity, and NaN,
145 which are represented as @code{+inf.0}, @code{-inf.0} and @code{+nan.0},
146 respectively. (@code{-nan.0} is also read as NaN.)
147
148 Gauche supports SRFI-169 (underscores in numbers) notation; you can
149 insert a character @code{_} between digits in numeric literals
150 to improve readability, e.g. @code{#b1100_1010_1111_1110}.
151 A valid underscore must be surrounded by digits; @code{1_2_3} is ok,
152 but @code{_123}, @code{123_}, and @code{12__3} are not.
153
154 Gauche also adopts Common-Lisp style radix prefixed numeric literals,
155 e.g. @code{#3r120} (@code{120} in base-3, 15 in decimal). Radix between
156 2 and 36 are recognized; alphabetic letters @code{a-zA-Z}
157 are used beyond decimal.
158
159 For the polar notation of complex numbers, Gauche allows the suffix @code{pi}
160 to denote the phase by multiples of pi. The Scheme syntax use radians for
161 the phase, but you can only approximate pi with the floating point numbers,
162 so it can't represent round numbers except zero angle.
163 @example
164 gosh> 2@@3.141592653589793
165 -2.0+2.4492935982947064e-16i
166 @end example
167 With the @code{pi} suffix, you can get a round numbers.
168 @example
169 gosh> 2@@1pi
170 -2.0
171 gosh> 2@@0.5pi
172 0.0+2.0i
173 gosh> 2@@-0.5pi
174 0.0-2.0i
175 @end example
176 @c JP
177 @item 数値リテラル
178 非正確な実数の整数部または小数部のいずれかが0の場合、それを省略することが
179 できます。例:@code{30.}, @code{.25}, @code{-.4}。
180 数値のリーダは、パディングの'@code{#}'を認識します。
181 複素数表記は直交座標形 (例:@code{1+0.3i}) でも極座標形 (例:@code{3.0@@1.57}) でも
182 認識されます。
183 非正確な実数は、正の無限大、負の無限大、非数(NaN)を含み、
184 それらはそれぞれ@code{+inf.0}、@code{-inf.0}、および@code{+nan.0}と
185 表記されます。(@code{-nan.0}も非数(NaN)として読まれます)。
186
187 Gaucheはまた、SRFI-169 (数値中のアンダースコア)構文をサポートしています。
188 読みやすくするために、数値中に@code{_}を挿入することができます
189 (例: @code{#b1100_1010_1111_1110})。
190 ただし、各アンダースコアは数字に囲まれていなければなりません。
191 @code{1_2_3}はokですが、@code{_123}、@code{123_}、@code{12__3}は
192 有効な数値リテラルではありません。
193
194 Gaucheはまた、Common Lisp風の基数を明示した数値リテラルも認識します。
195 例えば3進数の120は@code{#3r120}と書けます(10進数で15)。2進数から36進数まで
196 認識されます。10進数より上では@code{a-zA-Z}のアルファベットを使えます。
197
198 複素数の極座標記法において、Gaucheではサフィックス@code{pi}をつけることにより
199 偏角をπの係数で表すことができます。Schemeの構文では偏角はラジアンですが、
200 πは浮動小数点数では近似値でしか扱えないので、偏角ゼロ以外で切りの良い数値を
201 表現することができません。
202 @example
203 gosh> 2@@3.141592653589793
204 -2.0+2.4492935982947064e-16i
205 @end example
206 @code{pi}サフィックスを使うと切りの良い数値が書けます。
207 @example
208 gosh> 2@@1pi
209 -2.0
210 gosh> 2@@0.5pi
211 0.0+2.0i
212 gosh> 2@@-0.5pi
213 0.0-2.0i
214 @end example
215 @c COMMON
216
217 @c EN
218 @item Hex character escapes
219 @lxindex \x
220 You can denote a character using hexadecimal
221 notation of the character code in some literals; specifically,
222 character literals, charcter set literals, string literals,
223 symbols, regular expression literals.
224
225 R7RS adopted a hex escape notation @code{\xNNNN;} for strings
226 and symbols surrounded by vertical bars, and @code{#\xNNNN} for
227 characters. The number of digits is variable, and
228 the character code is Unicode codepoint.
229
230 Gauche had been using two types of escapes; @code{\u}
231 and @code{\x}. In general, @code{u} is for Unicode
232 codepoint, while @code{x} is for the character code in the internal
233 encoding. Besides, except character literals, we used fixed number
234 of digits, instead of using the terminator @code{;} as in R7RS.
235
236 Since 0.9.4, we interpret @code{\x}-escape as R7RS whenever if
237 it consists a valid R7RS hex-escape, and if not,
238 try to interpret it as legacy Gauche hex-escape.
239
240 Although rarely, there are cases that can interpreted
241 both in R7RS syntax and legacy Gauche syntax, but yielding different
242 characters. Reading legacy files with such literals in
243 the current Gauche may cause unexpected behavior.
244 You can switch the reader mode so that it becomes
245 backward-compatible. @xref{Reader lexical mode}, for the details.
246 @c JP
247 @item 16進数文字エスケープ
248 @lxindex \x
249 いくつかのリテラル中では、文字をその文字コードの16進数表記で記入できます。
250 具体的には、文字、文字集合、文字列、シンボル、正規表現リテラルです。
251
252 R7RSは文字列および縦棒で囲まれたシンボル中では@code{\xNNNN;}、文字単独では
253 @code{#\xNNNN}というエスケープ構文を採用しました。16進数の桁数は可変で、
254 文字コードはUnicodeコードポイントです。
255
256 Gaucheは歴史的に2種類のエスケープを使ってきました。
257 @code{\u}と@code{\x}です。
258 @code{u}はUnicodeコードポイントに、@code{x}は内部エンコーディングによる
259 文字コードに使っていました。また、文字リテラル以外では
260 16進数の桁数は固定で、したがってR7RSのような終端文字(@code{;})を必要としませんでした。
261
262 0.9.4からは、@code{\x}エスケープはそれが正しいR7RSの16進エスケープと解釈できる
263 限りはそう読まれます。R7RSの構文に合致しない場合は、以前のGaucheの構文とみなされます。
264
265 滅多にないことですが、R7RSとも以前のGauche構文ともみなすことができて、しかし解釈に違いが
266 生じるような場合というのが存在します。そのようなリテラルを含む昔のソースを
267 現在のGaucheで読むと、予想外の動作が起きるかもしれません。
268 その場合は、リーダのモードを以前との互換モードに変更できます。
269 詳細は@ref{Reader lexical mode}を参照してください。
270 @c COMMON
271
272 @c EN
273 @item Extended sharp syntax
274 Many more special tokens begins with '@code{#}' are defined.
275 See the table below.
276 @c JP
277 @item 拡張された#構文
278 '@code{#}' で始まる特殊構文がたくさん定義されています。
279 下の表を参照して下さい。
280 @c COMMON
281 @end table
282
283 @menu
284 * Sharp syntax::
285 * Hash-bang token::
286 @end menu
287
288 @node Sharp syntax, Hash-bang token, Lexical structure, Lexical structure
289 @subsection Sharp syntax
290 @c NODE #構文
291
292 @c EN
293 The table below lists sharp-syntaxes.
294 @c JP
295 以下の表はシャープ構文のリストです。
296 @c COMMON
297
298 @multitable {aaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
299 @item @code{#!}
300 @c EN
301 @tab [R6RS][R7RS][SRFI-22] It is either a beginning of an interpreter line
302 (shebang) of a script, or a special token that affects the mode
303 of the reader. See `hash-bang token' section below.
304 @c JP
305 @tab [R6RS][R7RS][SRFI-22] これは、スクリプトのインタプリタ指定行(shebang)の
306 開始を示すか、リーダのモードに影響を与える特別なトークンとして読まれます。
307 下の`hash-bang token'の節を参照してください。
308 @c COMMON
309 @item @code{#"}
310 @c EN
311 @tab Introduces an interpolated string. @xref{String interpolation}.
312 @c JP
313 @tab 補間された文字列を先導します。@ref{String interpolation}参照。
314 @c COMMON
315 @item @code{##}, @code{#$}, @code{#%}, @code{#&}, @code{#'}
316 @c EN
317 @tab Unused.
318 @c JP
319 @tab 未使用。
320 @c COMMON
321 @item @code{#(}
322 @c EN
323 @tab [R7RS] Introduces a vector.
324 @c JP
325 @tab [R7RS] リテラルベクタ表記の開始です。
326 @c COMMON
327 @item @code{#)}
328 @c EN
329 @tab Unused.
330 @c JP
331 @tab 未使用。
332 @c COMMON
333 @item @code{#*}
334 @c EN
335 @tab Bitvector or an incomplete string. @xref{Strings}.
336 @c JP
337 @tab ビットベクタあるいは不完全な文字列。@ref{Strings}参照。
338 @c COMMON
339 @item @code{#+}
340 @c EN
341 @tab Unused.
342 @c JP
343 @tab 未使用。
344 @c COMMON
345 @item @code{#,}
346 @c EN
347 @tab [SRFI-10] Introduces reader constructor syntax.
348 @c JP
349 @tab [SRFI-10] リーダ構築子構文を開始します。
350 @c COMMON
351 @item @code{#-}, @code{#.}
352 @c EN
353 @tab Unused.
354 @c JP
355 @tab 未使用。
356 @c COMMON
357 @item @code{#/}
358 @c EN
359 @tab Introduces a literal regular expression. @xref{Regular expressions}.
360 @c JP
361 @tab リテラルの正規表現を先導します。@ref{Regular expressions}参照。
362 @c COMMON
363 @item @code{#0} @dots{} @code{#9}
364 @c EN
365 @tab @code{#@var{n}#}, @code{#@var{n}=}: [SRFI-38] Shared substructure definition and reference.@*
366 @code{#@var{n}R}, @code{#@var{n}r}: Radix prefixed numeric literals.
367 @c JP
368 @tab @code{#@var{n}#}, @code{#@var{n}=}: [SRFI-38] 共有サブストラクチャの定義と参照。@*
369 @code{#@var{n}R}, @code{#@var{n}r}: 基数を明示した数値リテラル。
370 @c COMMON
371 @item @code{#:}
372 @c EN
373 @tab Uninterned symbol. @xref{Symbols}.
374 @c JP
375 @tab インターンされていないシンボル。@ref{Symbols}参照。
376 @c COMMON
377 @item @code{#;}
378 @c EN
379 @tab [SRFI-62] S-expression comment. Reads next one S-expression and
380 discard it.
381 @c JP
382 @tab [SRFI-62] S式コメント。続くS式をひとつ読んで捨てます。
383 @c COMMON
384 @item @code{#<}
385 @c EN
386 @tab Introduces an unreadable object.
387 @c JP
388 @tab 読み取り不可能オブジェクトを先導します。
389 @c COMMON
390 @item @code{#=}, @code{#>}
391 @c EN
392 @tab Unused.
393 @c JP
394 @tab 未使用。
395 @c COMMON
396 @item @code{#?}
397 @c EN
398 @tab Introduces debug macros. @xref{Debugging}.
399 @c JP
400 @tab デバッグマクロを先導します。@ref{Debugging}参照。
401 @c COMMON
402 @item @code{#@@}
403 @c EN
404 @tab Unused.
405 @c JP
406 @tab 未使用。
407 @c COMMON
408 @item @code{#a}
409 @c EN
410 @tab Unused.
411 @c JP
412 @tab 未使用。
413 @c COMMON
414 @item @code{#b}
415 @c EN
416 @tab [R7RS] Binary number prefix.
417 @c JP
418 @tab [R7RS] 2進数接頭辞。
419 @c COMMON
420 @item @code{#c}
421 @c EN
422 @tab Unused.
423 @c JP
424 @tab 未使用。
425 @c COMMON
426 @item @code{#d}
427 @c EN
428 @tab [R7RS] Decimal number prefix.
429 @c JP
430 @tab [R7RS] 10進数接頭辞。
431 @c COMMON
432 @item @code{#e}
433 @c EN
434 @tab [R7RS] Exact number prefix.
435 @c JP
436 @tab [R7RS] 正確数接頭辞。
437 @c COMMON
438 @item @code{#f}
439 @c EN
440 @tab [R7RS] Boolean false, or
441 introducing R7RS uniform vector. @xref{Uniform vectors}.
442 R7RS defines both @code{#f} and @code{#false} as a boolean false value.
443 @c JP
444 @tab [R7RS] 真理値の偽、あるいは
445 R7RS のユニフォームベクタを先導します。@ref{Uniform vectors}参照。
446 R7RSでは真理値の偽として@code{#f}と@code{#false}が使えます。
447 @c COMMON
448 @item @code{#g}, @code{#h}
449 @c EN
450 @tab Unused.
451 @c JP
452 @tab 未使用。
453 @c COMMON
454 @item @code{#i}
455 @c EN
456 @tab [R7RS] Inexact number prefix.
457 @c JP
458 @tab [R7RS] 非正確数接頭辞。
459 @c COMMON
460 @item @code{#j}, @code{#k}, @code{#l}, @code{#m}, @code{#n}
461 @c EN
462 @tab Unused.
463 @c JP
464 @tab 未使用。
465 @c COMMON
466 @item @code{#o}
467 @c EN
468 @tab [R7RS] Octal number prefix.
469 @c JP
470 @tab [R7RS] 8進数接頭辞。
471 @c COMMON
472 @item @code{#p}, @code{#q}, @code{#r}
473 @c EN
474 @tab Unused.
475 @c JP
476 @tab 未使用。
477 @c COMMON
478 @item @code{#s}
479 @c EN
480 @tab [R7RS vector.@@] introducing R7RS uniform vector. @xref{Uniform vectors}.
481 @c JP
482 @tab [R7RS vector.@@] R7RSのユニフォームベクタを先導します。@ref{Uniform vectors}参照。
483 @c COMMON
484 @item @code{#t}
485 @c EN
486 @tab [R7RS] Boolean true. R7RS defines @code{#t} and @code{#true}
487 as a boolean true value.
488 @c JP
489 @tab [R7RS] 真理値の真。R7RSでは真理値の真として@code{#t}と@code{#true}が使えます。
490 @c COMMON
491 @item @code{#u}
492 @c EN
493 @tab [R7RS vector.@@] introducing R7RS uniform vector. @xref{Uniform vectors}.
494 R7RS uses @code{#u8} prefix for bytevectors, which is compatible to
495 @code{u8} uniform vectors.
496 @c JP
497 @tab [R7RS vector.@@] R7RS のユニフォームベクタを先導します。@ref{Uniform vectors}参照。
498 R7RSもバイトベクタに@code{#u8}のプリフィクスを使いますが、これは
499 @code{u8}ユニフォームベクタと互換です。
500 @c COMMON
501 @item @code{#v}, @code{#w}
502 @c EN
503 @tab Unused.
504 @c JP
505 @tab 未使用。
506 @c COMMON
507 @item @code{#x}
508 @c EN
509 @tab [R7RS] Hexadecimal number prefix.
510 @c JP
511 @tab [R7RS] 16進数接頭辞
512 @c COMMON
513 @item @code{#y}, @code{#z}
514 @c EN
515 @tab Unused.
516 @c JP
517 @tab 未使用。
518 @c COMMON
519 @item @code{#[}
520 @c EN
521 @tab Introduces a literal character set. @xref{Character sets}.
522 @c JP
523 @tab リテラルの文字集合を先導します。@ref{Character sets}参照
524 @c COMMON
525 @item @code{#\}
526 @c EN
527 @tab [R7RS] Introduces a literal character. @xref{Characters}.
528 @c JP
529 @tab [R7RS] リテラルの文字を先導します。@ref{Characters}参照。
530 @c COMMON
531 @item @code{#]}, @code{#^}, @code{#_}
532 @c EN
533 @tab Unused.
534 @c JP
535 @tab 未使用。
536 @c COMMON
537 @item @code{#`}
538 @c EN
539 @tab Legacy syntax for string interpolation, superseded by @code{#"}.
540 @c JP
541 @tab 文字列補間の古い構文です。新しいコードでは@code{#"}を使ってください。
542 @c COMMON
543 @item @code{#@{}
544 @c EN
545 @tab Unused.
546 @c JP
547 @tab 未使用。
548 @c COMMON
549 @item @code{#|}
550 @c EN
551 @tab [SRFI-30] Introduces a block comment. Comment ends by matching '@code{|#}'.
552 @c JP
553 @tab [SRFI-30] ブロックコメントを先導します。コメントは対応する '@code{|#}' で終ります。
554 @c COMMON
555 @item @code{#@}}, @code{#~}
556 @c EN
557 @tab Unused.
558 @c JP
559 @tab 未使用。
560 @c COMMON
561 @end multitable
562
563 @menu
564 * Hash-bang token::
565 @end menu
566
567 @node Hash-bang token, , Sharp syntax, Lexical structure
568 @subsection Hash-bang token
569 @c NODE Hash-bangトークン
570 @lxindex #!
571
572 @c EN
573 A character sequence @code{#!} has two completely different
574 semantics, depending on how and where it occurs.
575
576 If a file begins with @code{#!/} or @code{#! } (hash, bang, and a
577 space), then the reader assumes it is an interpreter line (shebang) of
578 a script and ignores the rest of characters until the end of line.
579 (Actually the source doesn't need to be a file. The reader checks
580 whether it is the beginning of a port.)
581
582 Other than the above case, @code{#!@var{identifier}} is read as
583 a token with special meanings. This kind token can be a special
584 directive for the reader, instead of read as a datum.
585
586 By default, the following tokens are recognized.
587 @c JP
588 文字シーケンス@code{#!}は、どこでどのように現れるかによって2つの全く異なる
589 意味を持ちます。
590
591 ファイルが@code{#!/}もしくは@code{#! } (ハッシュ、バング、空白) から始
592 まっている場合、読み込みルーチンはそれをスクリプトのインタプリタ指定行
593 (shebang) とみなし、最初の行末までを無視します。
594 (実際は、ソースはファイルでなくても構いません。リーダはポートの先頭かどうかで
595 判断します。)
596
597 その場合以外では、@code{#!@var{identifier}}がひとつのトークンとして読ま
598 れ、特別な意味を持ちます。この種のトークンはデータとして読まれるのでは
599 なく、リーダに対して特別な指定を与えることもあります。
600
601 デフォルトでは、以下のトークンが認識されます。
602 @c COMMON
603
604 @table @code
605 @item #!fold-case
606 @itemx #!no-fold-case
607 @c EN
608 Switches the reader's case sensitivity;
609 @code{#!fold-case} makes the reader case insensitive, and
610 @code{#!no-fold-case} makes it case sensitive. (Also see
611 @ref{Case-sensitivity}).
612 @c JP
613 リーダが大文字小文字を区別するかどうかを切り替えます。
614 @code{#!fold-case}に出会うとリーダは大文字小
615 文字を区別しないモードになり、@code{#!no-fold-case}に出会うとその逆にな
616 ります。(@ref{Case-sensitivity}も参照してください。)
617 @c COMMON
618 @item #!r6rs
619 @c EN
620 This token is introduced in R6RS and used to indicate the
621 program strictly conforms R6RS. Gauche doesn't conform R6RS, but
622 currently it just issues warning when it sees @code{#!r6rs} token, and
623 it keeps reading on.
624 @c JP
625 このトークンはR6RSで導入され、プログラムがR6RSに準拠しているこ
626 とを示します。GaucheはR6RSに準拠していませんが、今のところこのトー
627 クンに出会っても警告を出すだけで読み込みは継続します。
628 @c COMMON
629 @item #!r7rs
630 @c EN
631 Make the reader @code{strict-r7} mode, that complies R7RS.
632 @xref{Reader lexical mode}, for the details.
633 @c JP
634 リーダをR7RSに準拠した@code{strict-r7}モードにします。
635 詳細は@ref{Reader lexical mode}を参照してください。
636 @c COMMON
637 @c TODO: Describe concrete details
638 @item #!gauche-legacy
639 @c EN
640 Make the reader @code{legacy} mode, that is
641 compatible to Gauche 0.9.3 and before.
642 @xref{Reader lexical mode}, for the details.
643 @c JP
644 リーダをGauche 0.9.3およびそれ以前のものと互換な@code{legacy}モードにします。
645 詳細は@ref{Reader lexical mode}を参照してください。
646 @c COMMON
647 @end table
648
649 @c ----------------------------------------------------------------------
650 @node Literals, Making procedures, Lexical structure, Core syntax
651 @section Literals
652 @c NODE リテラル
653
654 @defspec quote datum
655 [R7RS base]
656 @c EN
657 Evaluates to @var{datum}.
658 @c JP
659 @var{datum}それ自身を返します。
660 @c COMMON
661 @example
662 (quote x) @result{} x
663 (quote (1 2 3)) @result{} (1 2 3)
664 @end example
665 @end defspec
666
667 @deftp {Reader Syntax} @code{'@var{datum}}
668 @lxindex '
669 [R7RS]
670 @c EN
671 Equivalent to @code{(quote datum)}.
672 @c JP
673 @code{(quote datum)}の略記です。
674 @c COMMON
675 @example
676 'x @result{} x
677 '(1 2 3) @result{} (1 2 3)
678 @end example
679 @end deftp
680
681 @c EN
682 @emph{Note:} Literals are immutable. You'll get an error
683 if you try to change, for example, a quoted pair with @code{set-car!}
684 or a literal string with @code{string-set!}.
685 Mutability may be managed differently for each type, so some object
686 may not raise an error even if it appears in a part of literals
687 (it is often the case with user-defined class with read-time constructor,
688 @pxref{Read-time constructor}. However, if you ever modify a literal
689 data, it is not guaranteed that the program runs correctly.
690 @c JP
691 @emph{註:} リテラルは変更不可です。例えばクオートされたペアを@code{set-car!}に
692 渡したり、リテラル文字列を@code{string-set!}に渡すと、エラーが報告されるでしょう。
693 変更可能かどうかのチェックは型ごとに行われるので、時にはリテラルの一部であっても
694 変更でエラーにならないオブジェクトもあります
695 (読み込み時コンストラクタを持つユーザ定義クラスはそうです。
696 @ref{読み込み時コンストラクタ}参照。)
697 たとえエラーが発生しなくても、リテラルを変更しているプログラムの動作は
698 保証されません。
699 @c COMMON
700
701 @c ----------------------------------------------------------------------
702 @node Making procedures, Assignments, Literals, Core syntax
703 @section Making procedures
704 @c NODE 手続きを作る
705
706 @defspec lambda formals body @dots{}
707 @defspecx ^ formals body @dots{}
708 [R7RS+ base]
709 @c EN
710 Evaluates to a procedure. The environment in effect when this expression
711 is evaluated is stored in the procedure. When the procedure
712 is called, @var{body} is evaluated sequentially in the
713 stored environment extended by the bindings of the formal arguments,
714 and returns the value(s) of the last expression in the body.
715 @c JP
716 この式は評価されると手続きを生成します。この式が評価された時点の環境が手続き中に保持されます。
717 手続きが呼ばれると、記憶された環境に引数の束縛を追加した環境中で@var{body} が順に評価され、
718 最後の式の値が返されます。
719 @c COMMON
720
721 @c EN
722 @code{^} is a concise alias of @code{lambda}. It is Gauche's extension.
723 @c JP
724 @code{^}は@code{lambda}の短い別名です。これはGauche独自の拡張です。
725 @c COMMON
726
727 @example
728 (lambda (a b) (+ a b))
729 @c EN
730 @result{} @r{procedure that adds two arguments}
731 @c JP
732 @result{} @r{二つの引数を加算する手続き}
733 @c COMMON
734
735 ((lambda (a b) (+ a b)) 1 2) @result{} 3
736
737 ((^(a b) (+ a b)) 1 2) @result{} 3
738 @end example
739
740 @c EN
741 Gauche also extends R7RS @code{lambda} to take extended syntax in
742 @var{formals} to specify optional and keyword arguments easily.
743 The same functionality can be written in pure R7RS, with parsing
744 variable-length arguments explicitly, but the code tends
745 to be longer and verbose. It is recommended to use extended syntax
746 unless you're writing portable code.
747 @c JP
748 Gaucheはまた、@code{lambda}が取る@var{formals}の構文を拡張し、
749 省略可能引数やキーワード引数を簡単に指定できるようにしています。
750 同じ機能は純粋なR7RSでも、可変長引数を自力で解析することで実現可能ですが、
751 コードはより長く冗長になってしまいます。ポータブルなコードを書いているのでなければ
752 拡張構文を使った方が良いでしょう。
753 @c COMMON
754
755 @c EN
756 @var{Formals} should have one of the following forms:
757 @c JP
758 @var{Formals} は以下のいずれかの形でなければなりません。
759 @c COMMON
760 @itemize @bullet
761 @item
762 @code{(@var{variable} @dots{})} :
763 @c EN
764 The procedure takes a fixed number of arguments.
765 The actual arguments are bound to the corresponding variables.
766 @c JP
767 手続きは決まった数の引数を取ります。
768 実引数がそれぞれ対応する@var{variable}に束縛されます。
769 @c COMMON
770
771 @example
772 ((lambda (a) a) 1) @result{} 1
773
774 ((lambda (a) a) 1 2) @result{} @i{error - wrong number of arguments}
775 @end example
776
777 @item
778 @code{@var{variable}} :
779 @c EN
780 The procedure takes any number of arguments.
781 The actual arguments are collected to form a new list and bound to
782 the variable.
783 @c JP
784 手続きは不定個の引数を取ります。
785 実引数は新しいリストに集められて、そのリストが@var{variable}に束縛されます。
786 @c COMMON
787
788 @example
789 ((lambda a a) 1 2 3) @result{} (1 2 3)
790 @end example
791
792 @item
793 @code{(@var{variable_0} @dots{} @var{variable_N-1} . @var{variable_N})} :
794 @c EN
795 The procedure takes at least @var{N} arguments. The actual arguments
796 up to @var{N} is bound to the corresponding variables.
797 If more than @var{N} arguments are given, the rest arguments are
798 collected to form a new list and bound to @var{variable_N}.
799 @c JP
800 手続きは少なくとも@var{N}個の引数を取ります。@var{N}個までの実引数は対応する@var{variable}
801 に束縛されます。@var{N}個を越えた実引数は新しいリストに集められて
802 @var{variable_N}に束縛されます。
803 @c COMMON
804
805 @example
806 ((lambda (a b . c) (print "a=" a " b=" b " c=" c)) 1 2 3 4 5)
807 @result{} @r{prints} a=1 b=2 c=(3 4 5)
808 @end example
809
810 @item
811 @code{(@var{variable} @dots{} @var{extended-spec} @dots{})} :
812 @c EN
813 Extended argument specification. Zero or more variables that
814 specifies required formal arguments, followed
815 by an @emph{extended spec}, a list beginning with
816 a keyword @code{:optional}, @code{:key} or @code{:rest}.
817
818 The @var{extended-spec} part consists of the optional argument spec,
819 the keyword argument spec and the rest argument spec. They can
820 appear in any combinations.
821 @c JP
822 拡張引数指定です。ゼロ個以上の必須引数を示す変数のリストの後に、
823 キーワード@code{:optional}、@code{:key}あるいは@code{:rest}で
824 始まる@var{extended-spec}が続きます。
825
826 @var{extended-spec}は省略可能引数指定、キーワード引数指定、残余引数指定の
827 任意の組み合わせにより構成されます。
828 @c COMMON
829
830 @table @code
831 @item :optional @var{optspec} @dots{}
832 @c EN
833 Specifies optional arguments. Each @var{optspec} can be either
834 one of the following forms:
835 @c JP
836 省略可能引数を指定します。各@var{optspec}は以下のいずれかの形式です。
837 @c COMMON
838
839 @example
840 @var{variable}
841 (@var{variable} @var{init-expr})
842 @end example
843
844 @c EN
845 The @var{variable} names the formal argument, which is bound to
846 the value of the actual argument if given, or the value of
847 the expression @var{init-expr} otherwise. If @var{optspec} is just
848 a variable, and the actual argument is not given to it, then
849 @var{variable} will be bound to @code{#<undef>} (@pxref{Undefined values}).
850
851 The expression @var{init-expr} is only evaluated if the actual
852 argument for @var{variable} is not given. The scope in which
853 @var{init-expr} is evaluated includes the preceding formal arguments.
854 @c JP
855 仮引数@var{variable}は、対応する実引数が与えられればその値に、
856 与えられなければ@var{init-expr}を評価した値に束縛されます。
857 @var{optspec}が@code{@var{variable}}だけである場合は、
858 実引数が省略された時の仮引数の値は@code{#<undef>}になります
859 (@ref{Undefined values}参照)。
860
861 @var{init-expr}は対応する実引数が与えられなかった場合にのみ評価されます。
862 @var{init-expr}の評価環境にはこの@var{optspec}に先立つ仮引数が含まれます。
863 @c COMMON
864
865 @example
866 ((lambda (a b :optional (c (+ a b))) (list a b c))
867 1 2) @result{} (1 2 3)
868
869 ((lambda (a b :optional (c (+ a b))) (list a b c))
870 1 2 -1) @result{} (1 2 -1)
871
872 ((lambda (a b :optional c) (list a b c))
873 1 2) @result{} (1 2 #<undef>)
874
875 ((lambda (:optional (a 0) (b (+ a 1))) (list a b))
876 ) @result{} (0 1)
877 @end example
878
879 @c EN
880 The procedure signals an error if more actual arguments than
881 the number of required and optional arguments are given, unless it also has
882 @code{:key} or @code{:rest} argument spec.
883 @c JP
884 必須引数と省略可能引数の総数よりも多い実引数が渡された場合、
885 その手続きが@code{:key}や@code{:rest}引数指定を持っていない限りは
886 エラーが通知されます。
887 @c COMMON
888
889 @example
890 ((lambda (:optional a b) (list a b)) 1 2 3)
891 @result{} @i{error - too many arguments}
892
893 ((lambda (:optional a b :rest r) (list a b r)) 1 2 3)
894 @result{} (1 2 (3))
895 @end example
896
897 @item :key @var{keyspec} @dots{} [:allow-other-keys [@var{variable}]]
898 @c EN
899 Specifies keyword arguments. Each @var{keyspec} can be
900 either one of the following forms.
901 @c JP
902 キーワード引数を指定します。各@var{keyspec}は以下の形式のいずれかです。
903 @c COMMON
904
905 @example
906 @var{variable}
907 (@var{variable} @var{init-expr})
908 ((@var{keyword} @var{variable}) @var{init-expr})
909 @end example
910
911 @c EN
912 The @var{variable} names the formal argument, which is bound to the
913 actual argument given with the keyword of the same name as @var{variable}.
914 When the actual argument is not given, @var{init-expr} is evaluated
915 and the result is bound to @var{variable} in the second and third form,
916 or @code{#<undef>} is bound in the first form.
917 @c JP
918 仮引数@var{variable}は、実引数に@var{variable}と同名のキーワードによる
919 キーワード引数が与えられればその値に、そうでなければ@var{init-expr}を評価
920 した値に束縛されます。@var{init-expr}の無い最初の形式では、実引数が
921 与えられなければ@var{variable}は@code{#<undef>}に束縛されます。
922 @c COMMON
923
924 @example
925 (define f (lambda (a :key (b (+ a 1)) (c (+ b 1)))
926 (list a b c)))
927
928 (f 10) @result{} (10 11 12)
929 (f 10 :b 4) @result{} (10 4 5)
930 (f 10 :c 8) @result{} (10 11 8)
931 (f 10 :c 1 :b 3) @result{} (10 3 1)
932 @end example
933
934 @c EN
935 With the third form you can name the formal argument differently from
936 the keyword to specify the argument.
937 @c JP
938 三番目の形式では、仮引数の名前とは別に引数を与えるキーワードを指定できます。
939 @c COMMON
940
941 @example
942 ((lambda (:key ((:aa a) -1)) a) :aa 2)
943 @result{} 2
944 @end example
945
946 @c EN
947 By default, the procedure with keyword argument spec raises an
948 error if a keyword argument with an unrecognized keyword is given.
949 Giving @code{:allow-other-keys} in the formals suppresses this behavior.
950 If you give @var{variable} after @code{:allow-other-keys},
951 the list of unrecognized keywords and their arguments are bound to it.
952 Again, see the example below will help to understand the behavior.
953 @c JP
954 デフォルトでは、キーワード引数指定をもつ手続きは、認識できないキーワード引数が
955 与えられた場合にエラーを通知します。仮引数リストに@code{:allow-other-keys}を指定することで、
956 この動作を抑制することができます。
957 @code{:allow-other-keys}に続いて@var{variable}を与えれば、
958 認識できなかったキーワード引数のリストが@var{variable}に束縛されます。
959 @c COMMON
960
961 @example
962 ((lambda (:key a) a)
963 :a 1 :b 2) @result{} @i{error - unknown keyword :b}
964
965 ((lambda (:key a :allow-other-keys) a)
966 :a 1 :b 2) @result{} 1
967
968 ((lambda (:key a :allow-other-keys z) (list a z))
969 :a 1 :b 2) @result{} (1 (:b 2))
970 @end example
971
972 @c EN
973 When used with @code{:optional} argument spec, the keyword arguments
974 are searched after all the optional arguments are bound.
975 @c JP
976 @code{:optional}引数指定と同時に使われた場合、キーワード引数はすべての
977 省略可能引数が束縛された後の実引数リストから探されます。
978 @c COMMON
979
980 @example
981 ((lambda (:optional a b :key c) (list a b c))
982 1 2 :c 3) @result{} (1 2 3)
983
984 ((lambda (:optional a b :key c) (list a b c))
985 :c 3) @result{} (:c 3 #<undef>)
986
987 ((lambda (:optional a b :key c) (list a b c))
988 1 :c 3) @result{} @i{error - keyword list not even}
989 @end example
990
991
992 @item :rest @var{variable}
993
994 @c EN
995 Specifies the rest argument. If specified without @code{:optional}
996 argument spec, a list of remaining arguments after required arguments are
997 taken is bound to @var{variable}. If specified with @code{:optional}
998 argument spec, the actual arguments are first bound to required and
999 all optional arguments, and the remaining arguments are bound to @var{variable}.
1000 @c JP
1001 残余引数を指定します。@code{:optional}引数指定が無い場合は、
1002 必須引数の束縛が済んだ後で残っている実引数のリストが@var{variable}に
1003 束縛されます。@code{:optional}引数指定がある場合は、
1004 必須引数とすべての省略可能引数の束縛が済んだ後で残っている実引数の
1005 リストが@var{variable}に束縛されます。
1006 @c COMMON
1007
1008 @example
1009 ((lambda (a b :rest z) (list a b z))
1010 1 2 3 4 5) @result{} (1 2 (3 4 5))
1011
1012 ((lambda (a b :optional c d :rest z) (list a b c d z))
1013 1 2 3 4 5) @result{} (1 2 3 4 (5))
1014
1015 ((lambda (a b :optional c d :rest z) (list a b c d z))
1016 1 2 3) @result{} (1 2 3 #<undef> ())
1017 @end example
1018
1019 @c EN
1020 When the rest argument spec is used with the keyword argument spec,
1021 both accesses the same list of actual argument---the remaining arguments
1022 after required and optional arguments are taken.
1023 @c JP
1024 残余引数指定とキーワード引数指定の両方が与えられた場合、
1025 両者はともに、必須引数と省略可能引数が処理された後の実引数リストを処理の対象とします。
1026 @c COMMON
1027
1028 @example
1029 ((lambda (:optional a :rest r :key k) (list a r k))
1030 1 :k 3) @result{} (1 (:k 3) 3)
1031 @end example
1032
1033 @end table
1034
1035 @c EN
1036 See also @code{let-optionals*}, @code{let-keywords} and
1037 @code{let-keywords*} macros in @ref{Optional argument parsing}
1038 for an alternative way to receive optional/keyword arguments
1039 within the spec of R7RS.
1040 @c JP
1041 R7RSの仕様範囲内で省略可能引数、キーワード引数を受け取るように
1042 書くこともできます。@ref{Optional argument parsing}の
1043 @code{let-optionals*}、@code{let-keywords}、
1044 @code{let-keywords*}を参照してください。
1045 @c COMMON
1046
1047 @end itemize
1048
1049 @end defspec
1050
1051 @defmac ^c body @dots{}
1052 @findex ^_
1053 @findex ^a
1054 @findex ^b
1055 @findex ^d
1056 @findex ^e
1057 @findex ^f
1058 @findex ^g
1059 @findex ^h
1060 @findex ^i
1061 @findex ^j
1062 @findex ^k
1063 @findex ^l
1064 @findex ^m
1065 @findex ^n
1066 @findex ^o
1067 @findex ^p
1068 @findex ^q
1069 @findex ^r
1070 @findex ^s
1071 @findex ^t
1072 @findex ^u
1073 @findex ^v
1074 @findex ^w
1075 @findex ^x
1076 @findex ^y
1077 @findex ^z
1078 @c EN
1079 A shorthand notation of @code{(lambda (@var{c}) @var{body} @dots{})}.
1080 where @var{c} can be any character in @code{#[_a-z]}.
1081 @c JP
1082 @code{(lambda (@var{c}) @var{body} @dots{})}の短縮表記です。
1083 @var{c}には@code{#[_a-z]}に含まれる任意の一文字が使えます。
1084 @c COMMON
1085
1086 @example
1087 (map (^x (* x x)) '(1 2 3 4 5)) @result{} (1 4 9 16 25)
1088 @end example
1089 @end defmac
1090
1091
1092 @defmac cut expr-or-slot expr-or-slot2 @dots{}
1093 @defmacx cute expr-or-slot expr-or-slot2 @dots{}
1094 [SRFI-26]
1095 @c EN
1096 Convenience macros to notate a procedure compactly.
1097 This form can be used to realize partial application,
1098 a.k.a sectioning or projection.
1099 @c JP
1100 手続きを簡潔に書ける便利なマクロです。
1101 いわゆる部分適用を実現するために使えます。
1102 @c COMMON
1103
1104 @c EN
1105 Each @var{expr-or-slot} must be either an expression or a symbol @code{<>},
1106 indicating a 'slot'.
1107 The last @var{expr-or-slot} can be a symbol @code{<...>},
1108 indicating a 'rest-slot'.
1109 @code{Cut} expands into a @code{lambda} form that takes as many arguments
1110 as the number of slots in the given form, and
1111 whose body is an expression
1112 @example
1113 (@var{expr-or-slot} @var{expr-or-slot2} @dots{})
1114 @end example
1115 where each occurrence of @code{<>} is replaced to the corresponding
1116 argument.
1117 In case there is a rest-slot symbol, the resulting procedure is also
1118 of variable arity, and all the extra arguments are passed
1119 to the call of @var{expr-or-slot}. See the fourth example below.
1120 @c JP
1121 各@var{expr-or-slot}は式またはシンボル@code{<>}でなければなりません。
1122 最後の@var{expr-or-slot}はシンボル@code{<...>}であっても構いません。
1123 @code{cut}は、そのフォーム中にある@code{<>}と同じ数の引数を取る
1124 @code{lambda}フォームに展開されます。そのフォームのボディには次の式が置かれます。
1125 @example
1126 (@var{expr-or-slot} @var{expr-or-slot2} @dots{})
1127 @end example
1128 但し、各@code{<>}は対応する仮引数に置き換えられます。
1129 もしシンボル@code{<...>}が与えられていた場合、展開された@code{lambda}フォームは
1130 可変長引数を取る手続きとなり、作成される手続きに与えられたすべての引数が
1131 @var{expr-or-slot}を呼び出すのに使われます(下の4番目の例を参照のこと)。
1132 @c COMMON
1133
1134 @example
1135 (cut cons (+ a 1) <>) @equiv{} (lambda (x2) (cons (+ a 1) x2))
1136 (cut list 1 <> 3 <> 5) @equiv{} (lambda (x2 x4) (list 1 x2 3 x4 5))
1137 (cut list) @equiv{} (lambda () (list))
1138 (cut list 1 <> 3 <...>)
1139 @equiv{} (lambda (x2 . xs) (apply list 1 x2 3 xs))
1140 (cut <> a b) @equiv{} (lambda (f) (f a b))
1141
1142 ;; Usage
1143 (map (cut * 2 <>) '(1 2 3 4))
1144 (for-each (cut write <> port) exprs)
1145 @end example
1146
1147 @c EN
1148 @code{Cute} is a variation of @code{cut} that evaluates @var{expr-or-slot}s
1149 before creating the procedure.
1150 @c JP
1151 @code{cute}は@code{cut}とほぼ同じですが、@var{expr-or-slot}に与えられた
1152 フォームが手続きを作る前に評価される点が異なります。
1153 @c COMMON
1154
1155 @example
1156 (cute cons (+ a 1) <>)
1157 @equiv{} (let ((xa (+ a 1))) (lambda (x2) (cons xa x2)))
1158 @end example
1159
1160 @c EN
1161 Gauche provides a couple of different ways to write partial
1162 applications concisely; see the @code{$} macro below, and also
1163 the @code{pa$} procedure (@pxref{Combinators}).
1164 @c JP
1165 Gaucheには他にも二つほど、部分適用を簡潔に書く方法があります。
1166 下の@code{$}マクロ、及び組み込み手続き@code{pa$}です
1167 (@ref{Combinators}参照)。
1168 @c COMMON
1169 @end defmac
1170
1171 @defmac $ arg @dots{}
1172 @c EN
1173 A macro to chain applications, hinted from Haskell's @code{$} operator
1174 (although the meaning is different).
1175 Within the macro arguments @var{arg} @dots{}, @code{$} delimits
1176 the last argument. For example, the following code makes
1177 the last argument for the procedure @code{f} to be @code{(g c d @dots{})}
1178 @c JP
1179 関数適用をチェインするマクロです。Haskellの@code{$}にヒントを得ました
1180 (意味は異なりますが)。
1181 マクロ引数@var{arg} @dots{}中に@code{$}が出現すると、それが
1182 関数の最後の引数の区切りとなります。例えば次のコードでは、
1183 関数@code{f}の最後の引数が@code{(g c d @dots{})}となります。
1184 @c COMMON
1185 @example
1186 ($ f a b $ g c d ...)
1187 @equiv{} (f a b (g c d ...))
1188 @end example
1189
1190 @c EN
1191 The @code{$} notation can be chained.
1192 @c JP
1193 @code{$}はチェインすることができます。
1194 @c COMMON
1195
1196 @example
1197 ($ f a b $ g c d $ h e f ...)
1198 @equiv{} (f a b (g c d (h e f ...)))
1199 @end example
1200
1201 @c EN
1202 If @code{$*} appears in the argument list instead of @code{$},
1203 it fills the rest of the arguments, instead of just the last argument.
1204 @c JP
1205 @code{$}のかわりに@code{$*}が現れた場合は、最後の引数ひとつだけでなく
1206 「残りの引数全部」を示します。
1207 @c COMMON
1208
1209 @example
1210 ($ f a b $* g c d ...)
1211 @equiv{} (apply f a b (g c d ...))
1212
1213 ($ f a b $* g $ h $* hh ...)
1214 @equiv{} (apply f a b (g (apply h (hh ...))))
1215 @end example
1216
1217 @c EN
1218 Furthermore, if the argument list ends with @code{$} or @code{$*},
1219 the whole expression becomes a procedure expecting the last argument(s).
1220 @c JP
1221 さらに、もし引数リストが@code{$}または@code{$*}で終わっていた場合は、
1222 式全体が「残りの引数(リスト)」を受け取ることを期待する手続きとなります。
1223 @c COMMON
1224
1225 @example
1226 ($ f a b $ g c d $ h e f $)
1227 @equiv{} (lambda (arg) (f a b (g c d (h e f arg))))
1228 @equiv{} (.$ (cut f a b <>) (cut g c d <>) (cut h e f <>))
1229
1230 ($ f a b $ g c d $ h e f $*)
1231 @equiv{} (lambda args (f a b (g c d (apply h e f args))))
1232 @equiv{} (.$ (cut f a b <>) (cut g c d <>) (cut h e f <...>))
1233 @end example
1234
1235 @c EN
1236 The more functional the code becomes, the more you are
1237 tempted to write it as a chain of nested function calls.
1238 Scheme's syntax can get awkward in such code. Close
1239 parentheses tend to clutter at the end of expressions.
1240 Inner applications tends to pushed toward right columns
1241 with the standard indentation rules. Compare the
1242 following two code functionally equivalent to each other:
1243 @c JP
1244 関数的にコードを書いていると、関数呼び出しが深くネストすることが多くなります。
1245 しかしSchemeの構文はそのようなネストとあまり相性が良くありません。
1246 閉じ括弧が式の最後にずらずらと積み重なりますし、また通常のインデントルールでは
1247 コードが右のカラムにどんどん伸びていってしまいます。次の同等な二つの式を比べてみてください。
1248 @c COMMON
1249
1250 @example
1251 (intersperse ":"
1252 (map transform-it
1253 (delete-duplicates (map cdr
1254 (group-sequence input)))))
1255
1256 ($ intersperse ":"
1257 $ map transform-it
1258 $ delete-duplicates
1259 $ map cdr $ group-sequence input)
1260 @end example
1261
1262 @c EN
1263 It is purely a matter of taste, and also this kind of syntax sugars
1264 can be easily abused. Use with care, but it may work well if used
1265 sparingly, like spices.
1266 @c JP
1267 これはあくまで好みの問題ですし、こういった構文糖衣は濫用されがちなので
1268 気をつけてください。けれどもスパイスのように、控えめな隠し味として使うと、
1269 しばしばとても有用です。
1270 @c COMMON
1271
1272 @c EN
1273 As a corner case, if neither @code{$} nor @code{$*} appear in the
1274 argument list, it just calls the function. It is useful when
1275 the function has long name and you don't want to indent arguments
1276 too further right.
1277 @c JP
1278 コーナーケースとして、引数リストに@code{$}も@code{$*}も現れなかった場合は、
1279 単なる関数呼び出しになります。これだけでも、関数名が長くて引数をあまり右に
1280 深くインデントしたくない場合には便利です。
1281 @c COMMON
1282
1283 @example
1284 ($ f a b c) @equiv{} (f a b c)
1285 @end example
1286 @end defmac
1287
1288 @defmac case-lambda clause @dots{}
1289 [R7RS case-lambda]
1290 @c EN
1291 Each @var{clause} should have the form (@var{formals} @var{expr} @dots{}),
1292 where @var{formals} is a formal arguments list as for @code{lambda}.
1293
1294 This expression evaluates to a procedure that accepts a variable
1295 number of arguments and is lexically scoped in the same manner as
1296 procedures resulting from @code{lambda} expressions.
1297 When the procedure is called with some arguments,
1298 then the first @var{clause} for which the arguments agree with
1299 @var{formals} is selected, where agreement is specified as for
1300 the @var{formals} of a @code{lambda} expression.
1301 The variables of @code{formals} are bound to the given
1302 arguments, and the @var{expr} @dots{} are evaluated within the environment.
1303
1304 It is an error for the arguments not to agree with
1305 the @var{formals} of any @var{clause}.
1306 @c JP
1307 それぞれの @var{clause} は (@var{formals} @var{expr} @dots{}) という形式
1308 でなければなりません。ここで、@var{formals} は @code{lambda} の仮引数
1309 リストです。
1310
1311 この式は、評価されると可変数の引数をとり、@code{lambda} 式から結果として
1312 できる手続きと同じレキシカルスコープをもつ手続きになります。この手続きが
1313 いくつかの引数とともに呼ばれると、その引数が @var{formals} と一致する
1314 最初の @var{clause} が選択されます。この引数の一致というのは、
1315 @code{lambda} 式の @var{formals} に対応するものとして指定されるものです。
1316 @code{formals} の変数は実引数に束縛され、 @var{expr} @dots{} がその
1317 環境内で評価されます。
1318
1319 実際の引数がどの @var{clause} の @var{formals} にも一致しなければエラーです。
1320 @c COMMON
1321
1322 @example
1323 (define f
1324 (case-lambda
1325 [() 'zero]
1326 [(a) `(one ,a)]
1327 [(a b) `(two ,a ,b)]))
1328
1329 (f) @result{} zero
1330 (f 1) @result{} (one 1)
1331 (f 1 2) @result{} (two 1 2)
1332 (f 1 2 3) @result{} @r{Error: wrong number of arguments to case lambda}
1333
1334 (define g
1335 (case-lambda
1336 [() 'zero]
1337 [(a) `(one ,a)]
1338 [(a . b) `(more ,a ,@@b)]))
1339
1340 (g) @result{} zero
1341 (g 1) @result{} (one 1)
1342 (g 1 2 3) @result{} (more 1 2 3)
1343 @end example
1344
1345 @c EN
1346 Note that the clauses are examined sequentially to match the number of
1347 arguments, so in the following example @code{g2} never returns @code{(one ...)}.
1348 @c JP
1349 各@var{clause}は順に引数の個数とマッチするかどうか調べられます。次の例では
1350 @code{g2}が@code{(one ...)}を返すことはありません。
1351 @c COMMON
1352
1353 @example
1354 (define g2
1355 (case-lambda
1356 [() 'zero]
1357 [(a . b) `(more ,a ,@@b)]
1358 [(a) `(one ,a)]))
1359
1360 (g2 1) @result{} (more 1)
1361 @end example
1362 @end defmac
1363
1364 @c ----------------------------------------------------------------------
1365 @node Assignments, Conditionals, Making procedures, Core syntax
1366 @section Assignments
1367 @c NODE 代入
1368
1369 @defspec set! symbol expression
1370 @defspecx set! (proc arg @dots{}) expression
1371 [R7RS+ base][SRFI-17]
1372 @c EN
1373 First, @var{expression} is evaluated.
1374 In the first form, the binding of @var{symbol} is modified
1375 so that next reference of @var{symbol} will return
1376 the result of @var{expression}.
1377 If @var{symbol} is not locally bound, the global variable named @var{symbol}
1378 must already exist, or an error is signaled.
1379 @c JP
1380 まず@var{expression}が評価されます。最初の形式では、@var{symbol}への束縛が
1381 @var{expression}の結果を指すように変更されます。
1382 もし@var{symbol}がローカルに束縛されていない場合は、グローバルな変数@var{symbol}が
1383 存在していなければなりません。そうでなければエラーが報告されます。
1384 @c COMMON
1385
1386 @c EN
1387 The second form is a ``generalized set!'' specified in SRFI-17.
1388 It is a syntactic sugar of the following form.
1389 @example
1390 ((setter @var{proc}) @var{arg} @dots{} @var{expression})
1391 @end example
1392 Note the order of the arguments of the setter method
1393 differs from CommonLisp's @code{setf}.
1394
1395 Some examples:
1396 @c JP
1397 2番目の形式はSRFI-17に定義されている「一般化されたset!」です。
1398 これは構文的な装飾であり、実際は以下のように解釈されます。
1399 @example
1400 ((setter @var{proc}) @var{arg} @dots{} @var{expression})
1401 @end example
1402 CommonLispの@code{setf}とは、setter手続きに渡される引数の順序が異なることに注意して下さい。
1403
1404 例:
1405 @c COMMON
1406
1407 @example
1408 (define x 3)
1409 (set! x (list 1 2))
1410 x @result{} (1 2)
1411
1412 (set! (car x) 5)
1413 x @result{} (5 2)
1414 @end example
1415 @end defspec
1416
1417 @defmac set!-values (var @dots{}) expr
1418 @defmacx set!-values (var @dots{} . varn) expr
1419 @defmacx set!-values var expr
1420 [SRFI-210]
1421 @c EN
1422 Sets values of multiple variables at once. @var{Expr} must yield
1423 as many values as @var{var} @dots{}. Each value is set to
1424 the corresponding @var{var}.
1425 @c JP
1426 複数の変数を同時に更新します。@var{Expr}は与えられた変数と同じ
1427 数だけの値を生成しなけばなりません。各値が対応する@var{var}に
1428 セットされます。
1429 @c COMMON
1430 @example
1431 (define a 0)
1432 (define b 1)
1433 (define c 2)
1434 (set!-values (a b) (values 3 4))
1435 a @result{} 3
1436 b @result{} 4
1437 (set!-values (a b) (values b a))
1438 a @result{} 4
1439 b @result{} 3
1440 (set!-values (a b . c) (values 1 2 3 4))
1441 a @result{} 1
1442 b @result{} 2
1443 c @result{} (3 4)
1444 @end example
1445
1446 @c EN
1447 See also @code{define-values} (@ref{Definitions}).
1448 @c JP
1449 @code{define-values}も参照 (@ref{Definitions})。
1450 @c COMMON
1451 @end defmac
1452
1453 @defun setter proc
1454 [SRFI-17]
1455 @c EN
1456 Returns a setter procedure associated to the procedure @var{proc}.
1457 If no setter is associated to @var{proc}, its behavior is undefined.
1458
1459 A setter procedure @var{g} of a procedure @var{f} is such that when used
1460 as @code{(g a b @dots{} v)}, the next evaluation of @code{(f a b @dots{})}
1461 returns @var{v}.
1462
1463 To associate a setter procedure to another procedure, you can use
1464 the setter of @code{setter}, like this:
1465 @example
1466 (set! (setter @var{f}) @var{g})
1467 @end example
1468
1469 A procedure's setter can be ``locked'' to it. System default setters,
1470 like @code{set-car!} for @code{car}, is locked and can't be set by
1471 the above way. In order to lock a setter to a user defined procedure,
1472 use @code{getter-with-setter} below.
1473
1474 If @var{proc} is not a procedure, a setter generic function
1475 of @code{object-apply} is returned;
1476 it allows the applicable object extension to work
1477 seamlessly with the generalized @code{set!}.
1478 @xref{Applicable objects}, for the details.
1479 @c JP
1480 手続き@var{proc}のsetter手続きを返します。
1481 @var{proc}がsetter手続きを持たない場合の動作は未定義です。
1482
1483 ある関数@var{f}のsetter手続き@var{g}とは、もし@code{(g a b @dots{} v)}のように
1484 呼ばれた場合、次の@code{(f a b @dots{})}が@var{v}を返すようになる手続きのことです。
1485
1486 ある手続きにsetter手続きを関連付けるには、@code{setter}自身のsetter手続きを使うことが
1487 できます。
1488 @example
1489 (set! (setter @var{f}) @var{g})
1490 @end example
1491
1492 特定の手続きにsetter手続きを「ロック」することができます。システム既定のsetter手続き、
1493 例えば@code{car}に対する@code{set-car!}等はロックされていて、上記のような方法で
1494 変更することは出来ません。ユーザ定義手続きにsetter手続きをロックするには下記の
1495 @code{getter-with-setter}を使います。
1496
1497 @var{proc}が手続きでない場合は、@code{object-apply}ジェネリックファンクションの
1498 setterが返されます。これにより、適用可能オブジェクトが一般化された@code{set!}
1499 でもうまく動作します。詳しくは@ref{Applicable objects}を参照して下さい。
1500 @c COMMON
1501 @end defun
1502
1503 @defun has-setter? proc
1504 @c EN
1505 Returns @code{#t} if a setter is associated to @var{proc}.
1506 @c JP
1507 @var{proc}がsetter手続きを持っている場合は@code{#t}を返します。
1508 @c COMMON
1509 @end defun
1510
1511 @defun getter-with-setter get set
1512 [SRFI-17]
1513 @c EN
1514 Takes two procedure @var{get} and @var{set}. Returns a new
1515 procedure which does the same thing as @var{get}, and its setter is
1516 locked to @var{set}.
1517
1518 When an inlinable procedure has a locked setter, generalized @code{set!}
1519 may be inlined by the compiler. @xref{Definitions}, for inlinable
1520 procedure definition.
1521 @c JP
1522 2つの手続き、@var{get}と@var{set}を取り、新しい手続きを返します。
1523 新しい手続きは@var{get}と同じ動作をし、そのsetter手続きは@var{set}にロックされて
1524 います。
1525
1526 インライン化可能な手続きにsetterがロックされている場合、一般化された@code{set!}は
1527 コンパイラによってインライン展開される場合があります。
1528 インライン化可能な手続きの定義については@ref{Definitions}を参照してください。
1529 @c COMMON
1530 @end defun
1531
1532 @c EN
1533 A few macros that adopts the same semantics of generalized @code{set!}
1534 are also provided. They are built on top of @code{set!}.
1535 @c JP
1536 Gaucheでは、一般化された@code{set!}と同じセマンティクスを持ついくつかのマクロが定義
1537 されています。これらは@var{set!}を使った形に展開されます。
1538 @c COMMON
1539
1540 @defmac push! place item
1541 @c EN
1542 Conses @var{item}
1543 and the value of @var{place}, then sets the result to @var{place}.
1544 @var{place} is either a variable or a form
1545 @code{(@var{proc} @var{arg} @dots{})},
1546 as the second argument of @code{set!}. The result of this form
1547 is undefined.
1548 @c JP
1549 @var{item}と@var{place}の値をコンスし、その結果を再び@var{place}にセットします。
1550 @var{place}は@code{set!}と同様に、
1551 変数か@code{(@var{proc} @var{arg} @dots{})}という形式でなければなりません。
1552 このマクロの戻り値は未定義です。
1553 @c COMMON
1554
1555 @example
1556 (define x (list 2))
1557 (push! x 3)
1558 x @result{} (3 2)
1559
1560 (push! (cdr x) 4)
1561 x @result{} (3 4 2)
1562 @end example
1563
1564 @c EN
1565 When @var{place} is a list, it roughly expands like the following.
1566 @c JP
1567 @var{place}がリストの場合、だいたいこんなふうに展開されます。
1568 @c COMMON
1569
1570 @example
1571 (push! (foo x y) item)
1572 @equiv{}
1573 (let ((tfoo foo)
1574 (tx x)
1575 (ty y))
1576 ((setter tfoo) tx ty (cons item (tfoo tx ty))))
1577 @end example
1578
1579 @c EN
1580 Note: Common Lisp's @code{push} macro takes its argument reverse
1581 order. I adopted this order since it is consistent with other
1582 destructive operations. Perl's @code{push} function takes the same
1583 argument order, but it appends @var{item} at the end of the array
1584 (Perl's @code{unshift} is closer to @code{push!}).
1585 You can use a queue (@pxref{Queue}) if you need a behavior of Perl's
1586 @code{push}.
1587 @c JP
1588 註:Common Lispの@code{push}マクロは引数を逆の順番で取ります。
1589 @code{push!}は他の副作用を持つ形式との互換性を考えてこの順番としました。
1590 Perlの@code{push}関数は@code{push!}マクロと同じ引数順ですが、
1591 @var{item}はシーケンスの末尾に追加されます (Perlの@code{unshift}の方が
1592 @code{push!}の動作に近いです)。
1593 Perlのpushオペレータの動作が必要ならQueueが使えます (@ref{Queue}参照)。
1594 @c COMMON
1595 @end defmac
1596
1597 @defmac push-unique! place item [equal]
1598 @c EN
1599 The value in @var{place} must be a list. If @var{item} isn't
1600 already in the value in @var{place}, @var{place} is updated
1601 with the cons of @var{item} and its previous value. If @var{item}
1602 is already in the value, nothing is done.
1603
1604 The equivalence is checked with @var{equal} procedure. If omitted,
1605 @code{eqv?} is assumed.
1606
1607 This is similar to Common Lisp's @code{pushnew}, but the argument
1608 order is flipped.
1609
1610 See also @code{enqueue-unique!} and @code{queue-push-unique!}
1611 in @code{data.queue} (@ref{Queue}).
1612 @c JP
1613 @var{place}の値はリストでなければなりません。@var{item}がそのリスト中に
1614 なければ、@var{place}が@var{item}を先頭にコンスした値に更新されます。
1615 @var{item}が既に@var{place}の値の中にあれば、何も起きません。
1616
1617 @var{item}が既にある値と等しいかどうかは@var{equal}手続きで比較されます。
1618 省略された場合は@code{eqv?}が使われます。
1619
1620 Common Lispの@code{pushnew}に似ていますが、引数の順序が異なります。
1621
1622 @code{data.queue}の@code{enqueue-unique!}と@code{queue-push-unique!}も
1623 参照してください(@ref{Queue})。
1624 @c COMMON
1625
1626 @example
1627 (define v (vector '("a")))
1628
1629 (push-unique! (vector-ref v 0) "b")
1630 v @result{} #(("b" "a"))
1631
1632 (push-unique! (vector-ref v 0) "A" string-ci=?)
1633 v @result{} #(("b" "a"))
1634
1635 (push-unique! (vector-ref v 0) "A")
1636 v @result{} #(("A" "b" "a"))
1637 @end example
1638 @end defmac
1639
1640 @defmac pop! place
1641 @c EN
1642 Retrieves the value of @var{place}, sets its @code{cdr} back to @var{place}
1643 and returns its @code{car}.
1644 @c JP
1645 @var{place}の値を取り出し、その@code{cdr}を@var{place}にセットします。
1646 元の値の@code{car}を返します。
1647 @c COMMON
1648
1649 @example
1650 (define x (list 1 2 3))
1651 (pop! x) @result{} 1
1652 x @result{} (2 3)
1653
1654 (define x (vector (list 1 2 3)))
1655 x @result{} #((1 2 3))
1656 (pop! (vector-ref x 0)) @result{} 1
1657 x @result{} #((2 3))
1658 @end example
1659
1660 @c EN
1661 Note: This works the same as Common Lisp's @code{pop}.
1662 Perl's @code{pop} pops value from the end of the sequence;
1663 its @code{shift} does the same thing as @code{pop!}.
1664 @c JP
1665 註:この動作はCommon Lispの@code{pop}と同じです。
1666 Perlの@code{pop}はシーケンスの末尾から値を取ります。
1667 Perlなら@code{shift}が@code{pop!}の動作に近いです。
1668 @c COMMON
1669 @end defmac
1670
1671
1672 @defmac inc! place :optional delta
1673 @defmacx dec! place :optional delta
1674 @c EN
1675 Evaluates the value of @var{place}. It should be a number.
1676 Adds (@code{inc!}) or subtracts (@code{dec!}) @var{delta} to/from it,
1677 and then stores the result to @var{place}. The default value of
1678 @var{delta} is 1.
1679
1680 This is like Common Lisp's @code{incf} and @code{decf}, except
1681 that you can't use the result of @code{inc!} and @code{dec!}.
1682 @c JP
1683 @var{place}の値を評価します。それは数値にならなければなりません。
1684 その値に@var{delta}が加算(@code{inc!})もしくは減算(@code{dec!})され、
1685 結果が@var{place}に格納されます。@var{delta}の既定値は1です。
1686
1687 Common Lispの@code{incf}と@code{decf}に似ていますが、
1688 戻り値を使うことは出来ません。
1689 @c COMMON
1690 @end defmac
1691
1692 @defmac update! place proc
1693 @c EN
1694 Generalized form of @code{push!} etc. @var{Proc} must be a procedure
1695 which takes one argument and returns one value. The original value of
1696 @var{place} is passed to the @var{proc}, then its result is set to
1697 @var{place}.
1698 @c JP
1699 @code{push!}等のマクロの一般化された形式です。
1700 @var{proc}は一つの引数を取り、一つの値を返す手続きでなければなりません。
1701 @var{place}の値が@var{proc}に渡され、@var{proc}の結果が@var{place}に格納されます。
1702 @c COMMON
1703 @example
1704 (define a (cons 2 3))
1705 (update! (car a) (lambda (v) (* v 3)))
1706 a @result{} (6 . 3)
1707
1708 (update! (cdr a) (cut - <> 3))
1709 a @result{} (6 . 0)
1710 @end example
1711 @end defmac
1712
1713 @defmac rotate! place0 place1 @dots{} placeN
1714 @c EN
1715 It is roughly equivalent to:
1716 @c JP
1717 次のコードと概ね同じです:
1718 @c COMMON
1719
1720 @example
1721 (let ((tmp0 place0) (tmp1 place1) ... (tmpN placeN))
1722 (set! place0 tmp1)
1723 (set! place1 tmp2)
1724 :
1725 (set! placeN tmp0))
1726 @end example
1727
1728 @c EN
1729 However, if @code{place} is the form @code{(proc arg ...)}, then
1730 each @var{arg} is evaluated only once.
1731 @c JP
1732 但し、@code{place}が@code{(proc arg ...)}の形であった場合、
1733 各@var{arg}は1度しか評価されません。
1734 @c COMMON
1735
1736 @c EN
1737 It can be used to swap values of two locations:
1738 @c JP
1739 二ヶ所の値を交換するのにも使えます:
1740 @c COMMON
1741
1742 @example
1743 (let ((x (vector 1 2)))
1744 (rotate! (~ x 0) (~ x 1))
1745 x) @result{} #(2 1)
1746 @end example
1747
1748 @c EN
1749 This is similar to Common Lisp's @code{rotatef}.
1750 @c JP
1751 これはCommon Lispの@code{rotatef}に似ています。
1752 @c COMMON
1753 @end defmac
1754
1755 @c ----------------------------------------------------------------------
1756 @node Conditionals, Binding constructs, Assignments, Core syntax
1757 @section Conditionals
1758 @c NODE 条件式
1759
1760 @defspec if test consequent alternative
1761 @defspecx if test consequent
1762 [R7RS base]
1763 @c EN
1764 @var{Test} is evaluated. If it yields a true value,
1765 @var{consequent} is evaluated. Otherwise, @var{alternative} is
1766 evaluated. If @var{alternative} is not provided, it results undefined value.
1767 @c JP
1768 まず@code{test}を評価し、それが真の値を返したら@var{consequent}を評価します。
1769 そうでなければ@var{alternative}を評価します。もし@var{alternative}が与えられて
1770 いなければ未定義の値を返します。
1771 @c COMMON
1772
1773 @example
1774 (if (number? 3) 'yes 'no) @result{} yes
1775 (if (number? #f) 'yes 'no) @result{} no
1776
1777 (let ((x '(1 . 2)))
1778 (if (pair? x)
1779 (values (car x) (cdr x))
1780 (values #f #f)))
1781 @result{} 1 @r{and} 2
1782 @end example
1783 @end defspec
1784
1785 @defspec cond clause1 clause2 @dots{}
1786 [R7RS+ base][SRFI-61]
1787 @c EN
1788 Each @var{clause} must be the form
1789 @example
1790 (@var{test} @var{expr} @dots{})
1791 (@var{test} => @var{expr})
1792 (@var{test} @var{guard} => @var{expr})
1793 (else @var{expr} @var{expr2} @dots{})
1794 @end example
1795 The last form can appear only as the last clause.
1796
1797 @code{cond} evaluates @var{test} of each clauses in order, until
1798 it yields a true value. Once it yields true, if the clause
1799 is the first form, the corresponding @var{expr}s are evaluated
1800 and the result(s) of last @var{expr} is(are) returned; if the clause
1801 is the second form, the @var{expr} is evaluated and it must yield
1802 a procedure that takes one argument. Then the result of @var{test}
1803 is passed to it, and the result(s) it returns will be returned.
1804
1805 The third form is specified in SRFI-61. In this form, @var{test}
1806 can yield arbitrary number of values. The result(s)
1807 of @var{test} is(are) passed to @var{guard}; if it returns a true value,
1808 @var{expr} is applied with an equivalent argument list, and its
1809 result(s) is(are) returned. If @var{guard} returns @code{#f},
1810 the evaluation proceeds to the next clause.
1811
1812 If no test yields true, and the last clause is not the fourth form
1813 (else clause), an undefined value is returned.
1814
1815 If the last clause is else clause and all tests are failed,
1816 @var{expr}s in the else clause are evaluated, and its last
1817 @var{expr}'s result(s) is(are) returned.
1818 @c JP
1819 各@var{clause}節は次のいずれかの形式でなければなりません。
1820 @example
1821 (@var{test} @var{expr} @dots{})
1822 (@var{test} => @var{expr})
1823 (@var{test} @var{guard} => @var{expr})
1824 (else @var{expr} @var{expr2} @dots{})
1825 @end example
1826 最後の形式は最後の節のみに許されます。
1827
1828 最初の節から順に@var{test}が評価され、それが真の値を返すまで次の節の@var{test}が
1829 評価されます。@var{test}が真の値を返したら、それが最初の形式であれば
1830 その節の@var{expr}が順に評価され、最後の評価値が戻り値となります。
1831 それが2番目の形式であれば、@var{expr}がまず評価されます。
1832 @var{expr}は引数をひとつ取る手続きを返さねばなりません。
1833 続いて、@var{test}の結果がその手続きに渡され、その手続きの戻り値が@code{cond}形式の
1834 評価値となります。
1835
1836 3番目の形式はSRFI-61で定義されています。この形式では、@var{test}は
1837 任意の数の値に評価されることができます。それらの値がまず
1838 @var{guard}に渡され、もし@var{guard}が真の値を返したら、同じ引数が@var{expr}に
1839 適用されて、その戻り値が@code{cond}形式の評価値となります。
1840 @var{guard}が@code{#f}を返した場合は次の節へと評価が進みます。
1841 @var{guard}と@var{expr}
1842 は、@var{test}が返すのと同数の引数を取れなければいけません。
1843
1844 もし全てのテストが偽の値を返し、最後の節が4番目の形式(else節)でなければ、未定義の値が返されます。
1845
1846 最後の節が@code{else}節で、他の全てのテストが失敗した場合、@code{else}節の@var{expr}
1847 が順に評価され、その最後の値が@code{cond}形式の値となります。
1848 @c COMMON
1849
1850 @example
1851 (cond ((> 3 2) 'greater)
1852 ((< 3 2) 'less)) @result{} greater
1853 (cond ((> 3 3) 'greater)
1854 ((< 3 3) 'less)
1855 (else 'equal)) @result{} equal
1856 (cond ((assv 'b '((a 1) (b 2))) => cadr)
1857 (else #f)) @result{} 2
1858 @end example
1859 @end defspec
1860
1861
1862 @defspec case key-expr clause1 clause2 @dots{}
1863 [R7RS+ base][SRFI-87]
1864 @c EN
1865 @var{Key-expr} may be any expression that yields one value.
1866 Each @var{clause} should have the form
1867 @example
1868 ((@var{datum} ...) @var{expr} @var{expr2} @dots{})
1869 ((@var{datum} ...) => @var{proc})
1870 @end example
1871 where each @var{datum} is an external representation of some object.
1872 All the @var{datum}s must be distinct. The last @var{clause}
1873 may be an ``else clause,'' which has the form
1874 @example
1875 (else @var{expr} @var{expr2} @dots{})
1876 (else => @var{proc})
1877 @end example
1878
1879 First, @var{key-expr} is evaluated and its result is compared against each
1880 @var{datum}. If the result of evaluating @var{key-expr} is equivalent
1881 (using @code{eqv?}, @pxref{Equality}) to a @var{datum},
1882 then the expressions in the corresponding clause are evaluated
1883 sequentially, and the result(s) of the last expression in the
1884 @var{clause} is(are) returned from the case expression.
1885 The forms containing @code{=>} are specified in SRFI-87. In these
1886 forms, the result of @var{key-expr} is passed to @var{proc}, and its
1887 result(s) is(are) returned from the case expression.
1888
1889 If the result of evaluating @var{key-expr} is different from every @var{datum},
1890 then if there is an else clause its expressions are evaluated and
1891 the result(s) of the last is(are) the result(s) of the case expression;
1892 otherwise the result of the case expression is undefined.
1893 @c JP
1894 @var{key-expr}は1つの値を生成する任意の式です。
1895 @var{clause}は以下の形式でなければなりません。
1896 @example
1897 ((@var{datum} ...) @var{expr} @var{expr2} @dots{})
1898 ((@var{datum} ...) => @var{proc})
1899 @end example
1900 ここで、各@var{datum}はSchemeオブジェクトの外部表現であり、全ての@var{datum}は
1901 異なっていなければなりません。最後の@var{clause}には次の形式を持つelse節が許されます。
1902 @example
1903 (else @var{expr} @var{expr2} @dots{})
1904 (else => @var{proc})
1905 @end example
1906
1907 まず@var{key-expr}が評価され、その結果がそれぞれの@var{datum}と比較されます。
1908 @var{key-expr}の値と@code{eqv?}(@ref{Equality}参照)を使って一致する@var{datum}
1909 が見つかれば、対応する@var{expr}が順に評価され、その最後の値が@code{case}の
1910 値となります。@code{=>}を含む節はSRFI-87で定義されています。これらの節では、
1911 @var{key-expr}の結果が@var{proc}に渡され、その結果が@code{case}の値となります。
1912
1913 もし一致する@var{datum}が見つからない場合、else節が与えられていれば
1914 その@var{expr}が順に評価され、最後の値が返されます。else節がなければ@code{case}節
1915 の値は未定義です。
1916
1917 @c COMMON
1918 @example
1919 (case (* 2 3)
1920 ((2 3 5 7) 'prime)
1921 ((1 4 6 8 9) 'composite)) @result{} composite
1922
1923 (case (car '(c d))
1924 ((a) 'a)
1925 ((b) 'b)) @result{} @r{undefined}
1926
1927 (case (car '(c d))
1928 ((a e i o u) 'vowel)
1929 ((w y) 'semivowel)
1930 (else 'consonant)) @result{} consonant
1931
1932 (case 6
1933 ((2 4 6 8) => (cut + <> 1))
1934 (else => (cut - <> 1))) @result{} 7
1935
1936 (case 5
1937 ((2 4 6 8) => (cut + <> 1))
1938 (else => (cut - <> 1))) @result{} 4
1939 @end example
1940 @end defspec
1941
1942
1943 @defmac ecase key-expr clause1 clause2 @dots{}
1944 @c EN
1945 This works exactly like @code{case}, except when there's
1946 no @code{else} clause and the value of @var{key-expr} expression
1947 doesn't match any of @var{datum}s provided in @var{clause}s.
1948 While @code{case} form returns undefined value for such case,
1949 @code{ecase} raises an error.
1950
1951 It is taken from Common Lisp. It's a convenient form when
1952 you want to detect unexpected value getting passed just in case.
1953 @c JP
1954 このフォームはほぼ@code{case}と同等ですが、@code{else}節が与えられず、
1955 @var{key-expr}の値が@var{clause}中のどの@var{datum}とも一致しなかった場合の
1956 動作だけが異なります。@code{case}ではそのような場合は未定義値が返されますが、
1957 @code{ecase}はエラーを報告します。
1958
1959 このマクロはCommon Lispから採られました。想定外の値が渡されることを
1960 念のために検出したい、という場合に便利です。
1961 @c COMMON
1962
1963 @smallexample
1964 (ecase 5 ((1) 'a) ((2 3) 'b) ((4) 'c))
1965 @result{} ERROR: ecase test fell through: got 5, expecting one of (1 2 3 4)
1966 @end smallexample
1967 @end defmac
1968
1969 @defmac typecase key-expr clause1 clause2 @dots{}
1970 @c EN
1971 This is taken from Common Lisp.
1972 @var{Key-expr} is a Scheme expression yielding one value. Each @var{clause}
1973 must have either one of the following forms. The @code{else} clause
1974 can only appear as the last clause, if any.
1975 @c JP
1976 これはCommon Lispから採られました。
1977 @var{key-expr}はひとつの値を生成するScheme式です。
1978 各@var{clause}は次のいずれかの形式でなければなりません。
1979 @code{else}節は、現れるなら最後の@var{clause}でなければなりません。
1980 @c COMMON
1981
1982 @example
1983 (@var{type-expr} @var{expr} @dots{})
1984 (else @var{expr1} @var{expr2} @dots{})
1985 @end example
1986
1987 @c EN
1988 Each @var{type-expr} must be a type expression (@pxref{Types and classes}).
1989 @c JP
1990 各@var{type-expr}は型の式でなければなりません (@ref{Types and classes}参照)。
1991 @c COMMON
1992
1993 @c EN
1994 This macro first evaluates @var{key-expr}, then tests if its value is
1995 of the type @var{type-expr}, in turn. If the type constraint is satisfied,
1996 the @var{expr} @dots{} in the clause is evaluated and the last result(s)
1997 becomes the value of @code{typecase}. Type constrant is checked with
1998 @code{of-type?} (@pxref{Generic type predicates}).
1999 If none of the type constrait is
2000 satisfied, @var{expr}s in the @code{else} clause is evaluated if any,
2001 or an undefined value results.
2002 @c JP
2003 このマクロはまず@var{key-expr}を評価し、次にその値が型@var{type-expr}であるかどうかを
2004 順番に検査してゆきます。もし型の制約が満たされたら、その節にある@var{expr} @dots{}
2005 が評価され、その最後の値が@code{typecase}の値となります。
2006 型の制約は@code{of-type?}で検査されます (@ref{Generic type predicates}参照)。
2007 どの節の型でもなかった場合、@code{else}節があればその中の式が順に評価されて最後の値が
2008 @code{typecase}の値となり、@code{else}節がなければ未定義値が返されます。
2009 @c COMMON
2010
2011 @example
2012 (typecase key-expr
2013 (<integer> 'a)
2014 ((</> <string> <symbol>) 'b))
2015 @equiv{}
2016 (let ((tmp key-expr))
2017 (cond
2018 ((of-type? tmp <integer>) 'a)
2019 ((of-type? tmp (</> <string> <symbol>)) 'b)))
2020 @end example
2021 @end defmac
2022
2023 @defmac etypecase key-expr clause1 clause2 @dots{}
2024 @c EN
2025 Like @code{typecase}, but if the value of @var{key-expr}
2026 satisfies no @var{type-expr} and there's no @code{else} clause,
2027 an error is signaled. If there's an @code{else} clause,
2028 it is the same as @code{typecase}.
2029 @c JP
2030 @code{typecase}と似ていますが、@var{key-expr}の結果が
2031 どの@var{type-expr}の型でもなく、また@code{else}節がなかった場合、
2032 エラーが投げられます。@code{else}節がある場合は
2033 @code{typecase}と同じです。
2034 @c COMMON
2035
2036 @example
2037 (etypecase (sqrt -2)
2038 (<integer> 'int)
2039 (<real> 'real))
2040 @result{} *** ERROR: etypecase fell through: expecting one of types
2041 in (<integer> <real>), but got 0.0+1.4142135623730951i
2042 @end example
2043 @end defmac
2044
2045
2046 @defspec and test @dots{}
2047 [R7RS base]
2048 @c EN
2049 The @var{test} expressions are evaluated from left to right,
2050 and the value of the first expression that evaluates to a false
2051 value is returned.
2052 Any remaining expressions are not evaluated.
2053 If all the expressions evaluate to true values,
2054 the value of the last expression is returned.
2055 If there are no expressions then @code{#t} is returned.
2056 @c JP
2057 @var{test}式が順に評価されます。最初に偽の値を返したところで評価が止まり、
2058 偽の値が返されます。残りの式は評価されません。
2059 もし全ての式が真の値を返した場合は、最後の式の値が返されます。
2060 式が与えれない場合は@code{#t}が返されます。
2061 @c COMMON
2062
2063 @example
2064 (and (= 2 2) (> 2 1)) @result{} #t
2065 (and (= 2 2) (< 2 1)) @result{} #f
2066 (and 1 2 'c '(f g)) @result{} (f g)
2067 (and) @result{} #t
2068 @end example
2069 @end defspec
2070
2071
2072 @defspec or test @dots{}
2073 [R7RS base]
2074 @c EN
2075 The @var{test} expressions are evaluated from left to right,
2076 and the value of the first expression that evaluates to a true
2077 value is returned. Any remaining expressions are not evaluated.
2078 If all expressions evaluate to false values, the value of the
2079 last expression is returned. If there are no expressions then
2080 @code{#f} is returned.
2081 @c JP
2082 @var{test}式が順に評価されます。最初に真の値を返したところで評価が止まり、
2083 その値が返されます。残りの式は評価されません。
2084 もし全ての式が偽の値を返した場合は、偽の値が返されます。
2085 式が与えれない場合は@code{#f}が返されます。
2086 @c COMMON
2087
2088 @example
2089 (or (= 2 2) (> 2 1)) @result{} #t
2090 (or (= 2 2) (< 2 1)) @result{} #t
2091 (or #f #f #f) @result{} #f
2092 (or (memq 'b '(a b c))
2093 (/ 3 0)) @result{} (b c)
2094 @end example
2095 @end defspec
2096
2097 @defspec when test expr1 expr2 @dots{}
2098 @defspecx unless test expr1 expr2 @dots{}
2099 [R7RS base]
2100 @c EN
2101 Evaluates @var{test}. If it yields true value (or false in case of
2102 @code{unless}), @var{expr1} and @var{expr2} @dots{} are evaluated
2103 sequentially, and the result(s) of the last evaluation is(are) returned.
2104 Otherwise, undefined value is returned.
2105 @c JP
2106 まず@var{test}が評価されます。それが真の値(@code{unless}の場合は偽の値)を返した場合、
2107 引続き@var{expr1}および@var{expr2} @dots{}が順に評価され、最後の評価値が返されます。
2108 そうでなければ、未定義の値が返されます。
2109 @c COMMON
2110 @end defspec
2111
2112 @defmac assume test-expr [message obj @dots{}]
2113 [SRFI-145]
2114 @c EN
2115 Evaluates @var{text-expr} and returns its value.
2116
2117 Also, this form declares the programmer's intent that the code following
2118 this path always satisfy @var{test-expr}.
2119
2120 Currently, Gauche always signals an error
2121 if @var{text-expr} evaluates to @code{#f}.
2122 @c JP
2123 @var{test-expr}を評価してその値を返します。
2124
2125 また、このフォームに続くコードパスにおいて、@var{test-expr}が常に満たされている、
2126 というプログラマの意図を宣言します。
2127
2128 Gaucheでは今のところ、@var{text-expr}が偽となった場合は常にエラーが投げられます。
2129 @c COMMON
2130
2131 @example
2132 (define (rsqrt x)
2133 (assume (and (real? x) (>= x 0)))
2134 (sqrt x))
2135
2136 gosh> (rsqrt -1)
2137 *** ERROR: Invalid assumption: (and (real? x) (>= x 0))
2138 @end example
2139
2140 @c EN
2141 If optional arguments @var{message} @var{obj} @dots{} are passed, they
2142 are given to the @code{error} procedure when @var{test-expr} yields
2143 false.
2144 @c JP
2145 省略可能な引数 @var{message} @var{obj} @dots{} が与えられた場合、
2146 それらは@var{test-expr}が偽を返した際に@code{error}の引数に使われます。
2147 @c COMMON
2148
2149 @example
2150 (define (rsqrt x)
2151 (assume (and (real? x) (>= x 0))
2152 "Argument must be nonnegative real number, but got:" x)
2153 (sqrt x))
2154
2155 gosh> (rsqrt -1)
2156 *** ERROR: Argument must be nonnegative real number, but got: -1
2157 @end example
2158
2159 @c EN
2160 Note: This form is advisory---it isn't guaranteed for an error to be
2161 signaled when @var{test-expr} fails.
2162 For example, we may add an optimization
2163 option that omits testing in speed-optimized code in future.
2164 We may also enhance the compiler to generate better code using the
2165 given information---for example, in the above @code{real-sqrt} code,
2166 the compiler could theoretically deduce that @code{(sqrt x)} only needs to
2167 work as real functions, so it would be able to generate specialized code.
2168 Use this form to inform the compiler and the reader your intention.
2169 @c JP
2170 註: このフォームは勧告として機能します。
2171 @var{test-expr}が失敗してもエラーが投げられるとは限りません。例えば、
2172 将来、最適化オプションをつけて、
2173 速度最適化レベルが高い時はテストを省略するようになるかもしれません。
2174 また一方で、この情報を利用してコンパイラがより良いコードを出すように拡張される
2175 可能性もあります。例えば上の@code{real-sqrt}の例では、コンパイラは理論的には
2176 @code{(sqrt x)}が実関数として動作すれば良いということを演繹できるため、
2177 実数に特化したコードを生成できる可能性があります。
2178 このフォームを、コードの書き手の意図をコンパイラやコードの読み手に知らせるために
2179 用いると良いでしょう。
2180 @c COMMON
2181 @end defmac
2182
2183 @defmac assume-type expr type [message obj @dots{}]
2184 @c EN
2185 Evaluates @var{expr}, and Checks if the value has type @var{type}.
2186 If not, raises an error. The result of @var{expr} is returned.
2187 @c JP
2188 @var{expr}を評価し、その値が型@var{type}かどうかチェックします。
2189 型が合わなければエラーを投げます。@var{expr}の値が返されます。
2190 @c COMMON
2191
2192 @c EN
2193 As @var{type}, you can specify a Gauche class or a
2194 descriptive type. The value is of type
2195 if it satisfies @code{(of-type? value type)}
2196 (@pxref{Types and classes}).
2197 @c JP
2198 @var{type}にはGaucheのクラスか記述的な型を指定できます。
2199 値がその型かどうかは@code{(of-type? value type)}で判定されます
2200 (@ref{Types and classes}参照)。
2201 @c COMMON
2202
2203 @c EN
2204 If optional arguments @var{message} @var{obj} @dots{} are passed, they
2205 are given to the @code{error} procedure when @var{test-expr} yields
2206 false.
2207 @c JP
2208 省略可能な引数 @var{message} @var{obj} @dots{} が与えられた場合、
2209 それらは@var{test-expr}が偽を返した際に@code{error}の引数に使われます。
2210 @c COMMON
2211
2212 @example
2213 (define (strlen s)
2214 (assume-type s <string>)
2215 (string-length s))
2216
2217 gosh> (strlen 4)
2218 *** ERROR: s is supposed to be of type #<class <string>>, but got 4
2219
2220 (define (strlen s)
2221 (assume-type s <string> "Argument s must be a string, but got:" s)
2222 (string-length s))
2223
2224 gosh> (strlen 4)
2225 *** ERROR: Argument s must be a string, but got: 4
2226 @end example
2227
2228 @c EN
2229 The type assumption may be used by the compiler
2230 future compilers for optimizations.
2231 In order for the compiler to use the type constraint information,
2232 @var{type} must be an expression statically computable at compile-time.
2233 That is, it must be either a class or a type constructor expression,
2234 or a constant binding to them.
2235 @c JP
2236 このフォームによる情報は、将来のコンパイラで最適化に使われるかもしれません。
2237 コンパイラが型制約の情報を利用するためには、@var{type}が静的に型へと計算可能でなければ
2238 なりません。すなわち、クラスか、型コンストラクタか、それらへのconstantな束縛です。
2239 @c COMMON
2240
2241 @c EN
2242 Note: Like @code{assume}, this form is advisory; it is not guaranteed
2243 that the check is performed, nor @var{expr} is evaluated.
2244 @c JP
2245 註: @code{assume}と同様、このフォームも勧告として機能します。
2246 チェックがなされるか、@var{expr}が評価されるかどうかは保証されません。
2247 @c COMMON
2248 @end defmac
2249
2250 @node Binding constructs, Grouping, Conditionals, Core syntax
2251 @section Binding constructs
2252 @c NODE 変数束縛
2253
2254 @defspec let ((var expr) @dots{}) body @dots{}
2255 @defspecx let* ((var expr) @dots{}) body @dots{}
2256 @defspecx letrec ((var expr) @dots{}) body @dots{}
2257 @defspecx letrec* ((var expr) @dots{}) body @dots{}
2258 [R7RS base]
2259 @c EN
2260 Creates a local scope where @var{var} @dots{} are bound to the
2261 value of @var{expr} @dots{}, then evaluates @var{body} @dots{}.
2262 @var{Var}s must be symbols, and there shouldn't be duplicates.
2263 The value(s) of the last expression of @var{body} @dots{} becomes
2264 the value(s) of this form.
2265 @c JP
2266 各変数@var{var}が@var{expr}の値に束縛されているローカルな環境を作成し、
2267 その中で@var{body} @dots{}を評価します。@var{var}はシンボルでなければ
2268 ならず、重複があってはなりません。@var{body} @dots{}の最後の式の値が
2269 このフォームの値となります。
2270 @c COMMON
2271
2272 @c EN
2273 The four forms differ in terms of the scope and the order
2274 @var{expr}s are evaluated.
2275 @code{Let} evaluates @var{expr}s before (outside of) @code{let} form.
2276 The order of evaluation of @var{expr}s is undefined, and the compiler
2277 may reorder those @var{expr}s freely for optimization.
2278 @code{Let*} evaluates @var{expr}s, in the order they appears,
2279 and each @var{expr} is evaluated in the scope
2280 where @var{var}s before it are bound.
2281
2282 @code{Letrec} evaluates @var{expr}s, in an undefined order,
2283 in the environment where @var{var}s are already bound
2284 (to an undefined value, initially).
2285 @code{letrec} is necessary to define mutually recursive local procedures.
2286 Finally, @code{letrec*} uses the same scope rule as @code{letrec},
2287 and it evaluates @var{expr} in the order of appearance.
2288 @c JP
2289 これらの4つのフォームの違いは、@var{expr}が評価される時点のスコープと順序にあります。
2290 @code{let}は@var{expr}を@var{let}フォームに入る前の環境において評価します。
2291 各@var{expr}の評価される順序は不定で、コンパイラは最適化のために自由に順序を
2292 変更することがあります。
2293 一方、@code{let*}は@var{expr}を現れた順に、
2294 それ以前の@var{var}が束縛された環境において評価してゆきます。
2295
2296 @code{letrec}は全ての@var{var}が仮想的に不定の値に束縛された環境において
2297 各@var{expr}を(順不同で)評価します。
2298 @code{letrec}は相互再帰的なローカル関数を定義する場合に必要です。
2299 最後に、@code{letrec*}は@code{letrec}と同じスコープ規則を使い、
2300 さらに@var{expr}を現れる順に評価するものです。
2301 @c COMMON
2302
2303 @example
2304 (define x 'top-x)
2305
2306 (let ((x 3) (y x)) (cons x y)) @result{} (3 . top-x)
2307 (let* ((x 3) (y x)) (cons x y)) @result{} (3 . 3)
2308
2309 (let ((cons (lambda (a b) (+ a b)))
2310 (list (lambda (a b) (cons a (cons b 0)))))
2311 (list 1 2)) @result{} (1 2 . 0)
2312
2313 (letrec ((cons (lambda (a b) (+ a b)))
2314 (list (lambda (a b) (cons a (cons b 0)))))
2315 (list 1 2)) @result{} 3
2316 @end example
2317
2318 @c EN
2319 You need to use @code{letrec*} if evaluation of one @var{expr} requires
2320 the value of @var{var} that appears before the @var{expr}. In the following
2321 example, calculating the value of @var{a} and @var{b} requires the value
2322 of @var{cube}, so you need @code{letrec*}. (Note the difference from
2323 the above example, where @emph{calculating} the value of @var{list}
2324 doesn't need to take the value of @var{cons} bound in the same @code{letrec}.
2325 The value of @var{cons} isn't required until @var{list} is actually applied.)
2326 @c JP
2327 ひとつの@var{expr}を評価するために、それまでに現れた@var{var}の値を参照する
2328 必要がある時は、@code{letrec*}を使わなければなりません。
2329 下の例では、@var{a}と@var{b}の値を計算するために@var{cube}の値を
2330 使っているので、@code{letrec*}にする必要があります。
2331 (上の例との違いに注意してください。上の例では、@var{list}の値を@emph{計算する}
2332 時には、同時に束縛されている@var{cons}の値は必要とされません。@var{cons}の値が
2333 必要になるのは@var{list}が実際に適用される時です。)
2334 @c COMMON
2335
2336 @example
2337 (letrec* ((cube (lambda (x) (* x x x)))
2338 (a (+ (cube 1) (cube 12)))
2339 (b (+ (cube 9) (cube 10))))
2340 (= a b)) @result{} #t
2341 @end example
2342
2343 @c EN
2344 This example happens to work with @code{letrec} in the current Gauche, but
2345 it is not guaranteed to keep working in future. You just should not
2346 rely on evaluation order when you use @code{letrec}.
2347 In retrospect, it would be a lot simpler if we only had @code{letrec*}.
2348 Unfortunately @code{letrec} preceded for long time in Scheme history
2349 and it's hard to remove that. Besides, @code{letrec} does have more
2350 opportunities to optimize than @code{letrec*}.
2351 @c JP
2352 この例は現在のGaucheでは@code{letrec}を使ってもたまたま動作しますが、
2353 将来にわたって動作し続けることは保証されません。
2354 @code{letrec}を使うなら評価順に依存しないように、プログラマが気をつけないといけません。
2355 振り返ってみれば、@code{letrec*}だけが提供されてた方が単純だったでしょう。
2356 生憎、Schemeの歴史の中では@code{letrec}の方がずっと前からあったので、いまさら
2357 取り除くこともできないのです。また、@code{letrec*}に出来ない最適化が
2358 @code{letrec}だと可能になることもあります。
2359 @c COMMON
2360 @end defspec
2361
2362 @defmac let1 var expr body @dots{}
2363 @c EN
2364 A convenient macro when you have only one variable.
2365 Expanded as follows.
2366 @c JP
2367 変数が一つしか無い場合の便利なマクロです。次のように展開されます。
2368 @c COMMON
2369
2370 @example
2371 (let ((@var{var} @var{expr})) @var{body} @dots{})
2372 @end example
2373 @end defmac
2374
2375 @defmac if-let1 var expr then
2376 @defmacx if-let1 var expr then else
2377 @c EN
2378 This macro simplifies the following idiom:
2379 @c JP
2380 このマクロは次のようなイディオムを簡素化します。
2381 @c COMMON
2382 @example
2383 (let1 @var{var} @var{expr}
2384 (if @var{var} @var{then} @var{else}))
2385 @end example
2386 @end defmac
2387
2388 @defmac rlet1 var expr body @dots{}
2389 @c EN
2390 This macro simplifies the following idiom:
2391 @c JP
2392 このマクロは次のようなイディオムを簡素化します。
2393 @c COMMON
2394 @example
2395 (let1 @var{var} @var{expr}
2396 @var{body} @dots{}
2397 @var{var})
2398 @end example
2399 @end defmac
2400
2401 @defmac and-let* (binding @dots{}) body @dots{}
2402 [SRFI-2]
2403 @c EN
2404 In short, it works like @code{let*}, but returns @code{#f} immediately
2405 whenever the expression in @var{binding}s evaluates to @code{#f}.
2406 @c JP
2407 簡単に言うと、このフォームは@code{let*}のように動作しますが、
2408 @var{bindings}中の式が@code{#f}に評価されたらそこで評価を打ち切り
2409 @code{#f}を返します。
2410 @c COMMON
2411
2412 @c EN
2413 Each @var{binding} should be one of the following form:
2414 @c JP
2415 各@var{binding}は以下のいずれかの形式でなければなりません。
2416 @c COMMON
2417 @table @code
2418 @item (@var{variable} @var{expression})
2419 @c EN
2420 The @var{expression} is evaluated; if it yields true value, the value
2421 is bound to @var{variable}, then proceed to the next binding. If
2422 no more bindings, evaluates @var{body} @dots{}. If @var{expression}
2423 yields @code{#f}, stops evaluation and returns @code{#f} from @code{and-let*}.
2424 @c JP
2425 @var{expression}が評価されます。それが真の値を返したら、その値が@var{variable}
2426 に束縛され、次の@var{binding}へと進みます。もう@var{binding}が無ければ
2427 @var{body} @dots{}が評価されます。もし@var{expression}が@code{#f}を返したら、
2428 評価を打ち切り、@code{and-let*}から@code{#f}を返します。
2429 @c COMMON
2430 @item (@var{expression}x)
2431 @c EN
2432 In this form, @var{variable} is omitted. @var{Expression} is evaluated
2433 and the result is used just to determine whether we continue or
2434 stop further evaluation.
2435 @c JP
2436 この形式では@var{variable}が省略されています。@var{Expression}が評価され、
2437 その結果は評価を続行するか打ち切るかを判断するためにのみ使われます。
2438 @c COMMON
2439 @item @var{bound-variable}
2440 @c EN
2441 In this form, @var{bound-variable} should be an identifier denoting
2442 a bound variable. If its value is not @code{#f}, we continue
2443 the evaluation of the clauses.
2444 @c JP
2445 この形式では@var{bound-variable}は束縛変数を示す識別子でなければなりません。
2446 その変数の値が偽でなければ評価を続行します。
2447 @c COMMON
2448 @end table
2449
2450 @c EN
2451 Let's see some examples. The following code searches @var{key}
2452 from an assoc-list @var{alist} and returns its value if found.
2453 @c JP
2454 いくつか例を挙げます。次のコードは連想リスト@var{alist}から@var{key}を
2455 探し、見つかったらその値を返します。
2456 @c COMMON
2457 @example
2458 (and-let* ((entry (assoc key alist))) (cdr entry))
2459 @end example
2460
2461 @c EN
2462 If @var{arg} is a string representation of an exact integer, returns its value;
2463 otherwise, returns 0:
2464 @c JP
2465 もし@var{arg}が正確な整数の文字列表現だった場合は@var{num}を返し、そうでなければ
2466 0を返します:
2467 @c COMMON
2468 @example
2469 (or (and-let* ((num (string->number arg))
2470 ( (exact? num) )
2471 ( (integer? num) ))
2472 num)
2473 0)
2474 @end example
2475
2476 @c EN
2477 The following is a hypothetical code that searches a certain server port
2478 number from a few possibilities (environment variable, configuration file,
2479 ...)
2480 @c JP
2481 以下のコードはとあるサーバーのポート番号をいくつかの可能性
2482 (環境変数、設定ファイル…)の中から探す仮想的なコードです。
2483 @c COMMON
2484 @example
2485 (or (and-let* ((val (sys-getenv "SERVER_PORT")))
2486 (string->number val))
2487 (and-let* ((portfile (expand-path "~/.server_port"))
2488 ( (file-exists? portfile) )
2489 (val (call-with-input-string portfile port->string)))
2490 (string->number val))
2491 8080) ; default
2492 @end example
2493
2494 @end defmac
2495
2496 @defmac and-let1 var test exp1 exp2 @dots{}
2497 @c EN
2498 Evaluates @var{test}, and if it isn't @code{#f}, binds @var{var} to it
2499 and evaluates @var{exp1} @var{exp2} @dots{}. Returns the result(s) of
2500 the last expression. If @var{test} evaluates to @code{#f}, returns @code{#f}.
2501
2502 This can be easily written by @code{and-let*} or @code{if-let1} as follows.
2503 However, we've written this idiom so many times that it deserves
2504 another macro.
2505 @c JP
2506 @var{test}を評価し、それが@code{#f}でなければ@var{var}をその値に束縛して
2507 @var{exp1} @var{exp2} @dots{}を評価します。戻り値は最後の式の値です。
2508 @var{test}が@code{#f}だった場合は単に@code{#f}を返します。
2509
2510 これは@code{and-let*}や@code{if-let1}を使って次のとおり書くこともできます。
2511 しかし、このパターンを書くことがあまりに多いため、専用のマクロを用意する価値が
2512 あると判断しました。
2513 @c COMMON
2514
2515 @example
2516 (and-let1 var test
2517 exp1
2518 exp2 @dots{})
2519
2520 @equiv{}
2521
2522 (and-let* ([var test])
2523 exp1
2524 exp2 @dots{})
2525
2526 @equiv{}
2527
2528 (if-let1 var test
2529 (begin exp1 exp2 @dots{})
2530 #f)
2531 @end example
2532 @end defmac
2533
2534 @defmac fluid-let ((var val) @dots{}) body @dots{}
2535 @c EN
2536 A macro that emulates dynamic scoped variables.
2537 @var{Var}s must be variables bound in the scope including
2538 @code{fluid-let} form. @var{Val}s are expressions.
2539 @code{Fluid-let} first evaluates @var{val}s, then
2540 evaluates @var{body} @dots{}, with binding
2541 @var{var}s to the corresponding values during the dynamic
2542 scope of @var{body} @dots{}.
2543
2544 Note that, in multithreaded environment,
2545 the change of the value of @var{var}s are visible from
2546 all the threads. This form is provided mainly for
2547 the porting convenience. Use parameter objects instead
2548 (@pxref{Parameters}) for thread-local dynamic state.
2549 @c JP
2550 動的スコープの変数をエミュレートするマクロです。
2551 @var{var}は@code{fluid-let}フォームを含むスコープで定義されている
2552 変数でなければなりません。@var{val}は式です。
2553 @code{fluid-let}はまず@var{val}を評価し、
2554 @var{val}を@var{var}に動的スコープで束縛して@var{body} @dots{} を評価します。
2555
2556 マルチスレッド環境下では、@var{var}の値の変化は全てのスレッドから見えます。
2557 このフォームは主として他の処理系のコードを移植する際の利便性のために
2558 追加されました。スレッドローカルな動的状態を実現するには、
2559 パラメータオブジェクト(@ref{Parameters}参照)を
2560 使って下さい。
2561 @c COMMON
2562 @example
2563 (define x 0)
2564
2565 (define (print-x) (print x))
2566
2567 (fluid-let ((x 1))
2568 (print-x)) @result{} ;; prints 1
2569 @end example
2570 @end defmac
2571
2572 @defspec receive formals expression body @dots{}
2573 [SRFI-8]
2574 @c EN
2575 This is the way to receive multiple values.
2576 @var{Formals} can be a (maybe-improper) list of symbols.
2577 @var{Expression} is evaluated, and the returned value(s)
2578 are bound to @var{formals} like the binding of lambda formals,
2579 then @var{body} @dots{} are evaluated.
2580 @c JP
2581 この構文により、多値を受け取ることができます。
2582 @var{formals}はシンボルのリストです。不完全なリストであっても構いません。
2583 @var{expression}が評価され、返された値がlambda形式の引数の束縛と
2584 同じようにして@var{formals}内の変数と束縛され、その環境下で@var{body} @dots{}が
2585 評価されます。
2586 @c COMMON
2587
2588 @example
2589 (define (divrem n m)
2590 (values (quotient n m) (remainder n m)))
2591
2592 (receive (q r) (divrem 13 4) (list q r))
2593 @result{} (3 1)
2594
2595 (receive all (divrem 13 4) all)
2596 @result{} (3 1)
2597
2598 (receive (q . rest) (divrem 13 4) (list q rest))
2599 @result{} (3 (1))
2600 @end example
2601
2602 @c EN
2603 See also @code{call-with-values} in @ref{Multiple values}
2604 which is the procedural equivalent of @code{receive}.
2605 You can use @code{define-values} (@pxref{Definitions}) to
2606 bind multiple values to variables simultaneously.
2607 Also @code{let-values} and @code{let*-values} below
2608 provides @code{let}-like syntax with multiple values.
2609 @c JP
2610 なお、@ref{Multiple values}の@code{call-with-values}は
2611 @code{receive}と等価な手続き的インタフェースです。
2612 多値を複数の変数に同時に束縛するには、@code{define-values}
2613 (@ref{Definitions}参照) が使えます。
2614 また、下の@code{let-values}と
2615 @code{let*-values}は@code{let}のような形式で多値を扱うことができます。
2616 @c COMMON
2617 @end defspec
2618
2619 @defmac let-values ((vars expr) @dots{}) body @dots{}
2620 [R7RS base]
2621 @c EN
2622 @var{vars} are a list of variables. @var{expr} is evaluated, and
2623 its first return value is bound to the first variable in @var{vars},
2624 its second return value to the second variable, and so on, then
2625 @var{body} is evaluated.
2626 The scope of @var{expr}s are the outside of @code{let-values} form,
2627 like @code{let}.
2628 @c JP
2629 @var{vars} は変数のリストです。@var{expr} は評価され、最初の返り値は、
2630 @var{vars} の最初の変数に束縛されます。二番目の返り値は、@var{vars} の
2631 二番目の変数に束縛され、以下同様です。そのあと、@var{body} が評価されます。
2632 @var{expr} のスコープは @code{let} と同様に、@code{let-values} の
2633 外側になります。
2634 @c COMMON
2635
2636 @example
2637 (let-values (((a b) (values 1 2))
2638 ((c d) (values 3 4)))
2639 (list a b c d)) @result{} (1 2 3 4)
2640
2641 (let ((a 1) (b 2) (c 3) (d 4))
2642 (let-values (((a b) (values c d))
2643 ((c d) (values a b)))
2644 (list a b c d))) @result{} (3 4 1 2)
2645 @end example
2646
2647 @c EN
2648 @var{vars} can be a dotted list or a single symbol, like the
2649 lambda parameters.
2650 @c JP
2651 @var{vars} lambda パラメータと同様に、ドット付リストでも、
2652 単一のシンボルでもかまいません。
2653 @c COMMON
2654
2655 @example
2656 (let-values (((x . y) (values 1 2 3 4)))
2657 y) @result{} (2 3 4)
2658
2659 (let-values ((x (values 1 2 3 4)))
2660 x) @result{} (1 2 3 4)
2661 @end example
2662
2663 @c EN
2664 If the number of values returned by @var{expr} doesn't match
2665 what @var{vars} expects, an error is signaled.
2666 @c JP
2667 @var{expr} によって返された値の数と @var{vars} が期待する数とが一致しない
2668 場合にはエラーになります。
2669 @c COMMON
2670 @end defmac
2671
2672 @defmac let*-values ((vars expr) @dots{}) body @dots{}
2673 [R7RS base]
2674 @c EN
2675 Same as @code{let-values}, but each @var{expr}'s scope includes
2676 the preceding @var{vars}.
2677 @c JP
2678 @code{let-values}と同じですが、各 @var{expr} のスコープが先行する @var{vars}
2679 を含みます。
2680 @c COMMON
2681
2682 @example
2683 (let ((a 1) (b 2) (c 3) (d 4))
2684 (let*-values (((a b) (values c d))
2685 ((c d) (values a b)))
2686 (list a b c d))) @result{} (3 4 3 4)
2687 @end example
2688 @end defmac
2689
2690
2691 @defmac rec var expr
2692 @defmacx rec (name . vars) expr @dots{}
2693 [SRFI-31]
2694 @c EN
2695 A macro to evaluate an expression with recursive reference.
2696
2697 In the first form, evaluates expr while @var{var} in @var{expr} is
2698 bound to the result of @var{expr}.
2699 The second form is equivalent to the followings.
2700 @c JP
2701 再帰的な参照のある式の評価を行うマクロです。
2702
2703 最初の形式は、@var{var}が@var{expr}の結果に束縛される状態で@var{expr}を
2704 評価します。
2705 2番目の形式は以下の形式と等価です。
2706 @c COMMON
2707 @example
2708 (rec @var{name} (lambda @var{vars} @var{expr} @dots{}))
2709 @end example
2710
2711 @c EN
2712 Some examples:
2713 @c JP
2714 例:
2715 @c COMMON
2716
2717 @example
2718 ;; constant infinite stream
2719 (rec s (cons 1 (delay s)))
2720
2721 ;; factorial function
2722 (rec (f n)
2723 (if (zero? n)
2724 1
2725 (* n (f (- n 1)))))
2726 @end example
2727 @end defmac
2728
2729 @node Grouping, Iteration, Binding constructs, Core syntax
2730 @section Grouping
2731 @c NODE 式をまとめる
2732
2733
2734 @defspec begin form @dots{}
2735 [R7RS base]
2736 @c EN
2737 Evaluates @var{form}s sequentially, and returns the last result(s).
2738 @c JP
2739 @var{form}を順に評価し、最後の値を返します。
2740 @c COMMON
2741
2742 @c EN
2743 @code{Begin} doesn't introduce new scope like @code{let}, that is,
2744 you can't place "internal define" at the beginning of @var{form}s
2745 generally. Semantically @code{begin} behaves as if @var{form}s
2746 are spliced into the surrounding context.
2747 For example, toplevel expression like the following is the same
2748 as two toplevel definitions:
2749 @c JP
2750 @code{Begin}は@code{let}のように新たなスコープを作成するわけではありません。
2751 すなわち、一般的には@var{form} @dots{}の先頭に「内部のdefine (internal define)」を
2752 置くことはできません。意味的には、@code{begin}はまるで@var{form} @dots{}が
2753 @code{begin}を囲むコンテクスト中に展開されているかのように振舞います。
2754 例えば、トップレベルに次のような式があった場合、それは2つのトップレベルのdefineと
2755 同等です。
2756 @c COMMON
2757
2758 @example
2759 (begin (define x 1) (define y 2))
2760 @end example
2761
2762 @c EN
2763 Here's a trickier example:
2764 @c JP
2765 よりトリッキーな例:
2766 @c COMMON
2767
2768 @example
2769 (let ()
2770 (begin
2771 (define x 2)
2772 (begin
2773 (define y 3)
2774 ))
2775 (+ x y))
2776
2777 @equiv{}
2778
2779 (let ()
2780 (define x 2)
2781 (define y 3)
2782 (+ x y))
2783 @end example
2784 @end defspec
2785
2786 @defmac begin0 exp0 exp1 @dots{}
2787 @c EN
2788 Evaluates @var{exp0}, @var{exp1}, @dots{}, then returns the result(s)
2789 of @var{exp0}. The name is taken from MzScheme.
2790 This is called @code{prog1} in CommonLisp.
2791
2792 Unlike @code{begin}, this @emph{does} creates a new scope,
2793 for the @code{begin0} form is expanded as follows.
2794 @c JP
2795 @var{exp0}, @var{exp1}, @dots{} を評価し、@var{exp0}の結果を返します。
2796 この名前はMzSchemeから取られました。これはCommonLispで
2797 @code{prog1}と呼ばれているものです。
2798
2799 @code{begin}と違って、これは新たなスコープを構成します。
2800 @code{begin0}フォームは次のように展開されるからです。
2801 @c COMMON
2802
2803 @example
2804 (receive tmp @var{exp0}
2805 @var{exp1} @dots{}
2806 (apply values tmp))
2807 @end example
2808 @end defmac
2809
2810 @defmac independently exp @dots{}
2811 [SRFI-236]
2812 @c EN
2813 Evaluates @var{exp} @dots{} in an unspecified order, and returns
2814 an undefined result. This form is defined in SRFI-236.
2815
2816 You can use @code{begin} to group multiple expressions, but @code{begin}
2817 also specifies the order of evaluation even when it's unnecessary.
2818 Using @code{independently}, you can express the intention to the readers
2819 that the order does not matter, and also gives the compiler
2820 more opportunities for optimization.
2821
2822 As the order doesn't matter, SRFI-236 does not specify the return
2823 value of this form. Gauche returns @code{#<undef>}, but the caller
2824 shoudn't rely on it.
2825 @c JP
2826 @var{exp} @dots{}を順序を問わずに評価し、未定義値を返します。
2827 このフォームはSRFI-236で定義されました。
2828
2829 複数の式を評価するのには@code{begin}が使えますが、
2830 @code{begin}は順序も指定してしまいます。
2831 @code{independently}は、コードを読む人に順序が関係ないことを伝え、
2832 またコンパイラにより多くの最適化の機会を与えます。
2833
2834 式がどういう順で評価されるかわからないので、SRFI-236では結果の値は指定されていません。
2835 Gaucheは@code{#<undef>}を返しますが、返り値に依存するコードは書くべきではありません。
2836 @c COMMON
2837 @end defmac
2838
2839
2840 @node Iteration, Quasiquotation, Grouping, Core syntax
2841 @section Iteration
2842 @c NODE 繰り返し
2843
2844 @defspec do ((variable init [step]) @dots{}) (test expr @dots{}) body @dots{}
2845 [R7RS base]
2846 @c EN
2847 @enumerate
2848 @item
2849 Evaluates @var{init} @dots{} and binds @var{variable} @dots{} to each result.
2850 The following steps are evaluated under the environment where
2851 @var{variables} are bound.
2852 @item
2853 Evaluate @var{test}. If it yields true, evaluates
2854 @var{expr} @dots{} and returns the result(s) of last @var{expr}.
2855 @item
2856 Otherwise, evaluates @var{body} @dots{} for side effects.
2857 @item
2858 Then evaluates @var{step} @dots{} and binds each result
2859 to a fresh @var{variable} @dots{}, and repeat from the step 2.
2860 @end enumerate
2861
2862 The following example loops 10 times while accumulating each
2863 value of @var{i} to @var{j} and returns it.
2864 @c JP
2865 @enumerate
2866 @item
2867 @var{init} @dots{}を評価し、@var{variable} @dots{}をそれぞれの結果へと
2868 束縛します。以降のステップは@var{variable} @dots{}が束縛された環境で評価されます。
2869 @item
2870 @var{test}を評価します。真の値が得られたら、
2871 @var{expr} @dots{}を順に評価し、最後の@var{expr}の結果を返り値とします。
2872 @item
2873 そうでなければ、@var{body} @dots{}を(副作用のために)評価します。
2874 @item
2875 それから@var{step} @dots{}を評価し、それぞれの結果へ
2876 新たな@var{variable} @dots{}を束縛し、ステップ2から繰り返します。
2877 @end enumerate
2878
2879 次の例はループを10回繰り返し、その間@var{i}の値を@var{j}へと
2880 累積して最後に返すコードです。
2881 @c COMMON
2882
2883 @example
2884 (do ((i 0 (+ i 1))
2885 (j 0 (+ i j)))
2886 ((= i 10) j)
2887 (print j))
2888 @c EN
2889 @result{} 45 ; @r{also prints intermediate values of j}
2890 @c JP
2891 @result{} 45 ; @r{また、jの中間結果が途中で出力される}
2892 @c COMMON
2893 @end example
2894
2895 @c EN
2896 If @var{step} is omitted, the previous value of @var{variable}
2897 is carried over. When there's no @var{expr}, the non-false
2898 value returned by @var{test} becomes the value of the @code{do} expression.
2899 @c JP
2900 @var{step}が省略された場合は、@var{variable}の元の値がそのまま持ち越されます。
2901 @var{expr}が一つも無い場合は、@var{test}が真の値を返した時点でその値がそのまま
2902 @code{do}式の値となります。
2903 @c COMMON
2904
2905 @c EN
2906 Since @code{do} syntax uses many parentheses, some prefer using
2907 square brackets as well as parentheses to visually distinguish
2908 the groupings. A common way is to group each variable binding,
2909 and the test clause, by square brackets.
2910 @c JP
2911 @code{do}構文は括弧を多用するので、角括弧を併用してまとまりを視覚的に
2912 強調するのを好む人もいます。よくある書き方は、それぞれの変数の束縛と、
2913 テスト節全体に角括弧を使うという書き方です。
2914 @c COMMON
2915
2916 @example
2917 (do ([i 0 (+ i 1)]
2918 [j 0 (+ i j)])
2919 [(= i 10) j]
2920 (print j))
2921 @end example
2922
2923 @c EN
2924 Note: Unlike Common Lisp (and ``for loops'' in many languages),
2925 @var{variable} is freshly bound for each iteration.
2926 The following example loops 5 times and creates a list of
2927 closures, each of which closes the variable @var{i}.
2928 When you call each closures, you can see that each of them
2929 closes different @var{i} at the time of the iteration they were
2930 created.
2931 @c JP
2932 註:Common Lisp (や、多くの言語の``forループ'') とは違って、
2933 繰り返しの度に新しい@var{variable}が束縛されます。
2934 次の例では5回ループして、それぞれ@var{i}を閉じ込むクロージャのリストを
2935 作っています。各クロージャを呼び出せば、それぞれが自身が作られた時の@var{i}を
2936 閉じ込んでいることがわかります。
2937 @c COMMON
2938
2939 @example
2940 (define closures
2941 (do ([i 0 (+ i 1)]
2942 [c '() (cons (^[] i) c)])
2943 [(= i 5) (reverse c)]
2944 ))
2945
2946 ((car closures)) @result{} 0
2947 ((cadr closures)) @result{} 1
2948 @end example
2949
2950 @end defspec
2951
2952
2953 @defspec let name ((var init) @dots{}) body @dots{}
2954 [R7RS base]
2955 @c EN
2956 This variation of @code{let} is called ``named let''. It creates
2957 the following procedure and binds it to @var{name}, then calls
2958 it with @var{init} @dots{}.
2959 @c JP
2960 「名前付きlet」と呼ばれる@code{let}式のバリエーションです。
2961 この構文は、次の手続きを作り、それを@var{name}に束縛して、
2962 @var{init} @dots{}を引数として呼び出します。
2963 @c COMMON
2964
2965 @example
2966 (lambda (var @dots{}) body @dots{})
2967 @end example
2968
2969 @c EN
2970 This syntax itself isn't necessarily related to iteration. However,
2971 the whole point of named let is that the above lambda expression is within the
2972 scope of @var{name}---that is, you can call @var{name} recursively
2973 within @var{body}. Hence this is used very often to write
2974 a loop by recursion (thus,
2975 often the procedure is named @var{loop}, as in the following example.)
2976 @c JP
2977 この構文自体は繰り返しとは直接に関係ありませんが、
2978 名前付きletのポイントは、上のlambda式が@var{name}のスコープ内で作られることです。
2979 すなわち、@var{body}から@var{name}を再帰的に呼ぶことができます。したがって
2980 この構文は再帰によるループを書くのに非常に良く使われます
2981 (そのため、次の例のように作られる手続きを@var{loop}と名付けることが良く行われます)。
2982 @c COMMON
2983
2984 @example
2985 (let loop ([x 0] [y '()])
2986 (if (= x 10)
2987 y
2988 (loop (+ x 1) (cons x y))))
2989 @result{} (9 8 7 6 5 4 3 2 1 0)
2990 @end example
2991
2992 @c EN
2993 Of course you don't need to loop with a named let; you can call @var{name}
2994 in non-tail position, pass @var{name} to other higher-order procedure, etc.
2995 Named let exists since it captures a very common pattern of local recursive
2996 procedures.
2997 Some Schemers even prefer named let to @code{do}, for the better
2998 flexibility.
2999 @c JP
3000 もちろん名前付きletは必ずループに使わなければならないということはありません。
3001 @var{name}を非末尾再帰呼び出ししても良いですし、他の高階関数に渡しても構いません。
3002 名前付きletがあるのは、ローカルな再帰関数を定義する際の共通するパターンをうまく
3003 とらえているためです。@code{do}よりも名前付きletを好んで使うSchemerもいます。
3004 名前付きletの方が柔軟性が高いからです。
3005 @c COMMON
3006
3007 @c EN
3008 The following rewrite rule precisely explains the named let semantics.
3009 The tricky use of @code{letrec} in the expansion is to make
3010 @var{proc} visible from @var{body} @dots{} but not from @var{init} @dots{}.
3011 @c JP
3012 以下の書き換え規則が、名前付きletのセマンティクスを正確に説明します。
3013 @code{letrec}の使い方がちょっとひねってあるのは、@var{body}は@var{proc}の
3014 スコープに含まれるけれど@var{init}はそうではない、ということを表現するためです。
3015 @c COMMON
3016
3017 @example
3018 (let proc ((var init) @dots{}) body @dots{})
3019 @equiv{}
3020 ((letrec ((proc (lambda (var @dots{}) body @dots{})))
3021 proc)
3022 init @dots{})
3023 @end example
3024
3025 @end defspec
3026
3027
3028 @defmac dotimes ([variable] num-expr [result]) body @dots{}
3029 @defmacx dolist ([variable] list-expr [result]) body @dots{}
3030 @c EN
3031 Convenience loop syntaxes, imported from Common Lisp.
3032 They are not very Scheme-y, in a sense that these
3033 rely on some side-effects in @var{body} @dots{}. Nevertheless these
3034 capture some common pattern in day-to-day scripting.
3035 @c JP
3036 Common Lispから輸入された、簡易なループ構文です。
3037 これらは、@var{body} @dots{}における副作用を前提としているという点で
3038 あまりSchemeっぽくありませんが、
3039 日常のスクリプティングでよく出てくるパターンを手軽に書けます。
3040 @c COMMON
3041
3042 @c EN
3043 You can use @code{dotimes} to repeat @var{body} @dots{} for a number
3044 of times given by @var{num-expr},
3045 and @code{dolist} to repeat @var{body} @dots{}
3046 while traversing a list given by @var{list-expr}. While @var{body} @dots{}
3047 is evaluated,
3048 @var{variable} is bound to the current iteration count (in @code{dotimes}),
3049 or the current element in the list (in @code{dolist}).
3050 @c JP
3051 @code{dotimes}は、@var{num-expr}で与えられる回数@var{body} @dots{}を繰り返すのに、
3052 @code{dolist}は、@var{list-expr}で与えられるリストの各要素に対して
3053 @var{body} @dots{}を繰り返すのに使えます。@var{body} @dots{}の実行中、
3054 @var{variable}はそれまで繰り返した回数(@code{dotimes}の場合)もしくは
3055 現在のリスト要素(@code{dolist}の場合)に束縛されます。
3056 @c COMMON
3057
3058 @example
3059 (dotimes (n 5) (write n))
3060 @result{} @r{writes "01234"}
3061
3062 (dolist (v '(a b c d e)) (write v))
3063 @result{} @r{writes "abcde"}
3064 @end example
3065
3066 @c EN
3067 If you don't need to refer to @var{variable}, you can omit it.
3068 For example, the following example prints @code{year!} 10 times:
3069 @c JP
3070 @var{variable}を使う必要がなければ省略することができます。
3071 次の例は@code{year!}を10回出力します。
3072 @c COMMON
3073
3074 @example
3075 (dotimes (10) (print "yeah!"))
3076 @end example
3077
3078 @c EN
3079 If the third element (@var{result}) is given in @code{dotimes} or
3080 @code{dolist}, it is evaluated after all repetition is done, and its result
3081 becomes the result of @code{dotimes}/@code{dolist}. While @var{result}
3082 is evaluated, @var{variable} is bound to the number of repetitions
3083 (in @code{dotimes}) or @code{()} (in @code{dolist}).
3084 It is supported because Common Lisp has it.
3085 @c JP
3086 3番めの要素@var{result}が@code{dotimes}や@code{dolist}に与えられた場合、
3087 その式はすべての繰り返しが終わった後で評価され、
3088 その結果が@code{dotimes}/@code{dolist}の値となります。
3089 @var{result}は、@var{variable}が繰り返しの総回数(@code{dotimes}の場合)
3090 もしくは@code{()}(@code{dolist}の場合)に束縛された環境で評価されます。
3091 これはCommon Lispに倣っています。
3092 @c COMMON
3093
3094 @c EN
3095 Note that a fresh @var{variable} is bound for each iteration,
3096 as opposed to Common Lisp where @var{variable} is mutated.
3097 So if you create a closure closing @var{variable}, it won't be
3098 overwritten by the subsequent iteration.
3099 @c JP
3100 Common Lispと違い、新たな@var{variable}が繰り返しの度に束縛されます
3101 (Common Lispでは、一つの@var{variable}が繰り返しごとに変更を受けます)。
3102 したがって、@var{variable}を綴じ込むクロージャを作った場合でも、
3103 繰り返しの進行で@var{variable}が上書きされることはありません。
3104 @c COMMON
3105
3106 @c EN
3107 If you need more than simple iteration, you can use @code{do} form,
3108 named @code{let}, or @ref{Eager comprehensions}, which provides
3109 rich way to iterate.
3110 @c JP
3111 単純な繰り返し以上のことがしたいなら、@code{do}フォーム、名前付き@code{let}、あるいは
3112 @ref{Eager comprehensions}も参照してください。
3113 より複雑な繰り返しを記述できます。
3114 @c COMMON
3115 @end defmac
3116
3117 @c @defmac do-plist ((kvar vvar) plist-expr [default]) body @dots{}
3118 @c @c EN
3119 @c A @emph{plist} is a list alternating key and values. This macro
3120 @c provides a convenient way to loop over a plist.
3121
3122 @c @example
3123 @c ;; example of a plist, associating a => 1 and b => 2.
3124 @c (a 1 b 2)
3125 @c @end example
3126 @c @c JP
3127 @c @emph{plist}とは、キーと値が交互に現れるリストです。
3128 @c このマクロはplistに対する繰り返しを簡単にするものです。
3129
3130 @c @example
3131 @c ;; plistの例。a => 1、 b => 2 という関係を示す。
3132 @c (a 1 b 2)
3133 @c @end example
3134 @c @c COMMON
3135
3136 @c @c EN
3137 @c The @var{plist-expr} form must evaluate to a plist.
3138 @c It evaluates @code{body} @dots{} while binding
3139 @c @var{kvar} and @var{vvar} to each key and value.
3140 @c @c JP
3141 @c @var{plist-expr}はplistを返す式です。
3142 @c @var{kvar}と@var{vvar}にそれぞれキーと値に束縛された環境で
3143 @c @code{body} @dots{}が評価されます。
3144 @c @c COMMON
3145
3146 @c @example
3147 @c (doplist ((k v) '(a 1 b 2 c 3)) (print k " => " v))
3148 @c @result{} @r{prints:}
3149 @c a => 1
3150 @c b => 2
3151 @c c => 3
3152 @c @end example
3153
3154 @c @c EN
3155 @c By default, an error is signalled if @var{plist-expr} isn't even
3156 @c (we only detect the error when we reach to the end of the list).
3157 @c If you give @var{default} argument, however, it is used as the missing
3158 @c value for the last key.
3159 @c @c JP
3160 @c デフォルトでは、@var{plist-expr}の生成するリストの要素が偶数個でなければ
3161 @c エラーが報告されます(エラーはリストを最後までループした段階で検出されます)。
3162 @c しかし、@var{default}引数が与えられていれば、要素が奇数個の場合は@var{default}の
3163 @c 値が最後のキーに対応する値として使われます。
3164 @c @c COMMON
3165
3166 @c @example
3167 @c (doplist ((k v) '(a 1 b 2 c)) (print k " => " v))
3168 @c @result{} @r{Error, after printing} a => 1 @r{and} b => 2
3169
3170 @c (doplist ((k v) '(a 1 b 2 c) #f) (print k " => " v))
3171 @c @result{} @r{prints:}
3172 @c a => 1
3173 @c b => 2
3174 @c c => #f
3175 @c @end example
3176 @c @end defmac
3177
3178 @defmac while expr body @dots{}
3179 @defmacx while expr @code{=>} var body @dots{}
3180 @defmacx while expr guard @code{=>} var body @dots{}
3181 @c EN
3182 @var{Var} is an identifier and @var{guard} is a procedure
3183 that takes one argument.
3184 @c JP
3185 @var{var}は識別子で@var{guard}は一つの引数をとる手続きです。
3186 @c COMMON
3187
3188 @c EN
3189 In the first form, @var{expr} is evaluated, and if it yields a true
3190 value, @var{body} @dots{} are evaluated. It is repeated while
3191 @var{expr} yields true value.
3192 @c JP
3193 最初の形式ではまず@var{expr}が評価され、もしそれが真値を返したら
3194 @var{body} @dots{} が評価されます。そして@var{expr}が真値を返す
3195 限り繰り返されます。
3196 @c COMMON
3197
3198 @c EN
3199 In the second form, @var{var} is bound to a result of @var{expr}
3200 in the scope of @var{body} @dots{}.
3201 @c JP
3202 2番目の形式では、@var{body} @dots{} で有効な変数@var{var}を
3203 作成し、@var{expr}の結果に束縛します。
3204 @c COMMON
3205
3206 @c EN
3207 In the third form, the value @var{expr} yields are passed to
3208 @var{guard}, and the execution of @var{body} @dots{} is repeated
3209 while @var{guard} returns a true value. @var{var} is bound
3210 to the result of @var{expr}.
3211 @c JP
3212 最後の形式では、@var{expr} の返り値が @var{guard} に渡されこの
3213 結果が真値である限り @var{body} @dots{} が繰り返されます。
3214 変数 @var{var} は @var{expr} の結果に束縛されます。
3215 @c COMMON
3216
3217 @c EN
3218 The return value of @code{while} form itself isn't specified.
3219 @c JP
3220 @code{while} 自体の返り値は不定です。
3221 @c COMMON
3222
3223 @example
3224 (let ((a '(0 1 2 3 4)))
3225 (while (pair? a)
3226 (write (pop! a)))) @result{} @r{prints "01234"}
3227
3228 (let ((a '(0 1 2 3 #f 5 6)))
3229 (while (pop! a) integer? => var
3230 (write var))) @result{} @r{prints "0123"}
3231 @end example
3232 @end defmac
3233
3234 @defmac until expr body @dots{}
3235 @defmacx until expr guard @code{=>} var body @dots{}
3236 @c EN
3237 Like @code{while}, but the condition is reversed. That is,
3238 the first form repeats evaluation of @var{expr} and @var{body} @dots{}
3239 until @var{expr} yields true. In the second form,
3240 the result of @var{expr} is passed to @var{guard}, and the
3241 execution is repeated until it returns true. @var{Var} is bound
3242 to the result of @var{expr}.
3243 @c JP
3244 @code{while} の条件を逆にしたものです。 つまり、
3245 最初の形式では@var{expr}が真値を返すまで @var{body} @dots{} を
3246 繰り返します。2番目の形式では@var{expr}の結果が@var{guard} に渡され
3247 それが真値を返すまで繰り返します。 @var{Var}は@var{expr}返り値に
3248 束縛されます。
3249 @c COMMON
3250
3251 @c EN
3252 (The second form without @var{guard} isn't useful in @code{until}, since
3253 @var{var} would always be bound to @code{#f}).
3254 @c JP
3255 (@var{guard} を省いた2番目の形式では@var{var}は常に@code{#f}へ
3256 束縛されるため、あまり意味がありません。)
3257 @c COMMON
3258
3259 @c EN
3260 The return value of @code{until} form itself isn't specified.
3261 @c JP
3262 @code{until}自体の返り値は不定です。
3263 @c COMMON
3264
3265 @example
3266 (let ((a '(0 1 2 3 4)))
3267 (until (null? a)
3268 (write (pop! a)))) @result{} @r{prints "01234"}
3269
3270 (until (read-char) eof-object? => ch
3271 (write-char ch))
3272 @result{} @r{reads from stdin and writes char until EOF is read}
3273 @end example
3274 @end defmac
3275
3276
3277 @c ----------------------------------------------------------------------
3278 @node Quasiquotation, Definitions, Iteration, Core syntax
3279 @section Quasiquotation
3280 @c NODE 準クオート, 準クオート(Quasiquote)
3281
3282 @defspec quasiquote template
3283 [R7RS base]
3284 @c EN
3285 Quasiquotation is a convenient way to build a structure that has
3286 some fixed parts and some variable parts. See the explanation below.
3287 @c JP
3288 準クォートは固定部分と変数部分の両方を持つような構造を構成するのに便利
3289 です。詳細は以下の説明を参照してください。
3290 @c COMMON
3291 @end defspec
3292
3293 @deftp {Reader Syntax} @code{`@var{template}}
3294 @lxindex `
3295 [R7RS]
3296 @c EN
3297 The syntax @code{`x} is read as @code{(quasiquote x)}.
3298 @c JP
3299 @code{`x}は@code{(quasiquote x)}として読み込まれます。
3300 @c COMMON
3301 @end deftp
3302
3303 @defspec unquote datum @dots{}
3304 @defspecx unquote-splicing datum @dots{}
3305 [R7RS base]
3306 @c EN
3307 These syntaxes have meaning only when they appear in the @var{template}
3308 of quasiquoted form. R5RS says nothing about these syntaxes
3309 appear outside of quasiquote. Gauche signals an error in such case,
3310 for it usually indicates you forget quasiquote somewhere.
3311 @c JP
3312 これらの構文は準クォートされた@var{template}内にあるときにだけ意味を持ち
3313 ます。R5RSではこれらの構文が準クォートの外側で現われたときの意味に
3314 ついては何も言及していません。Gaucheではそのような場合にはエラーを通知します。
3315 そのような@code{unquote}や@code{unquote-splicing}が現れるのは、
3316 通常どこかで準クォートを忘れているからです。
3317 @c COMMON
3318
3319 @c EN
3320 R5RS only allows @code{unquote} and @code{unquote-splicing} to take
3321 a single argument; it is undefined if you have @code{(unquote)} or
3322 @code{(unquote x y)} inside quasiquoted form. R6RS allows zero
3323 or multi-arguments, and Gauche follows that.
3324 @c JP
3325 R5RSは@code{unquote}と@code{unquote-splicing}に一つだけ引数を取ることを
3326 許しています。@code{(unquote)}や@code{(unquote x y)}のようなフォームが
3327 準クオートの中に現れた場合の動作は定義されていません。
3328 R6RSではこれらの場合も定義されており、Gaucheもそれに倣っています。
3329 @c COMMON
3330 @end defspec
3331
3332 @deftp {Reader Syntax} @code{,@var{datum}}
3333 @deftpx {Reader Syntax} @code{,@@@var{datum}}
3334 @lxindex ,
3335 @lxindex ,@@
3336 [R7RS]
3337 @c EN
3338 The syntaxes @code{,x} and @code{,@@x} are read as @code{(unquote x)}
3339 and @code{(unquote-splicing x)}, respectively.
3340 @c JP
3341 @code{,x}および@code{,@@x}は、それぞれ@code{(unquote x)}および
3342 @code{(unquote-splicing x)}として読み込まれます。
3343 @c COMMON
3344 @end deftp
3345
3346 @c EN
3347 @subheading Quasiquote basics
3348 @c JP
3349 @subheading 準クォートの基本
3350 @c COMMON
3351
3352 @c EN
3353 Suppose you want to create a list @code{(foo bar @var{x} @var{y})},
3354 where @code{foo} and @code{bar} are symbols, and @var{x} and @var{y}
3355 are the value determined at runtime. (For the sake of explanation,
3356 let's assume we have variables @var{x} and @var{y} that provides those
3357 values.) One way to do that is to call the function @code{list}
3358 explicitly.
3359 @c JP
3360 @code{(foo bar @var{x} @var{y})}のようなリストを構成したいとしましょう。
3361 ここでは@code{foo}および@code{bar}はシンボルで、@var{x}および@var{y}は
3362 実行時に定まる値とします。(説明のために、変数@var{x}および@var{y}がそ
3363 れらの値を持っているものとします。) ひとつの方法は@code{list}関数を明
3364 示的に呼ぶことです。
3365 @c COMMON
3366
3367 @example
3368 (let ((x 0) (y 1))
3369 (list 'foo 'bar x y)) @result{} (foo bar 0 1)
3370 @end example
3371
3372 @c EN
3373 You can do the same thing with quasiquote, like this:
3374 @c JP
3375 同じことを準クォートを使うと以下のようになります。
3376 @c COMMON
3377
3378 @example
3379 (let ((x 0) (y 1))
3380 `(foo bar ,x ,y)) @result{} (foo bar 0 1)
3381 @end example
3382
3383 @c EN
3384 The difference between the two notations is that the explicit version
3385 quotes the parts that you want to insert literally into the result,
3386 while the quasiquote version @emph{unquotes} the parts that you
3387 don't want to quote.
3388 @c JP
3389 ふたつの記法の違いは、前者では結果に書いたとおりを入れ込みたいところで
3390 クォートを使い、後者ではクォートしたくないところに@emph{unquotes}を使
3391 うことです。
3392 @c COMMON
3393
3394 @c EN
3395 The quasiquote version gets simpler and more readable when you
3396 have lots of static parts with scattered variable parts in your
3397 structure.
3398 @c JP
3399 ほとんどが固定部分でその中に変数部分が散在するような場合には準クォート
3400 を使った方が単純で読みやすくなります。
3401 @c COMMON
3402
3403 @c EN
3404 That's why quasiquote is frequently used with
3405 legacy macros, which are basically a procedure that
3406 create program fragments from variable parts provided as
3407 macro arguments. See the simple-minded @code{my-if} macro
3408 that expands to @code{cond} form:
3409 @c JP
3410 そういうわけで、旧来のマクロでは準クォートが頻繁につかわれていました。
3411 旧来のマクロは基本的にマクロの引数として与えられた変数部分からプログラ
3412 ム断片を生成する手続だからです。簡単な@code{my-if}マクロの定義とそれが
3413 @code{cond}に展開されるようすを見てみましょう。
3414 @c COMMON
3415
3416 @example
3417 (define-macro (my-if test then else)
3418 `(cond (,test ,then)
3419 (else ,else)))
3420
3421 (macroexpand '(my-if (< n 0) n (- n)))
3422 @result{} (cond ((< n 0) n) (else (- n)))
3423 @end example
3424
3425 @c EN
3426 Note the two @code{else}s in the macro definition; one isn't unquoted,
3427 thus appears literally in the output, while another is unquoted and
3428 the corresponding macro argument is inserted in its place.
3429 @c JP
3430 マクロ定義内の2つの@code{else}に注目してください。ひとつはアンクォート
3431 されていませんので、出力にはそのまま現われます。もうひとつの方はといえ
3432 ば、こちらはアンクォートされていますので、その場所にマクロの引数が入り
3433 ます。
3434 @c COMMON
3435
3436 @c EN
3437 Of course you can use quasiquotes unrelated to macros. It is a general
3438 way to construct structures. Some even prefer using quasiquote to
3439 explicit construction even most of the structure is variable, for
3440 quasiquoted form can be more concise. Gauche also tries to minimize
3441 runtime allocation for quasiquoted forms, so it may potentially
3442 be more efficient; see "How static are quasiquoted forms?" below.
3443 @c JP
3444 もちろんマクロとは関係のないところでも準クォートは使えます。準クォート
3445 は構造のあるデータを構築する汎用的な方法です。ほとんどが変数部分である
3446 ような構造でも準クォートを好んで使うプログラマもいます。準クォートを使っ
3447 た方が簡潔に書けるからです。さらにGaucheでは準クォート形式に対する実行
3448 時アロケーションができるだけ少くなるようにしていますので、準クォートを
3449 使った方が効率がいいはずです。この点については後述の「準クォートはどの
3450 くらい静的か」を見てください。
3451 @c COMMON
3452
3453 @c EN
3454 @subheading Splicing
3455 @c JP
3456 @subheading スプライシング
3457 @c COMMON
3458
3459 @c EN
3460 When @code{(unquote-splicing @var{expr})} appears in a quasiquoted form,
3461 @var{expr} must evaluate to a list, which is @emph{spliced} into the
3462 surrounding context. It's easier to see examples:
3463 @c JP
3464 @code{(unquote-splicing @var{expr})}が準クォート形式の中で使われていれ
3465 ば、@var{expr}は評価されてリストになるものでなければならず、それをとり
3466 まくコンテキストで継ぎ合わされます。例を見ると簡単にわかります。
3467 @c COMMON
3468
3469 @example
3470 (let ((x '(1 2 3)))
3471 `(a ,@@x b)) @result{} (a 1 2 3 b)
3472
3473 (let ((x '(1 2 3)))
3474 `(a ,x b)) @result{} (a (1 2 3) b)
3475
3476 (let ((x '(1 2 3)))
3477 `#(a ,@@x b)) @result{} #(a 1 2 3 b)
3478 @end example
3479
3480 @c EN
3481 Compare the unquote version and unquote-splicing version. Splicing
3482 also works within a vector.
3483 @c JP
3484 アンクォート版とアンクォートスプライシング版を比べてください。スプライ
3485 シングはベクタに対しても機能します。
3486 @c COMMON
3487
3488 @c EN
3489 @subheading Multi-argument unquotes
3490 @c JP
3491 @subheading 複数の引数を取るunquote
3492 @c COMMON
3493
3494 @c EN
3495 If @code{unquote} or @code{unquote-splicing} takes multiple arguments,
3496 they are interpreted as if each of its arguments are unquoted
3497 or unquote-spliced.
3498 @c JP
3499 @code{unquote}や@code{unquote-splicing}が複数の引数を取る場合は、
3500 あたかも各引数がそれぞれ@code{unquote}または@code{unquote-splicing}されている
3501 かのように解釈されます。
3502 @c COMMON
3503
3504 @smallexample
3505 ;; This is the same result as `(,(+ 1 2) ,(+ 2 3) ,(+ 3 4))
3506 `((unquote (+ 1 2) (+ 2 3) (+ 3 4)))
3507 @result{} (3 5 7)
3508
3509 ;; This is the same result as
3510 ;; `(,@@(list 1 2) ,@@(list 2 3) ,@@(list 3 4))
3511 `((unquote-splicing (list 1 2) (list 2 3) (list 3 4)))
3512 @result{} (1 2 2 3 3 4)
3513
3514 ;; Edge cases
3515 `((unquote)) @result{} ()
3516 `((unquote-splicing)) @result{} ()
3517 @end smallexample
3518
3519 @c EN
3520 It is an error for zero or multiple argument
3521 @code{unquote}/@code{unquote-splicing} forms appear
3522 which you cannot splice multiple forms into.
3523 @c JP
3524 複数のフォームをスプライスできない位置に現れた
3525 ゼロ個もしくは複数の引数を取る@code{unquote}/@code{unquote-splicing}フォームは
3526 エラーとなります。
3527 @c COMMON
3528
3529 @smallexample
3530 ;; Multiple arguments unquotes are error in non-splicing context
3531 `(unquote 1 2) @result{} @r{error}
3532 `(unquote-splicing 1 2) @result{} @r{error}
3533 @end smallexample
3534
3535 @c EN
3536 Note that the abbreviated notations @code{,x} and @code{,@@x} are
3537 only for single-argument forms. You have to write @code{unquote}
3538 or @code{unquote-splicing} explicitly for zero or multiple argument
3539 forms; thus you don't usually need to use them. These forms
3540 are supported mainly to make the nested unquoting forms such
3541 as @code{,,@@} and @code{,@@,@@}---R5RS cannot handle
3542 the case the inner unquote-splicing
3543 form expands into zero or multiple forms.
3544 @c JP
3545 省略記法の@code{,x}と@code{,@@x}は単一引数のフォームとしてしか使えないので、
3546 ゼロ個もしくは複数の引数を取る場合は
3547 @code{unquote}や@code{unquote-splicing}を陽に表記しなければなりません。
3548 わざわざそんなコーディングをする必要はまず無いでしょう。
3549 この機能は、ネストしたアンクオートしているフォーム、
3550 @code{,,@@}や@code{,@@,@@}をサポートするためにあります。
3551 R5RSの仕様では、これらのフォームで、内側のunquote-splicingがゼロ個もしくは複数個の
3552 フォームへと展開された場合を処理できません。
3553 @c COMMON
3554
3555 @c EN
3556 @subheading How static are quasiquoted forms?
3557 @c JP
3558 @subheading 準クォートはどのくらい静的か
3559 @c COMMON
3560
3561 @c EN
3562 When quasiquoted form contains variable parts, what happens at
3563 runtime is just the same as when an explicit form is used:
3564 @code{`(,x ,y)} is evaluated exactly like @code{(list x y)}.
3565 However, Gauche tries to minimize runtime allocation when
3566 a quasiquoted form has static parts.
3567 @c JP
3568 準クォート形式が変数部分を含む場合、実行時には、明示的な形式が使われた
3569 ときと同じことがおこります。@code{`(,x ,y)}は@code{(list x y)}のように
3570 評価されます。しかし、Gaucheでは準クォート形式が固定部分を持つ場合には
3571 実行時アロケーションができるだけ少くなるようにします。
3572 @c COMMON
3573
3574 @c EN
3575 First of all, if there's no variable parts in quasiquoted
3576 form, like @code{`(a b c)}, the entire form is allocated statically.
3577 If there is a static tail in the structure, it is also allocated
3578 statically; e.g. @code{`((,x a b) (,y c d))} works like
3579 @code{(list (cons x '(a b)) (cons y '(c d)))}.
3580 @c JP
3581 まず、@code{`(a b c)}のように準クォート形式に変数部分がない場合、全体
3582 は静的にアロケートされます。構造の末尾が固定部分の場合にも静的にアロケー
3583 トされます。たとえば、@code{`((,x a b) (,y c d))}は@code{(list (cons x
3584 '(a b)) (cons y '(c d)))}のように機能します。
3585 @c COMMON
3586
3587 @c EN
3588 Furthermore, when an unquoted expression is a
3589 constant expression, Gauche embeds it into the static
3590 form. If you've defined a constant like
3591 @code{(define-constant x 3)}, then the form
3592 @code{`(,x ,(+ x 1))} is compiled as the constant @code{'(3 4)}.
3593 (See @ref{Definitions}, for the explanation of @code{define-constant} form.)
3594 @c JP
3595 さらにアンクォート式が定数式の場合,Gaucheはそれを準クォートの固定部分
3596 に埋め込みます。たとえば、@code{(define-constant x 3)}のように定義した
3597 としましょう。この場合@code{`(,x ,(+ x 1))}は定数@code{'(3 4)}のように
3598 コンパイルされます。(@code{define-constant}形式については、
3599 @ref{Definitions}を参照してください。)
3600 @c COMMON
3601
3602 @c EN
3603 In general it is hard to say which part of quasiquoted form is
3604 compiled as a static datum and which part is not, so you shouldn't
3605 write a code that assumes some parts of the structure returned from
3606 quasiquote are freshly allocated. In other words, you better avoid
3607 mutating such structures.
3608 @c JP
3609 一般的には、準クォート形式のどの部分が固定データとしてコンパイルされど
3610 の部分がされないのかを特定することは困難です。それゆえ、準クォートの返
3611 す構造の一部が新規にアロケートされていることを前提としたコードを書いて
3612 はいけません。いいかえると、そのような構造を変更するのは避けるべきです。
3613 @c COMMON
3614
3615 @c ----------------------------------------------------------------------
3616 @node Definitions, Inclusions, Quasiquotation, Core syntax
3617 @section Definitions
3618 @c NODE 定義
3619
3620 @defspec define variable expression
3621 @defspecx define (variable . formals) body @dots{}
3622 @defspecx define variable
3623 [R7RS+ base]
3624 @c EN
3625 This form has different meanings in the toplevel (without no
3626 local bindings) or inside a local scope.
3627
3628 On toplevel, it defines a global binding to a symbol @var{variable}.
3629 In the first form, it globally binds a symbol @var{variable}
3630 to the value of @var{expression}, in the current module.
3631 @c JP
3632 この形式はトップレベル (ローカルな束縛が無い状態) とローカルスコープがある状態とで
3633 別の意味を持ちます。
3634
3635 トップレベルでは、この形式は変数@var{variable}に対するグローバルな束縛を定義します。
3636 最初の形式では、@var{expression}が評価され、その結果が変数@var{variable}の値となります。
3637 @c COMMON
3638 @example
3639 (define x (+ 1 2))
3640 x @result{} 3
3641 (define y (lambda (a) (* a 2)))
3642 (y 8) @result{} 16
3643 @end example
3644
3645 @c EN
3646 If @var{variable} is already bound @emph{in the same module}, the subsequent
3647 definitions work just like assignments.
3648 @c JP
3649 @var{variable}が@emph{同じモジュールの中で}既に束縛されていた場合、
3650 二度目以降の定義は代入と同じです。
3651 @c COMMON
3652
3653 @example
3654 (define x 3)
3655 (define (value-of-x) x)
3656
3657 (value-of-x x) @result{} 3
3658
3659 (define x 4)
3660
3661 (value-of-x x) @result{} 4
3662 @end example
3663
3664 @c EN
3665 If @var{variable} is not bound in the current module, but has an
3666 imported bindings, things get interesting but complicated.
3667 @xref{Into the Scheme-Verse}, for the details.
3668 @c JP
3669 @var{variable}が現在のモジュールでは束縛されていないけれど、
3670 他のモジュールからインポートされた束縛があった場合、話はちょっとややこしく
3671 (おもしろく)なります。詳しくは
3672 @ref{Into the Scheme-Verse}で論じます。
3673 @c COMMON
3674
3675 @c EN
3676 The second form is a syntactic sugar of defining a procedure.
3677 It is equivalent to the following form.
3678 @c JP
3679 2番目の形式は手続きを定義するための構文的な修飾で、以下の形式と同じです。
3680 @c COMMON
3681 @example
3682 (define (@var{name} . @var{args}) @var{body} @dots{})
3683 @equiv{} (define @var{name} (lambda @var{args} @var{body} @dots{}))
3684 @end example
3685
3686 @c EN
3687 The third form is a shorthand of @code{(define variable (undefined))}.
3688 It is introduced in R6RS (but not a part of R7RS). You can use that
3689 form to indicate the initial value doesn't matter.
3690 @c JP
3691 3番目の形式は@code{(define variable (undefined))}の略記です。
3692 これはR6RSで導入されました(ただし、R7RSには入っていません)。
3693 初期値が重要でないことを示したい場合に使えます。
3694 @c COMMON
3695
3696 @c EN
3697 If the form appears inside a local scope (internal define),
3698 this introduce a local binding of the variable.
3699 @c JP
3700 このフォームがローカルスコープの中に現われた場合、ローカル変数の束縛となります。
3701 (内部define)。
3702 @c COMMON
3703
3704 @c EN
3705 Internal defines can appear in the beginning of body of @code{lambda}
3706 or other forms that introduces local bindings. They are equivalent
3707 to a @code{letrec*} form, as shown below.
3708 @c JP
3709 内部defineは@code{lambda}やその他のローカル束縛を作る構文の、本体部分の先頭に置けます。
3710 これらは、下に示すように@code{letrec*}フォームと等価です。
3711 @c COMMON
3712
3713 @example
3714 (lambda (a b)
3715 (define (cube x) (* x x x))
3716 (define (square x) (* x x))
3717 (+ (cube a) (square b)))
3718
3719 @equiv{}
3720
3721 (lambda (a b)
3722 (letrec* ([cube (lambda (x) (* x x x))]
3723 [square (lambda (x) (* x x))])
3724 (+ (cube a) (square b))))
3725 @end example
3726
3727 @c EN
3728 Since internal defines are essentially a @code{letrec*} form,
3729 you can write mutually recursive local functions, and you can
3730 use preceding bindings introduced in the same scope to calculate
3731 the value to be defined. However, you can't use a binding that is introduced
3732 after an internal define form to calculate its value; if you do so,
3733 Gauche may not report an error immediately, but you may get strange
3734 errors later on.
3735 @c JP
3736 内部defineは実質的に@code{letrec*}フォームなので、
3737 相互再帰する内部関数を書けますし、また同じスコープで先に導入された定義を
3738 使って定義される値を計算することもできます。しかし、
3739 内部defineフォームの後に定義される値を使うことはできません。
3740 そういったプログラムを書いてもGaucheは直ちにエラーを報告しませんが、
3741 あとでおかしな結果が出ることがあります。
3742 @c COMMON
3743
3744 @example
3745 (lambda (a)
3746 (define x (* a 2))
3747 @c EN
3748 (define y (+ x 1)) ; ok to use x to calculate y
3749 @c JP
3750 (define y (+ x 1)) ; yの値を計算するのにxを使ってよい
3751 @c COMMON
3752 (* a y))
3753
3754 (lambda (a)
3755 @c EN
3756 ;; You can refer to even? in odd?, since the value of even?
3757 ;; isn't used at the time odd? is defined; it is only used
3758 ;; when odd? is called.
3759 @c JP
3760 ;; odd?の中からeven?を参照するのはok。odd?が定義される時点ではeven?
3761 ;; の値は使われず、odd?が呼ばれた時に初めて使われるから。
3762 @c COMMON
3763 (define (odd? x) (or (= x 1) (not (even? (- x 1)))))
3764 (define (even? x) (or (= x 0) (not (odd? (- x 1)))))
3765 (odd? a))
3766
3767 (lambda (a)
3768 @c EN
3769 ;; This is not ok, for defining y needs to use the value
3770 ;; of x. However, you may not get an error immediately.
3771 @c JP
3772 ;; これはダメ。yを定義する時点でxの値を使わないとならないので。
3773 ;; ただし、すぐにはエラーとならないかもしれない。
3774 @c COMMON
3775 (define y (+ x 1))
3776 (define x (* a 2))
3777 (* a y))
3778 @end example
3779
3780 @c EN
3781 Inside the body of binding constructs, internal defines must appear
3782 before any expression of the same level. The following code isn't allowed, for
3783 an expression @code{(print a)} precedes the @code{define} form.
3784 @c JP
3785 束縛を作るフォームのボディー内で、内部defineは同じレベルにあるすべての式より前に
3786 現れなければなりません。例えば次のコードは、@code{define}フォームの
3787 前に式@code{(print a)}があるので不正です。
3788 @c COMMON
3789
3790 @example
3791 (lambda (a)
3792 (print a)
3793 (define (cube x) (* x x x)) ; error!
3794 (cube a))
3795 @end example
3796
3797 @c EN
3798 It is also invalid to put no expressions but internal defines
3799 inside the body of binding constructs, although Gauche don't report
3800 an error.
3801 @c JP
3802 束縛を作るフォームのボディー中に、式を置かず内部defineだけを書いておくのも不正ですが、
3803 Gaucheは特にエラーを出しません。
3804 @c COMMON
3805
3806 @c EN
3807 Note that @code{begin} (@pxref{Grouping}) doesn't introduce a new scope.
3808 @code{Define}s in the @code{begin} act as if @code{begin} and surrounding
3809 parenthesis are not there. Thus these two forms are equivalent.
3810 @c JP
3811 @code{begin}は新しいスコープを作らないことに注意してください(@ref{Grouping}参照)。
3812 @code{begin}の中に現われる@code{define}は、あたかも@code{begin}とそれを囲む
3813 括弧が無いかのように振舞います。すなわち、以下の2つの形式は等価です。
3814 @c COMMON
3815 @example
3816 (let ((x 0))
3817 (begin
3818 (define (foo y) (+ x y)))
3819 (foo 3))
3820 @equiv{}
3821 (let ((x 0))
3822 (define (foo y) (+ x y))
3823 (foo 3))
3824 @end example
3825 @end defspec
3826
3827 @defmac define-values (var @dots{}) expr
3828 @defmacx define-values (var var1 @dots{} . var2) expr
3829 @defmacx define-values var expr
3830 [R7RS base][SRFI-244]
3831 @c EN
3832 @var{Expr} is evaluated, and each value of the result
3833 is bound to each @var{var}s. In the first form, it is an error
3834 unless @var{expr} yields the same number of values as @var{var}s.
3835 @c JP
3836 まず@var{expr}が評価され、続いて各値が@var{var}に順に束縛されます。
3837 最初の形式では、@var{expr}
3838 @c COMMON
3839 @example
3840 (define-values (lo hi) (min&max 3 -1 15 2))
3841
3842 lo @result{} -1
3843 hi @result{} 15
3844 @end example
3845
3846 @c EN
3847 In the second form, @var{expr} may yield as many values as
3848 @var{var} @var{var1} @dots{} or more; the excess values are
3849 made into a list and bound to @var{var2}.
3850 @c JP
3851 二番目の形式では、@var{expr}は@var{var} @var{var1} @dots{}に対応する数か
3852 それ以上の値を生成しなければなりません。余った値はリストになって@var{var2}に
3853 束縛されます。
3854 @c COMMON
3855
3856 @example
3857 (define-values (a b . c) (values 1 2 3 4))
3858
3859 a @result{} 1
3860 b @result{} 2
3861 c @result{} (3 4)
3862 @end example
3863
3864 @c EN
3865 In the last form, all the values yielded by @var{expr} are gathered
3866 to a list and bound to @var{var}.
3867 @c JP
3868 最後の形式では、@var{expr}の生成する全ての値がリストにまとめられ、@var{var}に
3869 束縛されます。
3870 @c COMMON
3871
3872 @example
3873 (define-values qr (quotient&remainder 23 5))
3874
3875 qr @result{} (4 3)
3876 @end example
3877
3878 @c EN
3879 You can use @code{define-values} wherever @code{define} is allowed;
3880 that is, you can mix @code{define-values} in internal defines.
3881 @c JP
3882 @code{define-values}は@code{define}が許されるところならどこでも使えます。
3883 つまり、内部defineに@code{define-values}を混ぜて使えるということです。
3884 @c COMMON
3885
3886 @example
3887 (define (foo . args)
3888 (define-values (lo hi) (apply min&max args))
3889 (define len (length args))
3890 (list len lo hi))
3891
3892 (foo 1 4 9 3 0 7)
3893 @result{} (6 0 9)
3894 @end example
3895 @end defmac
3896
3897 @defspec define-constant variable expression
3898 @defspecx define-constant (variable . formals) body @dots{}
3899 @c EN
3900 This form is only effective in toplevel.
3901
3902 Like top-level @code{define}, it defines a top-level definition
3903 of @var{variable} with the value of @var{expression}, but additionally
3904 tells the compiler that (1) the binding won't change, and
3905 (2) the value of @var{expression} won't change from
3906 the one computed at the compile time.
3907 So the compiler can replace references of @var{variable}
3908 with the compile-time value of @var{expression}.
3909
3910 An error is signaled when you use @code{set!} to change the value
3911 of @var{variable}. It is allowed to redefine @var{variable},
3912 but a warning is printed.
3913 @c JP
3914 このフォームはトップレベルでしか使えません。
3915
3916 トップレベルの@code{define}と同様に、トップレベルで@var{variable}を
3917 @var{expression}の値に束縛しますが、さらに次の情報をコンパイラに伝えます:
3918 (1)その束縛は変わらない (2)@var{expression}の値はコンパイル時に計算したものと変わらない。
3919 コンパイラは@var{variable}が参照されている箇所を
3920 コンパイル時に計算した@var{expression}の値で置き換えて構わないと考えて最適化を行います。
3921
3922 @var{variable}の値を@code{set!}で変更しようとするとエラーとなります。
3923 @var{variable}を再定義することは許されますが、警告が表示されます。
3924 @c COMMON
3925
3926 @c EN
3927 The difference from @code{define-inline} below is that the value of
3928 @var{expression} is computed at the compile time and treated as a literal.
3929 Suppose you define @var{x} as follows:
3930 @c JP
3931 下の@code{define-inline}との違いは、@var{expression}の値がコンパイル時に
3932 計算され、リテラルとして扱われることです。
3933 例えば@var{x}を次のとおり定義したとします:
3934 @c COMMON
3935
3936 @example
3937 (define-constant x (vector 1 2 3))
3938 @end example
3939
3940 @c EN
3941 Then, the code @code{(list x)} is compiled to the same code
3942 as @code{(list '#(1 2 3))}.
3943
3944 This distinction is especially important when you do AOT (ahead of time)
3945 compilation.
3946 @c JP
3947 すると、コード@code{(list x)}は@code{(list '#(1 2 3))}と同じコードにコンパイルされます。
3948
3949 この違いは特にAOT (ahead of time)コンパイルをする場合に重要です。
3950 @c COMMON
3951
3952 @c EN
3953 There's no ``internal @code{define-constant}'', since the compiler
3954 can figure out whether a local binding is mutated,
3955 and optimize code accordingly, without a help of declarations.
3956 @c JP
3957 ``内部@code{define-constant}'' にあたるものはありません。宣言が無くても
3958 コンパイラはどのローカル束縛が変更されないかを検出して最適化できるからです。
3959 @c COMMON
3960 @end defspec
3961
3962 @defspec define-inline variable expression
3963 @defspecx define-inline (variable . formals) body @dots{}
3964 @c EN
3965 The second form is a shorthand of @code{(define-inline variable (lambda formals body @dots{}))}.
3966
3967 If this appears in the position of internal defines, it is the same
3968 as internal defines.
3969
3970 If it appears in the toplevel, it defines an @emph{inlinable} binding.
3971 An inlinable binding promises the compiler that the binding won't
3972 change, but unlike constant bindings introduced by @code{define-constant},
3973 the actual value of @var{expression} may be computed at runtime.
3974 Hence the compiler cannot
3975 simply replace the references of @var{variable} with the compile-time
3976 value of @var{expression}.
3977
3978 However, if the compiler can determine that the value of @var{expression}
3979 is to be a procedure, it may inline the procedure where it is invoked.
3980 @c JP
3981 2番目の形式は@code{(define-inline variable (lambda formals body @dots{}))}の略記です。
3982
3983 このフォームが内部defineの位置に現れた場合は、内部defineと全く同じです。
3984
3985 このフォームがトップレベルに現れた場合、それは@emph{インライン可能な}束縛をつくります。
3986 インライン可能な束縛とは、コンパイラに対してその束縛が変化しないことを約束するものです。
3987 ただし@code{define-constant}で導入される定数束縛と違って、@var{expression}の値が
3988 コンパイル時に計算可能とは限りません。
3989 従って、コンパイラは@code{define-constant}で定義される束縛でやっている、
3990 @code{variable}の参照を無条件で@var{expression}のコンパイル時値に置き換えることは
3991 できません。
3992
3993 しかし、コンパイラが@var{expression}の値が手続きになることを決定できれば、
3994 その手続きが呼び出される箇所に手続きの中身をインライン展開することができます。
3995 @c COMMON
3996
3997 @c EN
3998 In the example below, the body of @code{dot3} is inlined where @code{dot3}
3999 is called. Furthermore, since the second argument of @code{dot3} is
4000 a constant vector, you can see @code{vector-ref} on it is computed at
4001 compile time (e.g. @code{CONST -1.0} etc.)
4002 @c JP
4003 下の例では、@code{dot3}の本体が@code{dot3}を呼び出している箇所にインライン展開されています。
4004 さらに、@code{dot3}の呼び出しの第2引数が定数ベクタのため、それに対する@code{vector-ref}が
4005 コンパイル時に計算されていることがわかります (@code{CONST -1.0}など)。
4006 @c COMMON
4007
4008 @example
4009 gosh> (define-inline (dot3 a b)
4010 (+ (* (vector-ref a 0) (vector-ref b 0))
4011 (* (vector-ref a 1) (vector-ref b 1))
4012 (* (vector-ref a 2) (vector-ref b 2))))
4013 dot3
4014 gosh> (disasm (^[] (dot3 x '#(-1.0 -2.0 -3.0))))
4015 CLOSURE #<closure (#f)>
4016 === main_code (name=#f, code=0x28524e0, size=26, const=4 stack=6):
4017 signatureInfo: ((#f))
4018 0 GREF-PUSH #<identifier user#x.20d38e0>; x
4019 2 LOCAL-ENV(1) ; (dot3 x (quote #(-1.0 -2.0 -3.0)))
4020 3 LREF0 ; a
4021 4 VEC-REFI(0) ; (vector-ref a 0)
4022 5 PUSH
4023 6 CONST -1.0
4024 8 NUMMUL2 ; (* (vector-ref a 0) (vector-ref b 0))
4025 9 PUSH
4026 10 LREF0 ; a
4027 11 VEC-REFI(1) ; (vector-ref a 1)
4028 12 PUSH
4029 13 CONST -2.0
4030 15 NUMMUL2 ; (* (vector-ref a 1) (vector-ref b 1))
4031 16 NUMADD2 ; (+ (* (vector-ref a 0) (vector-ref b 0))
4032 17 PUSH
4033 18 LREF0 ; a
4034 19 VEC-REFI(2) ; (vector-ref a 2)
4035 20 PUSH
4036 21 CONST -3.0
4037 23 NUMMUL2 ; (* (vector-ref a 2) (vector-ref b 2))
4038 24 NUMADD2 ; (+ (* (vector-ref a 0) (vector-ref b 0))
4039 25 RET
4040 @end example
4041
4042 @c EN
4043 As an extreme case, if both arguments are compile-time constant,
4044 @code{dot3} is completely computed at compile time:
4045 @c JP
4046 最も極端なケースとして、両方の引数がコンパイル時定数であれば、
4047 @code{dot3}の呼び出し自体がコンパイル時に計算されます。
4048 @c COMMON
4049
4050 @example
4051 gosh> (disasm (^[] (dot3 '#(1 2 3) '#(4 5 6))))
4052 CLOSURE #<closure (#f)>
4053 === main_code (name=#f, code=0x2a2b8e0, size=2, const=0 stack=0):
4054 signatureInfo: ((#f))
4055 0 CONSTI(32)
4056 1 RET
4057 @end example
4058
4059 @c EN
4060 The same inlining behavior may be achieved by making @code{dot3} a macro,
4061 but if you use @code{define-inline}, @code{dot3} can be used as procedures
4062 when needed:
4063 @c JP
4064 インライン展開と同様の効果は@code{dot3}をマクロにすることでも得られますが、
4065 @code{define-inline}を使っておくと@code{dot3}を通常の手続きとしても使うことができます。
4066 @c COMMON
4067
4068 @example
4069 (map dot3 list-of-vectors1 list-of-vectors2)
4070 @end example
4071
4072 @c EN
4073 If @code{dot3} is a macro you can't pass it as a higher-order procedure.
4074 @c JP
4075 @code{dot3}がマクロだと、上のように関数引数として@code{map}に渡すことはできないでしょう。
4076 @c COMMON
4077
4078 @c EN
4079 The inline expansion pass is run top-to-bottom. Inlinable procedure must
4080 be defined before used in order to be inlined.
4081 @c JP
4082 インライン展開パスはソースの上から下へと進みます。インライン展開されるためには、
4083 呼び出される前にそれが定義されていなければなりません。
4084 @c COMMON
4085
4086 @c EN
4087 If you redefine an inlinable binding, Gauche warns you, since
4088 the redefinition won't affect already inlined call sites. So it should
4089 be used with care---either use it internal to the module, or use it
4090 for procedures that won't change in future. Inlining is effective for
4091 performance-critical parts. If a procedure is called sparingly,
4092 there's no point to define it inlinable.
4093 @c JP
4094 インライン可能な束縛が再定義されるとGaucheは警告を出します。
4095 その再定義は既にインライン展開された呼び出し箇所には影響を及ぼさないからです。
4096 なのでこれは注意して使わなければなりません。モジュール内だけで使うか、
4097 あるいは将来に渡って変わらなさそうな手続きに使うか。
4098 インライン展開は性能に重大な影響を与える場所では効果的ですが、
4099 滅多に使われない手続きをインライン可能に定義する意味はありません。
4100 @c COMMON
4101 @end defspec
4102
4103
4104 @defspec define-in-module module variable expression
4105 @defspecx define-in-module module (variable . formals) body @dots{}
4106 @c EN
4107 This form must appear in the toplevel.
4108 It creates a global binding of @var{variable} in
4109 @var{module}, which must be either a symbol of the module name or
4110 a module object. If @var{module} is a symbol, the named module
4111 must exist.
4112
4113 @var{Expression} is evaluated in the current module.
4114
4115 The second form is merely a syntactic sugar of:
4116 @c JP
4117 この形式はトップレベルでしか使えません。
4118 @var{variable}のグローバルな束縛を@var{module}中に作成します。
4119 @var{module}はモジュール名を表すシンボルか、モジュールオブジェクトで
4120 なければなりません。@var{module}がシンボルの場合、その名前を持つ
4121 モジュールが既に存在している必要があります。
4122
4123 @var{expression}は現在のモジュール中で評価されます。
4124
4125 2番目の形式は次の形式の構文的修飾です。
4126 @c COMMON
4127 @example
4128 (define-in-module module variable (lambda formals body @dots{}))
4129 @end example
4130 @end defspec
4131
4132 @c EN
4133 Note: to find out if a symbol has definition (global binding) in
4134 the current module, you can use @code{module-binds?}
4135 (@pxref{Module introspection}).
4136 @c JP
4137 註: シンボルが現在のモジュール中で定義されているか(グローバルな束縛を持つか)
4138 を調べるには、@code{module-binds?}が使えます
4139 (@ref{Module introspection}参照)。
4140 @c COMMON
4141
4142 @menu
4143 * Into the Scheme-Verse::
4144 @end menu
4145
4146 @node Into the Scheme-Verse, , Definitions, Definitions
4147 @subsection Into the Scheme-Verse
4148 @c NODE Scheme多世界へようこそ
4149
4150 @c EN
4151 @subsubheading Multiple toplevels are multiple scopes
4152 @c JP
4153 @subsubheading 複数のトップレベルと複数のスコープ
4154 @c COMMON
4155
4156 @c EN
4157 Once upon a time, the Scheme world was simple. We had one single global
4158 space we called the toplevel. Toplevel definitions can be understood
4159 as side-effects to this global space; if the name hasn't been exist
4160 there yet, create a new binding, otherwise, overwrite existing one.
4161 @c JP
4162 昔、Schemeの世界は単純でした。トップレベルと呼ばれる単一のグローバルな空間があり、
4163 トップレベル定義はその空間への副作用と考えることが出来ました。名前がそこに
4164 まだなければ新たに作り、既にあったなら書き換えます。
4165 @c COMMON
4166
4167 @c EN
4168 The problem was that it was hard to scale, thus many implementations
4169 introduced their own module systems. One of the main agenda of R6RS
4170 was to have a module system (which is called ``library'' in RnRS)
4171 consistent with the design of Scheme.
4172 Especially, since Scheme's hygienic macro system captures lexical scope,
4173 it is desirable that it interacts with the module system in the same way.
4174 @c JP
4175 ただ、その単純なモデルはスケールしなかったので、
4176 実装ごとに色々なモジュールシステムが試されました。R6RSの重要な目標のひとつは、
4177 Schemeの設計と一貫性のあるモジュールシステム (RnRSでは「ライブラリ」と呼ばれることになります)
4178 を決めることでした。
4179 特に、Schemeの衛生的マクロシステムはレキシカルスコープを捕捉するものでしたから、
4180 モジュールシステムもマクロから見て同じように扱えることが望まれました。
4181 @c COMMON
4182
4183 @c EN
4184 In modern Scheme, ``toplevel'' of each module creates its own lexical
4185 scope, and the definitions are understood in @code{letrec*} semantics.
4186 Hence, macro systems can consistently treat identifiers as
4187 a name associated with a scope.
4188 @c JP
4189 現代のSchemeでは、「トップレベル」、あるいは各モジュールは自分のレキシカルスコープを
4190 持っていて、そこでの定義は@code{letrec*}セマンティクスで理解できる、とされます。
4191 つまり、マクロシステムは識別子を常に「スコープと関連付けられた名前」として扱えます。
4192 @c COMMON
4193
4194 @c EN
4195 Suppose you see these toplevel definitions:
4196 @c JP
4197 次のようなトップレベル定義があるとします。
4198 @c COMMON
4199
4200 @example
4201 (define (odd? n) (if (zero? x) #f (even? (- n 1))))
4202 (define (even? n) (if (zero? x) #t (odd? (- n 1))))
4203 @end example
4204
4205 @c EN
4206 The first appearance of @code{even?} in the first line
4207 is understood as the one defined in the second line. It becomes
4208 apparent when we compare it with internal defines:
4209 @c JP
4210 最初の行に現れる@code{even?}は、次の行で定義される名前だとみなされます。
4211 これは内部defineと比べてみるとはっきりするでしょう。
4212 @c COMMON
4213
4214 @example
4215 (let ((even? error))
4216 (define (odd? n) (if (zero? x) #f (even? (- n 1))))
4217 (define (even? n) (if (zero? x) #t (odd? (- n 1))))
4218 ...)
4219 @end example
4220
4221 @c EN
4222 The @code{even?} in the definition of @code{odd?} refers to the
4223 one defined in the next line, never to the one bound by @code{let}.
4224 @c JP
4225 @code{odd?}の定義中に現れる@code{even?}は、@code{let}が導入する束縛ではなく、
4226 次の行で定義される@code{even?}を参照します。
4227 @c COMMON
4228
4229 @c EN
4230 So far, so good.
4231 @c JP
4232 ここまではいいですね。
4233 @c COMMON
4234
4235 @c EN
4236 Now, consider the following toplevel code:
4237 @c JP
4238 では、次のコードを考えてみてください。
4239 @c COMMON
4240
4241 @example
4242 ;; Invalid in RnRS, n >= 6
4243 (import (scheme base) (scheme write))
4244 (define orig-error error)
4245 (define (error . args)
4246 (write args) (newline)
4247 (apply orig-error args))
4248 @end example
4249
4250 @c EN
4251 The intention is to save the @emph{original} value of @code{error},
4252 which is imported from @code{(scheme base)}, into a variable
4253 @code{orig-error}, then redefine @code{error} to add logging feature.
4254 This technique was popular in pre-R6RS Scheme.
4255 @c JP
4256 コードの意図は、@code{(scheme base)}からインポートされた
4257 @code{error}の@emph{元の}値を変数@code{orig-error}にセーブしておいて、
4258 @code{error}を再定義してログを出す機能を仕込むというものです。
4259 このテクニックはR6RSより前のSchemeではよく見られました。
4260 @c COMMON
4261
4262 @c EN
4263 However, with our new toplevel-as-a-scope Scheme, the @code{error}
4264 in @code{(define orig-error error)} @emph{must} refer to the one defined
4265 in the same scope, which is the new definition below; otherwise lexical
4266 scoping gets broken. The value of inner @code{error} hasn't been calculated
4267 when @code{orig-error}'s value is calculated, so the above form is
4268 an invalid program in terms of RnRS.
4269 @c JP
4270 でも、トップレベルはスコープであるとした最近のSchemeでは、
4271 @code{(define orig-error error)}における@code{error}は
4272 同じスコープで定義されるもの、つまりその下で定義される@code{error}を参照しなければ
4273 なりません。でないとレキシカルスコープの規則が崩れてしまいます。
4274 とはいえ内側の@code{error}の値は@code{orig-error}の初期値を計算する時点では
4275 まだ計算されていないので、上のコードはRnRS的には無効なコードということになります。
4276 @c COMMON
4277
4278 @c EN
4279 In fact, to avoid confusion, R6RS prohibits defining a toplevel variable
4280 that conflicts with the imported name (in R7RS the behavior of such program
4281 is undefined). In the example above,
4282 the name @code{error} is imported from @code{(scheme base)} and also
4283 defined in the toplevel, hence it's a violation.
4284 @c JP
4285 実際、混乱を避けるために、R6RSではインポートした名前とぶつかる名前をトップレベルで
4286 定義することを禁止しています (R7RSでは未定義動作になっています)。
4287 上の例では、@code{error}は@code{(scheme base)}から既にインポートされているので、
4288 トップレベルで再定義するのは文法違反となります。
4289 @c COMMON
4290
4291 @c EN
4292 The modern way of such augumentation is to use renaming import:
4293 @c JP
4294 既存の手続きに機能を付け加える、現代風のやり方は、インポート時にリネームすることです。
4295 @c COMMON
4296
4297 @example
4298 (import (except (scheme base) error)
4299 (rename (scheme base) (error r7rs:error))
4300 (scheme write))
4301 (define (error . args)
4302 (write args) (newline)
4303 (apply r7rs:error args))
4304 @end example
4305
4306 @c EN
4307 @subsubheading Gauche's take
4308 @c JP
4309 @subsubheading Gaucheの選択
4310 @c COMMON
4311
4312 @c EN
4313 Gauche's module system predates R6RS and R7RS, and it regards a module
4314 as a first-class entity and supports class-like inheritance. It is
4315 upper-compatible to R7RS libraries, but we take freedom in interpreting
4316 R7RS undefined behaviors.
4317 @c JP
4318 GaucheのモジュールシステムはR6RS/R7RS以前からありました。
4319 モジュールは第一級のオブジェクトで、クラスのような継承関係も持てます。
4320 R7RSのライブラリの上位互換ですが、R7RSの未定義動作の部分については
4321 拡張解釈をしています。
4322 @c COMMON
4323
4324 @c EN
4325 First, you can define toplevel variables that conflict with
4326 imported or inherited bindings. The new definition simply shadows the
4327 old one.
4328 @c JP
4329 まず、インポートしたり継承している名前と同名のトップレベル変数を定義できます。
4330 新たな定義は前の定義を単にシャドウします。
4331 @c COMMON
4332
4333 @c EN
4334 Second, if multiple toplevel forms are processed at once
4335 e.g. it is enclosed in @code{begin} or the file is read by @code{include},
4336 we treat them in one scope. That is, if the above @code{orig-error} example
4337 is read by @code{include}, the first @code{error} refers to the to-be-defined
4338 @code{error} below. Since the value of @code{error} hasn't been calculated
4339 by the time it's used, you'll get the following error:
4340 @c JP
4341 次に、複数のトップレベルフォームが@code{begin}に包まれたり
4342 @code{include}経由で読まれるなどした場合、それらはひとつのスコープにあるものとして
4343 処理されます。上の@code{orig-error}の例が@code{include}で読まれたとすると、
4344 最初の@code{error}はその下で定義されるはずの@code{error}を参照することになります。
4345 そのスコープの@code{error}の値は、@code{orig-error}の初期値を計算する時点では
4346 計算されていないので、次のエラーが投げられます:
4347 @c COMMON
4348
4349 @example
4350 *** ERROR: uninitialized variable: error
4351 @end example
4352
4353 @c EN
4354 Third, Gauche compiles and executes each individual toplevel forms
4355 (the forms that's not enclosed in other S-expressions). It is the
4356 same as REPL semantics. If each form of @code{orig-error} example
4357 appears individually on the toplevel, the @code{(define orig-error error)}
4358 line actually refers to the R7RS @code{error} and assign it to
4359 @code{orig-error}, since @emph{we don't know yet if @code{error} will be
4360 defined in the same scope}.
4361 @c JP
4362 一方、Gaucheは他のS式で囲まれていないトップレベルフォームは、ひとつひとつ
4363 逐次的に処理します。つまりREPLと同じセマンティクスです。
4364 @code{orig-error}の例がそのまま読み込まれた場合、@code{(define orig-error error)}
4365 の@code{error}はR7RSの@code{error}を参照し、その値が@code{orig-error}に
4366 束縛されます。なぜなら@code{(define orig-error error)}を見た時点では、
4367 次に@code{error}が再定義されるかどうかはまだわからないからです。
4368 @c COMMON
4369
4370 @c EN
4371 The third rule is necessary to support REPL semantics, but note that
4372 the result would differ when the same file is @code{include}d. If
4373 you can, avoid writing such ambiguous code.
4374 @c JP
4375 最後の動作はREPLをサポートするのに必要な動作ですが、
4376 同じコードを@code{include}した場合に結果が変わってくることに気をつけてください。
4377 こういった曖昧性のあるコードを書くのを避けることが一番です。
4378 @c COMMON
4379
4380 @c EN
4381 Note: The second behavior is clarified in release 0.9.9 for the
4382 better compatibility with R7RS. Before that,
4383 the behavior of such case is undefined, but some code might have
4384 expected that it works in REPL semantics (the third rule).
4385 @c JP
4386 註:トップレベルフォームが@code{begin}で囲まれた場合の動作は、
4387 R7RSとの互換性を高めるために、0.9.9で明確化されました。
4388 それ以前のバージョンでは上の例の動作は未定義でしたが、
4389 REPLセマンティクスで動くことを期待していたコードはあるかもしれません。
4390 @c COMMON
4391
4392 @c EN
4393 In order to support the transition, if you set an environment
4394 variable @code{GAUCHE_LEGACY_DEFINE}, Gauche treats definitions
4395 in the same way as 0.9.8 and before.
4396 Note that if you do that, you may see Gauche can't include some valid R7RS
4397 code that has multiple libraries in one file.
4398 @c JP
4399 移行をスムースにするために、環境変数@code{GAUCHE_LEGACY_DEFINE}が定義されている
4400 場合は、Gaucheはトップレベル定義の評価を0.9.8以前と同じにします。
4401 その場合、複数のライブラリが1ファイルで定義されている
4402 R7RSとして有効なコードをincludeできない場合があります。
4403 @c COMMON
4404
4405 @c ----------------------------------------------------------------------
4406 @node Inclusions, Feature conditional, Definitions, Core syntax
4407 @section Inclusions
4408 @c NODE インクルード
4409
4410 @defspec include filename @dots{}
4411 @defspecx include-ci filename @dots{}
4412 [R7RS base]
4413 @c EN
4414 Reads @var{filename} @dots{} at compile-time, and insert their
4415 contents as if the forms are placed in the includer's source file,
4416 surrounded by @code{begin}. The @code{include} form reads files
4417 as is, while @code{include-ci} reads files in case-insensitive
4418 way, as if @code{#!fold-case} is specified in the beginning of the
4419 file (@pxref{Case-sensitivity}).
4420 @c JP
4421 @var{filename} @dots{}で指定されるファイルをコンパイル時に読み、
4422 その内容が@code{begin}で囲まれてインクルードフォームの位置に替わりに
4423 置かれているかのように解釈します。
4424 @code{include}フォームはファイルの内容をそのまま読みますが、
4425 @code{include-ci}フォームは、あたかもファイル先頭に@code{#!fold-case}が
4426 指定されたかのように、シンボルの大文字小文字を区別しないモードで読みます
4427 (@ref{Case-sensitivity}参照)。
4428 @c COMMON
4429
4430 @c EN
4431 The coding magic comment in each file is honored while reading
4432 that file (@pxref{Multibyte scripts}).
4433 @c JP
4434 読み込まれるファイルに文字エンコーディングを指定するコメントがあればそれは
4435 そのファイルを読んでいる間有効になります(@ref{Multibyte scripts}参照)。
4436 @c COMMON
4437
4438 @c EN
4439 If @var{filename} is absolute, the file is just searched. If it
4440 is relative, the file
4441 is first searched relative to the file containing the @code{include}
4442 form, then the directories in @code{*load-path*} are tried.
4443 @c JP
4444 @var{filename}が絶対パスならば、そのパスに正確に合致するファイルが探されます。
4445 相対パスなら、まず@code{include}フォームが
4446 置かれていたファイルからの相対で探され、見つからなければ
4447 @code{*load-path*}にあるディレクトリからの相対パスが順に試されます。
4448 @c COMMON
4449
4450 @c EN
4451 Example: Suppose a file @file{a.scm} contains the following code:
4452 @c JP
4453 例: ファイル@file{a.scm}には次のコードが書かれているとします。
4454 @c COMMON
4455
4456 @example
4457 (define x 0)
4458 (define y 1)
4459 @end example
4460
4461 @c EN
4462 You can include this file into another source, like this:
4463 @c JP
4464 このファイルを別ソースファイルからインクルードすることができます:
4465 @c COMMON
4466
4467 @example
4468 (define (foo)
4469 (include "a.scm")
4470 (list x y))
4471 @end example
4472
4473 @c EN
4474 It works as if the source is written as follows:
4475 @c JP
4476 この場合、元のソースファイルはあたかも次のように書かれていたかのように解釈されます。
4477 @c COMMON
4478
4479 @example
4480 (define (foo)
4481 (begin
4482 (define x 0)
4483 (define y 1))
4484 (list x y))
4485 @end example
4486
4487 @c EN
4488 (Note: In version 0.9.4, @code{include} behaved differently when
4489 pathname begins with either @code{./} or @code{../}---in which case
4490 the file is searched relative to the current working directory of the
4491 compiler. It is rather an artifact of @code{include} sharing file
4492 search routine with @code{load}. But unlike @code{load}, which is
4493 a run-time operation, using relative path to the current directory
4494 won't make much sense for @code{include}, so we changed the behavior
4495 in 0.9.5.)
4496 @c JP
4497 (註: バージョン0.9.4では、@code{include}は渡されたパス名が
4498 @code{./}か@code{../}で始まっていた場合は特別な動作をしていました。
4499 コンパイラのカレントディレクトリからの相対でファイルを探していたのです。
4500 これは意図した動作というより、@code{load}とファイル探索ルーチンを共有していた
4501 ために生じた仕様でした。しかし@code{load}が常に実行時の動作であるのに対し、
4502 @code{include}でカレントディレクトリからの相対パスを考える意味はほとんどありません。
4503 そこで0.9.5では仕様を変更しました。)
4504 @c COMMON
4505
4506 @end defspec
4507
4508 @c EN
4509 Gauche has other means to incorporate source code from
4510 another files. Here's the comparison.
4511 @c JP
4512 Gaucheには、他のファイルからソースを読んでくる方法がいくつかあります。
4513 以下にインクルードとの比較を説明します。
4514 @c COMMON
4515
4516 @table @asis
4517 @c EN
4518 @item @code{require} (@code{use} and @code{extend} calls @code{require} internally)
4519 @c JP
4520 @item @code{require} (@code{use}と@code{extend}も内部で@code{require}を呼んでいます)
4521 @c COMMON
4522 @itemize
4523 @item
4524 @c EN
4525 Both @code{require} and @code{include} work at compile-time.
4526 @c JP
4527 @code{require}も@code{include}もコンパイル時に解釈されます。
4528 @c COMMON
4529 @item
4530 @c EN
4531 @code{Require}
4532 works only in toplevel context, while @code{include} can be
4533 anywhere.
4534 @c JP
4535 @code{require}はトップレベルでのみ動作します。
4536 それに対し、@code{include}はどこにでも置けます。
4537 @c COMMON
4538 @item
4539 @c EN
4540 @code{Require} reads the file only once (second and later @code{require} on the
4541 same file becomes no-op), while @code{include} reads the file
4542 every place it appears.
4543 @c JP
4544 @code{require}は
4545 ファイルを一度だけ読みます(つまり、2度め以降の同じファイルに対する@code{require}は
4546 単なる無動作(no-op)になります)。これに対し、@code{include}は現れる場所ごとに
4547 ファイルを読み込みます。
4548 @c COMMON
4549 @item
4550 @c EN
4551 The file is searched from @code{*load-path*}. The location
4552 of the file @code{require} form appears doesn't matter.
4553 (You can add directories relative to the requiring file
4554 using the @code{:relative} flag in @code{add-load-path}, though).
4555 @c JP
4556 ファイルは@code{*load-path*}から探され、@code{require}フォームが
4557 使われたファイルの位置は関係ありません (但し、@code{add-load-path}の
4558 @code{:relative}フラグを使うことで、
4559 @code{require}フォームを使っているファイルの位置をロードパスに加えることはできます。)
4560 @c COMMON
4561 @item
4562 @c EN
4563 Even if the current module is changed by @code{select-module}
4564 inside the required file, it is only effective while the
4565 required file is read. On the other hand, @code{include}
4566 inserts any S-expressions in the included file to the
4567 place @code{include} appears, so the effect of @code{select-module}
4568 persists after @code{include} form (Note: Encoding magic comment
4569 and @code{#!fold-case}/@code{#!no-fold-case} are dealt with by
4570 the reader, so those effect is contained in the file even with
4571 @code{include}).
4572 @c JP
4573 @code{require}されたファイル中でカレントモジュールが
4574 @code{select-module}で変更されたとしても、その有効範囲はそのファイル内に留まります。
4575 一方@code{include}は、@code{include}フォームが出現した場所にS式が
4576 挿入されたように解釈されるので、@code{select-module}の効果は
4577 それが挿入された箇所から@code{include}しているファイルの最後までということになります。
4578 (文字エンコーディング指定コメントおよび、@code{#!fold-case}/@code{#!no-fold-case}
4579 による大文字小文字指定はリーダによって処理されるので、
4580 効果は@code{include}されるファイルの中に留まります。)
4581 @c COMMON
4582 @item
4583 @c EN
4584 It is forbidden to the file loaded by @code{require} to insert
4585 a toplevel binding without specifying a module. In other words,
4586 the file you require should generally use @code{define-module},
4587 @code{select-module} or @code{define-library}.
4588 @xref{Require and provide}, for further discussion.
4589 On the other hand, @code{include} has no such restrictions.
4590 @c JP
4591 @code{require}からロードするファイルが、モジュールを指定せずにトップレベル
4592 束縛を挿入することは禁じられています。別の言い方をすれば、
4593 @code{require}するファイルは一般的に@code{define-module}や@code{select-module}、
4594 あるいは@code{define-library}を使う必要があるということです。
4595 詳しい議論は@ref{Require and provide}を参照してください。
4596 一方で、@code{include}にはそういった制限はありません。
4597 @c COMMON
4598 @end itemize
4599
4600 @item @code{load}
4601 @itemize
4602 @item
4603 @c EN
4604 Works at runtime, while @code{include} works at compile-time.
4605 @c JP
4606 @code{include}がコンパイル時に解釈されるのに対し、こちらは実行時に解釈されます。
4607 @c COMMON
4608 @item
4609 @c EN
4610 Works only in toplevel context, while @code{include} can be
4611 anywhere.
4612 @c JP
4613 トップレベルでのみ動作します。それに対し、@code{include}はどこにでも置けます。
4614 @c COMMON
4615 @item
4616 @c EN
4617 The file is searched from @code{*load-path*}, except when the
4618 file begins with @code{./} or @code{../}, in which case it is
4619 first tried relative to the current directory before being searched
4620 from @code{*load-path*}.
4621 @c JP
4622 ファイルは@code{*load-path*}から探されます。
4623 但しファイルが@code{./}か@code{../}で始まっていた場合は、
4624 まずカレントディレクトリ相対で探され、それから@code{*load-path*}が探されます。
4625 @c COMMON
4626 @item
4627 @c EN
4628 As the case with @code{require}, change of the current module
4629 won't persist after @code{load}.
4630 @c JP
4631 @code{require}と同様、ロードされるファイル中でカレントモジュールを変えても、
4632 その効果は@code{load}終了時までです。
4633 @c COMMON
4634 @end itemize
4635
4636 @end table
4637
4638 @c EN
4639 Usually, @code{require} (or @code{use} and @code{extend}) are
4640 better way to incorporate sources in other files. The @code{include}
4641 form is mainly for the tricks that can't be achieved with
4642 @code{require}. For example, you have a third-party R5RS code
4643 and you want to wrap it with Gauche module system. Using @code{include},
4644 you place the following small source file along the third-party code,
4645 and you can load the code with @code{(use third-party-module)} without
4646 changing the original code at all.
4647 @c JP
4648 通常、他のファイルに書かれたソースファイルを取り込むのに適した方法は
4649 @code{require} (もしくは@code{use}や@code{extend}) です。
4650 @code{include}フォームは、@code{require}では実現が難しいトリックのために使います。
4651 例えば、第三者がR5RS向けに書いたコードを、Gaucheのモジュールシステムでラップしたい場合、
4652 @code{include}を使う
4653 次のような内容の小さなファイルをそのコードのファイルと同じディレクトリに置いておけば、
4654 第三者のコードを一切変えることなく、@code{(use third-party-module)} で
4655 コードをロードできます。
4656 @c COMMON
4657
4658 @example
4659 (define-module third-party-module
4660 (export proc ...)
4661 (include "third-party-source.scm"))
4662 @end example
4663
4664 @c ----------------------------------------------------------------------
4665 @node Feature conditional, Modules, Inclusions, Core syntax
4666 @section Feature conditional
4667 @c NODE 機能条件式
4668
4669 @c EN
4670 @subheading The @code{cond-expand} macro
4671 @c JP
4672 @subheading @code{cond-expand}マクロ
4673 @c COMMON
4674
4675 @c EN
4676 Sometimes you need to have a different piece of code
4677 depending on available features provided by the
4678 implementation and/or platform. For example,
4679 you may want to switch behavior depending on whether
4680 networking is available, or to embed an
4681 implementation specific procedures in otherwise-portable code.
4682 @c JP
4683 しばしば、実装やプラットフォームがどういった機能を提供しているかに
4684 応じてコードを切り替えたいことがあります。例えば、ネットワークが使えるか
4685 どうかで振る舞いを変えるとか、ほぼポータブルなコードの一部だけに
4686 特定の実装に依存するコードを入れたいとかいう場合です。
4687 @c COMMON
4688
4689 @c EN
4690 In C, you use preprocessor directives such as @code{#ifdef}.
4691 In Common Lisp, you use reader macro @code{#+} and @code{#-}.
4692 In Scheme, you have @code{cond-expand}:
4693 @c JP
4694 C言語なら、@code{#ifdef}のようなプリプロセッサディレクティブを、
4695 Common Lispなら、@code{#+}と@code{#-}のようなリーダマクロを使うところです。
4696 Schemeでは、こういう時には@code{cond-expand}を使います。
4697 @c COMMON
4698
4699 @defmac cond-expand (feature-requirement command-or-definition @dots{}) @dots{}
4700 [R7RS base]
4701 @c EN
4702 This macro expands to @var{command-or-definition} @dots{} if
4703 @var{feature-requirement} is supported by the current platform.
4704
4705 @var{feature-requirement} must be in the following syntax:
4706 @c JP
4707 プラットフォームが@var{feature-requirement}に示される機能をサポートしていれば、
4708 このマクロは@var{command-or-definition} @dots{} へと展開されます。
4709
4710 @var{feature-requirement} は以下のような構文でなければなりません。
4711 @c COMMON
4712 @example
4713 @var{feature-requirement}
4714 : @var{feature-identifier}
4715 | (and @var{feature-requirement} @dots{})
4716 | (or @var{feature-requirement} @dots{})
4717 | (not @var{feature-requirement})
4718 | (library @var{library-name})
4719 @end example
4720
4721 @c EN
4722 The macro tests each @var{feature-requirement} in order, and
4723 if one is satisfied, the macro itself expands to the
4724 corresponding @var{command-or-definition} @dots{}.
4725 @c JP
4726 このマクロは @var{feature-requirement} を順にテストし、そのひとつが
4727 満たされたら、対応する @var{command-or-definition} @dots{} に展開されます。
4728 @c COMMON
4729
4730 @c EN
4731 The last clause may have @code{else} in the position of
4732 @var{feature-requirement}, to make the clause expanded
4733 if none of the previous feature requirement is fulfilled.
4734 @c JP
4735 最後の節の@var{feature-requirement}の部分にはシンボル@code{else}を
4736 置くこともできます。他の節の@var{feature-requirement}が満たされなかった場合に
4737 その節が展開されます。
4738 @c COMMON
4739
4740 @c EN
4741 If there's neither a satisfying clause nor @code{else} clause,
4742 @code{cond-expand} form throws an error. It is to detect the case early
4743 that the platform doesn't have required features. If the feature
4744 you're testing is optional, that is, your program works without the
4745 feature as well, add empty @code{else} clause as follows.
4746 @c JP
4747 条件を満たす節も@code{else}節も無かった場合、@code{cond-expand}は
4748 エラーを通知します。この仕様は、
4749 必要な機能を欠いている実行環境を早めに検出するためです。
4750 もしチェックする機能がオプショナル、つまりそれが無くてもプログラムの実行自体は
4751 可能であるというものであるなら、最後に空の@code{else}節を忘れないようにしてください。
4752 @c COMMON
4753
4754 @example
4755 (cond-expand
4756 [feature expr] ; some optional feature
4757 [else])
4758 @end example
4759
4760
4761 @c EN
4762 @var{feature-identifier} is a symbol that indicates a feature.
4763 If such a feature is supported in the current platform, it satisfies the
4764 @var{feature-requirement}. You can do boolean combination
4765 of @var{feature-requirement}s to compose more complex conditions.
4766 @c JP
4767 @var{feature-identifier} は機能を示すシンボルです。その機能が現在のプラットフォームで
4768 サポートされているなら、それは、@var{feature-requirement} を満たします。
4769 より複雑な条件を構成するために、@var{feature-requirement} のブール代数による
4770 組み合わせを用いることが出来ます。
4771 @c COMMON
4772
4773 @c EN
4774 The form @code{(library @var{library-name})} is added in R7RS,
4775 and it is fulfilled when the named library is available. For the
4776 @var{library-name}, you can use both R7RS library name
4777 (e.g. @code{(srfi 13)}) and traditional Gauche module name
4778 (e.g. @code{srfi.13}).
4779 @c JP
4780 @code{(library @var{library-name})}という形式はR7RSで追加されたもので、
4781 指定されるライブラリが使える場合に条件が満たされます。
4782 @var{library-name}は、R7RS形式
4783 (例: @code{(srfi 13)}) も伝統的なGaucheのモジュール名
4784 (例: @code{srfi.13}) も使えます。
4785 @c COMMON
4786
4787 @c EN
4788 Here's a typical example: Suppose you want to have implementation-specific
4789 part for Gauche, Chicken Scheme and ChibiScheme. Most modern Scheme
4790 implementations defines a feature-identifier to identify itself.
4791 You can write the conditional part as follows:
4792 @c JP
4793 例えば、Gauche、Chicken Scheme、ChibiSchemeに依存するコードを
4794 入れたいとしましょう。現代の多くのScheme実装は、自分自身を示す
4795 feature-identifierを定義しているので、条件付きのコードは次のとおり書けます。
4796 @c COMMON
4797
4798 @example
4799 (cond-expand
4800 [gauche (gauche-specific-code)]
4801 [(or chicken chibi) (chicken-chibi-specific-code)]
4802 [else (fallback-code)]
4803 )
4804 @end example
4805
4806 @c EN
4807 It is important that the conditions of @code{cond-expand} is
4808 purely examined at the macro-expansion time, and
4809 unfulfilled clauses are discarded.
4810 Thus, for example, you can include macro calls or language extensions
4811 that may not be recognized on some implementations. You can also
4812 conditionally define global bindings.
4813 @c JP
4814 @code{cond-expand}の条件はマクロ展開時に全て処理され、
4815 不採用だった節のコードは捨てられる、という事実は重要です。
4816 このおかげで、例えば実装によって認識されないマクロ呼び出しや言語拡張を
4817 @var{command-or-definition}に含めることができます。
4818 また、グローバルな束縛を条件によって定義したりしなかったりすることができます。
4819 @c COMMON
4820
4821 @c EN
4822 Compare that to @code{cond}, which examines conditions at runtime.
4823 If you include unsupported macro call in one of the conditions, it
4824 may raise an error at macro expansion time, even if that clause
4825 will never be executed on the platform. Also, it is not possible
4826 to conditionally define global bindings using @code{cond}.
4827 @c JP
4828 これを@code{cond}と比較してみましょう。@code{cond}は条件を実行時に調べます。
4829 もし条件節の中に、あるプラットフォームではサポートされないマクロ呼び出しを
4830 書いた場合、たとえその節が現実には決して実行されないものであるとしても、
4831 マクロ展開時にエラーになってしまうかもしれません。
4832 また、@code{cond}を使って条件的にグローバルな束縛を定義することはできません。
4833 @c COMMON
4834
4835 @c EN
4836 There's a caveat, though. Suppose you want to save the result of macro
4837 expansion, and run the expanded result later on other platforms.
4838 The result code is based on the features of the platform the macro
4839 expansion takes place, which may not agree with the features
4840 of the platform the code will run. (This issue always arises
4841 in cross-compiling situation in general.)
4842
4843 @c Gauche has @emph{ahead-of-time compilation} feature, which is only
4844 @c used internally now, but will eventually be made for public use.
4845 @c JP
4846 ただし、マクロ展開した結果をどうにかして保存しておいて、
4847 別のプラットフォームでそれを走らせる、ということをする場合は注意が必要です。
4848 条件選択はマクロ展開をするプラットフォームの機能に応じて行われ、
4849 それはコードを実行するプラットフォームの機能とは一致しないかもしれません
4850 (これはもちろん、クロスコンパイルで常に問題となることです。)
4851 @c COMMON
4852
4853 @c EN
4854 NB: When @code{cond-expand} was defined as SRFI-0, there was no standard
4855 way to name Scheme libraries. So SRFI-0 suggested to use @code{srfi-N}
4856 as feature identifier to check the support of a specific srfi.
4857 @b{We deprecate this usage}. It should be replaced with
4858 @code{(library srfi.N)}. @xref{Testing availability of SRFIs}, for the
4859 details.
4860
4861 As of 0.9.13, a warning is issued when @code{srfi-N} feature identifier
4862 is used in @code{cond-expand}
4863 if the environment variable @code{GAUCHE_WARN_SRFI_FEATURE_ID} is set.
4864 In future versions the warning will be default; we recommend to
4865 set this environment variable to detect code to update.
4866 @c JP
4867 註: @code{cond-expand}がSRFI-0として定義された時には、
4868 Schemeライブラリの命名法の標準が決まっていませんでした。
4869 SRFI-0のドキュメントでは@code{srfi-N}という形式の機能識別子で特定のsrfiが
4870 サポートされているかどうかを検査するように推奨されていました。
4871 @b{この使い方は現在は非推奨です}。@code{(library srfi.N)}で検査するように
4872 変更してください。詳しくは@ref{Testing availability of SRFIs}参照。
4873
4874 0.9.13時点では、環境変数@code{GAUCHE_WARN_SRFI_FEATURE_ID}が
4875 セットされている場合に限り、機能識別子@code{srfi-N}が使われた時に
4876 警告が出されます。将来はデフォルトで警告を出すようにするので、
4877 いまのうちにこの環境変数をセットして古いコードを見つけて書き換えておくと良いでしょう。
4878 @c COMMON
4879
4880 @c EN
4881 See below for the list of feature identifiers defined in Gauche.
4882 @c JP
4883 Gaucheで使えるfeature identifier一覧は下を見てください。
4884 @c COMMON
4885 @end defmac
4886
4887 @c EN
4888 @subheading Gauche-specific feature identifiers
4889 @c JP
4890 @subheading Gauche特有のfeature identifier
4891 @c COMMON
4892
4893 @table @code
4894 @item gauche
4895 @itemx gauche-X.X.X
4896 @c EN
4897 Indicates you're running on Gauche. It is useful to put Gauche-specific
4898 code in a portable program.
4899 @code{X.X.X} is the gauche's version
4900 (e.g. @code{gauche-0.9.4}), in case you want to have code
4901 for specific Gauche version. (Such feature identifier is suggested by
4902 R7RS; but it might not be useful if we don't have means to compare versions.
4903 Something to consider in future versions.)
4904 @c JP
4905 プログラムをGaucheで実行していることを示します。ポータブルなプログラムの中に
4906 Gauche特有のコードを埋め込むのに便利です。
4907 @code{X.X.X}はGaucheのバージョンで(例: @code{gauche-0.9.4})、
4908 特定のGaucheのバージョンのみに依存したコードを入れるのに使えます。
4909 (処理系のバージョンつきの機能識別子はR7RSで示唆されているものですが、
4910 バージョンを比較する機能がないとあまり有用ではないでしょう。
4911 将来はそのような機能が追加されるかもしれません。)
4912 @c COMMON
4913
4914 @item gauche.os.windows
4915 @itemx gauche.os.cygwin
4916 @c EN
4917 Defined on Windows-native platform and Cygwin/Windows platform, respectively.
4918 If neither is defined you can assume it's a unix variant.
4919 (Cygwin is supposedly
4920 unix variant, but corners are different enough to deserve it's own
4921 feature identifier.)
4922 @c JP
4923 それぞれ、WindowネイティブプラットフォームとCygwin/Windowsプラットフォームを
4924 示します。どちらも定義されていなければ、Unix系と考えて構いません。
4925 (CygwinもUnix系と言えなくはありませんが、他のUnix系に比べて
4926 色々違いがあるので、機能識別子を用意してあります。)
4927 @c COMMON
4928
4929 @item gauche.ces.utf8
4930 @c EN
4931 This indicates Gauche's internal encoding is @code{utf-8}.
4932 Before 1.0, Gauche can be compiled with internal encodings other
4933 than @code{utf-8}, and the corresponding feature identifiers are
4934 defined so that the program can switch its behavior according to it.
4935 Now this is the only possible choice. This is kept for the
4936 backward compatibility.
4937 @c JP
4938 Gaucheの内部エンコーディングが@code{utf-8}であることを示します。
4939 1.0以前には、Gaucheはutf-8以外のいくつかの内部エンコーディングで
4940 コンパイルすることが可能で、それに合わせた機能識別子が定義され、
4941 プログラムが内部エンコーディングに応じて振る舞いを変えられるようになっていました。
4942 今はこれが唯一のエンコーディングです。過去のコードを変更なしで走らせられるように
4943 定義されています。
4944 @c COMMON
4945
4946 @item gauche.net.tls
4947 @itemx gauche.net.tls.axtls
4948 @itemx gauche.net.tls.mbedtls
4949 @c EN
4950 Defined if the runtime supports TLS in networking.
4951 The two sub feature identifiers, @code{gauche.net.tls.axtls} and
4952 @code{gauche.net.tls.mbedtls}, are defined if each subsystem
4953 @code{axTLS} and @code{mbedTLS} is supported, respectively.
4954 @c JP
4955 TLS/SSLサポートがあれば定義されます。サブ機能識別子@code{gauche.net.tls.axtls}と
4956 @code{gauche.net.tls.mbedtls}は、それぞれサブシステム@code{axTLS}
4957 と@code{mbedTLS}がサポートされていれば定義されます。
4958 @c COMMON
4959
4960 @item gauche.net.ipv6
4961 @c EN
4962 Defined if the runtime supports IPv6.
4963 Note that this only indicates Gauche has been built with IPv6 support;
4964 the OS may not allow IPv6 features, in that case you'll get system error
4965 when you try to use IPv6.
4966 @c JP
4967 IPv6サポートが組み込まれていれば定義されます。
4968 ただし、これがGaucheがIPv6サポート込みでビルドされたことを示すだけです。
4969 OSがIPv6サポートをオフにしている場合、IPv6機能を使おうとするとシステムエラーが
4970 投げられます。
4971 @c COMMON
4972
4973 @item gauche.sys.threads
4974 @itemx gauche.sys.pthreads
4975 @itemx gauche.sys.wthreads
4976 @c EN
4977 If the runtime supports multithreading, @code{gauche.sys.threads} is
4978 defined (@pxref{Threads}). Multithreading is based on either POSIX pthreads
4979 or Windows threads. The former defines @code{gauche.sys.pthreads},
4980 and the latter defines @code{gauche.sys.wthreads}.
4981 @c JP
4982 マルチスレッドがサポートされていれば@code{gauche.sys.threads}が定義されます
4983 (@ref{Threads}参照)。また、マルチスレッドは内部ではプラットフォームによって
4984 POSIXスレッドかWindowsスレッドを利用しますが、前者の場合は
4985 @code{gauche.sys.pthreads}が、後者では@code{gauche.sys.wthreads}が
4986 併せて定義されます。
4987 @c COMMON
4988
4989 @item gauche.sys.sigwait
4990 @itemx gauche.sys.setenv
4991 @itemx gauche.sys.unsetenv
4992 @itemx gauche.sys.clearenv
4993 @itemx gauche.sys.getloadavg
4994 @itemx gauche.sys.getrlimit
4995 @itemx gauche.sys.lchown
4996 @itemx gauche.sys.getpgid
4997 @itemx gauche.sys.nanosleep
4998 @itemx gauche.sys.crypt
4999 @itemx gauche.sys.symlink
5000 @itemx gauche.sys.readlink
5001 @itemx gauche.sys.select
5002 @itemx gauche.sys.fcntl
5003 @itemx gauche.sys.syslog
5004 @itemx gauche.sys.setlogmask
5005 @itemx gauche.sys.openpty
5006 @itemx gauche.sys.forkpty
5007 @c EN
5008 Those are defined based on the availability of these system features
5009 of the platform.
5010 @c JP
5011 これらシステムの機能が使えるのなら対応する機能識別子が定義されます。
5012 @c COMMON
5013 @end table
5014
5015
5016 @c EN
5017 @subheading R7RS feature identifiers
5018 @c JP
5019 @subheading R7RSで定義されたfeature identifiers
5020 @c COMMON
5021
5022 @table @code
5023 @item r7rs
5024 @c EN
5025 Indicates the implementation complies r7rs.
5026 @c JP
5027 実装がr7rs準拠であることを示します。
5028 @c COMMON
5029
5030 @item exact-closed
5031 @c EN
5032 Exact arithmetic operations are closed; that is, dividing
5033 an exact number by a non-zero exact number always yields an exact
5034 number.
5035 @c JP
5036 数値計算が正確な値について閉じている、つまり、正確な数を
5037 ゼロ以外の正確な数で割った結果が常に正確な数になることを示します。
5038 @c COMMON
5039
5040 @item ieee-float
5041 @c EN
5042 Using IEEE floating-point number internally.
5043 @c JP
5044 内部的にIEEE浮動小数点数を使っていることを示します。
5045 @c COMMON
5046
5047 @item full-unicode
5048 @c EN
5049 Full unicode support.
5050 @c JP
5051 Unicodeの全ての範囲をサポートしていることを示します。
5052 @c COMMON
5053
5054 @item ratios
5055 @c EN
5056 Rational number support
5057 @c JP
5058 有理数のサポートがあることを示します。
5059 @c COMMON
5060
5061 @item posix
5062 @itemx windows
5063 @c EN
5064 Either one is defined, according to the platform.
5065 @c JP
5066 プラットフォームによってどちらかが定義されます。
5067 @c COMMON
5068
5069 @item big-endian
5070 @itemx little-endian
5071 @c EN
5072 Either one is defined, according to the platform.
5073 @c JP
5074 プラットフォームによってどちらかが定義されます。
5075 @c COMMON
5076 @end table
5077
5078
5079
5080 @c ----------------------------------------------------------------------
5081 @node Modules, , Feature conditional, Core syntax
5082 @section Modules
5083 @c NODE モジュール
5084
5085 @c EN
5086 This section describes the semantics of Gauche modules and its API.
5087 See also @ref{Writing Gauche modules}, for the conventions
5088 Gauche is using for its modules.
5089 @c JP
5090 この章では、GaucheのモジュールのセマンティクスとAPIを述べます。
5091 Gaucheで使われているモジュールの書法については@ref{Writing Gauche modules}も
5092 併せて参照して下さい。
5093 @c COMMON
5094
5095 @c EN
5096 For R7RS programs, they are called ``libraries'' and have different
5097 syntax than Gauche modules. @xref{R7RS library form}, for the details.
5098 @c JP
5099 R7RSプログラムでは、モジュールに相当するものは「ライブラリ」と呼ばれ、
5100 Gaucheとは異なる構文で定義します。詳しくは@ref{R7RS library form}を参照してください。
5101 @c COMMON
5102
5103 @menu
5104 * Module semantics::
5105 * Modules and libraries::
5106 * Defining and selecting modules::
5107 * Using modules::
5108 * Module inheritance::
5109 * Module introspection::
5110 * Predefined modules::
5111 @end menu
5112
5113 @node Module semantics, Modules and libraries, Modules, Modules
5114 @subsection Module semantics
5115 @c NODE モジュールのセマンティクス
5116
5117 @c EN
5118 Module is an object that maps symbols onto @emph{bindings},
5119 and affects the resolution of global variable reference.
5120 @c JP
5121 モジュールは、シンボルを束縛へとマップするオブジェクトで、
5122 グローバル変数の解決に影響を与えます。
5123 @c COMMON
5124
5125 @c EN
5126 Unlike CommonLisp's packages, which map names to symbols,
5127 in Gauche symbols are @code{eq?} in principle if two have the
5128 same name (except uninterned symbols; @pxref{Symbols}).
5129 However, Gauche's symbol doesn't have a 'value'
5130 slot in it. From a given symbol, a module finds its binding that
5131 keeps a value.
5132 Different modules can associate different bindings
5133 to the same symbol, that yield different values.
5134 @c JP
5135 CommonLispのパッケージは名前からシンボルへのマッピングを行いますが、
5136 Gaucheでは同じ名前を持つシンボルは原則として@code{eq?}です
5137 (例外はインターンされていないシンボルです。@ref{Symbols}参照)。
5138 しかし、Gaucheのシンボルは「値」のスロットを持っていません。
5139 モジュールによってシンボルに対応する束縛が見付けられ、値はそこに
5140 格納されています。
5141 モジュールが違えば同じシンボルは別々の束縛へとマップされ、違う値を
5142 返します。
5143 @c COMMON
5144
5145 @example
5146 @c EN
5147 ;; Makes two modules A and B, and defines a global variable 'x' in them
5148 @c JP
5149 ;; 二つのモジュールAとBを作成し、グローバル変数'x'をその中で定義
5150 @c COMMON
5151 (define-module A (define x 3))
5152 (define-module B (define x 4))
5153
5154 ;; #<symbol 'x'> ---[module A]--> #<binding that has 3>
5155 (with-module A x) @result{} 3
5156
5157 ;; #<symbol 'x'> ---[module B]--> #<binding that has 4>
5158 (with-module B x) @result{} 4
5159 @end example
5160
5161 @c EN
5162 A module can @emph{export} a part or all of its bindings
5163 for other module to use.
5164 A module can @emph{import} other modules, and their exported
5165 bindings become visible to the module.
5166 A module can import any number of modules.
5167 @c JP
5168 モジュールは、自身が持つ一部または全ての束縛を他のモジュールからも
5169 使えるように@emph{export}することができます。あるモジュールXが他の
5170 モジュールYを@emph{import}すると、
5171 モジュールYでexportされている束縛が元のモジュールXから見えるようになります。
5172 モジュールはいくつでも他のモジュールをimportすることができます。
5173 @c COMMON
5174
5175 @example
5176 (define-module A
5177 (export pi)
5178 (define pi 3.1416))
5179
5180 (define-module B
5181 (export e)
5182 (define e 2.71828))
5183
5184 (define-module C
5185 (import A B))
5186
5187 (select-module C)
5188 (* pi e) @result{} 8.539748448
5189 @end example
5190
5191 @c EN
5192 A module can also be @emph{inherited}, that is, you can extend
5193 the existing module by inheriting it and adding new bindings
5194 and exports. From the new module, all ancestor's bindings (including
5195 non-exported bindings) are visible.
5196 (A new module inherits the @code{gauche} module by default, which is why
5197 the built-in procedures and syntax of @code{gauche} are available
5198 in the new module).
5199 From outside, the new module looks like having
5200 all exported bindings of the original module plus the newly
5201 defined and exported bindings.
5202 @c JP
5203 また、モジュールは継承することもできます。
5204 既存のモジュールを継承したモジュールに新しい束縛を足してexportすることにより、
5205 既存のモジュールを拡張することができます。新しいモジュールの内部からは、
5206 継承元のモジュールの束縛が(exportされていないものも含め)全て見えます。
5207 (新しく作られるモジュールはデフォルトで@code{gauche}モジュールを継承しています。
5208 新しいモジュールから@code{gauche}の組込み手続き等が使えるのはそのためです)。
5209 外からは、新しいモジュールには元のモジュールの全てのexportされた束縛と
5210 新たに追加されexportされた束縛が見えます。
5211 @c COMMON
5212
5213 @example
5214 ;; Module A defines and exports deg->rad.
5215 ;; A binding of pi is not exported.
5216 (define-module A
5217 (export deg->rad)
5218 (define pi 3.1416) ;; not exported
5219 (define (deg->rad deg) (* deg (/ pi 180))))
5220
5221 ;; Module Aprime defines and exports rad->deg.
5222 ;; The binding of pi is visible from inside Aprime.
5223 (define-module Aprime
5224 (extend A)
5225 (export rad->deg)
5226 (define (rad->deg rad) (* rad (/ 180 pi))))
5227
5228 ;; Module C imports Aprime.
5229 (define-module C
5230 (import Aprime)
5231 ;; Here, both deg->rad and rad->deg are visible,
5232 ;; but pi is not visible.
5233 )
5234 @end example
5235
5236 @c EN
5237 At any moment of the compilation, there is one "current module" available,
5238 and the global variable reference is looked for from the module.
5239 If there is a visible binding of the variable, the variable
5240 reference is compiled to the access of the binding.
5241 If the compiler can't find a visible binding,
5242 it marks the variable reference with the current module, and
5243 delays the resolution of binding at the time the variable is
5244 actually used. That is, when the variable is referenced
5245 at run time, the binding is again looked for from the marked module
5246 (@emph{not} the current module at the run time) and if found,
5247 the variable reference code is replaced for the the code to
5248 access the binding. If the variable reference is not found even
5249 at run time, an 'undefined variable' error is signaled.
5250 @c JP
5251 コンパイル中のどの時点でも、「カレントモジュール」が一意に決定され、
5252 グローバル変数の束縛はそのカレントモジュールを起点に探されます。
5253 その変数の束縛が見付かれば、変数参照の式はその束縛へアクセスするコードとして
5254 コンパイルされます。もしコンパイラが束縛を見付けられなかった場合、
5255 変数参照の式はカレントモジュールでマークされ、束縛の解決はランタイムへと
5256 先送りされます。すなわち、ランタイムにその変数が使われる時点で
5257 再びマークされていたモジュールから束縛の探索が行われます
5258 (ランタイムでのカレントモジュールからでは無いことに注意)。
5259 束縛が見付かれば、束縛へアクセスするコードがコンパイルされたコード列に
5260 挿入されます。見付からなければ'undefined variable'エラーが報告されます。
5261 @c COMMON
5262
5263 @c EN
5264 Once the appropriate binding is found for the global variable,
5265 the access to the binding is hard-wired in the compiled code
5266 and the global variable resolution will never take place again.
5267 @c JP
5268 グローバル変数に対して適切な束縛がひとたび発見されれば、
5269 その束縛へのアクセスはコンパイルされたコードに埋め込まれ、
5270 その変数の束縛の探索は二度と行われません。
5271 @c COMMON
5272
5273 @c EN
5274 The definition special form such as @code{define} and @code{define-syntax}
5275 inserts the binding to the current module. Thus it may shadow
5276 the binding of imported or inherited modules.
5277 @c JP
5278 @code{define}や@code{define-syntax}等の定義を行う特殊形式は
5279 カレントモジュールに束縛を挿入します。これは、importしたり継承したりしている
5280 モジュールの同名の束縛をシャドウします。
5281 @c COMMON
5282
5283 @c EN
5284 The resolution of binding of a global variable happens like this.
5285 First, the current module is searched. Then, each
5286 imported module is taken in the reverse order of import, and searched,
5287 including each module's ancestors.
5288 Note that import is not transitive; imported module list is not
5289 chased recursively.
5290 Finally, ancestors of the current module are searched in order.
5291 @c JP
5292 グローバル変数の束縛の解決は次の手順で行われます。
5293 まずカレントモジュールが探されます。次に、importしているモジュールが
5294 importされた逆の順番に並べられ、それぞれについてその
5295 モジュールおよびそのモジュールの先祖(継承されているモジュール)が順に探されます。
5296 importは遷移的ではありません;importされたモジュールがimportしているモジュール…
5297 というふうに再帰的に辿ることはしません。
5298 最後に、カレントモジュールの先祖が順に探されます。
5299 @c COMMON
5300
5301 @c EN
5302 This order is important when more than one modules
5303 defines the same name and your module imports both.
5304 Assuming your module don't define that name,
5305 if you first import a module @code{A} then a module @code{B},
5306 you'll see @code{B}'s binding.
5307
5308 If you import @code{A}, then @code{B}, then @code{A} again,
5309 the last import takes precedence; that is, you'll see @code{A}'s
5310 binding.
5311 @c JP
5312 この順序は、複数のモジュールで同じ名前が定義され、あなたのモジュールが
5313 その両方をインポートしている場合に重要になります。
5314 その名前があなたのモジュールで
5315 定義されていないとして、もしモジュール@code{A}がまずimportされ、
5316 次に@code{B}がimportされている場合、あなたのコードは@code{B}の
5317 束縛を見ることになります。
5318
5319 @code{A}をimportして@code{B}をimportした後に再び@code{A}をimport
5320 した場合、後のimportの方が効力を持ちます。すなわち、@code{A}の束縛が
5321 見えることになります。
5322 @c COMMON
5323
5324 @c EN
5325 If two modules you want to use exports bindings of the same name
5326 and you want to access both, you can add prefix to either one
5327 (or both). @xref{Using modules}, for the details.
5328 @c JP
5329 もし、二つのモジュールが同名の束縛をエクスポートしており、
5330 その両方にアクセスしたければ、一方もしくは両方の名前にプレフィクスを
5331 つけることができます。詳しくは@ref{Using modules}を参照してください。
5332 @c COMMON
5333
5334 @node Modules and libraries, Defining and selecting modules, Module semantics, Modules
5335 @subsection Modules and libraries
5336 @c NODE モジュールとライブラリ
5337
5338 @c EN
5339 Modules are run-time data structure; you can procedurally
5340 create modules with arbitrary names at run-time.
5341
5342 @c JP
5343 モジュールは実行時データ構造です。実行時に任意の名前のモジュールを
5344 手続き的に作成することができます。
5345
5346 @c EN
5347 However, most libraries use modules to create their own
5348 namespace, so that they can control which bindings to be
5349 visible from library users. (This ``library'' is a general
5350 term, broader than R7RS ``library'').
5351
5352 @c JP
5353 しかしほとんどのライブラリは、固有の名前空間を生成するために
5354 モジュールを用います。これにより、どの束縛をライブラリ使用者に
5355 見せるかを制御できます。(ここでの「ライブラリ」は、R7RSのライブラリだけでなく
5356 より広い意味で使っています。)
5357
5358 @c EN
5359 Usually a library is provided in the form of one or more Scheme
5360 source file(s), so it is convenient to have a convention to map
5361 module names to file names, and vice versa; then, you can load a
5362 library file and import its module by one action with @code{use}
5363 macro, for example.
5364
5365 @c JP
5366 通常ライブラリは1つ以上のSchemeソースファイル形式で提供されます。
5367 したがって、ファイル名をモジュール名に対応づける(またはその逆の)
5368 規約にしておけば便利です。そうすれば、たとえば、ライブラリーファイルを
5369 ロードしたり、@code{use} マクロを使ってモジュールを一動作で、
5370 インポートしたりできます。
5371
5372 @c EN
5373 For the time being, Gauche uses a simple rules for this mapping:
5374 Module names are organized hierarchically, using period `@code{.}'
5375 for separator, e.g. @code{gauche.mop.validator}. If such a
5376 module is requested and doesn't exist in the current running
5377 environment, Gauche maps the module name to a pathname by
5378 replacing periods to directory separator, i.e.
5379 @code{gauche/mop/validator}, and look for
5380 @code{gauche/mop/validator.scm} in the load paths.
5381
5382 @c JP
5383 当分の間、Gauche はこの対応づけのための単純なルールを使用します。すなわち、
5384 モジュール名は、例えば @code{gauche.mop.validator} のように `@code{.}'
5385 (ピリオド)記号で階層的に区切って構成されます。このようなモジュールが
5386 要求されても、現在の実行時環境に存在しない場合には、Gauche は
5387 ピリオド記号をディレクトリ区切りに変換して @code{gauche/mop/validator}
5388 のようにモジュール名からパス名に変換します。その後、
5389 @code{gauche/mop/validator.scm} をロードパスから探します。
5390
5391 @c EN
5392 Note that this is just a default behavior.
5393 Theoretically, one Scheme source file may contain multiple
5394 modules, or one module implementation may span to multiple files.
5395 In future, there may be some hook to customize this mapping
5396 for special cases. So, when you are writing routines that
5397 deal with modules and library files, do not apply the above
5398 default rule blindly. Gauche provides two procedures,
5399 @code{module-name->path} and @code{path->module-name},
5400 to do mapping for you (see @ref{Module introspection}, for details).
5401
5402 @c JP
5403 これが単にデフォルトの振る舞いであることに注意してください。
5404 理論上、1つのSchemeソース・ファイルは多数のモジュールを含むことがあります。
5405 あるいは、1つのモジュール実装は多数のファイルにまたがることもありえます。
5406 将来、特別なケースのために、この対応付けをカスタマイズするフックを
5407 用意するかもしれません。したがって、モジュールおよびライブラリーファイルを
5408 扱うルーチンを書く場合には、上記のデフォルトルールを盲目的に適用しないで
5409 ください。Gaucheは @code{module-name->path} と @code{path->module-name} という
5410 2つの対応づけ手続き(詳細に関しては、@ref{Module introspection}参照)
5411 を用意しています。
5412
5413 @c COMMON
5414 @node Defining and selecting modules, Using modules, Modules and libraries, Modules
5415 @subsection Defining and selecting modules
5416 @c NODE モジュールの定義と選択
5417
5418 @defspec define-module name body @dots{}
5419 @c EN
5420 @var{Name} must be a symbol.
5421 If a module named @var{name} does not exist, create one.
5422 Then evaluates @var{body} sequentially in the module.
5423 @c JP
5424 @var{name}はシンボルでなければなりません。
5425 名前@var{name}を持つモジュールが存在しなければまず作成します。
5426 それから、@var{body} @dots{} をモジュール@var{name}中で評価します。
5427 @c COMMON
5428 @end defspec
5429
5430 @defspec select-module name
5431 @c EN
5432 Makes a module named @var{name} as the current module.
5433 It is an error if no module named @var{name} exists.
5434
5435 If @code{select-module} is used in the Scheme file, its effect is
5436 limited inside the file, i.e. even if you load/require a file that uses
5437 @code{select-module} internally, the current module of requirer is
5438 not affected.
5439 @c JP
5440 名前@var{name}を持つモジュールをカレントモジュールとします。
5441 その名前を持つモジュールが無ければエラーとなります。
5442
5443 @code{select-module}がSchemeファイルの中で用いられた場合、
5444 その効果はそのファイルの終了までに限られます。@code{select-module}を中で呼んでいる
5445 ファイルをloadやrequireしても、呼んだ側のカレントモジュールは影響を受けません。
5446 @c COMMON
5447 @end defspec
5448
5449 @defspec with-module name body @dots{}
5450 @c EN
5451 Evaluates @var{body} sequentially in the module
5452 named @var{name}. Returns the last result(s).
5453 If no module named @var{name}, an error is signaled.
5454 @c JP
5455 名前@var{name}を持つモジュールをカレントモジュールとした状態で@var{body} @dots{}
5456 を順に評価し、最後の結果を返します。該当するモジュールが存在しなければエラーとなります。
5457 @c COMMON
5458 @end defspec
5459
5460 @defspec current-module
5461 @c EN
5462 Evaluates to the current module in the compile context.
5463 Note that this is a special form, not a function.
5464 Module in Gauche is statically determined at compile time.
5465 @c JP
5466 コンパイル時点でのカレントモジュールに評価されます。
5467 これは手続きではなく特殊形式です。
5468 Gaucheではモジュールはコンパイル時に静的に決定されます。
5469 @c COMMON
5470
5471 @example
5472 (define-module foo
5473 (export get-current-module)
5474 (define (get-current-module) (module-name (current-module))))
5475
5476 (define-module bar
5477 (import foo)
5478 (get-current-module)) @result{} foo @r{; not bar}
5479 @end example
5480 @end defspec
5481
5482
5483 @node Using modules, Module inheritance, Defining and selecting modules, Modules
5484 @subsection Using modules
5485 @c NODE モジュールの使用
5486
5487 @defspec export spec @dots{}
5488 [R7RS base]
5489 @c EN
5490 Makes bindings specified by each @var{spec} available
5491 to modules that imports the current module.
5492
5493 Each @var{spec} can be either one of the following forms,
5494 where @var{name} and @var{exported-name} are symbols.
5495
5496 @table @code
5497 @item @var{name}
5498 The binding with @var{name} is exported.
5499 @item (rename @var{name} @var{exported-name})
5500 The binding with @var{name} is exported under an alias @var{exported-name}.
5501 @end table
5502 @c JP
5503 カレントモジュールから、@var{spec}で指定される束縛をexportします。
5504 exportされた束縛は、カレントモジュールをimportしたモジュール中で見えるようになります。
5505
5506 各@var{spec}は次のどちらかの形式でなければなりません。@var{name}と@var{export-name}
5507 はシンボルです。
5508
5509 @table @code
5510 @item @var{name}
5511 名前@var{name}を持つ束縛がexportされます。
5512 @item (rename @var{name} @var{exported-name})
5513 名前@var{name}を持つ束縛が、@var{exported-name}という別名でexportされます。
5514 @end table
5515 @c COMMON
5516
5517 @c EN
5518 Note: In Gauche, @code{export} is just a special form
5519 you can put in the middle of the program, whereas
5520 R7RS defines @code{export} as a library declaration,
5521 that can only appear immediately below @code{define-library} form.
5522 @xref{R7RS library form}, for the details.
5523 @c JP
5524 註: Gaucheの@code{export}は単なるスペシャルフォームで
5525 プログラムの途中に書くこともできますが、
5526 R7RSの@code{export}はライブラリ宣言の一部で
5527 @code{define-library}フォームの直下にしか書けません。
5528 詳しくは@ref{R7RS library form}を参照してください。
5529 @c COMMON
5530 @end defspec
5531
5532 @defspec export-all
5533 @c EN
5534 Makes all bindings in the current module available to modules
5535 that imports it.
5536 @c JP
5537 カレントモジュール中の全ての束縛をexportします。
5538 @c COMMON
5539 @end defspec
5540
5541 @defspec import import-spec @dots{}
5542 @c EN
5543 Makes all or some exported bindings in the module specified by
5544 @var{import-spec} available in the current module.
5545 The syntax of @var{import-spec} is as follows.
5546 @c JP
5547 @var{import-spec}で指定されるモジュールがexportしている
5548 束縛のすべてもしくはいくつかを、カレントモジュール中で使えるようにします。
5549 @var{import-spec}は以下の形式です。
5550 @c COMMON
5551
5552 @example
5553 <import-spec> : <module-name>
5554 | (<module-name> <import-option> ...)
5555
5556 <import-option> : :only (<symbol> ...)
5557 | :except (<symbol> ...)
5558 | :rename ((<symbol> <symbol>) ...)
5559 | :prefix <symbol>
5560
5561 <module-name> : <symbol>
5562 @end example
5563
5564 @c EN
5565 The module named by @var{module-name} should exist
5566 when the compiler sees this special form.
5567 @c JP
5568 @var{module-name}で指定される名前のモジュールは
5569 このフォームがコンパイルされる時点までに存在していなければなりません。
5570 @c COMMON
5571
5572 @c EN
5573 Imports are not transitive. The modules that @var{module-name}s
5574 are importing are not automatically imported to the current module.
5575 This keeps modules' modularity; a library module can import whatever
5576 modules it needs without worrying about polluting the namespace of
5577 the user of the module.
5578 @c JP
5579 モジュールのimportは遷移的ではありません。
5580 つまり@code{module-name}で指定されたモジュールがその内部でimport
5581 しているモジュールは自動的にカレントモジュールにはimportされてません。
5582 モジュールの独立性を保つための設計です。この性質により、
5583 ライブラリモジュールの作者はいくら他のモジュールを
5584 importしようとも利用者の名前空間を不意に汚染してしまう心配はありません。
5585 (利用者からはそのモジュールでexportしている名前しか見えないからです。)
5586 @c COMMON
5587
5588 @c EN
5589 @var{import-option} can be used to change how the bindings
5590 are imported. With @code{:only}, only the bindings with
5591 the names listed in @code{<symbol> @dots{}} are imported.
5592 With @code{:except}, the exported bindings except the ones
5593 with the listed names are imported.
5594 With @code{:rename}, the binding of each name in the first
5595 of two-symbol list is renamed to the second of it.
5596 With @code{:prefix}, the exported bindings are visible with
5597 the names that are prefixed by the symbol to the original names.
5598 Without import options, all the exported bindings are imported
5599 without a prefix.
5600 @c JP
5601 @var{import-option}は束縛がどのようにインポートされるかを制御します。
5602 @code{:only}がある場合、@code{<symbol> @dots{}}に挙げられた名前を
5603 持つ束縛のみがインポートされます。@code{:except}がある場合は逆に、
5604 エクスポートされている束縛のうち挙げられた名前を持つもの以外が
5605 インポートされます。@code{:rename}は各2要素のリストの最初の名前
5606 を持つ束縛が2番目の名前へとリネームされます。
5607 @code{:prefix}があると、元の名前の前に
5608 指定されるシンボルが付加された名前で束縛が見えるようになります。
5609 @var{import-option}が指定されなければ、@var{module-name}のすべての
5610 エクスポートされた束縛がプレフィクス無しでインポートされます。
5611 @c COMMON
5612
5613 @example
5614 (define-module M (export x y)
5615 (define x 1)
5616 (define y 2)
5617 (define z 3))
5618
5619 (import M)
5620
5621 x @result{} 1
5622 @c EN
5623 z @result{} @r{error. z is not exported from M}
5624 @c JP
5625 z @result{} @r{エラー。 zはMからエクスポートされなていない}
5626 @c COMMON
5627
5628 (import (M :only (y)))
5629
5630 @c EN
5631 x @result{} @r{error. x is not in :only list.}
5632 @c JP
5633 x @result{} @r{エラー。xは:onlyリストに含まれない。}
5634 @c COMMON
5635
5636 (import (M :except (y)))
5637
5638 @c EN
5639 y @result{} @r{error. y is excluded by :except.}
5640 @c JP
5641 y @result{} @r{エラー。yは:exceptにより除外されている。}
5642 @c COMMON
5643
5644 (import (M :prefix M:))
5645
5646 x @result{} @r{error}
5647 M:x @result{} 1
5648 M:y @result{} 2
5649 @end example
5650
5651 @c EN
5652 If more than one import option are given, it is processed
5653 as the order of appearance. That is, if @code{:prefix}
5654 comes first, then @code{:only} or @code{:except} has to
5655 list the name with prefix.
5656 @c JP
5657 一つ以上のインポートオプションが与えられた場合、
5658 それは出現順に処理されます。すなわち、@code{:prefix}が
5659 最初に現れた場合、その後に来る@code{:only}や@code{:except}
5660 はプレフィックスつきの名前を使って指定しなければなりません。
5661 @c COMMON
5662
5663 @c EN
5664 Note: R7RS has @code{import} form, which has slightly different
5665 syntax and semantics. @xref{Three forms of import}, for the details.
5666 @c JP
5667 註: R7RSにも@code{import}フォームがありますが、若干構文と意味が異なります。
5668 @ref{Three forms of import}を参照してください。
5669 @c COMMON
5670 @end defspec
5671
5672 @defmac use name :key only except rename prefix
5673 @c EN
5674 A convenience macro that combines module imports and on-demand
5675 file loading. Basically, @code{(use foo)} is equivalent
5676 to the following two forms:
5677 @example
5678 (require "foo")
5679 (import foo)
5680 @end example
5681 That is, it loads the library file named ``@code{foo}'' (if not yet
5682 loaded) which defines a module named @code{foo} in it,
5683 and then import the module
5684 @code{foo} into the current module.
5685 @c JP
5686 モジュールのインポートと必要に応じてファイルのロードを合わせて行う、
5687 便利なマクロです。基本的に、@code{(use foo)} は以下のふたつのフォームと
5688 等価です。
5689 @example
5690 (require "foo")
5691 (import foo)
5692 @end example
5693 すなわち、まず名前``@code{foo}''を持つライブラリファイルが(まだロードされて
5694 いなければ)ロードされ、その中で定義されているモジュール@code{foo}をカレントモジュールに
5695 インポートします。
5696 @c COMMON
5697
5698 @c EN
5699 The keyword argument @var{only}, @var{except}, and @var{prefix}
5700 are passed to @code{import} as the import options.
5701 @c JP
5702 キーワード引数@var{only}, @var{except}, @var{prefix}は
5703 @code{import}にインポートオプションとして渡されます。
5704 @c COMMON
5705
5706 @example
5707 (use srfi.1 :only (iota) :prefix srfi-1:)
5708
5709 (srfi-1:iota 3) @result{} (0 1 2)
5710 @end example
5711
5712
5713 @c EN
5714 Although the files and modules are orthogonal concept,
5715 it is practically convenient to separate files by modules.
5716 Gauche doesn't force you to do so, and you can always use
5717 @code{require} and @code{import} separately. However, all
5718 modules provided with Gauche are arranged so that they can be
5719 used by @code{use} macro.
5720 @c JP
5721 ファイルのロードとモジュールとは直交する概念ですが、
5722 実用的にはモジュール毎にファイルを分割するのが便利です。
5723 必ずしもそうする必要は無く、@code{require} と @code{import} を別々に
5724 使っても構いません。が、Gaucheに附属してくるライブラリはすべて、
5725 @code{use} マクロで使えるように書かれています。
5726 @c COMMON
5727
5728 @c EN
5729 If a module is too big to fit in one file, you can split them
5730 into several subfiles and one main file. The main file defines
5731 the module, and either loads, requires, or autoloads subfiles.
5732 @c JP
5733 もしモジュールが一つのファイルに収めるには大きすぎる場合、一つのメインファイルと
5734 いくつかのサブファイルに分けることも出来ます。メインファイルの中でモジュールを
5735 定義し、サブファイルをまとめてロードするか、オートロードを設定します。
5736 @c COMMON
5737
5738 @c EN
5739 Actually, the file pathname of the given module name is
5740 obtained by the procedure @code{module-name->path} below.
5741 The default rule is to replace periods `@code{.}' in the @var{name}
5742 for `@code{/}'; for example,
5743 @code{(use foo.bar.baz)} is expanded to:
5744 @example
5745 (require "foo/bar/baz")
5746 (import foo.bar.baz)
5747 @end example
5748 This is not very Scheme-ish way, but nevertheless convenient.
5749 In future, there may be some mechanism to customize this mapping.
5750 @c JP
5751 実際は、与えられたモジュール名からファイルのパス名を得るのに
5752 手続き@code{module-name->path}が使われます。デフォルトの変換規則は、
5753 モジュール名@var{name}中のピリオド`@code{.}'を`@code{/}'に置換
5754 するというものです。例えば@code{(use foo.bar.baz)}は
5755 @example
5756 (require "foo/bar/baz")
5757 (import foo.bar.baz)
5758 @end example
5759 となります。これはあまりScheme風ではありませんが、便利ではあります。
5760 将来、このマッピングルールをカスタマイズする機構が導入されるかもしれません。
5761 @c COMMON
5762
5763 @c EN
5764 The file to be @code{use}'d must have explicit module selection
5765 to have any toplevel definitions
5766 (usually via @code{define-module}/@code{select-module} pair
5767 or @code{define-library}). If you get an error saying
5768 ``Attempted to create a binding in a sealed module:
5769 module: #<module gauche.require-base>'', that's because the file
5770 lacks module selection. @xref{Require and provide}, for further
5771 discussion.
5772 @c JP
5773 @code{use}されるファイルがトップレベル定義を持つ場合、ファイル内でモジュールが
5774 明示されてることが必要です (通常は@code{define-module}/@code{select-module}
5775 や@code{define-library}が使われます)。
5776 もし、``Attempted to create a binding in a sealed module:
5777 module: #<module gauche.require-base>''というエラーが出たら、
5778 @code{use}したファイルがモジュール指定を持っていないということです。
5779 詳しくは@ref{Require and provide}を参照してください。
5780 @c COMMON
5781 @end defmac
5782
5783 @node Module inheritance, Module introspection, Using modules, Modules
5784 @subsection Module inheritance
5785 @c NODE モジュールの継承
5786
5787 @c EN
5788 The export-import mechanism doesn't work well in some cases, such as:
5789 @c JP
5790 export-importメカニズムは、次のような場合をうまく処理できません。
5791 @c COMMON
5792
5793 @itemize @bullet
5794 @c EN
5795 @item You want to create a module that is mostly the same as the
5796 existing one, but adding or altering some definitions.
5797 @c JP
5798 @item 既存のモジュールとほぼ同等の機能を持つが、若干定義を足したり変更したり
5799 したモジュールを作りたい。
5800 @c COMMON
5801 @c EN
5802 @item You wrote a bunch of related modules that are often used together,
5803 and not want your users to repeat a bunch of 'use' forms every time
5804 they use your module.
5805 @c JP
5806 @item 相互に関連するいくつものモジュールを作成した。
5807 ユーザがこれらのモジュールを使う時に毎回いくつも 'use' を並べないで済むように
5808 一つにまとめたい。
5809 @c COMMON
5810 @end itemize
5811
5812 @c EN
5813 You can use module inheritance in these cases.
5814 @c JP
5815 このような場合にモジュールの継承が使えます。
5816 @c COMMON
5817
5818 @defmac extend module-name @dots{}
5819 @c EN
5820 Makes the current module inherit from named modules.
5821 The current inheritance information is altered by the
5822 inheritance information calculated from given modules.
5823 @c JP
5824 カレントモジュールが、@var{module-name} @dots{}に挙げられたモジュールを
5825 継承するようにします。それまでの継承の情報は捨てられ、@var{module-name} @dots{}
5826 から計算される継承情報が有効になります。
5827 @c COMMON
5828
5829 @c EN
5830 A new module inherits from @code{gauche} module when created.
5831 If you put @code{(extend scheme)} in that module, for example,
5832 the module resets to inherit directly from @code{scheme} module
5833 that has only bindings defined in R5RS, hence, after the extend
5834 form, you can't use 'import' or any other @code{gauche}-specific
5835 bindings in the module.
5836 @c JP
5837 新たに作られるモジュールはデフォルトで@code{gauche}モジュールを継承しています。
5838 例えばそのモジュールに@code{(extend scheme)}というフォームを入れた場合、
5839 その時点でそのモジュールは@code{scheme}モジュール(R5RSで定義された束縛
5840 のみを含む)を直接継承するようになります。したがって、そのフォームの後で
5841 'import' やその他@code{gauche}特有の束縛はそのモジュール内では
5842 使えなくなります。
5843 @c COMMON
5844
5845 @c EN
5846 If a named module is not defined yet, @code{extend}
5847 tries to load it, using the same convention @code{use} macro does.
5848 @c JP
5849 @var{module-name}に挙げられたモジュールがまだ存在しなかった場合、
5850 @code{extend}は@code{use}と同じメカニズムを使ってファイルをロードすることを
5851 試みます。
5852 @c COMMON
5853
5854 @c EN
5855 A module can inherit multiple modules, exactly the same way as
5856 a class can inherit from multiple classes. The resolution of
5857 order of inheritance needs to be explained a bit.
5858 @c JP
5859 モジュールは複数のモジュールを継承することができます。
5860 丁度、クラスが複数のクラスを継承できるのと同じようにです。
5861 多重継承の場合、次のようにしてモジュール間の優先順位が決められます。
5862 @c COMMON
5863
5864 @c EN
5865 Each module has a @emph{module precedence list}, which lists
5866 modules in the order of how they are searched. When the module
5867 inherits multiple modules, module precedence lists of inherited
5868 modules are merged into a single list, keeping the constraints
5869 that: (1) if a module A appears before module B in some module
5870 precedence list, A has to appear before B in the resulting module
5871 precedence list; and (2) if a module A appears before module B
5872 in @code{extend} form, A has to appear before B in the resulting
5873 module precedence list. If no precedence list can be constructed
5874 with these constraints, an error is signaled.
5875 @c JP
5876 各モジュールは@emph{module precedence list}というモジュールのリストを
5877 持っています。そこにリストされた順に束縛が探されます。
5878 モジュールが複数のモジュールを多重継承した場合、継承される各モジュールの
5879 module precedence listを、次に挙げる制約を満たすようにマージ
5880 したものが新たなmodule precedence listとなります:
5881 (1) あるmodule precedence listでモジュールAがモジュールBより前に現れていたら、
5882 結果のmodule precedence listでもAはBより前に現れる:
5883 (2) モジュールAがモジュールBより@code{extend}フォームで前に現れていたら、
5884 結果のmodule precedence listでもAはBより前に現れる。
5885 この条件を満たすようなmodule precedence listが構成できない場合はエラーとなります。
5886 @c COMMON
5887 @end defmac
5888
5889 @c EN
5890 For example, suppose you wrote a library in modules
5891 @code{mylib.base}, @code{mylib.util} and @code{mylib.system}.
5892 You can bundle those modules into one module by creating
5893 a module @code{mylib}, as follows:
5894 @c JP
5895 例えばあなたがライブラリを3つのモジュール、
5896 @code{mylib.base}、@code{mylib.util}、@code{mylib.system}に分けて
5897 書いたとしましょう。次のように書けば、これらのモジュールを
5898 一つの@code{mylib}モジュールに見せることができます。
5899 @c COMMON
5900
5901 @example
5902 (define-module mylib
5903 (extend mylib.system mylib.util mylib.base))
5904 @end example
5905
5906 @c EN
5907 The user of your module just says @code{(use mylib)} and
5908 all exported symbols from three submodules become available.
5909 @c JP
5910 このライブラリモジュールのユーザは @code{(use mylib)} とするだけで
5911 全てのサブモジュールのexportされた束縛を利用することができるようになります。
5912 @c COMMON
5913
5914 @node Module introspection, Predefined modules, Module inheritance, Modules
5915 @subsection Module introspection
5916 @c NODE モジュールイントロスペクション
5917
5918 @c EN
5919 This subsection lists procedures that operates on modules at run-time.
5920 With these procedures you can introspect the modules, create new modules
5921 procedurally, or check the existence of certain modules/libraries, for
5922 example. However, don't forget that modules are primarily
5923 compile-time structures. Tweaking modules at run-time is only
5924 for those who know what they are doing.
5925 @c JP
5926 この節では、実行時にモジュールを操作する手続きをリストします。
5927 これらの手続きにより、例えばモジュールの内部を調べたり、手続き的に
5928 新しいモジュールを作成したり、特定のモジュールやライブラリの存在を
5929 調べたりすることができます。ただし、モジュールは第一にコンパイル時の
5930 構造であることを忘れないでください。実行時にモジュールをいじくるのは、
5931 十分にモジュールの構造を理解した上で行ってください。
5932 @c COMMON
5933
5934 @deftp {Builtin Class} <module>
5935 @clindex module
5936 @c EN
5937 A module class.
5938 @c JP
5939 モジュールクラスです。
5940 @c COMMON
5941 @end deftp
5942
5943 @defun module? obj
5944 @c EN
5945 Returns true if @var{obj} is a module.
5946 @c JP
5947 @var{obj}がモジュールなら真の値を返します。
5948 @c COMMON
5949 @end defun
5950
5951 @defun find-module name
5952 @c EN
5953 Returns a module object whose name is a symbol @var{name}.
5954 If the named module doesn't exist, @code{#f} is returned.
5955 @c JP
5956 名前がシンボル@var{name}であるようなモジュールを返します。
5957 その名前をもつモジュールが存在しなければ、@code{#f}を返します。
5958 @c COMMON
5959 @end defun
5960
5961 @defun make-module name :key if-exists
5962 @c EN
5963 Creates and returns a module that has symbol @var{name}.
5964 If the named module already exists, the behavior is specified by
5965 @var{if-exists} keyword argument. If it is @code{:error} (default),
5966 an error is signaled. If it is @code{#f}, @code{#f} is returned.
5967 @c JP
5968 シンボルの名前@var{name}を持つモジュールを作成して返します。
5969 その名前を持つモジュールが既に存在していた場合、その動作は
5970 @var{if-exists}キーワード引数で指定されます。
5971 @var{if-exists}引数が@code{:error}である場合(デフォルト)、
5972 エラーが報告されます。それが@code{#f}である場合は単に@code{#f}が返されます。
5973 @c COMMON
5974
5975 @c EN
5976 Note that creating modules on-the-fly isn't usually necessary
5977 for ordinal scripts, since executing already written program requires modules
5978 to be specified by name, i.e. syntax @code{define-module}, @code{import},
5979 @code{extend}, @code{with-module} all take module names, not
5980 module objects.
5981 It is because module are inherently compile-time structures.
5982 However, there are some cases that dynamically created modules
5983 are useful, especially when the program itself is dynamically created.
5984 You can pass a module to @code{eval} to compile and
5985 evaluate such dynamically created
5986 programs in it (@pxref{Eval and repl}).
5987 @c JP
5988 モジュールを実行時に動的に生成することは、通常のスクリプトでは
5989 あまり必要とはされません。既に書かれたプログラムの解釈においては、
5990 モジュールは名前で指定されている必要があるからです。
5991 構文@code{define-module}、@code{import}、@code{extend}、@code{with-module}
5992 等はモジュールそのものではなくモジュール名を取ります。
5993 これは、モジュールが本質的にコンパイル時の構造であるためです。
5994 しかし、動的に作られるモジュールが有用な場合もあります---プログラムそのものが、
5995 動的に作られる場合です。@code{eval}にモジュールを渡して、
5996 そのような動的に作られたプログラムがそのモジュールの中で
5997 コンパイルされ評価されるようにできます。
5998 @c COMMON
5999
6000 @c EN
6001 You can also pass @code{#f} to @var{name} to create
6002 @emph{anonymous} module. Anonymous modules can't be
6003 looked up by @code{find-module}, nor can be imported
6004 or inherited (since @code{import} and @code{extend} take
6005 module names, not modules).
6006 It is useful when you want to have a temporary, segregated namespace
6007 dynamically---for example, you can create an anonymous module
6008 to evaluate code fragments sent from other program, and
6009 discards the module when the connection is terminated.
6010 Anonymous modules are not registered in the system dictionary
6011 and are garbage collected when nobody keeps reference to it.
6012 @c JP
6013 また、@var{name}に@code{#f}を渡すことで無名のモジュールを作ることもできます。
6014 無名のモジュールは@code{find-module}で探すことはできませんし、
6015 他のモジュールから@code{import}することも@code{extend}されることも
6016 できません(@code{import}や@code{extend}はモジュール名を必要とするからです)。
6017 無名のモジュールは、一時的に隔離された名前空間を動的に作りたい時に
6018 便利です。例えばネットワークで接続されたプログラムから送られた式を
6019 その中で評価して、コネクションが終了したら名前空間ごと捨ててしまうという
6020 ような場合です。無名のモジュールはシステムの内部辞書に登録されないので、
6021 モジュールへの参照が無くなればガベージコレクトされます。
6022 @c COMMON
6023
6024 @c EN
6025 R7RS provides another way to create a transient module
6026 with @code{environment} procedure. @pxref{R7RS eval} for the
6027 details.
6028 @c JP
6029 R7RSでは、@code{environment}手続きによって一時的なモジュールを
6030 作ることもできます。@ref{R7RS eval}を参照してください。
6031 @c COMMON
6032 @end defun
6033
6034 @defun all-modules
6035 @c EN
6036 Returns a list of all named modules.
6037 Anonymous modules are not included.
6038 @c JP
6039 現在存在する全ての名前付きモジュールのリストを返します。
6040 無名のモジュールは含まれません。
6041 @c COMMON
6042 @end defun
6043
6044 @defun module-name module
6045 @defunx module-imports module
6046 @defunx module-exports module
6047 @defunx module-table module
6048 @c EN
6049 Accessors of a module object. Returns the name of the module (a symbol),
6050 list of imported modules, list of exported symbols, and a hash table
6051 that maps symbols to bindings, of the @var{module} are returned,
6052 respectively.
6053 @c JP
6054 モジュールオブジェクトのアクセスメソッドです。
6055 @var{module}の名前(シンボル)、@var{module}がインポートしているモジュールのリスト、
6056 エクスポートしているシンボルのリスト、そして
6057 シンボルから束縛へのマップを行うハッシュテーブルを返します。
6058 @c COMMON
6059
6060 @c EN
6061 It is an error to pass a non-module object.
6062 @c JP
6063 モジュールオブジェクト以外が渡された場合はエラーになります。
6064 @c COMMON
6065
6066 @c EN
6067 If you want to check if a specific symbol is exported from a module,
6068 use @code{module-exports?} below instead of taking all exported
6069 symbols with @code{module-exports} and search from it. It is much
6070 faster to use @code{module-exports?}.
6071 @c JP
6072 特定のシンボルがモジュールからエキスポートされているかどうかを調べるには、
6073 下に説明する@code{module-exports?}を使ってください。
6074 @code{module-exports}で全エキスポートシンボルのリストを取ってそこから探すより
6075 ずっと速いです。
6076 @c COMMON
6077 @end defun
6078
6079 @defun module-parents module
6080 @defunx module-precedence-list module
6081 @c EN
6082 Returns the information of module inheritance.
6083 @code{Module-parents} returns the modules @var{module} directly inherits
6084 from. @code{Module-precedence-list} returns the module precedence
6085 list of @var{module} (@pxref{Module inheritance}).
6086 @c JP
6087 モジュールの継承に関する情報を返します。
6088 @code{module-parents}は@var{module}が直接継承しているモジュールのリストを
6089 返します。@code{module-precedence-list}は@var{module}のmodule precedence
6090 list (@ref{Module inheritance}参照) を返します。
6091 @c COMMON
6092 @end defun
6093
6094 @defun module-binds? module symbol
6095 @c EN
6096 Returns @code{#t} if @var{symbol}'s global binding is visible
6097 from inside @var{module}, @code{#f} otherwise. That is,
6098 if @code{(module-binds? 'M 'v)} returns @code{#t},
6099 @code{v} can be used as a globally bound identifier
6100 (either a variable or a syntactic keyword)
6101 in the module @code{M}.
6102 @var{Module} must be a module object or
6103 a symbol name of an existing module.
6104
6105 This deals with the view from inside @var{module}, so it isn't affected
6106 whether the named variable is exported from @var{module}, or if so,
6107 whether it is renamed on export. If you need to test a variable
6108 from the viewpoint external to @var{module},
6109 see @code{module-exports?} below.
6110
6111 Note: Lisp traditionally has @code{symbol-bound?} procedure to
6112 check whether a global variable is bound, but we feel the name
6113 focuses too much on symbols, whereas global bindings are managed
6114 by modules in Gauche. Previous versions of Gauche uses
6115 the name @code{global-variable-bound?} and it is still kept for
6116 the backward compatibility, but we decided on the current name
6117 to be clear that it is about modules, and it is not only about
6118 variables.
6119 @c JP
6120 @var{symbol}のグローバルな束縛が@var{module}内部から
6121 可視であれば@code{#t}を、そうでなければ@code{#f}を返します。
6122 つまり、@code{(module-binds? 'M 'v)}が@code{#t}を返したら、
6123 @code{v}はグローバルに束縛された識別子として
6124 (変数もしくは構文キーワードとして)
6125 モジュール@code{M}の中で使えるということです。
6126 @var{module}はモジュールオブジェクトか
6127 既存のモジュール名を示すシンボルでなければなりません。
6128
6129 この手続きは@var{module}内部からの見え方を扱うので、
6130 名前@var{symbol}が@var{module}からエクスポートされているか、
6131 あるいはエクスポート時にリネームされているかには影響されません。
6132 @var{module}外部からの視点で束縛の可視性を調べたい場合は、
6133 下の@code{module-exports?}を使ってください。
6134
6135 註: Lispは伝統的に、グローバル変数の束縛を調べる手続きを@code{symbol-bound?}
6136 と呼んできました。しかし、Gaucheでは束縛はモジュールにより管理されるのに対し、
6137 その名前はシンボルに焦点を当ててしまっています。
6138 以前のGaucheでは@code{global-variable-bound?}という名前を使っていて、
6139 それは互換性のために今でも残されていますが、この操作はモジュールに関するものであること、
6140 また、変数だけでなく構文キーワードを含めた束縛に関するものであることを明確にするために、
6141 現在の名前になりました。
6142 @c COMMON
6143 @end defun
6144
6145 @defun module-exports? module symbol
6146 @c EN
6147 Returns @code{#t} if the binding of @var{symbol} is visible from outside
6148 @var{module}, @code{#f} otherwise.
6149 That is, if @code{(module-exports? 'M 'v)} returns @code{#t},
6150 @code{v} can be used as a globally bound identifier
6151 (either as a variable or a syntactic keyword)
6152 in the module that imports module @code{M}.
6153 @var{Module} must be a module object or
6154 a symbol name of an existing module.
6155
6156 Unlike @code{module-binds?}, this checks the outside appearance
6157 of the binding. Suppose you have those modules:
6158 @c JP
6159 @var{symbol}の束縛が@var{module}の外部から可視であれば@code{#t}を、
6160 そうでなければ@code{#f}を返します。
6161 すなわち、@code{(module-exports? 'M 'v)}が@code{#t}を返したなら、
6162 @code{M}をインポートすれば@code{v}がグローバル変数もしくは構文キーワードとして
6163 使えるということです。
6164 @var{module}はモジュールオブジェクトか
6165 既存のモジュール名を示すシンボルでなければなりません。
6166
6167 @code{module-binds?}と違い、この手続きは束縛が外からどう見えるかを検査します。
6168 次のようなモジュールがあるとしましょう。
6169 @c COMMON
6170
6171 @example
6172 (define-module M
6173 (export a (rename b bee))
6174 (define a 1)
6175 (define b 2)
6176 (define c 3))
6177 @end example
6178
6179 @c EN
6180 @code{module-binds?} concerns how the binding are visible in @code{M}.
6181 So it returns @code{#t} for @code{a}, @code{b}, and @code{c},
6182 but not for @code{bee}, as in the following example:
6183 @c JP
6184 @code{module-binds?}は@code{M}の中で束縛がどう見えるかを調べるので、
6185 @code{a}、@code{b}、@code{c}については@code{#t}を返しますが、
6186 @code{bee}については@code{#f}を返します。
6187 @c COMMON
6188
6189 @example
6190 (module-binds? 'M 'a) @result{} #t
6191 (module-binds? 'M 'b) @result{} #t
6192 (module-binds? 'M 'c) @result{} #t
6193
6194 (module-binds? 'M 'bee) @result{} #f ; renamed export doesn't affect in M
6195 @end example
6196
6197 @c EN
6198 @code{module-exports?} concerts how the bindings are visible outside of
6199 @code{M}. So it returns @code{#t} for @code{a} and @code{bee}, but
6200 not for @code{b} and @code{c}, as in the following example.
6201 @c JP
6202 @code{module-exports?}はモジュールを外から見た場合の束縛を調べるので、
6203 @code{a}と@code{bee}について@code{#t}を返しますが、@code{b}と@code{c}には
6204 @code{#f}を返します。
6205 @c COMMON
6206
6207 @example
6208 (module-exports? 'M 'a) @result{} #t
6209 (module-exports? 'M 'b) @result{} #f ; because of renamed export
6210 (module-exports? 'M 'c) @result{} #f ; not exported
6211
6212 (module-exports? 'M 'bee) @result{} #t ; external name
6213 @end example
6214 @end defun
6215
6216
6217 @defun module-binding-ref module symbol :optional default
6218 @c EN
6219 Returns a value globally bound to the @var{symbol} inside @var{module}.
6220 @var{Module} must be a module object or
6221 a symbol name of an existing module. If there's no visible
6222 global binding inside @var{module} for @var{symbol},
6223 an error is signaled, unless the @var{default} argument is
6224 provided, in which case it is returned instead.
6225 @c JP
6226 モジュール@var{module}から可視の、シンボル@var{symbol}のグローバルな
6227 束縛値を返します。@var{module}はモジュールオブジェクトか
6228 既存のモジュール名を示すシンボルでなければなりません。
6229 @var{symbol}に対するグローバルな束縛が@var{module}から見えない場合は、
6230 @var{default}引数があたえられていればその値を返し、
6231 無ければエラーを通知します。
6232 @c COMMON
6233
6234 @c EN
6235 This used to be called as @code{global-variable-ref}, but we changed
6236 the name to be clear that it is module's operation, and the binding
6237 can be not only variables but also syntactic keywords.
6238 You can still use the old name but it is deprecated.
6239 @c JP
6240 これは以前は@code{global-variable-ref}と呼ばれていましたが、
6241 モジュールの操作であること、また束縛は変数とは限らず構文キーワードでも良いことを
6242 明確にするために名前を変えました。古い名前も使えますが非推奨です。
6243 @c COMMON
6244 @end defun
6245
6246 @defun global-variable-bound? module symbol
6247 @defunx global-variable-ref module symbol :optional default
6248 @c DEPRECATED
6249 @c EN
6250 Old name for @code{module-binds?} and @code{module-binding-ref},
6251 respectively. Use new names for the new code.
6252 @c JP
6253 それぞれ、@code{module-binds?}と@code{module-binding-ref}の古い名前です。
6254 新規コードでは新しい名前を使ってください。
6255 @c COMMON
6256 @end defun
6257
6258
6259
6260 @defun module-name->path symbol
6261 @c EN
6262 Converts a module name @var{symbol} to a fragment of pathname string
6263 (which you use for @code{require} and @code{provide}).
6264 @c JP
6265 モジュール名@var{symbol}を、パス名の一部(@code{require}や@code{provide}が
6266 使うような)へと変換します。
6267 @c COMMON
6268 @end defun
6269
6270 @defun path->module-name string
6271 @c EN
6272 Reverse function of @code{module-name->path}.
6273 @c JP
6274 @code{module-name->path}の逆関数です。
6275 @c COMMON
6276 @end defun
6277
6278 @c EN
6279 If you want to find out
6280 specific libraries and/or modules are installed in the system and
6281 available from the program, see @ref{Operations on libraries}.
6282 @c JP
6283 特定のライブラリやモジュールがシステムにインストールされて使える状態にあるか
6284 調べたりする場合は、@ref{Operations on libraries}を参照して下さい。
6285 @c COMMON
6286
6287 @node Predefined modules, , Module introspection, Modules
6288 @subsection Predefined modules
6289 @c NODE 組み込みモジュール
6290
6291 @c EN
6292 Several modules are predefined in Gauche.
6293 @c JP
6294 Gauche起動時にいくつかのモジュールがあらかじめ定義されています。
6295 @c COMMON
6296
6297 @deftp {Builtin Module} null
6298 @mdindex null
6299 @c EN
6300 This module corresponds to the null environment referred in R5RS.
6301 This module contains only syntactic bindings of R5RS syntax.
6302 @c JP
6303 このモジュールはR5RSで述べられている"null environment"に相当します。
6304 R5RSの構文要素への束縛だけを含んだモジュールです。
6305 @c COMMON
6306 @end deftp
6307
6308 @deftp {Builtin Module} scheme
6309 @mdindex scheme
6310 @c EN
6311 This module contains all the binding of @code{null} module,
6312 and the binding of procedures defined in R5RS.
6313 @c JP
6314 このモジュールは@code{null}モジュール内の束縛全てに加えて、
6315 R5RSで定義されている全ての手続きの束縛を含みます。
6316 @c COMMON
6317 @end deftp
6318
6319 @c EN
6320 Note that if you change the current module to @code{null} or
6321 @code{scheme} by @code{select-module}, there will be no way
6322 to switch back to other modules, since module-related syntaxes
6323 and procedures are not visible from @code{null} and @code{scheme}
6324 modules.
6325 @c JP
6326 @code{select-module}によって一度@code{null}や@code{scheme}モジュールに
6327 入ると、そこから他のモジュールに移ることはできなくなることに注意してください。
6328 これらのモジュールからは、あらゆるモジュール操作構文が不可視だからです。
6329 @c COMMON
6330
6331 @deftp {Builtin Module} gauche
6332 @mdindex gauche
6333 @c EN
6334 This module contains all the bindings of @code{scheme} module,
6335 plus Gauche specific built-in procedures.
6336 @c JP
6337 このモジュールは@code{scheme}モジュール内の全ての束縛に加え、
6338 Gaucheの組込み手続きや構文が含まれています。
6339 @c COMMON
6340 @end deftp
6341
6342 @deftp {Builtin Module} user
6343 @mdindex user
6344 @c EN
6345 This module is the default module the user code is compiled.
6346 all the bindings of @code{gauche} module is imported.
6347 @c JP
6348 このモジュールはユーザコードがコンパイルされる既定のモジュールです。
6349 @code{gauche}モジュール内の全ての束縛がインポートされています。
6350 @c COMMON
6351 @end deftp
6352
6353 @deftp {Builtin Module} gauche.keyword
6354 @deftpx {Builtin Module} keyword
6355 @mdindex gauche.keyword
6356 @mdindex keyword
6357 @c EN
6358 When Gauche is running with @code{GAUCHE_KEYWORD_IS_SYMBOL} mode (default)
6359 keywords (symbols beginning with @code{:}) is automatically bound
6360 to itself in these modules.
6361 (@pxref{Keywords}, for the details.)
6362
6363 The @code{keyword} module doesn't export those bindings, while
6364 @code{gauche.keyword} does. The former is intended to be
6365 used internally; the programmer need to know the latter.
6366
6367 If you use the default module inheritance,
6368 you don't need to use this module, since
6369 the @code{keyword} module is included in the inheritance chain.
6370 If you don't inherit @code{gauche} module, however, importing the
6371 @code{gauche.keyword} module gives you access to the keywords without quotes.
6372 For example, R7RS programs
6373 and libraries would require either @code{(import (gauche keyword))}
6374 or @code{(import (gauche base))} (the latter inherits
6375 @code{gauche.keyword}), or you have to quote all keywords.
6376 @c JP
6377 Gaucheがデフォルトの@code{GAUCHE_KEYWORD_IS_SYMBOL}モードで実行されている時は、
6378 キーワード (@code{:}で始まるシンボル) が自動的にこれらのモジュールの中で
6379 自分自身に束縛されます。(詳しくは@ref{Keywords}を参照してください。)
6380
6381 @code{keyword}モジュールはキーワードの自己束縛をエクスポートせず、
6382 @code{gauche.keyword}モジュールはエクスポートします。前者は主に
6383 内部的に使うためのもので、プログラマが知る必要があるのは後者です。
6384
6385 デフォルトのモジュール継承を使っている場合、キーワードモジュールは
6386 継承チェインの中に含まれているので、このモジュールを陽に使用する必要はありません。
6387 @code{gauche}を継承しないモジュールを書いていて、キーワードをクオートせずに
6388 使いたい場合にこのモジュールをインポートしてください。
6389 例えばR7RSプログラムやライブラリは、@code{(import (gauche keyword))}
6390 もしくは@code{(import (gauche base))} (後者は@code{gauche.keyword}を
6391 継承しています)しない限り、キーワードにクオートが必要です。
6392 @c COMMON
6393
6394 @c EN
6395 The following R7RS program imports @code{gauche.base}; it makes gauche
6396 built-in identifiers, @emph{and} all self-bound keywords, available:
6397 @c JP
6398 次のR7RSプログラムは@code{gauche.base}をimportすることで、
6399 Gaucheの組み込みの識別子とともに自己束縛されたキーワードが使えるようになります。
6400 @c COMMON
6401
6402 @example
6403 ;; R7RS program
6404 (import (scheme base)
6405 (gauche base)) ; import gauche builtins and keywords
6406
6407 ;; You can use :directory without quote, for it is bound to itself.
6408 (sys-exec "ls" '("ls" "-l") :directory "/")
6409 @end example
6410
6411 @c EN
6412 If you use more sophisticated import tricks, however, keep in mind
6413 that keywords are just imported symbols by default.
6414 The following code imports
6415 Gauche builtin identifiers with prefix @code{gauche/}. That causes
6416 keywords, imported via inheritance, also get the same prefix; if
6417 you don't want to bother adding prefix to all keywords or
6418 quote them, import @code{gauche.keyword} separately.
6419 @c JP
6420 ただ、もうちょっと凝ったインポートをする場合、
6421 デフォルトではキーワードも
6422 単なるインポートされるシンボルにすぎないということを覚えておいてください。
6423 例えば次のコードではGaucheの組み込み識別子を@code{gauche/}というプレフィクス
6424 つきでインポートしています。この場合、@code{gauche.base}の継承経由で
6425 インポートされているキーワードもプレフィクスがつけられることになります。
6426 キーワードをいちいちプレフィクスつき、もしくはクオートつきで書きたくなければ、
6427 @code{gauche.keyword}を別にインポートしましょう。
6428 @c COMMON
6429
6430 @example
6431 ;; R7RS program
6432 (import (scheme base)
6433 (prefix (gauche base) gauche/) ; use gauche builtin with gauche/ prefix
6434 (gauche keyword)) ; imports keywords
6435
6436 ;; Without importing gauche.keyword,
6437 ;; you need to write ':directory
6438 (gauche/sys-exec "ls" '("ls" "-l") :directory "/")
6439 @end example
6440
6441 @end deftp
6442
6443 @deftp {Builtin Module} gauche.require-base
6444 @c EN
6445 This is a module when @code{use} or @code{require} load the file.
6446 This is not meant to be @code{use}d directly; just to prevent
6447 library files lacking module switching from causing unexpected
6448 side effects. @xref{Using modules}, for more details.
6449 @c JP
6450 このモジュールは@code{use}や@code{require}がファイルを読み込む際に
6451 設定するモジュールです。@code{use}するものではなく、
6452 モジュール指定を欠いたライブラリファイルが予期せぬ副作用を起こすことを
6453 防ぐためのものです。詳しくは@ref{Using modules} を参照してください。
6454 @c COMMON
6455 @end deftp
6456
6457
6458 @c Local variables:
6459 @c mode: texinfo
6460 @c coding: utf-8
6461 @c end: