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.

a new coercion in a subexpression—but this is exactly the desired effect. Once this is done, then the subexpressions<br />

of the functional call are processed recursively.<br />

This process is general enough not only to provide for widening floating-point precision, but to handle two other<br />

cases of interest as well: widening point computations to interval computations, and widening computations involving<br />

aggregate data structures whose components are widenable. For example, in d(v × w) suppose that d is of type R64<br />

and v and w are 3-vectors with components of type R32; this strategy is capable of promoting v and w to 3-vectors<br />

with components of type R64 and then performing all computations with R64 precision.<br />

Let’s go through the example of c + a · b in detail. <strong>The</strong> relevant declarations might look something like this:<br />

trait R32<br />

. . .<br />

end<br />

trait R64<br />

widening coercion (x : R32) = . . .<br />

. . .<br />

end<br />

opr · (l: R32, r: R32): R32 = . . . (∗ 1 ∗)<br />

opr · (l: R64, r: R64): R64 = . . . (∗ 2 ∗)<br />

opr +(l: R32, r: R32): R32 = . . .(∗ 3 ∗)<br />

opr +(l: R64, r: R64): R64 = . . .(∗ 4 ∗)<br />

<strong>The</strong> bottom-up analysis observes that a and b are both of type R32 , and discovers that declarations 1 and 2 are<br />

both applicable to the product, but declaration 1 would be chosen because it requires no coercion and declaration<br />

2 would require coercion of both arguments to type R64 . <strong>The</strong> analysis then observes that c has type R64 and the<br />

product expression has type R32 , so declaration 3 is not applicable but declaration 4 is applicable (though requiring<br />

a coercion from R32 to R64 for its second argument).<br />

Now the top-down widening analysis is performed. <strong>The</strong> product is a functional call that is an argument to another<br />

functional call, and its result requires coercion, and that coercion is a widening coercion. <strong>The</strong>refore the compiler<br />

reconsiders the applicable declarations, which were declarations 1 and 2 . <strong>The</strong> result type of declaration 1 is not<br />

a subtype of R64 , but the result type of declaration 2 is a subtype of R64 (in fact, it is R64 ). Declaration 2 is<br />

the most specific among the applicable declarations with that property (in fact, in this example it’s the only one), so<br />

declaration 2 replaces declaration 1 for the product invocation. This in turn requires a and b to be coerced from<br />

type R32 to R64 before the product is performed.<br />

Widening is a tricky business, best left to expert library designers. When used judiciously, it can greatly improve the<br />

precision of numerical computations without requiring a great deal of thought on the part of the application programmer<br />

and without cluttering up code with explicit conversions.<br />

Future versions of this specification will include tables of coercions and widening coercions supported by the <strong>Fortress</strong><br />

standard libraries.<br />

145

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

Saved successfully!

Ooh no, something went wrong!