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.

arguments nor with variable number of arguments (as described in Section 15.4). If one of the expanded declarations<br />

is applicable with coercion to a call, then the original declaration (with keyword or varargs parameters) is applicable<br />

with coercion to the call.<br />

17.5 Coercion Resolution<br />

For a given functional call, we first determine whether there exists a declaration that is applicable without coercion.<br />

If so, the most specific declaration is selected; if not, then coercions are explicitly added to the functional call. If<br />

more than one coercion is possible then the coercion that yields the most specific type is chosen. Section 17.6 and<br />

Chapter 33 describe restrictions on the declarations of coercions and overloaded functionals that guarantee there exists<br />

always a most specific coercion when two or more are possible.<br />

We first define a notion of more specific types in the presence of coercion. Recall from Section 8.1 that <strong>Fortress</strong> defines<br />

an exclusion relation between types which is denoted by ♦.<br />

For types T and U, we say that T rejects U, and write T −〉 U, if for all coercions to T , the type coerced from excludes<br />

U:<br />

T −〉 U ⇐⇒ ∀A : A→T =⇒ A ♦ U.<br />

Note that T −〉 U implies U ̸T .<br />

We say that T is no less specific than U, and write T U, if T is a subtype of U or T excludes, can be coerced to,<br />

and rejects U:<br />

T U ⇐⇒ T ≼ U ∨ (T ♦ U ∧ T U ∧ T −〉 U).<br />

<strong>The</strong> relation is reflexive and antisymmetric but not necessarily transitive. It is possible, for example, for three types,<br />

T , U and V , each excluding the other two, to have coercions from T to U and from U to V . In this case, T U and<br />

U V but T ̸ V .<br />

Notice that if two tuple types have a bijective correspondence between their element types then the relationship<br />

between the tuple types is equivalent to applying the relation elementwise.<br />

We say that T is more specific than U, and write T ⊳ U, if T U ∧ T ≠ U.<br />

We extend the definitions of no less specific and more specific to declarations. We say that a declaration f (P) is no<br />

less specific than a declaration f (Q) if P Q. Similarly, we say that a declaration P 0 .f (P) is no less specific than<br />

a declaration Q 0 .f (Q) if (P 0 ,P) (Q 0 ,Q). A declaration f (P) is more specific than a declaration f (Q) if P ⊳ Q.<br />

Similarly, a declaration P 0 .f (P) is more specific than a declaration Q 0 .f (Q) if (P 0 ,P) ⊳ (Q 0 ,Q).<br />

Now we describe how to determine which coercion is applied to a given call in terms of rewriting functional calls. <strong>The</strong><br />

rewriting is for pedagogical purposes; implementation techniques may vary.<br />

Consider a static call f (A) or A 0 .f (A). Let Σ be the set of parameter types of functional declarations of f that are<br />

applicable to the call. Let Σ ′ be the set of parameter types of functional declarations of f that are applicable with<br />

coercion to the call. If Σ is not empty then we use the overloading resolution described in Section 15.5 to determine<br />

which element of Σ is called. If Σ is empty but Σ ′ is not, then we rewrite the call as follows. Define:<br />

C = {T ∈ Σ ′ | S →T ∧ A ≼ S}.<br />

Let T ∈ C be the most specific element of C. In other words, there does not exist T ′ ∈ C such that T ′ ⊳ T . <strong>The</strong><br />

restrictions given in Section 17.6 and Chapter 33 guarantee that such a T exists and that it is unique. <strong>The</strong> call f (A) or<br />

A 0 .f (A) is rewritten to f(T. coercion (A)) or A 0 .f(T. coercion (A)) and the declaration with parameter type<br />

T is applied to the call.<br />

As an example, consider the following definitions:<br />

141

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

Saved successfully!

Ooh no, something went wrong!