Repository navigation
feat: structural mutual recursion - #4575
Conversation
this is in preparation for #4542, and extracts from `findRecArg` the functionality for trying one particular argument. It also refactors the code a bit. In particular * It reports errors in the order of the parameters, not the order of in which they are tried (it tries non-indices first). * For every argument it will say why it wasn't tried, even if the reason is quite obviously (fixed prefix, or `Prop`-typed etc.) Therefore there is some error message churn.
this addresses #4546 the easy way
…mination_by_structurally
|
Mathlib CI status (docs):
|
|
@tobiasgrosser, took a while, but it seems we have a working mathlib branch. |
|
I moved our codebase to this PR: opencompl/lean-mlir#431. It fails with |
|
It shouldn't, but I had to extend other pieces of the code, so maybe made a mistake there. You could try the mathlib tag |
|
The issue does not arise in |
|
Thanks for the analysis, I’ll search for the bug early next week at the latest. |
|
I have a fix/workaround for your code: diff --git a/SSA/Core/Framework.lean b/SSA/Core/Framework.lean
index bf18ff1b..fb9db629 100644
--- a/SSA/Core/Framework.lean
+++ b/SSA/Core/Framework.lean
@@ -160,7 +160,7 @@ def Com.denoteLets : (com : Com d Γ eff ty) → (Γv : Valuation Γ) →
body.denoteLets (V.snoc Ve) >>= fun V =>
return V.cast (by simp [Com.outContext])
-def Lets.denote [DialectSignature d] [DialectDenote d]
+def Lets.denote [DialectSignature d] [DialectDenote d] {Γ₂}
(lets : Lets d Γ₁ eff Γ₂) (Γ₁'v : Valuation Γ₁) : (eff.toMonad d.m <| Valuation Γ₂) :=
match lets with
| .nil => return Γ₁'vwithout this, the implicit Why did it work before? In e461ea6 I changed Incidentially, since there is no |
|
Since this issue isn't about mutual recursion per se, let’s move it to #4671, if you don't mind. |
This works (opencompl/lean-mlir#431 (comment)). I also have the feeling that the issues I report may be a bit beyond what this PR should be about. Happy to see this merged. |
Just for the record: The error message you see here is due to #837: When recursing on an index, rather than the indexed type itself, then the function can be defined but lean struggles with proving the equational theorems. I’ll keep this in mind as I extend inferring recursive arguments to mutual recursion. |
| def Tree.size : Tree → Nat | ||
| | leaf => 0 | ||
| | node (t₁, t₂) => t₁.size + t₂.size | ||
| termination_by structural t => t |
There was a problem hiding this comment.
@nomeata, we started using this patch and it has already made some proofs easier. Thank you!
I'm curious: do you plan to add support for nested mutuals as well? We have both nested mutuals as well as mutual functions that also use inductive that are not part of a mutual block and are interested in beta-test some of your features. Interesting to use are the following mutuals:
Only Expr and Com are mutual inductive types, but HVector is also part of the mutual functions.
Termination due to well-foundedness requires high heartbeats, but our mutual recursion is a bit complex. We would really like to use mutual recursion here, if possible.
There was a problem hiding this comment.
I plan to add support for (possibly mutual) structural recursion over nested inductive data types, but not nested recursion.
In other words of you have inductive Tree where | node : List Tree -> Tree you will be able to define recursive functions over it, but you cannot use List.map but have to replicate that functionality in the mutual group.
There was a problem hiding this comment.
Lovely. Thank you for the update.
the support for mutual structural recursion (new since #4575) is extended so that Lean tries to infer it even without annotations. * The error message when termination checking fails looks quite different now. Maybe a bit better, maybe with more room for improvements. * If there are too many combinations (with an arbitrary cut-off) for a given argument type, it will just give up and ask the user to use `termination_by structural`. * It is now legal to specify `termination_by structural` on not necessarily all functions of a clique; this simply restricts the combinations of arguments that Lean considers. --------- Co-authored-by: Tobias Grosser <[email protected]>
This adds support for mutual structural recursive functions.
For now this is opt-in: The functions must have a
termination_by structural …annotation (new since #4542) for this to work:The recursive functions don’t have to be in a one-to-one relation to a set of mutually recursive inductive data types. It is possible to ignore some of the types:
or have more than one function per argument type:
This does not include
termination_by.maybe after beefing up the
.belowand.brecOnfunctions; we can look into this someother time, maybe when there are concrete use cases.