ruff
02ebb2ee - [ty] Change to BDD representation for constraint sets (#20533)

Commit
4 days ago
[ty] Change to BDD representation for constraint sets (#20533) While working on #20093, I kept running into test failures due to constraint sets not simplifying as much as they could, and therefore not being easily testable against "always true" and "always false". This PR updates our constraint set representation to use BDDs. Because BDDs are reduced and ordered, they are canonical — equivalent boolean formulas are represented by the same interned BDD node. That said, there is a wrinkle, in that the "variables" that we use in these BDDs — the individual constraints like `Lower ≤ T ≤ Upper` are not always independent of each other. As an example, given types `A ≤ B ≤ C ≤ D` and a typevar `T`, the constraints `A ≤ T ≤ C` and `B ≤ T ≤ D` "overlap" — their intersection is non-empty. So we should be able to simplify ``` (A ≤ T ≤ C) ∧ (B ≤ T ≤ D) == (B ≤ T ≤ C) ``` That's not a simplification that the BDD structure can perform itself, since those three constraints are modeled as separate BDD variables, and are therefore "opaque" to the BDD algorithms. That means we need to perform this kind of simplification ourselves. We look at pairs of constraints that appear in a BDD and see if they can be simplified relative to each other, and if so, replace the pair with the simplification. A large part of the toil of getting this PR to work was identifying all of those patterns and getting that substitution logic correct. With this new representation, all existing tests pass, as well as some new ones that represent test failures that were occuring on #20093. --------- Co-authored-by: Carl Meyer <carl@astral.sh>
Author
Parents
Loading