GrammaTech / retypd

GNU General Public License v3.0
69 stars 5 forks source link

Rework constraint generation #3

Closed aeflores closed 2 years ago

aeflores commented 2 years ago

This MR reworks the constraint generation.

The basic functionality is implemented in _generate_constraints_from_to which can use naive exploration or path expressions (from Tarjan's algorithm) depending on the solver's configuration. This basic functionality is used to generate constraints in two ways. _generate_type_scheme which roughly corresponds to the use of transducer in procedure in Algorithm F.1 and _generate_primitive_constraints which corresponds to the use of transducer in Solve (Algorithm F.2).

For the first use _generate_type_scheme, I have recovered some functionality to create type vars to capture recursive types, which allows us to get good types even in the case of recursive types with no primitive leafs such as test test_recursive_no_primitive. This is something that I think we do differently from the paper, which seems to imply generating these type variables from the path expressions. The problem is that if you try to do that for an example such as test_recursive_no_primitive, you will not get any path expression. The new type var creation for recursive types tries to introduce the minimum number of type vars (although it is not guaranteed) and does not break cycles that do not represent recursive types.

Finally, I have adapted the "instantiate" functionality to use both type schemes and sketches. Type schemes can give us constraints that relate procedure arguments among each other, constraints on primitive types, constraints describing recursive types. Sketches can give us capability information "Var alpha.l" constraints. The existentially quantified variables (anonymous variables) in type schemes need to be instantiated with fresh variable names to avoid collisions.

Apart from the reworking of constraint generation, this MR has two additional changes:

The MR also adds a bunch of tests that illustrate the need and the changes of this MR.