Skip to content

[Cubical] Constructor injectivity via orthogonal retract decomposition and parallel de Bruijn lifting (partially fixes #3733) - #1

Merged
clearnature merged 1 commit into
masterfrom
fix/cubical-injectivity-retract
Jul 5, 2026
Merged

clearnature merged 1 commit into
masterfrom
fix/cubical-injectivity-retract

Conversation

@clearnature

Copy link
Copy Markdown
Owner

Title

[Cubical] Constructor injectivity via orthogonal retract decomposition and parallel de Bruijn lifting (partially fixes #3733)


Description

Problem

In Cubical Agda, with-abstraction on inductive types (Fin, ℕ, etc.) triggers UnsupportedIndexedMatch warnings. Two interlocking defects cause this:

  1. digestUnifyLog missing Injectivity branch: the injectivity unification step falls through to _ → unsupported, so the left inverse is never computed.

  2. buildLeftInverse cond guard too conservative: null <$> getContext returns Left UnsupportedCxt whenever the coverage checker adds context bindings above the unification telescope, blocking ALL injectivity left inverses. The upstream TODO comment acknowledges this:

    -- TODO: handle open contexts: they happen during "higher dimensional"
    --       unification, in injectivity cases.

This PR fixes both, moving agda#3733 from icebox to partial resolution.


Architecture: CRT-inspired orthogonal decomposition

The architectural design is inspired by the Chinese Remainder Theorem (CRT) — formalised and generalised in the triqchem-lab/discrete-mathematics library. The core insight: for coprime moduli p, q, the isomorphism Z/M ≅ Z/p × Z/q allows decomposing a complex modulo-M problem into independent sub-problems, each solved locally, then composed via the isomorphism.

Constructor injectivity admits the same orthogonal decomposition:

CRT dimension This PR
mod p sub-problem Index dimension: HDU (higher-dimensional unification) solves Fin.suc index equations n = n'
mod q sub-problem Field dimension: dynamically generated projection functions decompose c us = c vs into field equations
CRT reconstruction Parallel lifting: raiseS (1 + nEq2 + nctel) embeds the HDU local left inverse into the global telescope

Telescope topology (innermost → outermost):

eqTel2' | ctel | eqTel1' | phi | gamma
         ──┬──   ───┬────
    projection   HDU scope
    scope

Each segment is handled independently in tau_full and leftInv_full:

  • gamma, phi, eqTel2': identity (homotopy = raiseS 1)
  • eqTel1': HDU's tau_hdu / leftInv_hdu, lifted via raiseS (1 + nEq2 + nctel)
  • ctel: projection-generated makeTau terms

Changes

7 files, +289/−18:

File Change
Types.hs New unifyHduTauInv field (stores HDU left inverse thunk) + tellHduTauInv. Either NoLeftInv downgraded to Either () to avoid module cycle
Unify.hs __IMPOSSIBLE__ → Nothing; capture 4th component getTauInvHDU from Unifies
LeftInverse.hs (1) DInjectivity digested step + PrettyTCM; (2) extraCxt = max(0, cxtLen - size(varTel)) open context handling; (3) buildEquiv dynamic projection generation + makeTau; (4) HDU thunk forcing + CRT parallel lifting
CHANGELOG.md Entry

Test coverage

Test Path Key verification
InjectivityWith.agda ℕ with (non-indexed, complete) raiseS 1 is NOT a stub
InjectivityIndexed.agda Fin with (indexed, complete) HDU thunk capture + context alignment
InjectivityPartial.agda Fin incomplete patterns Forces tau evaluation — coverage checker generates transpX clause, injects into AST. 0 warnings, 0 variable capture
InjectivityHITs.agda HIT regression Unmodified behavior

InjectivityPartial.agda is the crucible — it proves that tau and leftInv survive dynamic telescope alignment under extreme AST rewriting.


Design decisions

1. leftInv = raiseS 1 is correct, not a stub

For non-indexed types, transp distributes definitionally over constructors:

transp (λ i → D pars) φ (c us) = c (transp A₁ φ u₁, ...)

Thus rho[tau] = id holds strictly. raiseS 1 is the correct trivial homotopy.

2. Projections generated via addConstant during typechecking

Same pattern as Data.hs:defineProjections. Functions are non-recursive, single-clause (proj_i (c x₁...xₙ) = x_i). CompiledClauses constructed manually to avoid the compileClauses → Coverage → Unify module cycle.

3. All boundary paths audited

Path Injectivity step? getTauInv? Handled
HIT constructor No (consOfHIT rejects) No Not triggered
--without-K + HDU fail Yes, returns UnifyStuck No Not triggered
Non-indexed fallback Yes, returns Unifies Yes makeTau + raiseS 1
Indexed HDU success Yes, returns Unifies Yes CRT parallel lifting

Scope (what this does NOT cover)

This PR resolves injectivity for with-abstraction pattern matching. It does NOT address:

These remain open for future work.


Related


AI Assistance Declaration

Code implementation used AI assistance (architecture design, type inference, de Bruijn index calculation). All decisions and final code were manually reviewed and tested. The theoretical architecture (CRT orthogonal decomposition) derives from human research in the triqchem-lab/discrete-mathematics library.

…n and parallel de Bruijn lifting (partially fixes agda#3733)

- DInjectivity digested step + buildEquiv with projection generation
- HDU left inverse thunk capture (replaces __IMPOSSIBLE__)
- CRT-inspired orthogonal decomposition: index dimension (HDU) × field dimension (projections)
- Open context support via extraCxt + liftS for coverage checker
- 4 tests: non-indexed, indexed, HIT regression, partial pattern

AI assistance: architecture design, type inference, de Bruijn index calculation.
All decisions and final code manually reviewed and tested.
CRT orthogonal decomposition architecture derives from human research
in the triqchem-lab/discrete-mathematics library.

Co-authored-by: Yan Li <[email protected]>
@clearnature

Copy link
Copy Markdown
Owner Author

中文补充说明 / Chinese Supplement

本 PR 的理论架构来源于 律算合一 (Sovereign) 框架——在 triqchem-lab/discrete-mathematics 库中被形式化。核心洞察来自中国剩余定理(CRT)对正交维度分离的启发:

Z/M ≅ Z/p × Z/q 的 CRT 同构(p, q 互素)允许将模 M 的复杂问题拆解为两个独立子问题——各自在局部作用域求解,然后通过同构映射组合。构造子内射性证明遵循完全相同的分解模式:

  • 索引维度(≈ 模 p):HDU 在 addContext(varTel s) + hduTel 局部作用域求解索引等式
  • 字段维度(≈ 模 q):动态生成的投影函数将构造子展开为字段等式
  • CRT 组合(≈ 同构映射):raiseS (1 + nEq2 + nctel) 将 HDU 的局部左逆精确嵌入全局望远镜

此架构在 Sovereign 库中已有完整的形式化推导链:

GF(3) 三元场 → C3 手征旋转 → Christoffel 螺旋 → 五行组织 → A₄ 不可约表示 → 三代费米子

本 PR 是该理论框架在 Cubical 类型论编译器中的首次工程应用。


The theoretical architecture of this PR derives from the Sovereign (律算合一) framework, formalised in the triqchem-lab/discrete-mathematics library. The core insight — orthogonal dimension separation inspired by the Chinese Remainder Theorem — treats constructor injectivity as the composition of two independent retracts:

  • Index dimension: HDU solves index equations in local scope
  • Field dimension: dynamically-generated projections decompose constructor fields
  • CRT composition: parallel de Bruijn lifting embeds the local retract into the global telescope

This architecture is the first engineering application of the Sovereign framework's CRT generalisation to a Cubical type theory compiler.

@clearnature
clearnature merged commit ef99d9f into master Jul 5, 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.

1 participant