Skip to content

Conversation

@abentkamp
Copy link
Contributor

This PR implements associated types with equality constraints for the Lean backend.

This fixes one part of #1710. Some uses of associated types such as (A as T)::B are not supported.

Implementation Details

Problem

Associated types would be most straight-forwardly modeled by Lean type classes that contain fields of type Type. However, Rust allows us to put equality constraints on associated types, for example in function definitions. This is hard to reproduce in Lean.

Solution

For every Rust trait, we generate:

  • a Lean class that contains the associated types of the Rust trait. This is just an empty struct if there are no associated types.
  • a Lean class that takes two parameters: Self : Type and an instance of the class containing the associated types. To make type inference work, we must mark the second parameter as outParam. The class fields are all non-type fields of the Rust trait.

Every function with a trait parameter will receive two parameters in Lean:

  • an instance of the associated type class
  • an instance of the class containing the other fields, taking the associated type class instance as a parameter. If equality constraints are present, we override fields in the associated type class instance instance.

@abentkamp abentkamp added this to the Lean backend v1.0 milestone Dec 10, 2025
@abentkamp abentkamp added backend Issue in one of the backends (i.e. F*, Coq, EC...) lean Related to the Lean backend or library labels Dec 10, 2025
Copy link
Contributor

@maximebuyse maximebuyse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good though it is a bit hard to review backend code so we should make sure there is enough testing. Can you update the snapshots so that we can see the results of the tests you added? This should also fix the CI!

@abentkamp abentkamp force-pushed the lean-associated-types branch 4 times, most recently from 78007ad to a0e7ee2 Compare December 11, 2025 10:47
@abentkamp abentkamp marked this pull request as ready for review December 11, 2025 11:13
@abentkamp abentkamp requested a review from a team as a code owner December 11, 2025 11:13
@abentkamp abentkamp requested a review from W95Psp December 11, 2025 11:13
Copy link
Contributor

@maximebuyse maximebuyse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few suggestions to improve the style, but otherwise this looks fine. Trying this on Core models could be the best way to test if it really does what we want!

@abentkamp abentkamp force-pushed the lean-associated-types branch 2 times, most recently from 37296a8 to a195845 Compare December 15, 2025 08:40
@W95Psp W95Psp removed their request for review December 15, 2025 09:02
@abentkamp abentkamp force-pushed the lean-associated-types branch from a195845 to 024f6a1 Compare December 15, 2025 09:30
@abentkamp abentkamp added this pull request to the merge queue Dec 15, 2025
Merged via the queue into main with commit ce58911 Dec 15, 2025
17 of 18 checks passed
@abentkamp abentkamp deleted the lean-associated-types branch December 15, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Issue in one of the backends (i.e. F*, Coq, EC...) lean Related to the Lean backend or library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants