Skip to content

feat: structural mutual recursion - #4575

Merged
nomeata merged 111 commits into
masterfrom
joachim/mutual_structural
Jul 8, 2024
Merged

nomeata merged 111 commits into
masterfrom
joachim/mutual_structural

Conversation

@nomeata

@nomeata nomeata commented Jun 27, 2024 •

Copy link
Copy Markdown
Collaborator

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:

mutual
inductive A
  | self : A → A
  | other : B → A
  | empty
inductive B
  | self : B → B
  | other : A → B
  | empty
end

mutual
def A.size : A → Nat
  | .self a => a.size + 1
  | .other b => b.size + 1
  | .empty => 0
termination_by structural x => x
def B.size : B → Nat
  | .self b => b.size + 1
  | .other a => a.size + 1
  | .empty => 0
termination_by structural x => x
end

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:

def A.self_size : A → Nat
  | .self a => a.self_size + 1
  | .other _ => 0
  | .empty => 0
termination_by structural x => x

or have more than one function per argument type:

  def isEven : Nat → Prop
    | 0 => True
    | n+1 => ¬ isOdd n
  termination_by structural x => x

  def isOdd : Nat → Prop
    | 0 => False
    | n+1 => ¬ isEven n
  termination_by structural x => x

This does not include

  • Support for nested inductive data types or nested recursion
  • Inferring mutual structural recursion in the absence of termination_by.
  • Functional induction principles for these.
  • Mutually recursive functions that live in different universes. This may be possible,
    maybe after beefing up the .below and .brecOn functions; we can look into this some
    other time, maybe when there are concrete use cases.

nomeata added 22 commits June 23, 2024 11:25
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.
@nomeata
nomeata changed the base branch from master to joachim/termination_by_structurally June 27, 2024 15:36
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jun 27, 2024
@ghost

ghost commented Jun 27, 2024 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 18c97926a13bfc82c9266bf00483ab13c6acb64f --onto 62b6e5878981d1bb6d414f21b0c6f8d8a36e6834. (2024-06-27 15:51:16)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 18c97926a13bfc82c9266bf00483ab13c6acb64f --onto 5c978a2e2437e30e2c01a9b027b2944d709919c9. (2024-06-28 09:37:05)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 18c97926a13bfc82c9266bf00483ab13c6acb64f --onto bd091f119bc836e91d48c856307a3cfa271225af. (2024-06-28 14:17:10)
  • ✅ Mathlib branch lean-pr-testing-4575 has successfully built against this PR. (2024-06-28 19:17:20) View Log
  • 🟡 Mathlib branch lean-pr-testing-4575 build this PR didn't complete normally. (2024-07-01 08:31:31) View Log
  • 🟡 Mathlib branch lean-pr-testing-4575 build this PR didn't complete normally. (2024-07-01 08:47:24) View Log
  • 🟡 Mathlib branch lean-pr-testing-4575 build this PR didn't complete normally. (2024-07-01 14:50:24) View Log
  • 🟡 Mathlib branch lean-pr-testing-4575 build this PR didn't complete normally. (2024-07-01 15:00:47) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d5a45dfa8b2600e300bd1968e91f2b17951d5e98 --onto 4d0b7cf66c140dd83f8c8634293047335a385026. (2024-07-01 19:34:17)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 2cd23649749dc55d9216092ef86315843b463ad1 --onto 4d0b7cf66c140dd83f8c8634293047335a385026. (2024-07-03 07:44:55)
  • ❗ Batteries CI can not be attempted yet, as the nightly-testing-2024-07-03 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Batteries CI should run now. (2024-07-03 10:11:34)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase f6deaa8fb227c6fcfeff9ab23e4449a19926875a --onto 4d0b7cf66c140dd83f8c8634293047335a385026. (2024-07-03 13:13:41)
  • 💥 Mathlib branch lean-pr-testing-4575 build failed against this PR. (2024-07-03 15:32:49) View Log
  • 💥 Mathlib branch lean-pr-testing-4575 build failed against this PR. (2024-07-03 15:49:16) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 0594bc4e5a794ecd63cc5915f5266ef643ccf826 --onto 4d0b7cf66c140dd83f8c8634293047335a385026. (2024-07-03 17:11:00)
  • ❗ Batteries CI can not be attempted yet, as the nightly-testing-2024-07-04 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Batteries CI should run now. (2024-07-04 15:47:44)
  • ✅ Mathlib branch lean-pr-testing-4575 has successfully built against this PR. (2024-07-05 21:30:40) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase f36bbc8d56cf96d9137d8676d2899b91b4fe0992 --onto 4ed79472af1f85b13c51e9ed77d15d3b4c40e3cc. (2024-07-08 10:31:13)

@nomeata

nomeata commented Jul 5, 2024

Copy link
Copy Markdown
Collaborator Author

@tobiasgrosser, took a while, but it seems we have a working mathlib branch.

@tobiasgrosser

tobiasgrosser commented Jul 5, 2024 •

Copy link
Copy Markdown
Contributor

I moved our codebase to this PR: opencompl/lean-mlir#431.

It fails with failed to generate equational theorem for 'Lets.denote' in Framework.lean. I am surprised this breaks for an opt-in feature, so maybe this is a recent change in Lean itself? I am happy to reduce this further, but wanted to check beforehand if you may be able to already pinpoint if this is useful to you. Otherwise, don't feel held back by my comments. I am happy to test lean nightlys and report from those.

@nomeata

nomeata commented Jul 6, 2024

Copy link
Copy Markdown
Collaborator Author

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 nightly-testing-2024-07-05 first to see if it is due to my PR or some other change.

@tobiasgrosser

tobiasgrosser commented Jul 6, 2024 •

Copy link
Copy Markdown
Contributor

The issue does not arise in nightly-testing-2024-07-05. I just pushed my update (opencompl/lean-mlir#432) and will rebase the mutual-recursion PR once it is merged. We are likely using quite some dependent typing and our Com data structure as a non-computable precursor (which would eventually be nice to be resolved, too).

@nomeata

nomeata commented Jul 6, 2024

Copy link
Copy Markdown
Collaborator Author

Thanks for the analysis, I’ll search for the bug early next week at the latest.

@nomeata

nomeata commented Jul 6, 2024

Copy link
Copy Markdown
Collaborator Author

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 Γ₁'v

without this, the implicit Γ₂ is put before the [DialectSignature d]. That is part of the Let’s parameters, and with my code parameters must come before indices.

Why did it work before? In e461ea6 I changed hasBadParamDep?, thinking I am fixing a bug, but maybe the new code is wrong or too restrictive. Will have to think about that.

Incidentially, since there is no termination_by structural lets clause, lean then tries to find another recursive argument and settles on termination_by structural Γ₂, and for that (for some reason maybe also worth investigating) the equational theorem generation fails.

@nomeata

nomeata commented Jul 6, 2024

Copy link
Copy Markdown
Collaborator Author

Since this issue isn't about mutual recursion per se, let’s move it to #4671, if you don't mind.

@tobiasgrosser

tobiasgrosser commented Jul 6, 2024 •

Copy link
Copy Markdown
Contributor

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 Γ₁'v

without this, the implicit Γ₂ is put before the [DialectSignature d]. That is part of the Let’s parameters, and with my code parameters must come before indices.

Why did it work before? In e461ea6 I changed hasBadParamDep?, thinking I am fixing a bug, but maybe the new code is wrong or too restrictive. Will have to think about that.

Incidentially, since there is no termination_by structural lets clause, lean then tries to find another recursive argument and settles on termination_by structural Γ₂, and for that (for some reason maybe also worth investigating) the equational theorem generation fails.

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.

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jul 7, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Jul 7, 2024
@nomeata nomeata added will-merge-soon …unless someone speaks up and removed awaiting-review Waiting for someone to review the PR labels Jul 8, 2024
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc July 8, 2024 10:17 Inactive
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc July 8, 2024 10:28 Inactive
@nomeata
nomeata added this pull request to the merge queue Jul 8, 2024
Merged via the queue into master with commit 1311e36 Jul 8, 2024
@nomeata

nomeata commented Jul 10, 2024

Copy link
Copy Markdown
Collaborator Author

It fails with failed to generate equational theorem for 'Lets.denote' in Framework.lean. I

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

@tobiasgrosser tobiasgrosser Jul 11, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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.

https://github.com/opencompl/lean-mlir/blob/97f33691aec49b7b939ec0452b835e93c0a18adc/SSA/Core/Framework.lean#L523

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.

https://github.com/opencompl/lean-mlir/blob/97f33691aec49b7b939ec0452b835e93c0a18adc/SSA/Core/Framework.lean#L1720

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lovely. Thank you for the update.

github-merge-queue Bot pushed a commit that referenced this pull request Jul 15, 2024
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 branch was previously deployed

1 inactive deployment
lean-lang.org/lean4/doc — 1729c344 Deployed Jul 8, 2024 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN will-merge-soon …unless someone speaks up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants