File: [local] / OpenXM / src / asir-contrib / testing / tr.rr (download)
Revision 1.2, Sat Apr 2 05:56:57 2005 UTC (19 years, 6 months ago) by takayama
Branch: MAIN
Changes since 1.1: +241 -0
lines
The first draft of specifications of
tr_ (term rewriting) and
qt_ (functions for quote).
|
/* $OpenXM: OpenXM/src/asir-contrib/testing/tr.rr,v 1.2 2005/04/02 05:56:57 takayama Exp $ */
/* $Id: tr.rr,v 1.2 2005/04/02 05:55:37 taka Exp $ */
/*
OpenXM$BHG$N(B Risa/Asir $B$G<B9T$N$3$H(B. OpenXM $BHG$N4X?t$rMQ$$$k$?$a(B.
*/
/* $Id: tr.rr,v 1.2 2005/04/02 05:55:37 taka Exp $
$B$3$N%U%!%$%k$O(B quotetolist $B$G%j%9%H$KJQ49$7$?%G!<%?$KBP$7$F(B
$B%Q%?!<%s%^%C%A$*$h$S$=$l$r1~MQ$7$?JQ7A$r9T$&(B.
tr.oxt $B$N;EMM$H$3$H$J$j(B quotetolist $B$GJQ49$7$?$b$N$r07$&(B.
$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))
*/
extern Debug$
Debug=0$
def dprint(X) {
if (Debug) print(X);
}
def dprint0(X) {
if (Debug) print(X,0);
}
def qt_node(F) {
return [F[0],F[1]];
}
/* Number of child */
def qt_nchild(F) {
return length(F)-2;
}
def qt_child(F,K) {
return F[K+2];
}
/*
$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.
$B$=$&$G$J$$$+$i(B 0. $BI}M%@hC5:w(B.
Todo: P $B$KG$0U4X?t$r4^$`;EAH$_$O$^$@<BAu$7$F$J$$(B.
*/
def tr_match0(F,P) {
dprint0("tr_match0: F="); dprint(F);
dprint0("tr_match0: P="); dprint(P);
if (type(F) != type(P)) return 0;
if (type(F) != 4) {
if (F == P) return 1;
else return 0;
}
Node = qt_node(F);
Node2 = qt_node(P);
if (Node2 == ["function","pn"]) return 2;
if (Node != Node2) return 0;
N = qt_nchild(F);
if (N != qt_nchild(P)) return 0;
for (I=0; I<N; I++) {
C = qt_child(F,I);
C2 = qt_child(P,I);
if (!tr_match0(C,C2)) return 0;
}
return 1;
}
/* F $B$H(B P $B$,(B tr_match0 $B$9$k$H$-(B bindingTable $B$r$b$I$9(B.
[[$BJQ?t$NL>A0(B($BJ8;zNs(B), $BCM(B(list)], ...]
*/
def tr_make_binding(F,P) {
Ans = [ ];
if (F == P) return Ans;
Node = qt_node(F);
Node2 = qt_node(P);
if (Node2 == ["function", "pn"]) {
Ans = append(Ans,[[P[2][1],F]]);
return Ans;
}
N = qt_nchild(F);
for (I=0; I<N; I++) {
C = qt_child(F,I);
C2 = qt_child(P,I);
Ans = append(Ans,tr_make_binding(C,C2));
}
return Ans;
}
/*
Tree $B$NCf$rI}M%@hC5:w$G8!:w$7$F(B $BCV$-49$($k(B.
$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,
$BFbB&$OCV$-49$($i$l$J$$(B.
$B?<$5M%@hC5:w$K$7$?(B --> action $B4X?t$NCf$G:F5"E*$K8F$Y$P?<$5M%@h$H$J$k(B.
Todo: $B=q$-49$($,$*$3$C$?$+$N%U%i%0(B.
*/
def tr_rp(F,P,Q) {
dprint0("tr_rp, F="); dprint(F);
dprint0("tr_rp, P="); dprint(P);
dprint0("tr_rp, Q="); dprint(Q);
if (tr_match0(F,P)) {
BindTable = tr_make_binding(F,P);
dprint0("BindTable="); dprint(BindTable);
return tr_apply_function0(Q,BindTable);
}
if (type(F) != 4) return F;
Node = qt_node(F);
N = qt_nchild(F);
Ans = Node;
for (I=0; I<N; I++) {
T = tr_rp(qt_child(F,I),P,Q);
Ans = append(Ans,[T]);
}
return Ans;
}
/* ["f","x"],[["x",[internal,3]]] $B$N;~$O(B
f(3) $B$r7W;;$9$k(B.
*/
def tr_apply_function0(Q,BindTable) {
B = [ ];
N = length(BindTable);
/* BindTable $B$N1&JUCM$r(B quote(...) $B$J$kJ8;zNs$K(B */
for (I=0; I<N; I++) {
B = append(B,[[BindTable[I][0],"quote("+quote_input_form_quote_list(BindTable[I][1])+")"]]);
}
dprint0("tr_apply_function0: "); dprint(B);
N = length(Q)-1; /* $B0z?t$N?t(B */
M = length(B); /* binding table $B$N%5%$%:(B */
R = Q[0]+"(";
for (I=0; I<N; I++) {
X = rtostr(Q[I+1]); /* $BJQ?t(B */
/* binding Table $B$r%5!<%A(B */
for (J=0; J<M; J++) {
Y = rtostr(B[J][0]);
if (X == Y) {
R = R+B[J][1];
if (I != N-1) R = R+",";
break;
}
if (J == M-1) error("No binding data.");
}
}
R = R+")";
dprint0("R="); dprint(R);
return eval_str(R);
}
/* L $B$,:85,B'(B. R $B$,1&5,B'(B. $BI}M%@hC5:w(B.
$B=q$-49$($r$9$k$?$a$N%H%C%W%l%Y%k$N4X?t(B ($B$N$R$H$D(B).
$BNc(B:
tr_apply_rule1(quote(1+sin(3*@pi)*sin(@pi/2)),
quote(sin(pn("x")*@pi)),
["qt_sin_int","x"]);
*/
def tr_apply_rule1(Obj,L,R) {
dprint("-------- start of tr_apply_rule1 ------------ ");
Obj = quotetolist(Obj);
L = quotetolist(L);
R = tr_rp(Obj,L,R);
RR = "quote("+quote_input_form_quote_list(R)+")";
dprint("-------- end of tr_apply_rule1 ------------ ");
return eval_str(RR);
}
/* quote $B$KBP$9$k(B $B=R8l(B */
def qt_is_integer(Qlist) {
if (Qlist[0] == "internal") {
Z = eval_str(rtostr(Qlist[1]));
}else{
return 0;
}
if (type(Z) == 0) return 1;
if ((type(Z) == 1) && (ntype(Z) == 0)) return 1;
return 0;
}
/* quote $B$N@8@.(B */
/* $B1&5,B'4X?t(B. 0 $B$rLa$9(B. */
def qt_zero() {
return quotetolist(quote(0));
}
/* $B1&5,B'4X?t(B. $B91Ey<0(B */
def qt_id(X) {
return quotetolist(X);
}
/* ------------ test --------------------------- */
extern Rule_test2$
Rule_test2=[quote(sin(pn("x")*@pi)),["qt_sin_int2","x"]]$
def test2() {
/* $BI}M%@hC5:w$N>l9g(B, R0 $B$O(B simplify $B$G$-$:(B. */
Rule1=[quote(sin(pn("x")*@pi)),["qt_sin_int","x"]];
R0 = quote(1+sin(sin(2*@pi)*@pi)*sin(@pi/2));
print(print_input_form(R0));
R=tr_apply_rule1(R0,Rule1[0],Rule1[1]);
print(print_input_form(R));
print("-----------------------");
/* $B<!$N$h$&$K=q$/$H?<$5M%@h$G=q$1$k(B */
R0 = quote(1+sin(sin(2*@pi)*@pi)*sin(@pi/2));
print(print_input_form(R0));
R=tr_apply_rule1(R0,Rule_test2[0],Rule_test2[1]);
print(print_input_form(R));
}
/* $B1&5,B'4X?t(B. sin($B@0?t(B*@pi) $B$r(B 0 $B$K(B */
def qt_sin_int(X) {
/* $B$$$^(B X $B$O(B quote $B7?(B */
Y = quotetolist(X);
/* Todo: $B$3$N$h$&$J$b$N$r:n$k5!G=$OAH$_9~$_$GM_$7$$(B. */
R = "quote(sin("+quote_input_form_quote_list(Y)+"*@pi))";
print(R);
R = eval_str(R);
/* Todo: X $B$,(B $B?t;z$+$I$&$+D4$Y$k5!G=$bAH$_9~$_$GM_$7$$(B.
*/
if (Y[0] == "internal") {
Z = eval_str(rtostr(Y[1]));
}else{
return quotetolist(R);
}
if (type(Z) == 0) return quotetolist(quote(0));
if ((type(Z) == 1) && (ntype(Z) == 0)) return quotetolist(quote(0));
return quotetolist(R);
}
/* $B1&5,B'4X?t(B. sin($B@0?t(B*@pi) $B$r(B 0 $B$K(B. $B?<$5M%@hMQ(B */
def qt_sin_int2(X) {
/* tr_apply_rule1 $B$r:F5"E*$K$h$V(B. $B$3$NJ}K!$G9=J82r@O$b$+$1$k(B. */
X = tr_apply_rule1(X,Rule_test2[0],Rule_test2[1]);
Y = quotetolist(X);
R = "quote(sin("+quote_input_form_quote_list(Y)+"*@pi))";
print(R);
R = eval_str(R);
if (qt_is_integer(Y)) return quotetolist(quote(0));
else return quotetolist(R);
}
/* $B$3$l$i0J30$N%F%9%H%W%m%0%i%`$O(B test1-tr.rr $B$r(B
*/
end$