Skip to content

refactor: introduce generic Env<T> and consolidate environment handlng#46

Merged
iljakuklic merged 4 commits intodevelfrom
ctx
Apr 6, 2026
Merged

refactor: introduce generic Env<T> and consolidate environment handlng#46
iljakuklic merged 4 commits intodevelfrom
ctx

Conversation

@iljakuklic
Copy link
Copy Markdown
Owner

Add common::env::Env<T>, a level-indexed environment backed by a Vec with typed depth()/push/pop/truncate/get(Lvl) accessors.

Port all four environment sites to use it:

  • core/pretty.rs: Vec<&Name>Env<&Name>
  • core/value.rs: Vec<Value> type alias → Env<Value>; eval, eval_pi, eval_lam now take &Env instead of &[Value]
  • checker/ctx.rs: three parallel vecs (names, env, types) → single Env<CtxEntry> with a value_env() helper
  • staging/mod.rs: inner Vec<Binding>LevelEnv<Binding>

Also fix Ix::lvl_at to return Lvl instead of Self, and add Display to Lvl, Ix, and Depth via derive_more.

…ng (closes #31)

Add `common::env::Env<T>`, a level-indexed environment backed by a Vec
with typed `depth()`/`push`/`pop`/`truncate`/`get(Lvl)` accessors.

Port all four environment sites to use it:
- `core/pretty.rs`: `Vec<&Name>` → `Env<&Name>`
- `core/value.rs`: `Vec<Value>` type alias → `Env<Value>`; `eval`,
  `eval_pi`, `eval_lam` now take `&Env` instead of `&[Value]`
- `checker/ctx.rs`: three parallel vecs (`names`, `env`, `types`) →
  single `Env<CtxEntry>` with a `value_env()` helper
- `staging/mod.rs`: inner `Vec<Binding>` → `LevelEnv<Binding>`

Also fix `Ix::lvl_at` to return `Lvl` instead of `Self`, and add
`Display` to `Lvl`, `Ix`, and `Depth` via `derive_more`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@iljakuklic iljakuklic self-assigned this Apr 6, 2026
iljakuklic and others added 3 commits April 6, 2026 20:19
Address all 12 review comments on Env<T> with the following improvements:

T1: Add De Bruijn index (Ix) based access and conversion methods:
  - Rename get/get_mut → get_at_lvl/get_at_lvl_mut
  - Add ix_to_lvl/lvl_to_ix conversion helpers
  - Add get_at_ix/get_at_ix_mut for direct index access
  - Implement Index<Ix>/IndexMut<Ix> traits
  - Simplify call sites in pretty.rs and staging/mod.rs

T2: Add predicate-based and name-based lookup methods:
  - Add find_innermost<F>() for generic innermost-first search
  - Add lookup<N, F>() for name-based lookup with key extractor
  - Simplify Ctx::lookup_local to use the new lookup() method

T3: Rename and remove iterator methods:
  - Rename iter() → iter_by_lvl() for clarity (oldest-first = by level)
  - Remove unused iter_mut() method
  - Update all call sites in value.rs and ctx.rs

T4: Widen iter_with_lvl() trait bounds:
  - Declare impl DoubleEndedIterator + ExactSizeIterator
  - No implementation change needed, just accurate bounds

T5: Replace extend() method with Extend trait impl:
  - Remove pub fn extend()
  - Implement std::iter::Extend<T> trait
  - Call sites unchanged (same method name)

T6: Add FromIterator impl and simplify inst_n:
  - Implement FromIterator<T> for Env<T>
  - Replace two-step extend in inst_n with single collect()

T7: Clean up documentation:
  - Trim struct doc comment to first line only
  - Remove premature panic documentation blocks

T8: Fix misleading variable name in pretty.rs:
  - Rename env_before → depth_before (it holds a Depth, not an Env)

All tests pass (55 snapshots, 0 failures). CI clean (no clippy warnings).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace get_at_ix/get_at_lvl method calls with idiomatic indexing operators
now that Index<Ix>/Index<Lvl> traits are implemented. This is cleaner and
more consistent with Rust conventions.

Changes:
  - pretty.rs: env.get_at_ix(*ix) → &env[*ix]
  - value.rs: env.get_at_ix(*ix) → env[*ix]
  - value.rs: doc comment updated to reference env[lvl]
  - staging/mod.rs: self.bindings.get_at_ix(ix) → &self.bindings[ix]

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Break up the iterator chain in inst_n to name the closure environment
iterator separately, making the code structure clearer.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@iljakuklic iljakuklic merged commit a648b7f into devel Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Ctx: merge parallel vecs into Vec<CtxEntry>

1 participant