A `have` proved by `rfl` is re-typed at each use (`infer_let` substitutes the value), so proofs Lean checks instantly overflow the stack

Open
#25 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
compilers

Research direction

Start at the .letE branch of infer_let in ConLeche/Kernel/Core.lean, where the body is inferred with b.instantiate1 v instead of a local declaration carrying ty and v. Read the official infer_let in src/kernel/type_checker.cpp alongside it to see the mk_local_decl pattern to mirror. Then check Repro/ViaHave.lean with con-leche --olean --search-path=...; done means it is accepted without the stack overflow, with Repro/ViaLemma.lean still passing.

Written by the indexing model from the issue text.

Description

ConLeche/Kernel/Core.lean infers a let body with the value substituted:

| .letE ty v b => do
  -- Task #342 S0: official `infer_let`
  let _ ← ensureSort r env depth (← r.infer depth ty)
  unless ← r.defeq depth (← r.infer depth v) ty do
    throw (.invalid "let value type mismatch")
  r.infer depth (b.instantiate1 v)

The official infer_let (src/kernel/type_checker.cpp) does the same two checks, but then infers the body with a local declaration that carries the declared type (m_lctx.mk_local_decl(…, type, val)), not with the value substituted. When the value's own inferred type differs from the declared one, the two differ. The declared type is then lost at every use of the variable, and con-leche has to re-establish it at the arguments it is used at.

Reproduction (Lean core only, toolchain v4.36.0; also as a project with a run script at https://github.com/zksecurity/con-leche-let-repro):

-- Repro/Basic.lean
def not32 (a : Nat) : Nat := a ^^^ 0xffffffff
def Ch (e f g : Nat) : Nat := (e &&& f) ^^^ (not32 e &&& g)

def deep : Nat → Nat
  | 0 => 0
  | n + 1 => deep n + 1

theorem Ch_def : ∀ a b c : Nat, Ch a b c = (a &&& b) ^^^ ((a ^^^ 4294967295) &&& c) :=
  fun _ _ _ => rfl

-- Repro/ViaHave.lean
theorem viaHave :
    Ch (deep 10000000) 1 2 = (deep 10000000 &&& 1) ^^^ ((deep 10000000 ^^^ 4294967295) &&& 2) := by
  have h : ∀ a b c : Nat, Ch a b c = (a &&& b) ^^^ ((a ^^^ 4294967295) &&& c) := fun _ _ _ => rfl
  exact h _ _ _

-- Repro/ViaLemma.lean
theorem viaLemma :
    Ch (deep 10000000) 1 2 = (deep 10000000 &&& 1) ^^^ ((deep 10000000 ^^^ 4294967295) &&& 2) :=
  Ch_def _ _ _

Here is what happens when each module is checked with con-leche --olean --search-path="$(lean --print-libdir):.lake/build/lib/lean" -- MODULE:

lean con-leche bc667610
Repro.ViaLemma 0.2 s accepted, 3.6 s
Repro.ViaHave 0.2 s Stack overflow detected. Aborting. (exit 134) after 43 s, 13 GB peak

viaHave's value is have h : ∀ a b c, Ch a b c = R a b c := fun x y z => @rfl Nat (Ch x y z); h (deep 10000000) 1 2, where R a b c is the right-hand side. Checking the value against the declared type with a b c as variables is cheap: unfolding Ch and not32 makes the sides syntactically equal. After the substitution, though, h (deep 10000000) 1 2 has type Ch (deep 10000000) 1 2 = Ch (deep 10000000) 1 2. Matching that against the statement is Ch t 1 2 ≡ R t 1 2 at the closed t = deep 10000000, and that is what overflows. The official kernel cannot do this equality either: adding viaLemma's statement with value Eq.refl _ through addDecl fails with "(kernel) deep recursion detected" after about 100 s. It just never has to check it.

This is not a soundness issue, but proofs Lean accepts are refused, and by an abort rather than a decline. We hit it on a real proof, where have Ch_def : … := fun _ _ _ => rfl; rw [Ch_def] was used at sums of 32 bit-valued field elements in a SHA-256 circuit's spec proof. Hoisting the have to a top-level lemma, as in viaLemma, avoids it. The fix we would expect is to infer the body with the let variable in the local context, carrying ty and v, as the official kernel does. The value then stays available to ζ-reduction where it's needed, but no use has to re-derive the declared type.

This issue was written by AI (Claude and Codex), as was the reproduction. The numbers above come from running the reproduction as shown.

Dominant language
Lean
Stars
212
Forks
19
Avg merge
2d 8h
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/con-leche

All issues in leanprover/con-leche

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.