Next: Categories Up: An introduction to the ALDOR Previous: Dependent Types

## Domains

• A domain exports a collection of related constants.
```#include "algebra"
macro Z == Integer;
Utils == add {
import from Z;
pi: Fraction(Z) == 355/113;
fib(n:Z): Z ==       ................
factorial(n:Z): Z ==       ................
............................................
}
```
• All constants defined within a domain are exported by default unless declared local.
• Constants exported by domains may be imported into a program's scope.
• Within a domain-valued expression, the name ```%`'' refers to the domain being computed.
• A type `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);
...
}
```
• The `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));
}
}
```

Next: Categories Up: An introduction to the ALDOR Previous: Dependent Types
Marc Moreno Maza
2003-06-06