APrioriInvestments / typed_python

An llvm-based framework for generating and calling into high-performance native code from Python.
Apache License 2.0
197 stars 8 forks source link

Simplify type conversion model #429

Open braxtonmckee opened 1 year ago

braxtonmckee commented 1 year ago

Right now we have a complicated hierarchy of conversion types, ranging from Signature to Upcast to ImplicitContainers to New. Its more complicated than we really need and supports a use case we don't care about, at the cost of complexity and poor performance, both in the compiler and in the interpreter. Matching a function signature should be one shot and clear, not a multiple-pass process.

To do this, we should end up with three conversion levels: Signature, Implicit, and New. Signature means change nothing about the object - it can only do things like unwrap an 'int' that's known as 'object', or pack an int into an object. New means create a new instance however you can - including converting an int -> str, and 'Implicit' means you can cast untyped containers to typed containers.

In this model, f(x: int) won't accept 1.5 or 1.0, and f(x: TupleOf(OneOf(None, int)) won't accept TupleOf(int), but would try to coerce 'tuple'. In practice, we've found that we don't really want to rely on type-conversion to pick which implementation of a function we use, since that tends to be hard to predict. So instead, we want to support minimal conversion, but still allow promotion of untyped data to typed data, since that allows for more idiomatic usages and can often be implemented efficiently.