-- This is written in Haskell.
module MarkerType where
	{
	-- Booleans expressed as Haskell types.
	data True;
	data False;
	
	class Is a;
	instance Is True;

	class Isnt a;
	instance Isnt False;

	class EitherIs a b;
	instance EitherIs True b;
	instance EitherIs False True;

	class BothAre a b;
	instance BothAre True True;
	
	class Not a b | a -> b;
	instance Not True False;
	instance Not False True;
	
	class Or a b c | a b -> c;
	instance Or True b True;
	instance Or False b b;
	
	class And a b c | a b -> c;
	instance And True b b;
	instance And False b False;

	{--
	Integers expressed as Haskell types. Zero is 0.
	If type a is Zero or positive, then (Succ a) is positive;
	if type a is Zero or negative, then (Pred a) is negative.
	This is very crude and should be replaced with sign-magnitude 
	representation.
	--}

	data Zero;
	data Succ n;
	data Pred n;

	type One = Succ Zero;
	type Two = Succ One;
	type Three = Succ Two;
	type MinusOne = Pred Zero;
	type MinusTwo = Pred MinusOne;
	type MinusThree = Pred MinusTwo;

	class Successor a b | a -> b;
	instance Successor Zero One;
	instance Successor (Succ a) (Succ (Succ a));
	instance Successor (Pred a) a;

	class Predecessor a b | a -> b;
	instance Predecessor Zero MinusOne;
	instance Predecessor (Succ a) a;
	instance Predecessor (Pred a) (Pred (Pred a));

	class AtLeast a b; -- a >= b
	instance AtLeast Zero Zero;
	instance AtLeast (Succ a) Zero;
	instance AtLeast Zero (Pred b);
	instance AtLeast (Succ a) (Pred b);
	instance (AtLeast a b) => AtLeast (Succ a) (Succ b);
	instance (AtLeast a b) => AtLeast (Pred a) (Pred b);

	class Natural a;
	instance Natural Zero;
	instance Natural (Succ a);

	class AtLeastOne a;
	instance (Natural a) => AtLeastOne (Succ a);

	class Even a;
	instance Even Zero;
	instance (Even a) => Even (Succ (Succ a));
	instance (Even a) => Even (Pred (Pred a));

	-- Integer addition. Both operands are either positive, negative
	-- or zero, so we consider 3*3=9 cases.
	class Add a b c | a b -> c;

	instance Add Zero Zero Zero;

	instance Add Zero (Succ b) (Succ b);
	instance Add Zero (Pred b) (Pred b);

	instance Add (Succ b) Zero (Succ b);
	instance Add (Pred b) Zero (Pred b);

	instance Add a b c => Add (Succ a) (Succ b) (Succ (Succ c));
	instance Add a b c => Add (Pred a) (Pred b) (Pred (Pred c));

	instance Add a b c => Add (Succ a) (Pred b) c;
	instance Add a b c => Add (Pred a) (Succ b) c;

	-- Subtraction is very similar to addition.
	class Sub a b c | a b -> c;

	instance Sub Zero Zero Zero;

	instance Sub Zero (Succ b) (Pred b);
	instance Sub Zero (Pred b) (Succ b);

	instance Sub (Succ b) Zero (Succ b);
	instance Sub (Pred b) Zero (Pred b);

	instance Sub a b c => Sub (Succ a) (Succ b) c;
	instance Sub a b c => Sub (Pred a) (Pred b) c;

	instance Sub a b c => Sub (Succ a) (Pred b) (Succ (Succ c));
	instance Sub a b c => Sub (Pred a) (Succ b) (Pred (Pred c));
	}
