=================================================================== RCS file: /home/cvs/OpenXM/src/asir-doc/parts/asir.texi,v retrieving revision 1.3 retrieving revision 1.14 diff -u -p -r1.3 -r1.14 --- OpenXM/src/asir-doc/parts/asir.texi 1999/12/21 02:47:31 1.3 +++ OpenXM/src/asir-doc/parts/asir.texi 2003/12/21 17:36:29 1.14 @@ -1,4 +1,4 @@ -@comment $OpenXM$ +@comment $OpenXM: OpenXM/src/asir-doc/parts/asir.texi,v 1.13 2003/12/20 13:38:44 ohara Exp $ \BJP @node ユーザ言語 Asir,,, Top @chapter ユーザ言語 Asir @@ -67,7 +67,7 @@ capital alphabetical letter in @b{Asir}. @item \JP 変数の型がない. \EG No types for variables. - +@* \BJP 既に説明したとおり, @b{Asir} で扱われる対象自身は全て何らかの型 を持っている. しかし, プログラム変数自体は, どのような対象でも @@ -93,7 +93,7 @@ object can be assigned to it. @item \BJP 函数内の変数は, デフォルトでは仮引数をこめてすべて局所変数. - +@* ただし, @code{extern} 宣言された変数は, トップレベルにおける大域変数となる. すなわち, 変数のスコープは大域変数と局所変数の 2 種類に単純化されている. トップレベル, すなわちプロンプトに対して入力された変数は全て大域変数 @@ -102,7 +102,7 @@ object can be assigned to it. \BEG Variables, together with formal parameters, in a function (procedure) are all local to the function by default. - +@* Variables can be global at the top level, if they are declared with the key word @code{extern}. Thus, the scope rule of @b{Asir} is very simple. @@ -157,7 +157,7 @@ end$ @item \JP プログラム変数は大文字で始まり, 不定元, 函数は小文字で始まる. \EG Program variables and algebraic indeterminates are distinguished in @b{Asir}. - +@* \BJP この点は, 既存の数式処理システムのほとんどと異なる点である. @b{Asir} がこの仕様を採用したのは, ユーザが不定元のつもりで使用した変数に @@ -182,7 +182,7 @@ variables and indeterminates in a program. @item \JP @code{switch} 文, @code{goto} がない. \EG No @code{switch} statements, and @code{goto} statements. - +@* \JP @code{goto} がないため, 多重ループを一度に抜けるのがやや複雑になる場合がある. \EG Lack of @code{goto} statement makes it rather bothering to exit from within multiple loops. @@ -195,7 +195,7 @@ variables and indeterminates in a program. Comma expressions are allowed only in @code{A}, @code{B} and @code{C} of the constructs @code{for (A;B;C)} or @code{while(A)}. \E - +@* \JP これは, リストを正式なオブジェクトとして加えたことによる. \EG This limitation came from adopting lists as legal data objects for @b{Asir}. @@ -227,7 +227,13 @@ Use of lists enables to write programs more easily, sh comprehensible than use of structure like C programs. \E +\BJP @item +ユーザ定義函数における一行ヘルプ. Emacs-Lisp に類似した機能である. +詳しくは, @xref{ユーザ定義函数} を見よ. +\E + +@item \JP ユーザ定義函数におけるオプション指定. \EG Options can be specified in calling user defined functions. @@ -236,6 +242,26 @@ comprehensible than use of structure like C programs. @end itemize \BJP +@noindent +Asir では次の語句がキーワードとして定められている. +@itemize +@item C 言語に由来: + +@code{break}, @code{continue}, @code{do}, @code{else}, @code{extern}, +@code{for}, @code{if}, @code{return}, @code{static}, @code{struct}, +@code{while} +@item C 言語からの拡張: + +@code{def}, @code{endmodule}, @code{function}, @code{global}, +@code{local}, @code{localf}, @code{module} +@item 函数: + +@code{car}, @code{cdr}, @code{getopt}, @code{newstruct}, @code{map}, +@code{pari}, @code{quote}, @code{recmap}, @code{timer} +@end itemize +\E + +\BJP @node ユーザ定義函数の書き方,,, ユーザ言語 Asir @section ユーザ定義函数の書き方 \E @@ -254,9 +280,11 @@ comprehensible than use of structure like C programs. * return 文:: * if 文:: * ループ break return continue:: +* 構造体定義:: * さまざまな式:: * プリプロセッサ:: * オプション指定:: +* モジュール:: \E \BEG * User defined functions:: @@ -267,9 +295,11 @@ comprehensible than use of structure like C programs. * return statement:: * if statement:: * loop break return continue:: +* structure definition:: * various expressions:: * preprocessor:: * option:: +* module:: \E @end menu @@ -345,6 +375,16 @@ def c(N) @} return A; @} + +@tex +/* $A+B$ */ +@end tex + +def add(A,B) +"add two numbers." +@{ + return A+B; +@} @end example @noindent @@ -370,6 +410,18 @@ ICJ @noindent \BJP +3 つ目の例では, 引数並びのあとに文字列が置かれているが、これは +Emacs-Lisp の関数定義に類似の機能で、ヘルプ用の文字列である。 +この例の場合、@code{help(add)} によってこの文字列が出力される。 +\E +@table @t +\JP @item 参照 +\EG @item References +@fref{help}. +@end table + +@noindent +\BJP 以下では, C によるプログラミングの経験がない人のために, @b{Asir} 言語 によるプログラムの書き方を解説する. \E @@ -434,9 +486,9 @@ All the program variables are initialized to the value \BJP @item 不定元 -小文字で始まり, アルファベット, 数字, @samp{_} からなる文字列 - -不定元とは, 多項式環を構成する際に添加される変数をいう. @b{Asir} に +小文字で始まり, アルファベット, 数字, @samp{_} からなる文字列, +またはシングルクオートで囲まれた文字列, もしくは函数形式. +不定元とは, 多項式環を構成する際に添加される変数をいう. @b{Asir} に おいては, 不定元は値をもたない超越的な元であり, 不定元への値の代入は 許されない. \E @@ -455,6 +507,11 @@ An indeterminate cannot have any value. No assignment x [4] X^2+X+1; x^2+x+1 +[5] A='Dx'*(x-1)+x*y-y; +(y+Dx)*x-y-Dx +[6] function foo(x,y); +[7] B=foo(x,y)*x^2-1; +foo(x,y)*x^2-1 @end example @end table @@ -574,7 +631,7 @@ def afo(X) @{ @samp{/*} がいくつあっても最初のもののみが有効となり, 最初に現れた @samp{*/} でコメントは終了したと見なされる. プログラムなどで, コメント を含む可能性がある部分をコメントアウトした場合には, @code{#if 0}, -@code{#endif}を使えばよい. (@xref{プリプロセッサ}) +@code{#endif}を使えばよい. (@xref{プリプロセッサ}.) \E \BEG A comment can span to several lines, but it cannot be nested. @@ -583,7 +640,7 @@ in the subsequent text exist, and the comment terminat @samp{*/}. In order to comment out a program part that may contain comments in it, -use the pair, @code{#if 0} and @code{#endif}. (@xref{preprocessor}) +use the pair, @code{#if 0} and @code{#endif}. (@xref{preprocessor}.) \E @example @@ -650,7 +707,7 @@ S = sum(N); \BJP のように, 式に終端記号 (@samp{;} または @samp{$}) をつけたものである. この単文及び類似の @code{return} 文, @code{break} 文などが文の最小構成 -単位となる. @code{if} 文や @code{for} 文の定義 (@xref{文法の詳細}) を見れ +単位となる. @code{if} 文や @code{for} 文の定義 (@ref{文法の詳細}) を見れ ばわかる通り, それらの本体は, 単なる一つの文として定義されている. 通常 は, 本体には複数の文が書けることが必要となる. このような場合, @samp{@{} と @samp{@}} で文の並びを括って, 一つの文として扱うことがで @@ -834,6 +891,19 @@ if ( expression1 ) @{ @noindent \JP としなければならない. +\BJP +@noindent +関数の中でなく, top level で @code{if} 文を用いるときは @code{$} または @code{;} +で終了する必要がある. +これらがないと次の文がよみとばされる. +\E +\BEG +@noindent +When @code{if} is used in the top level, the @code{if} expression should be +terminated with @code{$} or @code{;}. +If there is no terminator, the next expression will be skipped to be evaluated. +\E + \BJP @node ループ break return continue,,, ユーザ定義函数の書き方 @subsection ループ, @code{break}, @code{return}, @code{continue} @@ -858,7 +928,7 @@ the @code{while} statement, the @code{for} statement, @item \JP @code{while} 文 \EG @code{while} statement - +@* \JP 形式は, \EG It has the following form. @@ -882,13 +952,13 @@ infinite loop. @item \JP @code{for} 文 \EG @code{for} statement - +@* \JP 形式は, \EG It has the following form. @example \JP for ( 式並び-1; 式; 式並び-2 ) 文 -\EG for ( expression list-1; expression; expression list-2 ) statement +\EG for ( expr list-1; expr; expr list-2 ) statement @end example \JP で, これは @@ -903,10 +973,10 @@ while ( 式 ) @{ @} \E \BEG -expression list-1 (transformed into a sequence of simple statement) -while ( expression ) @{ +expr list-1 (transformed into a sequence of simple statement) +while ( expr ) @{ statement - expression list-2 (transformed into a sequence of simple statement) + expr list-2 (transformed into a sequence of simple statement) @} \E @end example @@ -916,7 +986,7 @@ while ( expression ) @{ @item \JP @code{do} 文 \EG @code{do} statement - +@* @example \BJP do @{ @@ -957,12 +1027,12 @@ the control to a certain point of the loop. @item @code{break} - +@* \JP @code{break} 文は, それを囲むループを一つだけ抜ける. \EG The @code{break} statement is used to exit the inner most loop. @item @code{return} - +@* \BJP @code{return} 文は, 一般に函数から抜けるための文であり, ループの中からでも有効である. @@ -974,7 +1044,7 @@ and it is also effective in a loop. @item @code{continue} - +@* \BJP @code{continue} 文は, ループの本体の文の末端に制御を移す. 例えば @code{for} 文では, 最後の式並びの実行を行い, @code{while} @@ -990,6 +1060,57 @@ statement, and the termination condition will be evalu @end itemize \BJP +@node 構造体定義,,, ユーザ定義函数の書き方 +@subsection 構造体定義 +\E +\BEG +@node structure definition,,, Writing user defined functions +@subsection structure definition +\E + +\BJP +構造体とは, 各成分の要素が名前でアクセスできる固定長配列と思ってよい. +各構造体は名前で区別される. 構造体は, @code{struct} 文により宣言される. +構造体が宣言されるとき, asir は内部で構造体のそれぞれの型に固有の識別 +番号をつける. この番号は, 組み込み関数 @code{struct_type} により取得 +できる. +ある型の構造体は, 組み込み関数 @code{newstruct} により生成される. +構造体の各メンバは, 演算子 @code{->} によりアクセスする. +メンバが構造体の場合, @code{->} による指定は入れ子にできる. +\E + +\BEG +A structure data type is a fixed length array and each component of the array +is accessed by its name. Each type of structure is distinguished by its name. +A structure data type is declared by @code{struct} statement. +A structure object is generated by a builtin function @code{newstruct}. +Each member of a structure is accessed by an operatator @code{->}. +If a member of a structure is again a structure, then the specification +by @code{->} can be nested. +\E + +@example +[1] struct rat @{num,denom@}; +0 +[2] A = newstruct(rat); +@{0,0@} +[3] A->num = 1; +1 +[4] A->den = 2; +2 +[5] A; +@{1,2@} +[6] struct_type(A); +1 +@end example + +@table @t +\JP @item 参照 +\EG @item References +@fref{newstruct}, @fref{struct_type} +@end table + +\BJP @node さまざまな式,,, ユーザ定義函数の書き方 @subsection さまざまな式 \E @@ -1006,7 +1127,7 @@ statement, and the termination condition will be evalu @item \JP 加減乗除, 冪 \EG addition, subtraction, multiplication, division, exponentiation - +@* \BJP 冪は, @samp{^} により表す. 除算 @samp{/} は, 体としての演算に用いる. 例えば, @code{2/3} は有理数の @code{2/3} を表す. @@ -1027,7 +1148,7 @@ x+1 A^2*B*afo X/3 @item \JP インデックスつきの変数 \EG programming variables with indices - +@* \BJP ベクトル, 行列, リストの要素はインデックスを用いることにより取り出せる. インデックスは 0 から始まることに注意する. 取り出した要素がベクトル, @@ -1048,7 +1169,7 @@ V[0] M[1][2] @item \JP 比較演算 \EG comparison operation - +@* \BJP 等しい (@samp{==}), 等しくない (@samp{!=}), 大小 (@samp{>}, @samp{<}, @samp{>=}, @samp{<=}) の 2 項演算がある. 真ならば有理数の 1, 偽ならば @@ -1065,7 +1186,7 @@ or 0 for the false. @item \JP 論理式 \EG logical expression - +@* \BJP 論理積 (@samp{&&}), 論理和 (@samp{||}) の 2 項演算と, 否定 (@samp{!}) が用意されている. 値はやはり 1, 0 である. @@ -1082,7 +1203,7 @@ and 0 for the false. @item \JP 代入 \EG assignment - +@* \BJP 通常の代入は @samp{=} で行う. このほか, 算術演算子と組み合わせて 特殊な代入を行うこともできる. @@ -1095,17 +1216,17 @@ There are special assignments combined with arithmetic @example \JP A = 2 A *= 3 (これは A = A*3 と同じ; その他の演算子も同様) -\EG A = 2 A *= 3 (the same as A = A*3; The other combination are alike.) +\EG A = 2 A *= 3 (the same as A = A*3; The others are alike.) @end example @item \JP 函数呼び出し \EG function call - +@* \JP 函数呼び出しも式の一種である. \EG A function call is also an expression. @item @samp{++}, @samp{--} - +@* \JP これらは, 変数の前後について, それぞれ次のような操作, 値を表す. \BEG These operators are attached to or before a program variable, @@ -1121,8 +1242,8 @@ A-- 値は元の A の値, A = A-1 \BEG A++ the expression value is the previous value of A, and A = A+1 A-- the expression value is the previous value of A, and A = A-1 -++A A = A+1, and the expression value is the value after increment of A ---A A = A-1, and the expression value is the value after decrement of A +++A A = A+1, and the value is the one after increment of A +--A A = A-1, and the value is the one after decrement of A \E @end example @@ -1156,10 +1277,13 @@ through @code{cpp}. This enables @b{Asir} user to use @itemize @bullet @item @code{#include} - +@* \BJP -@code{cpp} に特に引数を渡さないため, インクルードファイルは, -@code{#include} が書かれているファイルと同じディレクトリでサーチされる. +UNIX では インクルードファイルは, Asir のライブラリディレクトリ +(環境変数 @b{ASIR_LIBDIR} で指定されたディレクトリ) +と @code{#include} が書かれているファイルと同じディレクトリをサーチする. +UNIX 以外では @code{cpp} に特に引数を渡さないため, +@code{#include} が書かれているファイルと同じディレクトリのみをサーチする. \E \BEG Include files are searched within the same directory as the file @@ -1168,13 +1292,13 @@ containing @code{#include} so that no arguments are pa @item @code{#define} - +@* \JP これは, C におけるのと全く同様に用いることができる. \EG This can be used just as in C language. @item @code{#if} - +@* \BJP @code{/*}, @code{*/} によるコメントは入れ子にできないので, プログラム の大きな部分をコメントアウトする際に, @code{#if 0}, @code{#endif} @@ -1228,6 +1352,21 @@ because such comments cannot be nested. #define MAX(a,b) ((a)>(b)?(a):(b)) @end example +\BJP +@noindent +C のプリプロセッサを流用しているため, プリプロセッサは @code{$} を正しく処理できない. +たとえば @code{LIST} が定義されていても +@code{LIST$}は置換されない. @code{$} の前に空白をおいて +@code{LIST $} と書かないといけない. +\E +\BEG +@noindent +Since we are utilizing the C preprocessor, it cannot properly preprocess expressions +with @code{$}. +For example, even if @code{LIST} is defined, @code{LIST} in the expression +@code{LIST$} is not replaced. Add a blank before @code{$}, i.e., +write as @code{LIST $} to make the proprocessor replace it properly. +\E \BJP @node オプション指定,,, ユーザ定義函数の書き方 @@ -1313,3 +1452,195 @@ After @samp{|} one can append any number of options se @end example +\BJP +@node モジュール,,, ユーザ定義函数の書き方 +@subsection モジュール +\E +\BEG +@node module,,, Writing user defined functions +@subsection module +\E + +\BJP +ライブラリで定義されている関数, 変数をカプセル化する仕組みが +モジュール (module) である. +はじめにモジュールを用いたプログラムの例をあげよう. +\E +\BEG +Function names and variables in a library may be +encapsulated by module. +Let us see an example of using module +\E + +@example +module stack; + +static Sp $ +Sp = 0$ +static Ssize$ +Ssize = 100$ +static Stack $ +Stack = newvect(Ssize)$ +localf push $ +localf pop $ + +def push(A) @{ + if (Sp >= Ssize) @{print("Warning: Stack overflow\nDiscard the top"); pop();@} + Stack[Sp] = A; + Sp++; +@} +def pop() @{ + local A; + if (Sp <= 0) @{print("Stack underflow"); return 0;@} + Sp--; + A = Stack[Sp]; + return A; +@} +endmodule; + +def demo() @{ + stack.push(1); + stack.push(2); + print(stack.pop()); + print(stack.pop()); +@} +@end example + +\BJP +モジュールは @code{module} モジュール名 〜 @code{endmodule}で囲む. +モジュールは入れ子にはできない. +モジュールの中だけで使う大域変数は @code{static} で宣言する. +この変数はモジュールの外からは参照もできないし変更もできない. +モジュールの外の大域変数は @code{extern} で宣言する. +\E +\BEG +Module is encapsulated by the sentences +@code{module} module name +and +@code{endmodule}. +A variable of a module is declared with the key word @code{static}. +The static variables cannot be refered nor changed out of the module, +but it can be refered and changed in any functions in the module. +A global variable which can be refered and changed at any place +is declared with the key word @code{extern}. +\E + +\BJP +モジュール内部で定義する関数は @code{localf} を用いて宣言しないといけない. +上の例では @code{push} と @code{pop} を宣言している. +この宣言は必須である. +\E +\BEG +Any function defined in a module must be declared forward +with the keyword @code{localf}. +In the example above, @code{push} and @code{pop} are declared. +This declaration is necessary. +\E + +\BJP +モジュール @code{moduleName} で定義された関数 @code{functionName} を +モジュールの外から呼ぶには + @code{moduleName.functionName(引数1, 引数2, ... )} +なる形式でよぶ. +モジュールの中からは, 関数名のみでよい. +次の例では, モジュールの外からモジュール @code{stack} で定義された関数 @code{push}, +@code{pop} を呼んでいる. +\E +\BEG +A function @code{functionName} defined in a module @code{moduleName} +can be called by the expression +@code{moduleName.functioName(arg1, arg2, ...)} +out of the module. +Inside the module, @code{moduleName.} is not necessary. +In the example below, the functions @code{push} and @code{pop} defined +in the module @code{stack} are called out of the module. +\E + +@example + stack.push(2); + print( stack.pop() ); + 2 +@end example + +\BJP +モジュールで用いる関数名は局所的である. +つまりモジュールの外や別のモジュールで定義されている関数名と同じ名前が +利用できる. +\E +\BEG +Any function name defined in a module is local. +In other words, the same function name may be used out of the module +to define a different function. +\E + +\BJP +モジュール機能は大規模ライブラリの開発を想定している. +ライブラリを必要に応じて分割ロードするには, 関数 @code{module_definedp} を用いるのが +便利である. +デマンドロードはたとえば次のように行なえば良い. +\E +\BEG +The module structure of asir is introduced to develop large libraries. +In order to load libraries on demand, the command @code{module_definedp} +will be useful. +The below is an example of demand loading. +\E + +@example +if (!module_definep("stack")) load("stack.rr") $ +@end example + +\BJP +asir では局所変数の宣言は不要であった. +しかしモジュール stack の例を見れば分かるように, @code{local A;} なる形式で +局所変数を宣言できる. +キーワード @code{local} を用いると, 宣言機能が有効となる. +宣言機能を有効にすると, 宣言されてない変数はロードの段階で +エラーを起こす. +変数名のタイプミスによる予期しないトラブルを防ぐには, +宣言機能を有効にしてプログラムするのがよい. +\E +\BEG +It is not necessary to declare local variables in asir. +As you see in the example of the stack module, +we may declare local variables by the key word @code{local}. +Once this key word is used, asir requires to declare all the +variables. +In order to avoid some troubles to develop a large libraries, +it is recommended to use @code{local} declarations. +\E + +\BJP +モジュール内の関数をそのモジュールが定義される前に +呼び出すような関数を書くときには, その関数の前でモジュールを次のように +プロトタイプ宣言しておく必要がある. +\E +\BEG +When we need to call a function in a module before the module is defined, +we must make a prototype declaration as the example below. +\E + +@example +/* Prototype declaration of the module stack */ +module stack; +localf push $ +localf pop $ +endmodule; + +def demo() @{ + print("----------------"); + stack.push(1); + print(stack.pop()); + print("---------------"); +@} + +module stack; + /* The body of the module stack */ +endmodule; +@end example + +@table @t +\JP @item 参照 +\EG @item References +@fref{module_list}, @fref{module_definedp}, @fref{remove_module}. +@end table