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

feat: functional induction #3432

Merged
merged 51 commits into from
Mar 5, 2024
Merged

feat: functional induction #3432

merged 51 commits into from
Mar 5, 2024

Conversation

nomeata
Copy link
Collaborator

@nomeata nomeata commented Feb 21, 2024

This adds the concept of functional induction to lean.

Derived from the definition of a (possibly mutually) recursive function, a functional
induction principle
is tailored to proofs about that function. For example from:

def ackermann : Nat → Nat → Nat
  | 0, m => m + 1
  | n+1, 0 => ackermann n 1
  | n+1, m+1 => ackermann n (ackermann (n + 1) m)
derive_functional_induction ackermann

we get

ackermann.induct (motive : Nat → Nat → Prop) (case1 : ∀ (m : Nat), motive 0 m)
  (case2 : ∀ (n : Nat), motive n 1 → motive (Nat.succ n) 0)
  (case3 : ∀ (n m : Nat), motive (n + 1) m → motive n (ackermann (n + 1) m) → motive (Nat.succ n) (Nat.succ m))
  (x x : Nat) : motive x x

At the moment, the user has to ask for the functional induction principle explicitly using

derive_functional_induction ackermann

The module docstring of Lean/Meta/Tactic/FunInd.lean contains more details on the
design and implementation of this command.

More convenience around this (e.g. a functional induction tactic) will follow eventually.

This PR includes a bunch of PSum/PSigma related functions in the Lean.Tactic.FunInd
namespace. I plan to move these to PackArgs/PackMutual afterwards, and do some cleaning
up as I do that.

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 21, 2024 11:17 Inactive
@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 Feb 21, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Feb 21, 2024

Mathlib CI status (docs):

  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase c9aea32d3e1da1287d099d41dcec831dc5151610 --onto 8b8e001794e6a8b481d37f24fa77bb07797682a1. (2024-02-21 11:21:01)
  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 88fbe2e5313bb418abdb035c48cae2680ea9bcea --onto 5b15e1a9f3c0d1283df7f2e2f3f1b1dc59f92cf6. (2024-02-25 22:49:29)
  • ❗ Std CI can not be attempted yet, as the nightly-testing-2024-02-26 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Std CI should run now. (2024-02-26 11:15:17)
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-27 20:17:00) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-27 21:41:56) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-28 09:56:31) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-28 11:03:30) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-28 14:11:55) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-28 18:20:20) View Log
  • 💥 Mathlib branch lean-pr-testing-3432 build failed against this PR. (2024-02-29 11:43:08) View Log
  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase b9f9ce874dcbc31acbaf3a09ae547c5546d087f5 --onto 5a330917326cfc6e8afd37c0b78a72ccb9d1f09d. (2024-02-29 14:21:39)

nomeata added a commit that referenced this pull request Feb 21, 2024
PR #3432 will introduce more operations on `MatcherApp`, including somet
that have more dependencies.

This change prepares by introducing `Lean.Meta.Match.MatcherApp.Basic`
for the basic definition, and `Lean.Meta.MatcherApp.Transform` for the
transformations, currently `addArg` and `refineThrough`, but more to
come.
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 21, 2024 13:29 Inactive
nomeata added a commit that referenced this pull request Feb 21, 2024
PR #3432 will introduce more operations on `MatcherApp`, including somet
that have more dependencies.

This change prepares by introducing `Lean.Meta.Match.MatcherApp.Basic`
for the basic definition, and `Lean.Meta.MatcherApp.Transform` for the
transformations, currently `addArg` and `refineThrough`, but more to
come.
github-merge-queue bot pushed a commit that referenced this pull request Feb 22, 2024
PR #3432 will introduce more operations on `MatcherApp`, including somet
that have more dependencies.

This change prepares by introducing `Lean.Meta.Match.MatcherApp.Basic`
for the basic definition, and `Lean.Meta.MatcherApp.Transform` for the
transformations, currently `addArg` and `refineThrough`, but more to
come.
@leodemoura leodemoura force-pushed the code_action branch 2 times, most recently from 98452f1 to 9daaadb Compare February 25, 2024 19:02
@nomeata nomeata changed the base branch from code_action to master February 25, 2024 22:32
@nomeata nomeata marked this pull request as ready for review February 25, 2024 22:33
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 25, 2024 22:45 Inactive
Comment on lines +1351 to +1353
Removes `fvarId` from the local context, and replaces occurrences of it with `e`.
It is the responsibility of the caller to ensure that `e` is well-typed in the context
of any occurrence of `fvarId`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This operation sounds like "substitution" to me, but you're not using that terminology. Why not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No deep reason, just out of consistency with existing function names and doc strings: https://leanprover-community.github.io/mathlib4_docs/Lean/Expr.html#Lean.Expr.replaceFVar

/--
Performs a possibly type-changing transformation to a `MatcherApp`.

* `onParams` is run on each parameter and discrimitant
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know the term "discrimitant", and my dictionary doesn't either. Is this specialized technical vocabulary, or should it be "scrutinee"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Scrurtinee seems to be mostly GHC lingo, and discrs is used in the lean code base. 🤷‍♂️

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
* `onParams` is run on each parameter and discrimitant
* `onParams` is run on each parameter and discriminant

Copy link
Contributor

Choose a reason for hiding this comment

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

Consistency is good here!

"Scrutinee" is also what we called it in Idris 1, but there were plenty of GHC-isms in that code as well, like "zonk".

Copy link
Contributor

Choose a reason for hiding this comment

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

Though "-ant" is "a person or thing performing or causing the stated action". But the thing we're talking about here is not that which is causing discrimination to occur, it's that which is being subject to discrimination. "Discriminee" isn't in any English dictionaries that I could find, but it follows the regular pattern of "-ee" pointing at the object of a verb.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed googling shows up Coq docs that (occasionally? rarely?) use of discriminee, but never discriminant.

But yet:

~/build/lean/lean4 $ git grep discriminant|wc -l
84
~/build/lean/lean4 $ git grep discriminee|wc -l
0

I don’t mind changing that, but if we do, then everywhere separate of this PR.

Do we need a little “glossary and nomenclature” document somewhere where we can keep track of such decisions?

Copy link
Contributor

Choose a reason for hiding this comment

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

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 29, 2024 13:46 Inactive
@nomeata nomeata enabled auto-merge March 4, 2024 15:46
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc March 4, 2024 16:00 Inactive
@nomeata nomeata added this pull request to the merge queue Mar 4, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 4, 2024
@nomeata nomeata requested a review from kim-em as a code owner March 5, 2024 08:50
@nomeata nomeata enabled auto-merge March 5, 2024 08:50
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc March 5, 2024 09:03 Inactive
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc March 5, 2024 12:25 Inactive
@nomeata nomeata added this pull request to the merge queue Mar 5, 2024
Merged via the queue into master with commit 8038604 Mar 5, 2024
23 checks passed
@nomeata nomeata deleted the joachim/funind branch March 5, 2024 13:50
nomeata added a commit that referenced this pull request Mar 10, 2024
This adds the concept of **functional induction** to lean.

Derived from the definition of a (possibly mutually) recursive function,
a **functional
induction principle** is tailored to proofs about that function. For
example from:

```
def ackermann : Nat → Nat → Nat
  | 0, m => m + 1
  | n+1, 0 => ackermann n 1
  | n+1, m+1 => ackermann n (ackermann (n + 1) m)
derive_functional_induction ackermann
```
we get
```
ackermann.induct (motive : Nat → Nat → Prop) (case1 : ∀ (m : Nat), motive 0 m)
  (case2 : ∀ (n : Nat), motive n 1 → motive (Nat.succ n) 0)
  (case3 : ∀ (n m : Nat), motive (n + 1) m → motive n (ackermann (n + 1) m) → motive (Nat.succ n) (Nat.succ m))
  (x x : Nat) : motive x x
```

At the moment, the user has to ask for the functional induction
principle explicitly using
```
derive_functional_induction ackermann
```

The module docstring of `Lean/Meta/Tactic/FunInd.lean` contains more
details on the
design and implementation of this command.

More convenience around this (e.g. a `functional induction` tactic) will
follow eventually.


This PR includes a bunch of `PSum`/`PSigma` related functions in the
`Lean.Tactic.FunInd`
namespace. I plan to move these to `PackArgs`/`PackMutual` afterwards,
and do some cleaning
up as I do that.

---------

Co-authored-by: David Thrane Christiansen <[email protected]>
Co-authored-by: Leonardo de Moura <[email protected]>
tydeu pushed a commit to tydeu/lean4 that referenced this pull request Mar 11, 2024
This adds the concept of **functional induction** to lean.

Derived from the definition of a (possibly mutually) recursive function,
a **functional
induction principle** is tailored to proofs about that function. For
example from:

```
def ackermann : Nat → Nat → Nat
  | 0, m => m + 1
  | n+1, 0 => ackermann n 1
  | n+1, m+1 => ackermann n (ackermann (n + 1) m)
derive_functional_induction ackermann
```
we get
```
ackermann.induct (motive : Nat → Nat → Prop) (case1 : ∀ (m : Nat), motive 0 m)
  (case2 : ∀ (n : Nat), motive n 1 → motive (Nat.succ n) 0)
  (case3 : ∀ (n m : Nat), motive (n + 1) m → motive n (ackermann (n + 1) m) → motive (Nat.succ n) (Nat.succ m))
  (x x : Nat) : motive x x
```

At the moment, the user has to ask for the functional induction
principle explicitly using
```
derive_functional_induction ackermann
```

The module docstring of `Lean/Meta/Tactic/FunInd.lean` contains more
details on the
design and implementation of this command.

More convenience around this (e.g. a `functional induction` tactic) will
follow eventually.


This PR includes a bunch of `PSum`/`PSigma` related functions in the
`Lean.Tactic.FunInd`
namespace. I plan to move these to `PackArgs`/`PackMutual` afterwards,
and do some cleaning
up as I do that.

---------

Co-authored-by: David Thrane Christiansen <[email protected]>
Co-authored-by: Leonardo de Moura <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants