On 8 July 2014 09:31, 1989lzhh <
1989...@gmail.com> wrote:
>
>
> 发自我的 iPhone
>
>> 在 Jul 8, 2014,12:47,Robert Bradshaw <
robe...@gmail.com> 写道:
>>
>>> On Mon, Jul 7, 2014 at 9:45 PM, Sid <
tmf...@gmail.com> wrote:
>>> Right, but I'm guessing Cython doesn't do automatic type inference?
>>
>> Cython does some type inference.
> I tried the inference and find the type inference is too aggressive for now, it is possible to do type inference like follow:
> a=1 #a is int
> a=[] #a is object
> a=1.0 #a is back to double
> This can be done by using different named variable to identify different type, like:
> cdef int a_int, object a_object, double a_double
> a_int=1
> a_object=[]
> a_double=1.0
> The difficult part is handle control flow.
>
It may be easier to translate to SSA first, to make merges of dataflow
and variable naming explicit. Alternatively you could make the type
inferencer aware of the reaching definitions and exploit that, and
rename the entries based on that same information.
You may want to look into the cartesian product algorithm. The idea is
to infer for every definition the set of possible types, and take the
cartesian product for the input types for function/method calls. E.g.
for obj.method(x) where obj has type {Foo, Bar} and x has type {Spam,
Ham} you can analyze Foo.method(Spam), Foo.method(Ham),
Bar.method(Spam) and Bar.method(Ham). If you find it helpful, here is
an example CPA implementation (the fixed point that drives the whole
thing is in 'infer_graph'):
https://github.com/markflorisson88/flypy/blob/master/flypy/compiler/typing/inference.py
This can however blow up quite quickly for very polymorphic code (it
is still exponential in the arity of the function).