Top-down typing
Top-down typing of the model involves pushing types from enclosing expressions into their subexpressions.
This allows us to determine a real type for every type found to be bottom
during bottom-up type-checking.
For example, in the expression [1, <>]
, bottom-up typing would give:
1
is anint
<>
is anopt bottom
[1, <>]
: is anarray [int] of opt int
In top-down typing, we start from the outer-most expression and work inwards:
[1, <>]
: is anarray [int] of opt int
(found from bottom-up typing)1
: is anopt int
(because the element type of the enclosing array isopt int
)<>
: is anopt int
(because the element type of the enclosing array isopt int
)
This allows us to remove the bottom
from the MIR entirely.
For a declaration item, the top-down type of the outer-most RHS expression is taken to be the declared LHS type from the declaration.
For example, in tuple(opt int, float): x = (3, 5)
:
(3, 5)
is atuple(opt int, float)
(taken from LHS)3
is anopt int
(first field of tuple)5
is afloat
(second field of tuple)
The top-down type of a call argument is taken to be the declared type of the parameter.
For example:
function int: foo(opt int: x);
int: y = foo(3);
foo(3)
is anint
(taken from the LHS ofy
)3
is anopt int
(taken from the parameterx
of the functionfoo
)
Polymorphic types
For a polymorphic function which with a type-inst variable in the return type, we may need to use the return type to determine the type of the parameters:
For example;
function array [$X] of any $T: foo(array [$X] of any $T: x);
array [int] of int: y = foo([]);
foo([])
is anarray [int] of int
(taken from the LHS ofy
)- For this call, the return type
array [$X] of any $T
is thereforearray [int] of int
- So
$X = int
and$T = int
- Therefore the parameter
x
isarray [int] of int
- So
[]
is anarray [int] of int
(taken from the parameterx
of the functionfoo
)
Making coercion to option types explicit
Since option types are going to be transformed into tuples, we must keep track of values which are known to occur, but are used as optional values. These coercions must be made explicit since one the option types become tuples, the non-tuple occurring type will no longer be a subtype of the optional one.