19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20<br />

Type Inference<br />

Type inference in <strong>Fortress</strong> is performed independently on each simple component (described in Chapter 22). For<br />

separation of concerns, in this chapter, we describe the <strong>Fortress</strong> type inference as a procedure performed over a whole<br />

<strong>Fortress</strong> program. We explain how this procedure can be adapted to perform type inference over a simple program<br />

component in Section 22.5. Note that type inference cannot be performed on each functional (function or method)<br />

declaration in isolation because it may be declared mutually recursively or may contain free variables.<br />

20.1 What Is Inferred<br />

Types of functional parameters, functional results, and variables may be elided in a program where they can be inferred<br />

automatically. Similarly, instantiations of static parameters of generic functional invocations may be elided where they<br />

can be inferred automatically. A <strong>Fortress</strong> compiler must allow types and static arguments inferable via the procedure<br />

described in Section 20.2 to be elided, no more and no less. This strict requirement is made for the sake of source-code<br />

portability; it is important that a program that compiles on one compiler will compile on all compilers. Of course,<br />

there is nothing preventing a development environment from aiding programmers by performing more sophisticated<br />

analyses and filling in additional information, but the text produced is not a valid <strong>Fortress</strong> program unless all elided<br />

types and static arguments can be inferred via the described procedure.<br />

20.2 Type Inference Procedure<br />

To perform type inference, we first infer any elided parameter type in each functional declaration that can be inferred<br />

from other declaration as follows:<br />

1. If the declaration is a functional declaration and the type of the declared functional is declared via a separate<br />

declaration, any elided parameter type is inferred to have the type provided by the separate declaration.<br />

2. Otherwise, if the declaration is a method declaration that overloads a method declaration provided by a supertrait,<br />

any elided parameter type is inferred to have the type provided (or inferred) by the overloaded method<br />

declaration.<br />

All remaining parameter types are inferred along with instantiations of static parameters.<br />

In the following, we adopt the convention of writing “primed” static variables, T ′ 0, ..., T ′ m, U ′ 0, ..., to stand for fresh<br />

static variables. Our procedure will introduce primed static variables as placeholders that are to be replaced with<br />

153

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

Saved successfully!

Ooh no, something went wrong!