ntop[0] := 0 ntop[x_] := s[ntop[x - 1]] pton[0] := 0 pton[s[x_]] := pton[x] + 1 padd[0, x_] := x padd[s[x_], y_] := s[padd[x, y]] pmul[0,_] := 0 pmul[s[y_], x_] := padd[pmul[y, x], x] psub[0, _] := 0 psub[x_, 0] := x psub[s[x_], s[y_]] := psub[x, y] bar[pp[0, x_]] := pp[0, x] bar[pp[x_, 0]] := pp[x, 0] bar[pp[s[x_], s[y_]]] := bar[pp[x, y]] ntoz[z_] := pp[ntop[z], 0] /; z >= 0 ntoz[z_] := pp[0, ntop[-z]] /; z < 0 zton[pp[0, x_]] := -pton[x] zton[pp[x_, 0]] := pton[x] zton[x_] := zton[bar[x]] pifz[0, x_, _] := x pifz[_, _, y_] := y pgt[x_, y_] := pifz[psub[x, y], y, x] plt[x_, y_] := pifz[psub[x, y], x, y] pgcm[0, x_] := x pgcm[x_, y_] := pgcm[psub[pgt[x, y], plt[x, y]], plt[x, y]] pdiv[0, _] := 0 pdiv[x_, y_] := pifz[psub[s[x],y],0,s[pdiv[psub[x, y], y]]] prem[x_, y_] := psub[x, pmul[y, pdiv[x, y]]] zminus[pp[x_,y_]] := pp[y,x] zabs[pp[0,x_]] := pp[x,0] zabs[z_] := z ztop[pp[x_,0]] := x ztop[pp[0,y_]] := y ptoz[x_] := pp[x,0] zadd[pp[x,y],pp[u,v]] := bar[pp[padd[x,u],padd[y,v]]] zsub[x_,y_] := zadd[x,zminus[y]] zmul[pp[x_,y_],pp[u_,v_]] := pp[padd[pmul[x,u],pmul[y,v]],padd[pmul[x,v],pmul[y,u]]] zpmul[pp[x_,y_],z_] := pp[pmul[x,z],pmul[y,z]] zpdiv[pp[x_,y_],z_] := pp[pdiv[x,z],pdiv[y,z]] qbar[qq[x_,y_]] := qq[zpdiv[x,pgcm[ztop[x],y]],pdiv[y,pgcm[ztop[x],y]]] ntoq[{x_,y_}] := qbar[qq[ntoz[x],ntop[y]]] qton[qq[x_,y_]] := {zton[x],pton[y]} qmul[qq[x_,y_],qq[u_,v_]] := qbar[qq[zmul[x,u],pmul[y,v]]] qadd[qq[x_,y_],qq[u_,v_]] := qbar[qq[zadd[zpmul[x,v],zpmul[u,y]],pmul[y,v]]]