Skip to content

feat: infer termination arguments like xs.size - i - #3666

Merged
nomeata merged 58 commits into
masterfrom
joachim/guesslex-diff
Mar 16, 2024
Merged

nomeata merged 58 commits into
masterfrom
joachim/guesslex-diff

Conversation

@nomeata

@nomeata nomeata commented Mar 13, 2024 •

Copy link
Copy Markdown
Collaborator

a common pattern for recursive functions is

def countUp (n i acc : Nat) : Nat :=
  if i < n then
    countUp n (i+1) (acc + i)
  else
    acc

where we increase a value i until it hits an upper bound. This is
particularly common with array processing functions:

$ git grep 'termination_by.*size.*-' src/|wc -l
26

GuessLex now recognizes this pattern. The general approach is:

For every recursive call, check if the context contains hypotheses of
the form e₁ < e₂ (or similar comparisions), and then consider e₂ - e₁ as a termination argument.

Currently, this only fires when e₁ and e₂ only depend on the
functions parameters, but not local let-bindings or variables bound in
local pattern matches.

Duplicates are removed.

In the table showing the termination argument failures, long termination
arguments are now given a number and abbreviated as e.g. #4 in the
table headers.

More examples in the test file, here as some highlights:

def distinct (xs : Array Nat) : Bool :=
  let rec loop (i j : Nat) : Bool :=
    if _ : i < xs.size then
      if _ : j < i then
        if xs[j] = xs[i] then
          false
        else
          loop i (j+1)
      else
        loop (i+1) 0
    else
      true
  loop 0 0

infers

termination_by (Array.size xs - i, i - j)

and the weird functions where i goes up or down

def weird (xs : Array Nat) (i : Nat) : Bool :=
  if _ : i < xs.size then
    if _ : 0 < i then
      if xs[i] = 42 then
        weird xs.pop (i - 1)
      else
        weird xs (i+1)
    else
      weird xs (i+1)
  else
    true
decreasing_by all_goals simp_wf; omega

infers

termination_by (Array.size xs - i, i)

but unfortunately needs decreasing_by pending the “big decreasing_tactic refactor” that
I expect we’ll want to do at some point.

nomeata added 22 commits March 10, 2024 22:37
This introduces the `ArgsPacker` module and abstraction, to replace the
exising `PackDomain`/`PackMutual` code. The motivation was that we now
have more uses besides `Fix.lean` (`GuessLex` and `FunInd`), and the
code was spread in various places.

The goals are

 * consistent function naming withing the the `PSigma` handling, the
   `PSum` handling, and the combined interface
 * avoid taking a type apart just based on the `PSigma`/`PSum` nesting,
   to be robust in case the user happens to be using `PSigma`/`PSum`
   somewhere. Therefore, always pass an `arity` or `numFuncs` or
   `varNames` around.
 *  keep all the `PSigma`/`PSum` encoding logic contained within one
    module (`ArgsPacker`), and keep that module independent of its users
    (so no `EqnInfos` visible here).
 * pick good variable names when matching on a packed argument
 * the unary function now is either called `fun1._unary` or
   `fun1._mutual`, never `fun1._unary._mutual`.

This file has less heavy dependencies than `PackMutual` had, so build
parallelism is improved as well.
this refactor prepares GuessLex to be able to infer more complex
termination arguments.

As a side-effect it fixes an (obscure) bug where `sizeOf` would be
applied to a term of the wrong type and thus a wrong `SizeOf` instance
could be inferred.
a common pattern for recursive functions is
```
def countUp (n i acc : Nat) : Nat :=
  if i < n then
    countUp n (i+1) (acc + i)
  else
    acc
```
where we increase a value `i` until it hits an upper bound. This is
particularly common with array processing functions:
```
$ git grep 'termination_by.*size.*-' src/|wc -l
26
```

GuessLex now recognizes this pattern. The general approach is:

For every recursive call, check if the context contains hypotheses of
the form `e₁ < e₂` (or similar comparisions), and then consider `e₂ -
e₁` as a termination argument.

Currently, this only fires when `e₁` and `e₂` only depend on the
functions parameters, but not local let-bindings or variables bound in
local pattern matches.

Duplicates are removed.

In the table showing the termination argument failures, long termination
arguments are now given a number and abbreviated as e.g. `#4` in the
table headers.
@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 Mar 13, 2024
@ghost

ghost commented Mar 13, 2024 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 1388f6bc83b5ea085fe14faa1db213e745c3e398 --onto 32dcc6eb895b58df3d3241a2521963e64995b621. (2024-03-13 15:59:13)
  • 🟡 Mathlib branch lean-pr-testing-3666 build against this PR was cancelled. (2024-03-13 23:13:53) View Log
  • 💥 Mathlib branch lean-pr-testing-3666 build failed against this PR. (2024-03-13 23:27:49) View Log
  • 💥 Mathlib branch lean-pr-testing-3666 build failed against this PR. (2024-03-13 23:42:16) View Log
  • ❌ Mathlib branch lean-pr-testing-3666 built against this PR, but was unexpectedly noisy. (2024-03-14 09:54:40) View Log
  • ❌ Mathlib branch lean-pr-testing-3666 built against this PR, but was unexpectedly noisy. (2024-03-14 10:22:39) View Log
  • ❌ Mathlib branch lean-pr-testing-3666 built against this PR, but was unexpectedly noisy. (2024-03-14 10:45:50) View Log
  • ✅ Mathlib branch lean-pr-testing-3666 has successfully built against this PR. (2024-03-14 11:30:04) View Log
  • 💥 Mathlib branch lean-pr-testing-3666 build failed against this PR. (2024-03-16 10:59:54) View Log
  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase f0ff01ae28b24733d041413bfa9b0b6da28f462b --onto 0ec8862103e397715854a7a6962ce542bba4884d. (2024-03-16 12:31:05)

@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 13, 2024 16:22 Inactive
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 13, 2024 23:03 Inactive
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 13, 2024
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 14, 2024 13:55 Inactive
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 16, 2024 09:59 Inactive
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 16, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 16, 2024
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 16, 2024 10:49 Inactive
@ghost ghost added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan and removed builds-mathlib CI has verified that Mathlib builds against this PR labels Mar 16, 2024
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 16, 2024 11:03 Inactive
@nomeata
nomeata marked this pull request as ready for review March 16, 2024 11:32
@nomeata
nomeata requested a review from kim-em as a code owner March 16, 2024 11:32
@nomeata
nomeata changed the base branch from joachim/guesslex-expr to master March 16, 2024 11:34
@nomeata
nomeata enabled auto-merge March 16, 2024 11:34
@github-actions
github-actions Bot temporarily deployed to lean-lang.org/lean4/doc March 16, 2024 11:34 Inactive
@nomeata
nomeata added this pull request to the merge queue Mar 16, 2024
Merged via the queue into master with commit 4c57da4 Mar 16, 2024

This branch was previously deployed

1 inactive deployment
lean-lang.org/lean4/doc — 9848b27b Deployed Mar 16, 2024 by github-actions[bot]
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.

1 participant