[BACK]Return to tr.rr CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / testing

Annotation of OpenXM/src/asir-contrib/testing/tr.rr, Revision 1.4

1.4     ! takayama    1: /* $OpenXM: OpenXM/src/asir-contrib/testing/tr.rr,v 1.3 2005/04/03 11:05:21 takayama Exp $ */
        !             2: /* $Id: tr.rr,v 1.7 2005/04/03 11:05:46 taka Exp $ */
1.2       takayama    3:
                      4: /*
                      5:   OpenXM$BHG$N(B Risa/Asir $B$G<B9T$N$3$H(B. OpenXM $BHG$N4X?t$rMQ$$$k$?$a(B.
                      6: */
1.4     ! takayama    7: /* $Id: tr.rr,v 1.7 2005/04/03 11:05:46 taka Exp $
1.2       takayama    8:   $B$3$N%U%!%$%k$O(B quotetolist $B$G%j%9%H$KJQ49$7$?%G!<%?$KBP$7$F(B
                      9:   $B%Q%?!<%s%^%C%A$*$h$S$=$l$r1~MQ$7$?JQ7A$r9T$&(B.
                     10:   tr.oxt $B$N;EMM$H$3$H$J$j(B quotetolist $B$GJQ49$7$?$b$N$r07$&(B.
                     11:   $B%F%9%H%W%m%0%i%`$N$?$a8zN($OL5;k(B.   (append $B$NB?MQ(B, $BL5BL$J(B2$B=E8F$S=P$7(B, $B$J$I(B))
                     12: */
                     13:
                     14: extern Debug$
                     15: Debug=0$
                     16: def dprint(X) {
                     17:   if (Debug) print(X);
                     18: }
                     19: def dprint0(X) {
                     20:   if (Debug) print(X,0);
                     21: }
                     22:
1.3       takayama   23: /* quotetolist $B$N5U4X?t(B. $B$?$@$7J8;zNs$G(B */
                     24: def listtoquote_str(L) {
                     25:   return quote_input_form_quote_list(L);
                     26: }
1.2       takayama   27:
                     28: def qt_node(F) {
1.3       takayama   29:    if (type(F) == 17) F=quotetolist(F);
                     30:    return [rtostr(F[0]),rtostr(F[1])];
1.2       takayama   31: }
                     32: /* Number of  child */
                     33: def qt_nchild(F) {
1.3       takayama   34:    if (type(F) == 17) F=quotetolist(F);
1.2       takayama   35:    return length(F)-2;
                     36: }
                     37: def qt_child(F,K) {
1.3       takayama   38:    if (type(F) == 17) F=quotetolist(F);
1.2       takayama   39:    return F[K+2];
                     40: }
                     41:
                     42: /*
                     43:    $B%j%9%H(B F $B$,(B $B%j%9%H(B P $B$K(B($B@hF,$+$i$NHf3S$G(B)$B%^%C%A$7$?$i(B 1.
                     44:    $B$=$&$G$J$$$+$i(B 0.  $BI}M%@hC5:w(B.
                     45:     Todo: P $B$KG$0U4X?t$r4^$`;EAH$_$O$^$@<BAu$7$F$J$$(B.
                     46: */
                     47: def tr_match0(F,P)  {
                     48:   dprint0("tr_match0: F="); dprint(F);
                     49:   dprint0("tr_match0: P="); dprint(P);
                     50:
                     51:   if (type(F) != type(P)) return 0;
                     52:   if (type(F) != 4) {
                     53:     if (F == P) return 1;
                     54:     else return 0;
                     55:   }
                     56:   Node = qt_node(F);
                     57:   Node2 = qt_node(P);
1.3       takayama   58:   /* pn $B$K2?$N@)Ls$b$J$1$l$P(B 2 $B$rLa$9(B. */
                     59:   if (Node2 == ["function","pn"]) return tr_check_pn(F,P);
1.2       takayama   60:   if (Node != Node2) return 0;
                     61:   N = qt_nchild(F);
                     62:   if (N != qt_nchild(P)) return 0;
                     63:   for (I=0; I<N; I++) {
                     64:      C = qt_child(F,I);
                     65:      C2 = qt_child(P,I);
                     66:      if (!tr_match0(C,C2)) return 0;
                     67:   }
                     68:   return 1;
                     69: }
                     70:
1.3       takayama   71: /*
                     72:    P $B$NNc(B: P = pn("x");  P=pn("x",qt_is_integer(x));
                     73:    P $B$O(B [function,pn,[internal,x],[function,is_int,[internal,x]]]
                     74:    FF $B$O(B ["is_int","x"]
                     75:    $B%F%9%H%G!<%?(B.
                     76:  tr_check_pn(quote(1/2),quote(pn("x",qt_is_integer(x))));
                     77: */
                     78: def tr_check_pn(F,P) {
                     79:   if (type(F) ==17) F=quotetolist(F);
                     80:   if (type(P) == 17) P=quotetolist(P);
                     81:   N=qt_nchild(P);
                     82:   if (N == 1) return 2;
                     83:   X = rtostr(qt_child(P,0)[1]);
                     84:   BindingTable = [[X,F]];
                     85:   FF = [rtostr(qt_child(P,1)[1]),rtostr(qt_child(P,1)[2][1])];
                     86:   R = tr_apply_function0(FF,BindingTable);
                     87:   return R;
                     88: }
                     89:
1.2       takayama   90: /* F $B$H(B P $B$,(B tr_match0 $B$9$k$H$-(B  bindingTable $B$r$b$I$9(B.
                     91:   [[$BJQ?t$NL>A0(B($BJ8;zNs(B), $BCM(B(list)], ...]
                     92: */
                     93: def tr_make_binding(F,P) {
                     94:   Ans = [ ];
                     95:   if (F == P) return Ans;
                     96:
                     97:   Node = qt_node(F);
                     98:   Node2 = qt_node(P);
                     99:
                    100:   if (Node2 == ["function", "pn"]) {
1.3       takayama  101:      Ans = append(Ans,[[rtostr(P[2][1]),F]]);
1.2       takayama  102:      return Ans;
                    103:   }
                    104:   N = qt_nchild(F);
                    105:   for (I=0; I<N; I++) {
                    106:      C = qt_child(F,I);
                    107:      C2 = qt_child(P,I);
                    108:      Ans = append(Ans,tr_make_binding(C,C2));
                    109:   }
                    110:   return Ans;
                    111: }
                    112:
                    113: /*
                    114:    Tree $B$NCf$rI}M%@hC5:w$G8!:w$7$F(B $BCV$-49$($k(B.
                    115:    $BI}M%@hC5:w$J$N$G(B, $BF1$8(B rule $B$K%^%C%A$9$k$b$N$,F~$l;R$K$J$C$?>l9g(B,
                    116:    $BFbB&$OCV$-49$($i$l$J$$(B.
                    117:    $B?<$5M%@hC5:w$K$7$?(B --> action $B4X?t$NCf$G:F5"E*$K8F$Y$P?<$5M%@h$H$J$k(B.
                    118:    Todo: $B=q$-49$($,$*$3$C$?$+$N%U%i%0(B.
                    119: */
                    120: def tr_rp(F,P,Q) {
                    121:   dprint0("tr_rp, F="); dprint(F);
                    122:   dprint0("tr_rp, P="); dprint(P);
                    123:   dprint0("tr_rp, Q="); dprint(Q);
                    124:   if (tr_match0(F,P)) {
                    125:      BindTable = tr_make_binding(F,P);
                    126:      dprint0("BindTable="); dprint(BindTable);
                    127:      return tr_apply_function0(Q,BindTable);
                    128:   }
                    129:   if (type(F) != 4) return F;
                    130:   Node = qt_node(F);
                    131:   N = qt_nchild(F);
                    132:   Ans = Node;
                    133:   for (I=0; I<N; I++) {
                    134:     T = tr_rp(qt_child(F,I),P,Q);
                    135:     Ans = append(Ans,[T]);
                    136:   }
                    137:   return Ans;
                    138: }
                    139:
                    140: /* ["f","x"],[["x",[internal,3]]]  $B$N;~$O(B
                    141:    f(3) $B$r7W;;$9$k(B.
                    142: */
                    143: def tr_apply_function0(Q,BindTable) {
                    144:   B = [ ];
                    145:   N = length(BindTable);
                    146:   /* BindTable $B$N1&JUCM$r(B quote(...) $B$J$kJ8;zNs$K(B */
                    147:   for (I=0; I<N; I++) {
1.3       takayama  148:     B = append(B,[[BindTable[I][0],"quote("+listtoquote_str(BindTable[I][1])+")"]]);
1.2       takayama  149:   }
                    150:   dprint0("tr_apply_function0: "); dprint(B);
                    151:   N = length(Q)-1; /* $B0z?t$N?t(B */
                    152:   M = length(B);   /*  binding table $B$N%5%$%:(B */
1.3       takayama  153:   R = rtostr(Q[0])+"(";
1.2       takayama  154:   for (I=0; I<N; I++) {
                    155:     X = rtostr(Q[I+1]); /* $BJQ?t(B */
                    156:     /* binding Table $B$r%5!<%A(B */
                    157:     for (J=0; J<M; J++) {
                    158:        Y = rtostr(B[J][0]);
                    159:        if (X == Y) {
                    160:           R = R+B[J][1];
                    161:           if (I != N-1) R = R+",";
                    162:           break;
                    163:        }
1.3       takayama  164:        if (J == M-1) {
                    165:          dprint0("No binding data. Use the X itself. X="); dprint(X);
                    166:          R = R+X;
                    167:          if (I != N-1) R = R+",";
                    168:        }
1.2       takayama  169:     }
                    170:   }
                    171:   R = R+")";
                    172:   dprint0("R="); dprint(R);
                    173:   return eval_str(R);
                    174: }
                    175:
                    176:
                    177: /* L $B$,:85,B'(B. R $B$,1&5,B'(B.  $BI}M%@hC5:w(B.
                    178:    $B=q$-49$($r$9$k$?$a$N%H%C%W%l%Y%k$N4X?t(B ($B$N$R$H$D(B).
                    179:   $BNc(B:
                    180:     tr_apply_rule1(quote(1+sin(3*@pi)*sin(@pi/2)),
                    181:                 quote(sin(pn("x")*@pi)),
                    182:                 ["qt_sin_int","x"]);
                    183: */
                    184: def tr_apply_rule1(Obj,L,R) {
                    185:   dprint("--------  start of tr_apply_rule1 ------------ ");
                    186:   Obj = quotetolist(Obj);
                    187:   L = quotetolist(L);
                    188:   R = tr_rp(Obj,L,R);
1.3       takayama  189:   if (type(R) == 17) R=quotetolist(R);
                    190:   RR = "quote("+listtoquote_str(R)+")";
1.2       takayama  191:   dprint("--------  end of tr_apply_rule1 ------------ ");
                    192:   return eval_str(RR);
                    193: }
                    194:
                    195: /* quote $B$KBP$9$k(B $B=R8l(B */
                    196: def qt_is_integer(Qlist) {
1.3       takayama  197:   if (type(Qlist) == 17) Qlist=quotetolist(Qlist);
                    198:   if ((rtostr(Qlist[0]) == "u_op")  && (rtostr(Qlist[1]) == "-")) {
                    199:     return qt_is_integer(cdr(cdr(Qlist))[0]);
                    200:   }
1.2       takayama  201:   if (Qlist[0] == "internal") {
                    202:      Z = eval_str(rtostr(Qlist[1]));
                    203:   }else{
                    204:      return 0;
                    205:   }
                    206:   if (type(Z) == 0) return 1;
                    207:   if ((type(Z) == 1) && (ntype(Z) == 0)) return 1;
                    208:   return 0;
                    209: }
                    210:
                    211: /* quote $B$N@8@.(B */
                    212: /* $B1&5,B'4X?t(B.   0 $B$rLa$9(B. */
                    213: def qt_zero() {
                    214:   return quotetolist(quote(0));
                    215: }
                    216:
                    217: /* $B1&5,B'4X?t(B.   $B91Ey<0(B */
                    218: def qt_id(X) {
1.3       takayama  219:   if (type(X) == 17) return quotetolist(X);
                    220:   else return X;
1.2       takayama  221: }
                    222:
                    223: /* ------------  test --------------------------- */
                    224: extern Rule_test2$
1.3       takayama  225: /* " " $B$HIU$1$F$b$D$1$J$/$F$b$h$$(B. $BFbIt$G(B rtostr $B$7$F$k(B. */
                    226: /* Rule_test2=[quote(sin(pn("x")*@pi)),["qt_sin_int2","x"]]$ */
                    227: Rule_test2=[quote(sin(pn(x)*@pi)),[qt_sin_int2,x]]$
                    228:
1.2       takayama  229:
                    230: def test2() {
                    231:   /* $BI}M%@hC5:w$N>l9g(B, R0 $B$O(B simplify $B$G$-$:(B.  */
                    232:   Rule1=[quote(sin(pn("x")*@pi)),["qt_sin_int","x"]];
                    233:   R0 = quote(1+sin(sin(2*@pi)*@pi)*sin(@pi/2));
                    234:   print(print_input_form(R0));
                    235:   R=tr_apply_rule1(R0,Rule1[0],Rule1[1]);
                    236:   print(print_input_form(R));
                    237:   print("-----------------------");
                    238:   /* $B<!$N$h$&$K=q$/$H?<$5M%@h$G=q$1$k(B */
                    239:   R0 = quote(1+sin(sin(2*@pi)*@pi)*sin(@pi/2));
                    240:   print(print_input_form(R0));
                    241:   R=tr_apply_rule1(R0,Rule_test2[0],Rule_test2[1]);
                    242:   print(print_input_form(R));
                    243: }
                    244:
                    245: /* $B1&5,B'4X?t(B.  sin($B@0?t(B*@pi) $B$r(B 0 $B$K(B */
                    246: def qt_sin_int(X) {
                    247:   /* $B$$$^(B X $B$O(B quote $B7?(B */
                    248:   Y = quotetolist(X);
                    249:   /* Todo: $B$3$N$h$&$J$b$N$r:n$k5!G=$OAH$_9~$_$GM_$7$$(B. */
1.3       takayama  250:   R = "quote(sin("+listtoquote_str(Y)+"*@pi))";
1.2       takayama  251:   print(R);
                    252:   R = eval_str(R);
                    253:   /* Todo: X $B$,(B $B?t;z$+$I$&$+D4$Y$k5!G=$bAH$_9~$_$GM_$7$$(B.
                    254:   */
                    255:   if (Y[0] == "internal") {
                    256:      Z = eval_str(rtostr(Y[1]));
                    257:   }else{
                    258:     return quotetolist(R);
                    259:   }
                    260:   if (type(Z) == 0) return quotetolist(quote(0));
                    261:   if ((type(Z) == 1) &&   (ntype(Z) == 0)) return quotetolist(quote(0));
                    262:   return quotetolist(R);
                    263: }
                    264:
                    265: /* $B1&5,B'4X?t(B.  sin($B@0?t(B*@pi) $B$r(B 0 $B$K(B. $B?<$5M%@hMQ(B */
                    266: def qt_sin_int2(X) {
                    267:   /* tr_apply_rule1 $B$r:F5"E*$K$h$V(B. $B$3$NJ}K!$G9=J82r@O$b$+$1$k(B. */
                    268:   X = tr_apply_rule1(X,Rule_test2[0],Rule_test2[1]);
                    269:   Y = quotetolist(X);
1.3       takayama  270:   R = "quote(sin("+listtoquote_str(Y)+"*@pi))";
1.2       takayama  271:   print(R);
                    272:   R = eval_str(R);
                    273:   if (qt_is_integer(Y)) return quotetolist(quote(0));
                    274:   else return quotetolist(R);
                    275: }
                    276:
1.3       takayama  277: /* --------------- end test -----------------------*/
                    278: def qt_replace(F,Rule) {
                    279:   return base_replace(F,Rule);
                    280: }
                    281:
                    282: /*  F $B$NCf$KITDj85(B X $B$,4^$^$l$F$$$k$+(B?
                    283:     qt_dependent(quotetolist(quote(1+1/x)),x)
                    284: */
                    285: def qt_dependent(F,X) {
                    286:   if (type(F) == 17) F = quotetolist(F);
                    287:   Node = qt_node(F);
                    288:   if ((F[0] == "internal") && (rtostr(F[1]) == rtostr(X))) {
                    289:     return 1;
                    290:   }else{
                    291:      N = qt_nchild(F);
                    292:      for (I=0; I<N;I++) {
                    293:        C = qt_child(F,I);
                    294:        if (qt_dependent(C,X)) return 1;
                    295:      }
                    296:      return 0;
                    297:   }
                    298: }
                    299:
                    300: /* tr_check_pn $B$NF0:n%F%9%H(B */
                    301: def test2b() {
                    302:   Rule=[quote(sin(pn(x,qt_is_integer(x))*@pi)),[qt_zero]]$
                    303:   R0 = quote(1+sin(2*@pi)*sin(a*@pi));;
                    304:   print(print_input_form(R0));
                    305:   R=tr_apply_rule1(R0,Rule[0],Rule[1]);
                    306:   return R;
                    307: }
                    308:
                    309: /* $BCm0U(B: @pi  $B$b4X?t07$$(B. */
                    310: def qt_is_function(X) {
                    311:   if (type(X) == 17) X=quotetolist(X);
                    312:   if (rtostr(X[0]) == "function") return 1;
                    313:   else return 0;
                    314: }
                    315:
                    316: /* qt_map_arg(nn,quote(f(x,y)))  --> nn(f(nn(x),nn(y)))
                    317:    qt_map_arg(nn,quote(1/4+f(x)))     -->
                    318:    $B%F%9%H$O(B test4().
                    319: */
                    320: def qt_map_arg(F,Q) {
                    321:   F = rtostr(F);
                    322:   if (type(Q) == 17) Q=quotetolist(Q);
                    323:   if (rtostr(Q[0]) == "internal") {
                    324:      T = listtoquote_str(Q);
                    325:      return eval_str( "quote("+F+"("+T+"))" );
                    326:   }
                    327:   /* node $B$N;R6!$r(B F $B$GI>2A$9$k(B. */
                    328:   N = qt_nchild(Q);
                    329:   L = [];
                    330:   for (I=0; I<N; I++) {
                    331:     L = append(L,[quotetolist(qt_map_arg(F,qt_child(Q,I)))]);
                    332:   }
                    333:   dprint0("qt_map_arg:L="); dprint(L);
                    334:   T = [Q[0],Q[1]];
                    335:   for (I=0; I<N; I++) {
                    336:     T = append(T,[L[I]]);
                    337:   }
                    338:   /* $B:G8e$K;R6!$r?F(B Q[0],Q[1] $B$GI>2A$7$F$+$i(B F $B$GI>2A(B */
                    339:   T = ["function",F,T];
                    340:   dprint0("qt_map_arg:T="); dprint(T);
                    341:   T = listtoquote_str(T);
                    342:   return eval_str("quote("+T+")");
                    343: }
1.2       takayama  344:
                    345: /* $B$3$l$i0J30$N%F%9%H%W%m%0%i%`$O(B test1-tr.rr $B$r(B
                    346: */
                    347:
                    348: end$

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>