lean4-htt/doc
Marc Huisinga e9f69d1068
feat: partial context info (#3159)
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:
```lean
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 `InfoTree`s 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, `ContextInfo`s 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
`ContextInfo`s in several places other than `liftTermElabM` and work
around the limitation that `ContextInfo`s 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

- `lean-pr-testing-3159` branch at Std, not yet opened as a PR
- `lean-pr-testing-3159` branch at Mathlib, not yet opened as a PR
- https://github.com/leanprover/LeanInk/pull/57
- https://github.com/hargoniX/LeanInk/pull/1
- https://github.com/tydeu/lean4-alloy/pull/7
- https://github.com/leanprover-community/repl/pull/29

---------

Co-authored-by: Sebastian Ullrich <sebasti@nullri.ch>
2024-01-22 12:34:20 +00:00
..
bin chore: update domain 2023-09-20 15:13:27 -07:00
dev chore: allow updating stage0 via workflow_dispatch (#3052) 2023-12-14 22:46:32 +00:00
examples feat: per-function termination hints 2024-01-10 17:27:35 +01:00
images doc: update quickstart guide to reference vs code setup guide (#2968) 2023-12-12 08:36:27 +00:00
latex chore: fix more typos in comments 2023-10-08 14:37:34 -07:00
make doc: remove Nix docs 2023-12-01 08:32:20 +00:00
monads chore: fix more typos in comments 2023-10-08 14:37:34 -07:00
.gitignore
alectryon.css doc: fix overwide view when using Alectryon 2022-06-23 18:23:28 +02:00
alectryon.js
array.md doc: add new example 2022-07-09 17:04:08 -07:00
autobound.md doc: Semantic highlighting 2022-10-02 08:37:15 -07:00
book.toml
bool.md
BoolExpr.lean chore: remove Bootstrap package 2022-09-02 16:39:03 -07:00
builtintypes.md
char.md
declarations.md doc: mention termination_by and decreasing_by (#3016) 2024-01-10 16:35:19 +00:00
decltypes.md
definitions.md
dep.md
deptypes.md
do.md
elaborators.md
enum.md
examples.md
expressions.md doc: fix typos (#2160) 2023-03-22 10:01:59 +01:00
faq.md doc: fix the link to contribution guidelines (#2623) 2023-10-05 12:02:55 +11:00
flake.lock feat: partial context info (#3159) 2024-01-22 12:34:20 +00:00
flake.nix feat: partial context info (#3159) 2024-01-22 12:34:20 +00:00
float.md
fplean.md chore: update domain 2023-09-20 15:13:27 -07:00
funabst.md
functions.md doc: mention termination_by and decreasing_by (#3016) 2024-01-10 16:35:19 +00:00
highlight.js fix: highlight of deriving instance (#1717) 2022-10-12 14:24:16 -07:00
implicit.md chore: prune ancient keywords 2022-08-01 13:32:56 +02:00
inductive.md chore: update domain 2023-09-20 15:13:27 -07:00
int.md
introdef.md
lean3changes.md chore: expand remark 2023-05-05 12:21:32 -07:00
lexical_structure.md feat: Rust-style raw string literals (#2929) 2023-12-20 16:53:08 +00:00
list.md
macro_overview.md doc: fix typos (#2160) 2023-03-22 10:01:59 +01:00
metaprogramming-arith.lean refactor: remove some unnecessary antiquotation kind annotations 2022-07-23 17:09:32 +02:00
metaprogramming-arith.md doc: metaprogramming-arith: deduplicate 2022-05-03 18:38:36 +02:00
mission.md
namespaces.md
nat.md
notation.md feat: reduce precedence of unary neg 2022-11-06 18:13:48 -08:00
option.md
organization.md
other_commands.md
perf.md
pygments.css doc: add documentation on monads (#1505) 2022-09-05 13:33:15 -07:00
quickstart.md doc: update quickstart guide to reference vs code setup guide (#2968) 2023-12-12 08:36:27 +00:00
sections.md
semantic_highlighting.md chore: update domain 2023-09-20 15:13:27 -07:00
setup.md fix: fixing path of the generated binary in documentation (#3093) 2023-12-19 17:26:55 +00:00
simptypes.md
string.md
stringinterp.md
struct.md
SUMMARY.md doc: remove Nix docs 2023-12-01 08:32:20 +00:00
syntax.md
syntax_example.lean
syntax_example.md
syntax_examples.md
syntax_highlight_in_latex.md doc: explain how to use custom lexers in the latest minted (#3047) 2023-12-11 09:16:40 +00:00
tactics.md doc: fix typos (#2160) 2023-03-22 10:01:59 +01:00
task.md
thunk.md
tour.md
tpil.md chore: update domain 2023-09-20 15:13:27 -07:00
typeclass.md doc: avoid universe issue in example type class code (#3098) 2023-12-21 16:57:26 +00:00
typeobjs.md
types.md
uint.md
unifhint.md
using_lean.md
whatIsLean.md chore: update domain 2023-09-20 15:13:27 -07:00