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.

161cat: MyAdditiveType 161  (159b)
define MyAdditiveType: Category == with {
        exports: MyAdditiveType 162
    default {
        defaults: MyAdditiveType 164b
    }
}

Defines:
MyAdditiveType, used in chunk 175.

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.

162exports: MyAdditiveType 162  (161)  163
MyPrimitiveType;

Uses MyPrimitiveType 149.

Export of MyAdditiveType

0: %

Usage

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

Description

Returns the 0 constant of the type.

See also

zero?

163exports: MyAdditiveType 162+   (161)  162  164a
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

164aexports: MyAdditiveType 162+   (161)  163  165
zero?: % -> Boolean;
164bdefaults: MyAdditiveType 164b  (161)  166b
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!

165exports: MyAdditiveType 162+   (161)  164a  166a
+: (%, %) -> %;

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!

166aexports: MyAdditiveType 162+   (161)  165  167
-: (%, %) -> %;
166bdefaults: MyAdditiveType 164b+   (161)  164b  169b
(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!

167exports: MyAdditiveType 162+   (161)  166a  169a
-: % -> %;

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!

169aexports: MyAdditiveType 162+   (161)  167  170a
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.
169bdefaults: MyAdditiveType 164b+   (161)  166b  170b
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!

170aexports: MyAdditiveType 162+   (161)  169a  172a
minus!: (%, %) -> %;
170bdefaults: MyAdditiveType 164b+   (161)  169b  172b
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! -

172aexports: MyAdditiveType 162+   (161)  170a
minus!: % -> %;
172bdefaults: MyAdditiveType 164b+   (161)  170b
minus!(x: %): % == -x;