#include "algebra" macro Z == Integer; Utils == add { import from Z; pi: Fraction(Z) == 355/113; fib(n:Z): Z == ................ factorial(n:Z): Z == ................ ............................................ }
%
''
refers to the domain being computed.
Rep
is defined to give a representation for %
.
rep
and per
are type conversions:
rep: % -> Rep per: Rep -> %
Complex == add { R == DoubleFloat; Rep == Record(real: R, imag: R); import from Rep, R; real(u: %): R == rep(u).real; imag(u: %): R == rep(u).imag; complex(a: R, b: R): % == per [a, b]; (u: %) + (v: %): % == complex(real u + real v, imag u + imag v); (k: R) * (u: %): % == complex(k * real u, k * imag u); abs(u: %): R == sqrt(real(u)^2 + imag(u)^2); ... }
add
operator may be used to build on an existing domain.
Polygon == add { Rep == List Complex; new(l: List Complex): % == per l; vertex(p: %, i: Integer): Complex == rep(p).i; } Square == Polygon add { area(s: %): DoubleFloat == { s1 := vertex(s,1) - vertex(s,0); s2 := vertex(s,2) - vertex(s,0); abs(vectorial(s1,s2)); } }