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: partial context info #3159

Merged
merged 6 commits into from
Jan 22, 2024

Conversation

mhuisi
Copy link
Contributor

@mhuisi mhuisi commented Jan 10, 2024

This PR facilitates augmenting the context of an InfoTree with partial contexts while elaborating a command. Using partial contexts, this PR also adds support for tracking the parent declaration name of a term in the InfoTree. The parent declaration name is needed to compute the call hierarchy in #3082.

Specifically, the Lean.Elab.InfoTree.context constructor is refactored to take a value of the new type Lean.Elab.PartialContextInfo instead of a Lean.Elab.ContextInfo, which now refers to a full InfoTree context. The PartialContextInfo is then merged into a ContextInfo while traversing the tree using Lean.Elab.PartialContextInfo.mergeIntoOuter?. The partial context after executing liftTermElabM is stored in values of a new type Lean.Elab.CommandContextInfo.

As a result of this, Lean.Elab.ContextInfo.save moves to Lean.Elab.CommandContextInfo.save.

For obtaining the parent declaration for a term, a new typeclass MonadParentDecl is introduced to save the parent declaration in Lean.Elab.withSaveParentDeclInfoContext. Lean.Elab.Term.withDeclName x now calls withSaveParentDeclInfoContext x to save the declaration name.

Migration

The changes to the InfoTree.context constructor break backwards compatibility with all downstream users that traverse the InfoTree manually instead of going through the functions in InfoUtils.lean.
To fix this, you can merge the outer ContextInfo in a traversal with the PartialContextInfo of an InfoTree.context node using PartialContextInfo.mergeIntoOuter?. See e.g. Lean.Elab.InfoTree.foldInfo for an example:

partial def InfoTree.foldInfo (f : ContextInfo → Info → α → α) (init : α) : InfoTree → α :=
  go none init
where go ctx? a
  | context ctx t => go (ctx.mergeIntoOuter? ctx?) a t
  | node i ts =>
    let a := match ctx? with
      | none => a
      | some ctx => f ctx i a
    ts.foldl (init := a) (go <| i.updateContext? ctx?)
  | _ => a

Downstream users that manually save InfoTrees may need to adjust calls to ContextInfo.save to use CommandContextInfo.save instead and potentially wrap their CommandContextInfo in a PartialContextInfo.commandCtx constructor when storing it in an InfoTree or ContextInfo.mk when creating a full context.

Motivation

As of now, ContextInfos are always full contexts, constructed as if they were always created in liftTermElabM after running the TermElabM action. This is not strictly true; we already create ContextInfos in several places other than liftTermElabM and work around the limitation that ContextInfos are always full contexts in certain places (e.g. Info.updateContext? is a crux that we need because we can't always create partial contexts at the term-level), but it has mostly worked out so far. Note that one must be very careful when saving a ContextInfo in places other than liftTermElabM because the context may not be as complete as we would like (e.g. it may lack meta-variable assignments, potentially leading to a language server panic).

Unfortunately, the parent declaration of a term is another example of a context that cannot be provided in liftTermElabM: The parent declaration is usually set via withDeclName, which itself lives in TermElabM. So by the time we are trying to save the full ContextInfo, the declaration name is already gone. There is no easy fix for this like in the other cases where we would really just like to augment the context with an extra field.

The refactor that we decided on to resolve the issue is to refactor the InfoTree to take a PartialContextInfo instead of a ContextInfo and have code that traverses the InfoTree merge inner contexts with outer contexts to produce a full ContextInfo value.

Bumps for downstream projects

@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 Jan 10, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 10, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Jan 10, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Jan 10, 2024

leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 10, 2024
@kim-em
Copy link
Collaborator

kim-em commented Jan 11, 2024

I've added full-ci, as we're going to need to update ProofWidgets4 to get this tested.

Next steps when that is ready:

  • update lean-pr-testing-3159 at ProofWidgets (which I've created, but not updated)
  • push a v0.0.26-pre tag to ProofWidgets4 from the lean-pr-testing-3159 branch
  • change the Mathlib lean-pr-testing-3159 branch to use ProofWidgets @ v0.0.26-pre
  • see what else breaks?

kim-em added a commit to leanprover-community/ProofWidgets4 that referenced this pull request Jan 11, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 11, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Jan 12, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 12, 2024
src/Lean/Elab/InfoTree/Types.lean Show resolved Hide resolved
src/Lean/Elab/Tactic/Basic.lean Outdated Show resolved Hide resolved
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 19, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 19, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jan 19, 2024
@mhuisi mhuisi requested a review from Kha January 22, 2024 08:56
@mhuisi mhuisi added this pull request to the merge queue Jan 22, 2024
Merged via the queue into leanprover:master with commit e9f69d1 Jan 22, 2024
19 checks passed
mhuisi added a commit to mhuisi/lean4 that referenced this pull request Jan 22, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jan 22, 2024
kim-em added a commit to leanprover-community/batteries that referenced this pull request Jan 23, 2024
kim-em added a commit to leanprover-community/batteries that referenced this pull request Jan 23, 2024
antonkov added a commit to Paper-Proof/paperproof that referenced this pull request Jan 25, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jan 25, 2024
This PR adds support for the "call hierarchy" feature of LSP that allows
quickly navigating both inbound and outbound call sites of functions. In
this PR, "call" is taken to mean "usage", so inbound and outbound
references of all kinds of identifiers (e.g. functions or types) can be
navigated. To implement the call hierarchy feature, this PR implements
the LSP requests `textDocument/prepareCallHierarchy`,
`callHierarchy/incomingCalls` and `callHierarchy/outgoingCalls`.

<details>
  <summary>Showing the call hierarchy (click to show image)</summary>
  

![show_call_hierarchy](https://github.com/leanprover/lean4/assets/10852073/add13943-013c-4d0a-a2d4-a7c57ad2ae26)
  
</details>

<details>
  <summary>Incoming calls (click to show image)</summary>
  

![incoming_calls](https://github.com/leanprover/lean4/assets/10852073/9a803cb4-6690-42b4-9c5c-f301f76367a7)
  
</details>

<details>
  <summary>Outgoing calls (click to show image)</summary>
  

![outgoing_calls](https://github.com/leanprover/lean4/assets/10852073/a7c4f193-51ab-4365-9473-0309319b1cfe)
  
</details>

It is based on #3159, which should be merged before this PR.

To route the parent declaration name through to the language server, the
`.ilean` format is adjusted, breaking backwards compatibility with
version 1 of the ILean format and yielding version 2.

This PR also makes the following more minor adjustments:
- `Lean.Server.findModuleRefs` now also combines the identifiers of
constants and FVars and prefers constant over FVars for the combined
identifier. This is necessary because e.g. declarations declared using
`where` yield both a constant (for usage outside of the function) and an
FVar (for usage inside of the function) with the same range, whereas we
would typically like all references to refer to the former. This also
fixes a bug introduced in #2462 where renaming a declaration declared
using `where` would not rename usages outside of the function, as well
as a bug in the unused variable linter where `where` declarations would
be reported as unused even if they were being used outside of the
function.
- The function converting `Lean.Server.RefInfo` to `Lean.Lsp.RefInfo`
now also computes the `Lean.DeclarationRanges` for parent declaration
names via `MetaM` and must hence be in `IO` now.
- Add a utility function `Array.groupByKey` to `HashMap.lean`.
- Stylistic refactoring of `Watchdog.lean` and `LanguageFeatures.lean`.
kim-em added a commit to leanprover-community/batteries that referenced this pull request Feb 1, 2024
* feat: hover info for `rcases h : ...` (#486)

* feat: hover info for `rcases h : ...`

* fix

* chore: adaptations for leanprover/lean4#3123 (#502)

* chore: adaptations for leanprover/lean4#3123

* update toolchain

* chore: remove unnecessary `have` (#516)

After leanprover/lean4#3132 the linter will complain about this.

* chore: simproc PR changes (#496)

See leanprover/lean4#3124

Co-authored-by: Scott Morrison <[email protected]>

* chore: adaptions for nightly-2023-01-11 (#524)

* advance toolchain to nightly-2024-01-12, no updates required

* chore: updates to DiscrTree for changes in nightly (#536)

* doc: extend docstrings for `ext` and `ext1` (#525)

Makes sure to mention that the patterns are processed using `rintro`, and makes sure `ext` mentions `ext1`.

* docs(Data/List): typo (#529)

* feat: Eq.rec lemma (#385)

* chore: Add empty collection instance to BinomialHeap (#532)

* Incremental Library Search (#421)

This ports library_search from Mathlib to Std.  It preserves the ability to do caching, but is designed to support a cacheless mode.  The exact and apply tactics are named `std_exact?` and `std_apply?`, but will eventually be renamed.

Co-authored-by: Scott Morrison <[email protected]>

* fix termination_by clauses in LazyDiscrTree

fix LibrarySearch

* bump toolchain

---------

Co-authored-by: Kyle Miller <[email protected]>
Co-authored-by: Martin Dvořák <[email protected]>
Co-authored-by: François G. Dorais <[email protected]>
Co-authored-by: Joe Hendrix <[email protected]>

* feat: adaptations for leanprover/lean4#3159 (#557)

* merge origin/main

* chore: fixes for `simp` refactor (#571)

* move to v4.6.0-rc1

* fix proofs

---------

Co-authored-by: Mario Carneiro <[email protected]>
Co-authored-by: Leonardo de Moura <[email protected]>
Co-authored-by: Joachim Breitner <[email protected]>
Co-authored-by: Kyle Miller <[email protected]>
Co-authored-by: Martin Dvořák <[email protected]>
Co-authored-by: François G. Dorais <[email protected]>
Co-authored-by: Joe Hendrix <[email protected]>
fgdorais added a commit to fgdorais/batteries that referenced this pull request Feb 18, 2024
)

* feat: hover info for `rcases h : ...` (leanprover-community#486)

* feat: hover info for `rcases h : ...`

* fix

* chore: adaptations for leanprover/lean4#3123 (leanprover-community#502)

* chore: adaptations for leanprover/lean4#3123

* update toolchain

* chore: remove unnecessary `have` (leanprover-community#516)

After leanprover/lean4#3132 the linter will complain about this.

* chore: simproc PR changes (leanprover-community#496)

See leanprover/lean4#3124

Co-authored-by: Scott Morrison <[email protected]>

* chore: adaptions for nightly-2023-01-11 (leanprover-community#524)

* advance toolchain to nightly-2024-01-12, no updates required

* chore: updates to DiscrTree for changes in nightly (leanprover-community#536)

* doc: extend docstrings for `ext` and `ext1` (leanprover-community#525)

Makes sure to mention that the patterns are processed using `rintro`, and makes sure `ext` mentions `ext1`.

* docs(Data/List): typo (leanprover-community#529)

* feat: Eq.rec lemma (leanprover-community#385)

* chore: Add empty collection instance to BinomialHeap (leanprover-community#532)

* Incremental Library Search (leanprover-community#421)

This ports library_search from Mathlib to Std.  It preserves the ability to do caching, but is designed to support a cacheless mode.  The exact and apply tactics are named `std_exact?` and `std_apply?`, but will eventually be renamed.

Co-authored-by: Scott Morrison <[email protected]>

* fix termination_by clauses in LazyDiscrTree

fix LibrarySearch

* bump toolchain

---------

Co-authored-by: Kyle Miller <[email protected]>
Co-authored-by: Martin Dvořák <[email protected]>
Co-authored-by: François G. Dorais <[email protected]>
Co-authored-by: Joe Hendrix <[email protected]>

* feat: adaptations for leanprover/lean4#3159 (leanprover-community#557)

* merge origin/main

* chore: fixes for `simp` refactor (leanprover-community#571)

* move to v4.6.0-rc1

* fix proofs

---------

Co-authored-by: Mario Carneiro <[email protected]>
Co-authored-by: Leonardo de Moura <[email protected]>
Co-authored-by: Joachim Breitner <[email protected]>
Co-authored-by: Kyle Miller <[email protected]>
Co-authored-by: Martin Dvořák <[email protected]>
Co-authored-by: François G. Dorais <[email protected]>
Co-authored-by: Joe Hendrix <[email protected]>
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants