-- This is written in Haskell.
module Dimension
	(plus,minus,times,divby,
	Dimensioned,
	Unit,Mass,Length,Time,
	unit,kilogramme,metre,second
	) where
	{
	import MarkerType;

	{--
	A Dimensioned type carries along exponents for three basic
	dimensions: mass, length, and time.
	--}

	newtype Dimensioned mass length time rep = MkDimensioned rep;

	-- Some handy abbreviations.
	type Unit rep	= Dimensioned Zero Zero Zero rep;
	type Mass rep	= Dimensioned One  Zero Zero rep;
	type Length rep	= Dimensioned Zero One  Zero rep;
	type Time rep	= Dimensioned Zero Zero One  rep;
	
	-- instance properties for Dimensioneds in general and Units
	-- in particular
	undimension1  f (MkDimensioned x) = f x;
	undimension1r f x = MkDimensioned (undimension1 f x);
	undimension2  f (MkDimensioned x) (MkDimensioned y) = f x y;
	undimension2r f x y = MkDimensioned (undimension2 f x y);

	instance (Eq rep) => Eq (Dimensioned mass length time rep) where
		{
		(==) = undimension2 (==);
		};

	instance (Ord rep) => Ord (Dimensioned mass length time rep) where
		{
		compare = undimension2 compare;
		};

	instance (Show rep) => Show (Unit rep) where
		{
		show = undimension1 show;
		};

	instance (Num rep) => Num (Unit rep) where
		{
		(+)			= undimension2r (+);
		(-)			= undimension2r (-);
		(*)			= undimension2r (*);
		negate		= undimension1r negate;
		abs			= undimension1r abs;
		signum		= undimension1r signum;
		fromInteger	= MkDimensioned . fromInteger;
		fromInt		= MkDimensioned . fromInt;
		};

	instance (Real rep) => Real (Unit rep) where
		{
		toRational = undimension1 toRational;
		};

	instance (Fractional rep) => Fractional (Unit rep) where
		{
		(/)				= undimension2r (/);
		recip			= undimension1r recip;
		fromRational	= MkDimensioned . fromRational;
		fromDouble		= MkDimensioned . fromDouble;
		};

	instance (Floating rep) => Floating (Unit rep) where
		{
		pi		=	MkDimensioned pi;
		exp		=	undimension1r exp;
		log		=	undimension1r log;
		sqrt	=	undimension1r sqrt;
		(**)	=	undimension2r (**);
		logBase	=	undimension2r logBase;
		sin		=	undimension1r sin;
		cos		=	undimension1r cos;
		tan		=	undimension1r tan;
		asin	=	undimension1r asin;
		acos	=	undimension1r acos;
		atan	=	undimension1r atan;
		sinh	=	undimension1r sinh;
		cosh	=	undimension1r cosh;
		tanh	=	undimension1r tanh;
		asinh	=	undimension1r asinh;
		acosh	=	undimension1r acosh;
		atanh	=	undimension1r atanh;
		};
	
	instance (RealFrac rep) => RealFrac (Unit rep) where
		{
		properFraction (MkDimensioned x) = (p,MkDimensioned q) where
			{
			(p,q) = properFraction x;
			};
		truncate		= undimension1 truncate;
		round			= undimension1 round;
		ceiling			= undimension1 ceiling;
		floor			= undimension1 floor;
		};
	
	instance (RealFloat rep) => RealFloat (Unit rep) where
		{
		floatRadix		= undimension1 floatRadix;
		floatDigits		= undimension1 floatDigits;
		floatRange		= undimension1 floatRange;
		decodeFloat		= undimension1 decodeFloat;
		encodeFloat x i	= MkDimensioned (encodeFloat x i);
		exponent		= undimension1 exponent;
		significand		= undimension1r significand;
		scaleFloat i	= undimension1r (scaleFloat i);
		isNaN			= undimension1 isNaN;
		isInfinite		= undimension1 isInfinite;
		isDenormalized	= undimension1 isDenormalized;
		isNegativeZero	= undimension1 isNegativeZero;
		isIEEE			= undimension1 isIEEE;
		atan2			= undimension2r atan2;
		};

	-- You can add or subtract only identically dimensioned quantities.
	plus, minus :: Num rep =>
		Dimensioned mass length time rep ->
		Dimensioned mass length time rep ->
		Dimensioned mass length time rep;
	plus  (MkDimensioned x) (MkDimensioned y) = MkDimensioned (x+y);
	minus (MkDimensioned x) (MkDimensioned y) = MkDimensioned (x-y);

	-- You can multiply any two dimensioned quantities; exponents
	-- from the operands add up in the result.
	times :: (Num rep,
		Add mass mass' mass'',
		Add length length' length'',
		Add time time' time'') =>
		Dimensioned mass length time rep ->
		Dimensioned mass' length' time' rep ->
		Dimensioned mass'' length'' time'' rep;
	times (MkDimensioned x) (MkDimensioned y) = MkDimensioned (x * y);

	-- Similarly for division, except exponents are subtracted.
	divby :: (Fractional rep,
		Sub mass mass' mass'',
		Sub length length' length'',
		Sub time time' time'') =>
		Dimensioned mass length time rep ->
		Dimensioned mass' length' time' rep ->
		Dimensioned mass'' length'' time'' rep;
	divby (MkDimensioned x) (MkDimensioned y) = MkDimensioned (x / y);

	-- base units as SI.
	kilogramme	:: (Num rep) => Mass rep;
	kilogramme	= (MkDimensioned 1);
	metre		:: (Num rep) => Length rep;
	metre		= (MkDimensioned 1);
	second		:: (Num rep) => Time rep;
	second		= (MkDimensioned 1);
	unit		:: (Num rep) => Unit rep;
	unit		= 1;
	}
