10 MyAdditiveType

Type Constructor

MyAdditiveType

Usage

define MyGroup: Category == Join(MyAdditiveType, ...) with {...}
MyFloat: Join(MyAdditiveType, ...) with {...} == add {...}

Description

The category of types with addition and subtraction operations.

MyAdditiveType is the category of types that provide operations that can be performed in a group.

Remarks

MyAdditiveType provides only the signatures of group operations. There is no statement made about which axioms these operations have to fulfil. MyAdditiveType and MyArithmeticType are useful to provide a ring signatures to domains like SingleFloat, DoubleFloat, and similar types that in many respect behave like rings but are by their imprecise nature not mathematical rings themselves.

60cat: MyAdditiveType 60  (58b)
define MyAdditiveType: Category == with {
        exports: MyAdditiveType 61
    default {
        defaults: MyAdditiveType 63b
    }
}

Defines:
MyAdditiveType, used in chunk 74.

Exports of MyAdditiveType

MyPrimitiveType;

0: % Returns the 0 constant of the type.

zero?: % -> Boolean Tests whether its argument is 0.

+: (%, %) -> % Returns the sum of its arguments.

-: (%, %) -> % Returns the difference of its arguments.

-: % -> % Returns the negation of its argument.

add!: (%, %) -> % Returns the sum of its arguments.

minus!: (%, %) -> % Returns the difference of its arguments.

minus!: % -> % Returns the negation of its argument.

The category MyAdditiveType also exports equality and inequality testing.

61exports: MyAdditiveType 61  (60)  62
MyPrimitiveType;

Uses MyPrimitiveType 48.

Export of MyAdditiveType

0: %

Usage

if x = 0 then ...
x := 0

Description

Returns the 0 constant of the type.

See also

zero?

62exports: MyAdditiveType 61+   (60)  61  63a
0: %;

Export of MyAdditiveType

zero?: % -> Boolean

Usage

if zero? x then ...

Parameters

x: %

Element to be tested.

Description

Tests whether its argument is 0.

The boolean value of zero? x is identical to the test x = 0 in the type, but might be implemented more efficiently.

Remarks

Note that one would usually not use this function on floating point types. A zero test might fail because of small errors introduced during a computation.

See also

0

63aexports: MyAdditiveType 61+   (60)  62  64
zero?: % -> Boolean;
63bdefaults: MyAdditiveType 63b  (60)  65b
zero?(x: %): Boolean == x = 0;

Export of MyAdditiveType

+: (%, %) -> %

Usage

z := x + y

Parameters

x: %

y: %

Elements to be added.

Description

Returns the sum of its arguments.

See also

add! minus!

64exports: MyAdditiveType 61+   (60)  63a  65a
+: (%, %) -> %;

Export of MyAdditiveType

-: (%, %) -> %

Usage

z := x - y

Parameters

x: %

y: %

Elements to be subtracted.

Description

Returns the difference of its arguments.

See also

add! minus!

65aexports: MyAdditiveType 61+   (60)  64  66
-: (%, %) -> %;
65bdefaults: MyAdditiveType 63b+   (60)  63b  68b
(x: %) - (y: %): % == x + (-y);

Export of MyAdditiveType

-: % -> %

Usage

z := - x

Parameters

x: %

Element to be negated.

Description

Returns the negation of its argument.

See also

add! minus!

66exports: MyAdditiveType 61+   (60)  65a  68a
-: % -> %;

Export of MyAdditiveType

add!: (%, %) -> %

Usage

z := add!(y, z);
z := 0;
for x in somelist repeat z := add!(z, x);

Parameters

x: %

y: %

z: %

Elements to be added.

Description

Returns the sum of its arguments.

Remarks

The function add! is allowed to work destructively on its first argument, i. e., the storage used by y is allowed to be destroyed or reused, so y is lost after this call.

Another destructive operation (provided by the specific type that implements MyAdditiveType) which operates on z is not allowed to have any destructive influence on the x values.

An implementation of this function should take care of constant values of the type appearing in the first argument. In particular, add!(0, x) should not work destructively on the constant 0.

Furthermore, it is allowed to call add!(x, x) to obtain 2x. Implementation of this function should start with

if (x pretend Pointer) = (y pretend Pointer) then ...

or appropriate code to check whether the input elements are identical. For other cases where x shares memory with y, the outcome of this function is not defined.

See also

+ minus!

68aexports: MyAdditiveType 61+   (60)  66  69a
add!: (%, %) -> %;

The implementation of + will most probably simply return y for 0 + y. So if we just say

add!(x: %, y: %): % == x + y;

and foo!: % -> % were some function that destructively modifies its argument, then

foo!(add!(0, y))

would indirectly modify the second argument of add! which is not allowed.
68bdefaults: MyAdditiveType 63b+   (60)  65b  69b
local copy?: Boolean == % has CopyableType;
add!(x: %, y: %): % == {
        zero? x => {
                copy? => copy(y) $ (% pretend CopyableType);
                y
        }
        x + y;
}

Export of MyAdditiveType

minus!: (%, %) -> %

Usage

z := minus!(y, z);
z := 0;
for x in somelist repeat z := minus!(z, x);

Parameters

x: %

y: %

z: %

Elements to be added.

Description

Returns the difference of its arguments.

Remarks

See add!.

See also

+ - add!

69aexports: MyAdditiveType 61+   (60)  68a  71a
minus!: (%, %) -> %;
69bdefaults: MyAdditiveType 63b+   (60)  68b  71b
minus!(x: %, y: %): % == x - y;

Export of MyAdditiveType

minus!: % -> %

Usage

z := minus! x

Parameters

x: %

Element to be negated.

Description

Returns the negation of its argument.

Remarks

The storage of x may be destroyed or reused, so x is lost after the call.

See also

add! -

71aexports: MyAdditiveType 61+   (60)  69a
minus!: % -> %;
71bdefaults: MyAdditiveType 63b+   (60)  69b
minus!(x: %): % == -x;