Debian Codegraph @

doc/concepts.texi

raw
1 @node Concepts, Programming in Gauche, Introduction, Top
2 @chapter Concepts
3 @c NODE 主要な概念
4
5 @c EN
6 In this chapter I describe a few Gauche's design concepts
7 that help you to understand how Gauche works.
8 @c JP
9 本章では、Gaucheの設計の背景となるいくつかの概念を説明します。
10 Gaucheの動作を正確に理解する助けになると思います。
11 @c COMMON
12
13 @menu
14 * Standard conformance::
15 * Multibyte strings::
16 * Multibyte scripts::
17 * Case-sensitivity::
18 * Integrated object system::
19 * Module system::
20 * Compilation::
21 @end menu
22
23 @node Standard conformance, Multibyte strings, Concepts, Concepts
24 @section Standard conformance
25 @c NODE 標準への準拠
26
27 @c EN
28 Gauche conforms ``Revised^7 Report of Algorithmic Language Scheme,''
29 (R7RS)
30 including optional syntax and procedures. We cover R7RS small language
31 (@pxref{R7RS small language}), as well as part of R7RS large libraries
32 (@pxref{R7RS large}).
33 @c JP
34 Gaucheは、オプショナルな構文や手続きも含め
35 ``Revised^7 Report of Algorithmic Language Scheme'' (R7RS) に準拠しています。
36 ``R7RS small'' と呼ばれるコア言語(@ref{R7RS small language}参照)に加え、
37 ``R7RS large'' ライブラリの一部をサポートします(@ref{R7RS large}参照)。
38 @c COMMON
39
40 @itemize @bullet
41 @item
42 @c EN
43 Gauche has a special kind of symbols, called keywords.
44 They're symbols with its name beginning with a colon
45 (e.g. @code{:key}), but behaves as if it is automatically bound to
46 itself. @xref{Keywords}, for the details.
47 Keywords are used extensively when
48 passing so-called keyword arguments (@pxref{Making procedures}).
49 @c JP
50 Gaucheには、キーワードと呼ばれる特殊なシンボルがあります。
51 コロンで始まる名前(例:@code{:key})を持つシンボルはキーワードで、
52 自動的に自分自身に束縛されます。
53 詳しくは@ref{Keywords}を参照してください。
54 主にキーワード引数を渡すときに使われます(@ref{Making procedures}参照)。
55 @c COMMON
56
57 @item
58 @c EN
59 Continuations created in a certain situation
60 (specifically, inside a Scheme code that is called from external
61 C routine) have limited extent (@xref{Continuations}, for details).
62 @c JP
63 ある条件下で作られた継続は限られたエクステントを持ちます
64 (具体的には、Cのコードからコールバックとして呼ばれるSchemeコード内で作られる継続
65 がこれにあたります)。詳しくは@ref{Continuations}を参照して下さい。
66 @c COMMON
67
68 @item
69 @c EN
70 Full numeric tower (integer, rational, real and complex numbers) are supported,
71 but rationals are only exact, and complex numbers are always inexact.
72 @c JP
73 完全なNumeric tower(整数、有理数、実数、複素数)がサポートされますが、
74 有理数は正確な表現のみで、また複素数は非正確な表現のみを持ちます。
75 @c COMMON
76 @end itemize
77
78 @c EN
79 Note that, since Gauche predates R7RS, most existing Gauche source
80 code doesn't follow the R7RS program/library structure. Gauche can
81 read both traditional Gauche modules/scripts and R7RS programs/libraries
82 seamlessly.
83 @xref{Library modules - R7RS standard libraries}, for the details of how R7RS
84 is integrated into Gauche.
85 @c JP
86 GaucheはR7RS以前から開発されているため、既存のGaucheソースコードの多くはR7RSの
87 プログラムやライブラリの構造に従っていないことに注意してください。
88 Gaucheは、伝統的なGaucheのモジュール形式やスクリプトも、R7RS形式のプログラムや
89 ライブラリも、両方解釈することができます。
90 R7RSがどのようにGaucheに統合されているかについては、
91 @ref{Library modules - R7RS standard libraries}を参照してください。
92 @c COMMON
93
94
95 @c EN
96 Gauche also supports the following SRFIs (Scheme Request for Implementation).
97 @c JP
98 Gaucheはまた、以下のSRFIをサポートしています。
99 @c COMMON
100
101 @table @asis
102 @c xinclude srfis.texi
103 @end table
104
105 @node Multibyte strings, Multibyte scripts, Standard conformance, Concepts
106 @section Multibyte strings
107 @c NODE マルチバイト文字列
108
109 @c EN
110 Traditionally, a string is considered as a simple array of bytes.
111 Programmers tend to imagine a string as a simple
112 array of characters (though a character may occupy more than one byte).
113 It's not the case in Gauche.
114 @c JP
115 従来、文字列は単なるバイトの配列として扱われてきました。
116 そのため一文字が複数バイトを占めるようになっても
117 文字列は単純な文字の配列であると考えられがちですが、
118 Gaucheにおいては、そうではありません。
119 @c COMMON
120
121 @c EN
122 Gauche supports @emph{multibyte string} natively,
123 which means characters are represented by variable number of bytes
124 in a string. Gauche retains semantic compatibility of
125 Scheme string, so such details can be hidden, but it'll be helpful
126 if you know a few points.
127 @c JP
128 Gaucheは内部的に@emph{マルチバイト文字列}をサポートします。
129 すなわち、文字列中の文字が占めるバイト数は一定していません。
130 Schemeの文字列プリミティブのセマンティクスは保たれているので、
131 詳細を気にしないでもプログラムは書けますが、
132 下にあげるいくつかの点を知っておいたほうが良いでしょう。
133 @c COMMON
134
135 @c EN
136 A string object keeps a type tag and a pointer to the storage
137 of the string body. The storage of the body is managed in a sort of
138 ``copy-on-write''
139 way---if you take substring, e.g. using directly by @code{substring}
140 or using regular expression matcher, or even if you copy a string
141 by @code{copy-string}, the underlying storage is shared
142 (the ``anchor'' of the string is different, so the copied string
143 is not @code{eq?} to the original string).
144 The actual string is copied only if you destructively modify it.
145 @c JP
146 文字列オブジェクトは型タグと文字列本体へのポインタを保持しています。
147 文字列本体は「copy-on-write」方式で管理されます。すなわち、@code{substring}
148 や正規表現を使って部分文字列を切り出したり、あるいは単に文字列をコピーした場合、
149 文字列オブジェクトそのものは別につくられますが、文字列本体は共有されます。
150 文字列が破壊的に変更される場合にのみ、文字列本体がコピーされます。
151 @c COMMON
152
153 @c EN
154 Consequently the algorithm like pre-allocating a string by
155 @code{make-string} and filling it with @code{string-set!}
156 becomes @emph{extremely} inefficient in Gauche. Don't do it.
157 (It doesn't work with mulitbyte strings anyway).
158 Sequential access of string is much more efficient
159 using @emph{string ports} (@pxref{String ports}).
160 @c JP
161 したがって、@code{make-string}である大きさの文字列をあらかじめアロケート
162 しておき、@code{string-set!}で順に埋めて行くようなアルゴリズムは
163 Gaucheでは非常に効率が悪くなります。そのようなアルゴリズムは使わない方が良いでしょう。
164 (そのようなアルゴリズムはマルチバイト文字列とも相性が良くありません)。
165 文字列を順に埋めて行く場合は@emph{string ports}を使うのが
166 効率の良い方法です (@ref{String ports}参照)。
167 @c COMMON
168
169 @c EN
170 String search primitives such as @code{string-scan} (@pxref{String utilities})
171 and regular expression matcher (@pxref{Regular expressions})
172 can return a matched string directly, without
173 using index access at all.
174 @c JP
175 @code{string-scan} (@ref{String utilities}参照) や正規表現
176 (@ref{Regular expressions}参照) など文字列を検索するプリミティブは、
177 インデックスを介さずに一致した文字列を直接返すことができます。
178 @c COMMON
179
180 @c EN
181 Conversions from other encoding scheme is provided
182 as a special port. @xref{Character code conversion}, for details.
183 @c JP
184 他の文字エンコーディングからの変換は特別なポートを使って行われます。
185 @ref{Character code conversion} を参照して下さい。
186 @c COMMON
187
188 @c EN
189 The way to specify the encoding of source programs will be
190 explained in the next section.
191 @c JP
192 ソースプログラムのエンコーディングを指定する方法については
193 次の章で説明します。
194 @c COMMON
195
196 @node Multibyte scripts, Case-sensitivity, Multibyte strings, Concepts
197 @section Multibyte scripts
198 @c NODE マルチバイトスクリプト
199
200 @c EN
201 You can use characters other than @code{us-ascii} not only in
202 literal strings and characters, but in comments, symbol names,
203 literal regular expressions, and so on.
204 @c JP
205 リテラル文字列や文字以外にも、コメント、シンボル名、
206 リテラル正規表現など様々な箇所で、@code{us-ascii}以外の文字を
207 使うことができます。
208 @c COMMON
209
210 @c EN
211 By default, Gauche assumes a Scheme program is written in utf-8.
212 If you need to write a source other than utf-8, however,
213 you can add the following ``magic comment'' near the beginning of
214 the source code:
215 @c JP
216 デフォルトでは、GaucheはSchemeプログラムをutf-8で書かれているものとして扱います。
217 もし何らかの事情でutf-8以外のエンコーディングでソースを書かなければならない場合、
218 コードの先頭付近に次のような特殊なコメントを置いておくことができます。
219 @c COMMON
220
221 @c EN
222 When Gauche finds a comment something like the following within
223 the first two lines of the program source, it assumes the rest of
224 the source code is written in @code{<encoding-name>}, and does
225 the appropriate character encoding conversion to read the source code:
226 @c JP
227 Gaucheは、次のようなコメントがプログラムソースコードの
228 2行目までに現れた場合、ソースコードの残りの部分が@code{<encoding-name>}で
229 指定されるエンコーディングで書かれているものとして、必要ならば
230 適切なエンコーディング変換を行います。
231 @c COMMON
232 @example
233 ;; coding: <encoding-name>
234 @end example
235
236 @c EN
237 More precisely, a comment in either first or second line that matches
238 a regular expression @code{#/coding[:=]\s*([\w.-]+)/} is recognized,
239 and the first submatch is taken as an encoding name.
240 If there are multiple matches, only the first one is effective.
241 The first two lines must not contain characters other than us-ascii
242 in order for this mechanism to work.
243 @c JP
244 より正確には、1行目または2行目のコメントで、
245 正規表現@code{#/coding[:=]\s*([\w.-]+)/}にマッチするものがあった場合に、
246 最初の部分マッチがエンコーディング名として認識されます。
247 複数のマッチがあった場合は最初のものが有効になります。
248 このメカニズムを利用するためには、最初の2行以内にus-ascii以外の文字を
249 含めないようにして下さい。
250 @c COMMON
251
252 @c EN
253 The following example tells Gauche that the script is written
254 in EUC-JP encoding. Note that the string "@code{-*-}" around the coding
255 would be recognized by Emacs to select the buffer's encoding
256 appropriately.
257 @c JP
258 例えば次の例では、Gaucheはスクリプトがeuc-jpで書かれているものと
259 認識します。coding指定の周囲の"@code{-*-}"は、Emacsが
260 バッファのエンコーディングを適切に設定するのに使われます。
261 @c COMMON
262
263 @example
264 #!/usr/bin/gosh
265 ;; -*- coding: euc-jp -*-
266
267 ... script written in euc-jp ...
268 @end example
269
270 @c EN
271 Internally, the handling of this @emph{magic comment} is done by
272 a special type of port. See @ref{Coding-aware ports} for the
273 details. See also @ref{Loading Scheme file} for how to disable
274 this feature.
275 @c JP
276 内部では、この特殊なコメントの処理は特別なポートによって
277 行われています。詳細は@ref{Coding-aware ports}を参照して下さい。
278 また、この処理を行わないようにする方法については
279 @ref{Loading Scheme file}を参照して下さい。
280 @c COMMON
281
282 @node Case-sensitivity, Integrated object system, Multibyte scripts, Concepts
283 @section Case-sensitivity
284 @c NODE 大文字小文字の区別
285
286 @c EN
287 Historically, most Lisp-family languages are case-insensitive
288 for symbols. Scheme departed from this tradition since R6RS, and
289 the symbols are read in case-sensitive way.
290 (Note that symbols have been case-sensitive internally even in R5RS Scheme;
291 case-insensitivity is about readers.)
292 @c JP
293 Lisp系の言語は歴史的にシンボルの大文字小文字を区別していません。
294 しかしSchemeはR6RSからその伝統より離脱し、シンボルを読む際に大文字小文字を区別
295 するようになりました。(但し、R5RSの範囲内でも処理系内部では
296 大文字小文字を区別することになってます。
297 ここで問題としているのは読み込み時の動作です。)
298 @c COMMON
299
300 @c EN
301 Gauche reads and writes symbols in case-sensitive manner by default, too.
302 However, to support legacy code, you can set the reader to case-insensitive
303 mode, in the following ways:
304 @c JP
305 Gaucheの入出力ルーチンも、デフォルトではシンボルの大文字小文字を区別します。
306 ただし、古いコードとの互換性のために、リーダを大文字小文字を区別しないモードにすることが
307 できます。
308 @c COMMON
309
310 @table @asis
311 @c EN
312 @item Use @code{#!fold-case} reader directive
313 When Gauche sees a token @code{#!fold-case} during reading a program,
314 the reader switches to case-insensitive mode. A token
315 @code{#!no-fold-case} has an opposite effect---to make the
316 reader case-sensitive. These tokens affect the port
317 from which they are read, and are in effect until EOF
318 or another instance of these tokens are read.
319 See @ref{Lexical structure} for more details on @code{#!} syntax.
320 This is the way defined in R6RS and R7RS.
321 @c JP
322 @item @code{#!fold-case}リーダディレクティブを使う
323 Gaucheはプログラムを読み込む時に、トークン@code{#!fold-case}を見ると大文字小文字を
324 区別しないモードに切り替わります。トークン@code{#!no-fold-case}は
325 その逆の効果、すなわち大文字小文字を区別するモードへと切り替えます。
326 これらのトークンは、それが読まれたポートからの以降の読み込みに影響を
327 与えます。切り替えるトークンが再び読まれるか、EOFに出会うまで影響は継続します。
328 @code{#!}構文についてより詳しくは@ref{Lexical structure}を参照してください。
329 この動作はR6RS及びR7RSで規定されています。
330 @c COMMON
331
332 @c EN
333 @item Use @code{-fcase-fold} command-line argument
334 Alternatively, you can give a command-line argument @code{-fcase-fold}
335 to the @code{gosh} command (@pxref{Gosh command-line options}).
336 In this mode, the reader folds uppercase characters in symbols to lowercase
337 ones. If a symbol name contains uppercase characters, it is written
338 out using |-escape (@pxref{Symbols}).
339 @c JP
340 @item @code{-fcase-fold}コマンドライン引数を使う
341 また、@code{gosh}インタプリタの起動時に@code{-fcase-fold}コマンドライン引数を
342 与えると、Gaucheは大文字小文字を区別しないモードで動作します
343 (@ref{Gosh command-line options}参照)。
344 このモードでは、リーダはシンボルの読み込みの際に大文字を小文字に変換します。
345 大文字を含んでいるシンボルに関しては、`|' 文字でエスケープされて書き出されます
346 (@ref{Symbols}参照)。
347 @c COMMON
348 @end table
349
350
351 @node Integrated object system, Module system, Case-sensitivity, Concepts
352 @section Integrated object system
353 @c NODE 統合されたオブジェクトシステム
354
355 @c EN
356 Gauche has a STklos-style object system, similar to CLOS.
357 If you have used some kind of object oriented (OO) languages,
358 you'll find it easy to understand the basic usage:
359 @c JP
360 GaucheはCLOSに類似した、STklosスタイルのオブジェクトシステムを持っています。
361 あなたが何らかのオブジェクト指向(OO)言語を使ったことがあれば、
362 基本的な使い方は簡単にわかるでしょう。
363 @c COMMON
364
365 @example
366 @c EN
367 ;; @r{Defines a class point, that has x and y coordinate}
368 @c JP
369 ;; @r{x, y座標を持つpointクラスを定義}
370 @c COMMON
371 (define-class point ()
372 ((x :init-value 0)
373 (y :init-value 0))
374 )
375
376 (define-method move ((p point) dx dy)
377 (inc! (slot-ref p 'x) dx)
378 (inc! (slot-ref p 'y) dy))
379
380 (define-method write-object ((p point) port)
381 (format port "[point ~a ~a]"
382 (slot-ref p 'x)
383 (slot-ref p 'y)))
384 @end example
385
386 @c EN
387 However, if you are familiar with mainstream OO languages but new to CLOS-style
388 object system, Gauche's object system may look strange
389 when you look deeper into it.
390 Here I describe several characteristics of Gauche object system quickly.
391 @xref{Object system}, for details.
392 @c JP
393 しかし、主流のオブジェクト指向言語に慣れてはいるがCLOSスタイルのオブジェクトシステムに
394 慣れていないプログラマは、Gaucheのオブジェクトシステムの詳細を見てゆくと
395 奇妙に感じることがあるのではないかと思います。
396 ここではGaucheのオブジェクトシステムの代表的な性質を簡単に述べておきます。
397 詳しくは@ref{Object system}の章を参照して下さい。
398 @c COMMON
399
400 @table @emph
401 @c EN
402 @item Everything is an object (if you care)
403 You have seen this tagline for the other languages.
404 And yes, in Gauche, everything is an object in the sense that you can
405 query its class, and get various meta information of
406 the object at run time. You can also define a new method
407 on any class, including built-in ones.
408
409 Note that, however, in CLOS-like paradigm it doesn't really matter
410 whether everything is an object or not, because of the following
411 characteristics:
412 @c JP
413 @item 全てはオブジェクトである (それが気になる人には)
414 このような主張は他のOO言語でも見たことがあるでしょう。
415 Gaucheもその例にもれず、実行時にクラスを得たりメソッドを呼び出したりといった
416 操作が任意のオブジェクトに対して出来るという意味で、全てはオブジェクトです。
417 また組込みクラスを含めたどんなクラスに対しても新しいメソッドを定義することができます。
418
419 しかし、CLOS系のパラダイムでは、全てがオブジェクトかどうかという議論はあまり
420 問題ではありません。というのは次のような性質があるからです。
421 @c COMMON
422
423 @c EN
424 @item Method is dispatched by all of its arguments.
425 Unlike other object-oriented languages such as C++, Objective-C,
426 Python, Ruby, etc., in which a method always belong to a single
427 class, a Gauche method doesn't belong to a specific class.
428
429 For example, suppose you define a numeric vector class
430 @code{<num-vector>} and a numeric matrix class @code{<num-matrix>}.
431 You can define a method @code{product} with all possible combinations
432 of those type of arguments:
433 @c JP
434 @item メソッドは全ての引数によってディスパッチされる
435 C++、Objective C、Python、RubyなどのOO言語では、メソッドは特定のクラスに
436 所属しています。Gaucheではメソッドはクラスに従属しません。
437
438 例えば、数値だけを要素に持つベクタークラス@code{<num-vector>}と
439 行列クラス@code{<num-matrix>}を定義したとします。
440 プログラマは、以下のようなあらゆる場合についてメソッド@code{product}を
441 別々に定義することができます。
442 @c COMMON
443
444 @example
445 (product <num-vector> <num-matrix>)
446 (product <num-matrix> <num-vector>)
447 (product <num-vector> <num-vector>)
448 (product <num-matrix> <num-matrix>)
449 (product <number> <num-vector>)
450 (product <number> <num-matrix>)
451 (product <number> <number>)
452 @end example
453
454 @c EN
455 Each method belongs to neither @code{<num-vector>} class nor
456 @code{<num-matrix>} class.
457
458 Since a method is not owned by a class, you can always define your
459 own method on the existing class (except a few cases
460 that the system prohibits altering pre-defined methods). The above
461 example already shows it; you can make @code{product} method work
462 on the built-in class @code{<number>}. That is why I said
463 it doesn't make much sense to discuss whether everything is object
464 or not in CLOS-style object system.
465 @c JP
466 これらの各メソッドは、@code{<num-vector>}クラスや@code{<num-matrix>}
467 クラスに所属するわけではありません。
468
469 メソッドがクラスに所有されているわけではないので、既に存在するクラスに対していつでも
470 独自のメソッドを定義することができます(但し、いくつかの組込みクラスの既定のメソッドには
471 変更できないものがあります)。上の例で既にこれは示されています。プログラマは
472 メソッド@code{product}を組込みクラス@code{<number>}に対して定義することが
473 できます。これが、全てはオブジェクトであるかどうかはCLOSスタイルのオブジェクトシステム
474 では問題にならないと言った理由です。
475 @c COMMON
476
477 @c EN
478 To step into the details a bit, the methods are belong to a
479 @emph{generic function}, which is responsible for dispatching
480 appropriate methods.
481 @c JP
482 少し詳しいことを言えば、メソッドは@emph{ジェネリック関数}に属しており、
483 ジェネリック関数が適切なメソッドを選ぶ役割を果たします。
484 @c COMMON
485
486 @c EN
487 @item Class is also an instance.
488 By default, a class is also an instance of class
489 @code{<class>}, and a generic function is an instance of class
490 @code{<generic>}. You can subclass @code{<class>} to customize
491 how a class is initialized or how its slots are accessed.
492 You can subclass @code{<generic>} to customize how the
493 applicable methods are selected, which order those methods are
494 called, etc. The mechanism is called @emph{metaobject protocol}.
495 Metaobject protocol allows you to extend the language by
496 the language itself.
497
498 To find examples, see the files @code{lib/gauche/singleton.scm} and
499 @code{lib/gauche/mop/validator.scm} included in the distribution.
500 You can also read @code{lib/gauche/mop/object.scm}, which actually
501 defines how a class is defined in Gauche.
502 For more details about metaobject protocol, see
503 Gregor Kiczales, Jim Des Rivieres, Daniel Bobrow,
504 The Art of Metaobject Protocol,
505 The MIT Press.
506 @c JP
507 @item クラスはインスタンスでもある
508 デフォルトでは、クラスは@code{<class>}というクラスのインスタンスであり、
509 ジェネリック関数は@code{<generic>}というクラスのインスタンスです。
510 しかし、@code{<class>}を継承したクラスを定義することにより、
511 オブジェクトの初期化がどのように行われるかとか、スロットがどのように
512 アクセスされるかといった動作をカスタマイズすることができます。
513 また、@code{<generic>}を継承したクラスを定義することにより、
514 適用可能なメソッドがどのように選択されて、どのような順序で適用されるか
515 といったことがカスタマイズ可能です。このメカニズムは@emph{メタオブジェクトプロトコル}
516 と呼ばれています。メタオブジェクトプロトコルは、言語をその言語そのもので拡張する方法と
517 言えるでしょう。
518
519 例として、ディストリビューションに含まれる@code{lib/gauche/mop/singleton.scm}や
520 @code{lib/gauche/mop/validator}等があります。@code{src/libobj.scm}を
521 読めば、クラス自身がGaucheでどのように定義されているかがわかります。
522 メタオブジェクトプロトコルの更に詳しいことについては、次の書籍が参考になります:
523 Gregor Kiczales, Jim Des Rivieres, Daniel Bobrow,
524 The Art of Metaobject Protocol,
525 The MIT Press.
526 @c COMMON
527
528 @c EN
529 @item Class doesn't create namespace
530 In the mainstream OO language, a class often creates its own namespace.
531 This isn't the case in CLOS-style object system.
532 In Gauche, a namespace is managed by the module system
533 which is orthogonal to the object system.
534 @c JP
535 @item クラスは名前空間を作らない
536 メインストリームのOO言語ではクラスが名前空間を作ることがよくあります。
537 CLOSスタイルのオブジェクトシステムはそうではありません。
538 Gaucheでは、名前空間はオブジェクトシステムとは直交する、
539 モジュールシステムによって管理されます。
540 @c COMMON
541
542 @end table
543
544 @node Module system, Compilation, Integrated object system, Concepts
545 @section Module system
546 @c NODE モジュールシステム
547
548 @c EN
549 Gauche has a simple module system that allows
550 modularized development of large software.
551 @c JP
552 Gaucheは大きなソフトウェアをモジュール化して開発するための、
553 単純なモジュールシステムを備えています。
554 @c COMMON
555
556 @c EN
557 A higher level interface is simple enough from the user's point
558 of view. It works like this:
559 When you want to use the features provided by module
560 @code{foo}, you just need to say @code{(use foo)} in your code.
561 This form is a macro and interpreted at compile time.
562 Usually it loads the files that defines @code{foo}'s features,
563 and imports the external APIs into the calling module.
564 @c JP
565 高レベルのインタフェースを使うのはユーザにとっては非常に簡単です。
566 モジュール@code{foo}が提供する機能を使いたければ、
567 プログラム中で@code{(use foo)}と表記するだけです。
568 このフォームはマクロで、コンパイル時に解釈されます。
569 通常は、@code{foo}の機能を実装したファイルをロードし、
570 その外部APIを呼び出したモジュール中にインポートします。
571 @c COMMON
572
573 @c EN
574 The @code{use} mechanism is built on top of two independent
575 lower mechanisms, namespace separation and file loading mechanism.
576 Those two lower mechanisms can be used separately, although it is much more
577 convenient when used together.
578 @c JP
579 @code{use}メカニズムは、名前空間の分離とファイルローディングという
580 二つの独立な低レベルのメカニズムの上に実装されています。
581 これらの低レベルメカニズムはそれぞれ別々に使うこともできます。
582 @c COMMON
583
584 @c EN
585 The @code{use} mechanism is not transitive; that is,
586 if a module B uses a module A, and a module C uses the module B,
587 C doesn't see the bindings in A. It is because B and A is not in the
588 @code{is-a} relationship.
589 Suppose the module A implements a low-level functionality and
590 the module B implements a high-level abstraction; if C is using
591 B, what C wants to see is just a high-level abstraction, and
592 doesn't concern how B implements such functionality. If C wants
593 to access low-level stuff, C has to @code{use} A explicitly.
594 @c JP
595 @code{use}メカニズムは非遷移的です。すなわち、
596 モジュールBがモジュールAを'use'し、さらにモジュールCがモジュールBを
597 'use'している場合、モジュールCからはモジュールA内の束縛は見えません。
598 BとAは@code{is-a}関係ではないためです。
599 例えばモジュールAが低レベルの操作を実装し、
600 モジュールBがその上に高レベルの抽象的なインタフェースを実装しているとしましょう。
601 モジュールCがモジュールBを'use'しているということは、
602 CはBの提供する抽象レイヤに興味があるということです。
603 もしCがさらに低レベルの層にもアクセスしたいならば、CはAを明示的に'use'しなければ
604 なりません。
605 @c COMMON
606
607 @c EN
608 There is another type of relationship, though. You might want to
609 take an exiting module A, and add some interface to it and provide
610 the resulting module B as an extension of A. In such a case,
611 B is-a A, and it'd be natural that the module that uses B can also
612 see A's bindings. In Gauche, it is called @emph{module inheritance}
613 and realized by @code{extend} form.
614 @c JP
615 しかし、別の形の関係が必要な場合もあります。例えば既存のモジュールAに
616 ちょっと新しいインタフェースを付け加えたモジュールBをAの拡張として
617 提供したいという場合です。この時、BとAは@code{is-a}関係であり、Bを'use'
618 しているモジュールからA内の束縛も見えるのが自然です。
619 Gaucheでは、これをモジュールの継承と呼び、
620 @code{extend}フォームにより実現しています。
621 @c COMMON
622
623 @c EN
624 The following sections in this manual describes
625 modules in details.
626 @itemize @bullet
627 @item
628 @ref{Writing Gauche modules} explains the convention of
629 writing modules.
630 @item
631 @ref{Modules} describes special forms and macros to
632 define and to use modules, along the built-in functions
633 to introspect module internals.
634 @end itemize
635 @c JP
636 以下のセクションでモジュールの機能について詳しく説明しています。
637 @itemize @bullet
638 @item
639 @ref{Writing Gauche modules} ではモジュールの書き方について
640 説明します。
641 @item
642 @ref{Modules} ではモジュールを定義したり使うための特殊形式とマクロ、
643 およびモジュールの内部を調べるための組込み手続きについて説明します。
644 @end itemize
645 @c COMMON
646
647 @node Compilation, , Module system, Concepts
648 @section Compilation
649 @c NODE コンパイル
650
651 @c EN
652 By default, Gauche reads toplevel Scheme forms one at a time,
653 compile it immediately to intermediate form and execute it on the VM.
654 As long as you use Gauche interactively, it looks like an
655 interpreter.
656 (There's an experimental ahead-of-time compiler as well.
657 See @file{HOWTO-precompile.txt} if you want to give a try.)
658 @c JP
659 デフォルトでは、GaucheはSchemeのトップレベルフォームをひとつづつ読み、
660 直ちに中間コードへとコンパイルして仮想マシンで実行します。
661 Gaucheをインタラクティブに使っている限りは、インタプリタのように思えるでしょう。
662 (実験的なAOTコンパイラもあります。試してみたい方は
663 @file{HOWTO-precompile.txt}を見てください。)
664 @c COMMON
665
666 @c EN
667 The fact that we have separate compilation/execution phase, even
668 interleaved, may lead a subtle surprise if you think Gauche as an interpreter.
669 Here's a few points to keep in mind:
670 @c JP
671 コンパイルと実行のフェーズが分かれていることによって、
672 Gaucheをインタプリタと考えていると少々驚くことがあるかもしれません。
673 いくつか念頭に置いておくべき点をあげておきます。
674 @c COMMON
675
676 @table @emph
677 @c EN
678 @item load is done at run time.
679 @code{load} is a procedure in Gauche, therefore evaluated at run time.
680 If the loaded program defines a macro, which is available for the compiler
681 after the toplevel form containing @code{load} is evaluated. So, suppose
682 @code{foo.scm} defines a macro @code{foo}, and you use the macro
683 like this:
684 @c JP
685 @item loadは実行時に評価される
686 @code{load}はGaucheでは一般の関数なので、実行時に評価されます。
687 ロードされるファイル中でマクロを定義している場合、そのマクロは
688 その@code{load}の呼び出しを含むトップレベルフォームが評価された後で
689 有効になります。例えば、@code{foo.scm}がマクロ@code{foo}を定義しているとして、
690 次のような用法を考えてみてください。
691 @c COMMON
692 @example
693 ;; @r{in ``foo.scm''}
694 (define-syntax foo
695 (syntax-rules () ((_ arg) (quote arg))))
696
697 ;; @r{in your program}
698 (begin (load "foo") (foo (1 2 3)))
699 @result{} @r{error, bad procedure: `1'}
700
701 (load "foo")
702 (foo (1 2 3)) @result{} '(1 2 3)
703 @end example
704 @c EN
705 The @code{(begin (load ...))} form fails, because the compiler
706 doesn't know @code{foo} is a special form at the compilation time
707 and compiles @code{(1 2 3)} as if it is a normal procedure call.
708 The latter example works, however, since the execution
709 of the toplevel form @code{(load "foo")} is done before
710 @code{(foo (1 2 3))} is compiled.
711 @c JP
712 @code{begin}で@code{load}と@code{foo}の呼び出しを囲んだ場合、
713 コンパイラはまず@code{begin}全体をコンパイルします。その時点でマクロ
714 @code{foo}は定義されていませんから、これは評価時にエラーになります。
715 一方、後者では@code{load}が評価されてから@code{foo}がコンパイルされるので、
716 問題は起きません。
717 @c COMMON
718
719 @c EN
720 To avoid this kind of subtleties, use @code{require} or @code{use}
721 to load a program fragments. Those are recognized by the compiler.
722 @c JP
723 このような混乱を避けるために、別のプログラムファイルを読み込む必要がある時は
724 @code{require}か@code{use}を使うことを勧めます。これらは構文であり、
725 コンパイラに認識されます。
726 @c COMMON
727
728 @c EN
729 @item require is done at compile time
730 On the other hand, since @code{require} and @code{use} is recognized
731 by the compiler, the specified file is loaded even if the form
732 is in the conditional expression. If you really need to load
733 a file on certain condition, use @code{load} or do dispatch in macro
734 (e.g. @code{cond-expand} form (@pxref{Feature conditional}).)
735 @c JP
736 @item require はコンパイル時に評価される
737 上記の裏返しですが、@code{require}と@code{use}はコンパイル時に
738 解釈されます。したがって、@code{if}などの条件文のボディにこれらのフォームを
739 置いておいても、指定されたファイルは条件にかかわらず読み込まれてしまいます。
740 どうしてももし条件によって読み込むかどうかを変えたい場合は、@code{load}を使うか、
741 条件判断自体をマクロで行うようにしてください。
742 (例えば、@code{cond-expand}フォーム等を使って。(@ref{Feature conditional}参照))
743 @c COMMON
744 @end table
745
746 @c Local variables:
747 @c mode: texinfo
748 @c coding: utf-8
749 @c end: