Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support field projectors for recursive structs in Lean backend #194

Merged
merged 9 commits into from
May 24, 2024

Conversation

R1kM
Copy link
Member

@R1kM R1kM commented May 23, 2024

Lean structures cannot be recursive. For this reason, recursive Rust structs are extracted to inductives with one constructor.

Similarly to what is already done for Coq, this PR generates projectors which allows to retrieve the expected behavior of structs once extracted to Lean. The translation function is similar to the one for Coq, however, there are some minor differences throughout the function, which is why I duplicated the code instead of making extract_type_decl_lean_record_field_projectors backend-generic.

This PR also removes the rewriting of Lean field projectors for recursive structs into let-bindings, to instead use the newly generated projectors.

I tested this feature with the following Rust code, which should be added to the testsuite once the refactoring of Aeneas tests (#182) is complete.

struct AVLNode<T> {
    value: T,
    left: AVLTree<T>,
    right: AVLTree<T>,
}

type AVLTree<T> = Option<Box<AVLNode<T>>>;


fn get_val<T>(x: AVLNode<T>) -> T  { x.value }

fn get_left<T>(x: AVLNode<T>) -> AVLTree<T> { x.left }

Fixes #185

@sonmarcho
Copy link
Member

sonmarcho commented May 23, 2024

The output looks good. I just want to investigate the problem of generating simplification lemmas (see #185) before merging this.

@R1kM
Copy link
Member Author

R1kM commented May 23, 2024

It seems that for structures, Lean 4 automatically adds the attribute reducible to projectors, which allows to retrieve the behavior you wanted (i.e., simplify even with simp only []).
Maybe we could just add this attribute to generated projectors?

@sonmarcho
Copy link
Member

sonmarcho commented May 23, 2024

Following some experimentations, it seems we need to add @[reducible] and we also need to register explicitly register the projection functions for simp to reduce them. The function which does so is Lean.addProjectionFnInfo. We can write a custom command for this for the time being.

@sonmarcho
Copy link
Member

There is an interesting discussion here as to why recursive structures are not supported:
leanprover/lean4#4182

@sonmarcho
Copy link
Member

After some discussions: we will mark the projectors as reducible and simp for the time being.

@R1kM
Copy link
Member Author

R1kM commented May 23, 2024

After some discussions: we will mark the projectors as reducible and simp for the time being.

This is done in e76ef95

@sonmarcho sonmarcho merged commit fbfa0e1 into main May 24, 2024
6 checks passed
@sonmarcho sonmarcho deleted the afromher/recursive_projectors branch May 24, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate projectors for recursive structs during Lean extraction
2 participants