20 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.

165cat: MyAdditiveType 165  (163b)
define MyAdditiveType: Category == with {
        exports: MyAdditiveType 166
    default {
        defaults: MyAdditiveType 168b
    }
}

Defines:
MyAdditiveType, used in chunk 179.

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.

166exports: MyAdditiveType 166  (165)  167
MyPrimitiveType;

Uses MyPrimitiveType 152.

Export of MyAdditiveType

0: %

Usage

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

Description

Returns the 0 constant of the type.

See also

zero?

167exports: MyAdditiveType 166+   (165)  166  168a
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

168aexports: MyAdditiveType 166+   (165)  167  169
zero?: % -> Boolean;
168bdefaults: MyAdditiveType 168b  (165)  170b
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!

169exports: MyAdditiveType 166+   (165)  168a  170a
+: (%, %) -> %;

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!

170aexports: MyAdditiveType 166+   (165)  169  171
-: (%, %) -> %;
170bdefaults: MyAdditiveType 168b+   (165)  168b  173b
(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!

171exports: MyAdditiveType 166+   (165)  170a  173a
-: % -> %;

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!

173aexports: MyAdditiveType 166+   (165)  171  174a
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.
173bdefaults: MyAdditiveType 168b+   (165)  170b  174b
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!

174aexports: MyAdditiveType 166+   (165)  173a  176a
minus!: (%, %) -> %;
174bdefaults: MyAdditiveType 168b+   (165)  173b  176b
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! -

176aexports: MyAdditiveType 166+   (165)  174a
minus!: % -> %;
176bdefaults: MyAdditiveType 168b+   (165)  174b
minus!(x: %): % == -x;