[bitc-dev] Type compatibility at application

Swaroop Sridhar swaroop at cs.jhu.edu
Fri May 19 14:21:36 EDT 2006


Jonathan S. Shapiro wrote:
> On Fri, 2006-05-19 at 12:53 -0400, Swaroop Sridhar wrote:
> 
>>Jonathan S. Shapiro wrote:
>>
>>>The reason I ask is that I am concerned about the number of cases that
>>>may require polyinstantiation due to mutability. I do not have any
>>>intuition about how important this is.
>>
>>Why does this require polyinstantiation? The type of the procedure never 
>>changes. The type of the copy of arguments is exactly equal to that 
>>expected by the function call.
> 
> 
> Think about (vector T) vs. (vector (mutable T))

OK. Are you thinking of a case like:

(define (f x:(vector 'a) #t)

(define vec1:(vector int32)  (vector 1 2 3))
(define vec2:(vector (mutable int32)) (vector 1 2 3 ))

(define vecApp
    (begin
	(f vec1)
	(f vec2)
)


This will certainly cause polyinstantiation of f. This is not because of 
the type-compatibility rule, but due to the universal compatibility of 
polymorphism. I agree that this is an issue. This can even happen with:

(define vec1:(vector int32)  (vector 1 2 3))
(define vec2:(mutable (vector int32)) (vector 1 2 3 ))

(define vecApp
    (begin
	(f vec1)
	(f vec2)
)

The solution may be an optimization that the polyinstantiator can 
perform -- if there are two instantiations that only differ in the 
mutability of arguments, only one of them needs to be emitted during 
code generation.

I initially thought that you were talking about a case such as:

(define (f x:(vector int32)) #t)
f: (fn ((vector int32)) bool)

(define vec:(vector (mutable int32)) (vector 1 2 3)
vec: (vector (mutable int32))

(define vecApp (f vec))

Here, f is not polyinstantiated.

In the definition
(define vecApp (f vec))

the use of f in vecApp is given the type:
use_f: (fn ((vector int32)) bool)

The use of vec (actual argument) in vecApp is given the type:
use_vec: (vector (mutable int32))

The copy of vec, (the formal argument) is given the type (this 
mutability adjustment is currently not done in a deep manner, but 
assuming this):
copy_vec: (vector (mutable int32))

and then adjusted wrt the function arguments as:
copy_vec: (vector int32)

and the application happens as:

(use_f:(fn ((vector int32)) bool) copy_vec:(vector int32))

Polyinstantiation only takes place if the type of use_f is different 
from f, as was the case in the first example:

f: (fn ((vector 'a)) bool)
(use_f1:(fn ((vector int32)) bool) use_vec1:(vector int32))
(use_f2:(fn ((vector (mutable int32))) bool)
                         use_vec2:(mutable (vector int32)))


Swaroop.



More information about the bitc-dev mailing list