[Cython] CF based type inference

mark florisson markflorisson88 at gmail.com
Thu May 10 10:34:31 CEST 2012


On 10 May 2012 08:27, Vitja Makarov <vitja.makarov at gmail.com> wrote:
> 2012/5/10 Stefan Behnel <stefan_ml at behnel.de>:
>> Vitja Makarov, 08.05.2012 15:47:
>>> 2012/5/8 Stefan Behnel:
>>>> Vitja has rebased the type inference on the control flow, so I wonder if
>>>> this will enable us to properly infer this:
>>>>
>>>>  def partial_validity():
>>>>    """
>>>>    >>> partial_validity()
>>>>    ('Python object', 'double', 'str object')
>>>>    """
>>>>    a = 1.0
>>>>    b = a + 2   # definitely double
>>>>    a = 'test'
>>>>    c = a + 'toast'  # definitely str
>>>>    return typeof(a), typeof(b), typeof(c)
>>>>
>>>> I think, what is mainly needed for this is that a NameNode with an
>>>> undeclared type should not report its own entry as dependency but that of
>>>> its own cf_assignments. Would this work?
>>>>
>>>> (Haven't got the time to try it out right now, so I'm dumping it here.)
>>>
>>> Yeah, that might work. The other way to go is to split entries:
>>>
>>>  def partial_validity():
>>>    """
>>>    >>> partial_validity()
>>>    ('str object', 'double', 'str object')
>>>    """
>>>    a_1 = 1.0
>>>    b = a_1 + 2   # definitely double
>>>    a_2 = 'test'
>>>    c = a_2 + 'toast'  # definitely str
>>>    return typeof(a_2), typeof(b), typeof(c)
>>>
>>> And this should work better because it allows to infer a_1 as a double
>>> and a_2 as a string.
>>
>> How would type checks fit into this? Stupid example:
>>
>>   def test(x):
>>       if isinstance(x, MyExtType):
>>           x.call_c_method()    # type known, no None check needed
>>       else:
>>           x.call_py_method()   # type unknown, may be None
>>
>> Would it work to consider a type checking branch an assignment to a new
>> (and differently typed) entry?
>>
>
> No, at least without special handler for this case.
> Anyway that's not that hard to implement isinstance() condition may
> mark x as being assigned to MyExtType, e.g.:
>
> if isinstance(x, MyExtType):
>    x = <MyExtType> x  # Fake assignment
>    x.call_c_method()
>

That would be nice. It might also be useful to do branch pruning
before that stage, which may avoid a merge after the branch leading to
a different (unknown, i.e. object) type. That could be useful in the
face of fused types, where people write generic code triggering only a
certain branch depending on the specialization. Bit of a special case
maybe :)

>
>
>
> --
> vitja.
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel


More information about the cython-devel mailing list