# A sample revised prelude for numeric classes

Ashley Yakeley ashley@semantic.org
Sun, 11 Feb 2001 22:16:02 -0800

```At 2001-02-11 21:18, Tom Pledger wrote:

>The main complication is that the type system needs to deal with
>integer exponents of dimensions, if it's to do the job well.

Very occasionally non-integer or 'fractal' exponents of dimensions are
useful. For instance, geographic coastlines can be measured in km ^ n,
where 1 <= n < 2. This doesn't stop the CIA world factbook listing all
coastline lengths in straight kilometres, however.

More unit weirdness occurs with logarithms. For instance, if y and x are
distances, log (y/x) = log y - log x. Note that 'log x' is some number +
log (metre). Strange, huh?

Interestingly, in C++ you can parameterise types by values. For instance:

--
// Mass, Length and Time
template <long M,long L,long T>
class Unit
{
public:
double mValue;

inline explicit Unit(double value)
{
mValue = value;
}
};

template <long M,long L,long T>
Unit<M,L,T> operator + (Unit<M,L,T> a,Unit<M,L,T> b)
{
return Unit<M,L,T>(a.mValue + b.mValue);
}

template <long Ma,long La,long Ta,long Mb,long Lb,long Tb>
Unit<Ma+Mb,La+Lb,Ta+Tb> operator * (Unit<Ma,La,Ta> a,Unit<Mb,Lb,Tb> b)
{
return Unit<Ma+Mb,La+Lb,Ta+Tb>(a.mValue * b.mValue);
}

// etc.

int main()
{
Unit<0,1,0> oneMetre(1);
Unit<0,1,0> twoMetres = oneMetre + oneMetre;
Unit<0,2,0> oneSquareMetre = oneMetre * oneMetre;
}
--

Can you do this sort of thing in Haskell?

--
Ashley Yakeley, Seattle WA

```