19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

37.3 Operators and <strong>The</strong>ir Properties<br />

For some types, such as the integers Z or the rationals Q , results are always exact, and algebraic properties can be<br />

expected to be obeyed exactly. For other types, such as floating-point numbers, results are not always numerically<br />

exact, and algebraic properties can be expected to be obeyed only approximately. For example, given three floatingpoint<br />

values a and b and c , it may well be that a + (b + c) is not equal to (a + b) + c ; but we would expect their<br />

values to be reasonably close—unless, of course, overflow occurred in one expression but not the other.<br />

In order to address the difficulties of such approximate computation, many of the traits described in this section come<br />

in two varieties: approximate and exact. <strong>The</strong> + operator on integers or rationals is correctly described by the trait<br />

Associative, and the + operator on floating-point numbers is correctly described by the trait ApproximatelyAssociative.<br />

An important distinction is that the predicate used to test acceptability of exact algebraic properties is = , which is<br />

required to be an equivalence relation and therefore transitive, but a type-dependent binary predicate (typically ≈ )<br />

may be used to test acceptability of approximate algebraic properties, and this predicate is required only to be reflexive<br />

and symmetric.<br />

trait UnaryOperatorT extends UnaryOperatorT, ⊙,opr ⊙<br />

opr ⊙(self): T<br />

end<br />

A unary operator is a prefix operator that takes one argument and returns a value of the same type. Note that ⊙ is a<br />

static parameter, used here as a “variable” name for an operator.<br />

trait BinaryOperatorT extends BinaryOperatorT, ⊙,opr ⊙<br />

opr ⊙(self,other: T): T<br />

end<br />

A binary operator is an infix operator that takes two arguments of the same type and returns a value of that type. Thus,<br />

for example, any trait T that extends BinaryPredicateT, + necessarily has an infix method for the operator + ,<br />

and that operator takes two operands of type T and returns a value of type T .<br />

trait IdentityOperatorT extends IdentityOperatorT<br />

extends { UnaryOperatorT,IDENTITY }<br />

opr IDENTITY(self):T = self<br />

property ∀(a:T) (IDENTITY a) = a<br />

end<br />

<strong>The</strong> trait IdentityOperator provides a definition of the unary IDENTITY operator, which simply returns its argument.<br />

<strong>The</strong> trait Object extends IdentityOperatorObject , so the IDENTITY operator is defined for every type whatsoever.<br />

(This operator may not be terribly useful for applications programming, but it has technical uses for specifying<br />

contracts and algebraic properties in libraries. It is used, for example, when defining the trait BooleanAlgebra in<br />

terms of the trait Ring: because every value is its own inverse with respect to the “exclusive OR” operator in a Boolean<br />

Algebra, IDENTITY is the appropriate additive inverse operator for use with the trait Ring in this connection.)<br />

trait ApproximatelyCommutativeT extends ApproximatelyCommutativeT, ⊙, ≈,opr ⊙,opr ≈<br />

extends { BinaryOperatorT, ⊙, ReflexiveT, ≈, SymmetricT, ≈ }<br />

property ∀(a:T, b: T) (a ⊙ b) :≈: (b ⊙ a)<br />

end<br />

<strong>The</strong> trait ApproximatelyCommutative requires the operator ⊙ to be approximately commutative; that is, reversing<br />

the operands produces a result that is considered to be “close enough” as determined by the specified ≈ predicate.<br />

trait CommutativeT extends CommutativeT, ⊙,opr ⊙<br />

extends { ApproximatelyCommutativeT, ⊙, =, EquivalenceRelationT, = }<br />

end<br />

252

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!