Skip to content

A template passed through a ~ parameter bypasses descent and proves False{} == True{} #902

Closed

Description

@jasisz

What you did

bun bend2/main.ts template_cycle.bend --check-only

What happened

All terms check.

The file contains a closed boom() -> Empty and derives False{} == True{} from it. It does not import Base and uses no @unsafe, FFI, law, hole or runtime backend.

The direct self-call control is rejected as expected:

def direct(~k: Key, u: Unit) -> Empty:
  direct(~k, u)
Error:
- expected : a decreasing self-call (arguments are read left to right: each passed unchanged until one shrinks)
- observed : direct

The accepted version moves the partial self-reference through a template parameter:

def bounce(~f: Unit -> Empty, u: Unit) -> Empty:
  f(u)

def loop(~k: Key, u: Unit) -> Empty:
  bounce(~loop(~k), u)

During the generic check of loop, loop(~k) is the erased ~f argument, so the reference is checked dead and the self-call descent check does not run. Checking the closed call later creates loop~0, whose body creates bounce~0. The instantiated body of bounce~0 uses its opaque template argument live and becomes a call back to loop~0.

At that point loop~0 already exists as the in-progress placeholder installed by def_inst. Reusing that instance does not recheck that its body is still null, and the current lhs is bounce~0, so it is not recognized as a self-call either. The accepted live graph is therefore:

loop~0(u)   = bounce~0(u)
bounce~0(u) = loop~0(u)

This is a two-definition cycle with no decreasing argument. It breaks the checker's weak-normalization claim and admits a closed inhabitant of Empty.

The file

type Empty is Data:

type Unit is Data:
  Unit{}

type Key is Data:
  Key{}

type Bit is Data:
  False{}
  True{}

def bounce(~f: Unit -> Empty, u: Unit) -> Empty:
  f(u)

def loop(~k: Key, u: Unit) -> Empty:
  bounce(~loop(~k), u)

def boom() -> Empty:
  loop(~Key{}, Unit{})

def absurd(-A: Type, e: Empty) -> A:
  match e:

def false_theorem() -> {False{} == True{} : Bit}:
  absurd({False{} == True{} : Bit}, boom())

bend --version

bend 2.0.20
commit 75616561

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. added
    bug
    on Sep 20, 2026
  2. jasisz commented on Sep 20, 2026

    @jasisz
    ContributorAuthor

    Two follow-ups confirm that this is not limited to the two-node example.

    Adding main() -> Empty: boom() and importing Base lets both backends build. The generated JS and C programs both remain in the accepted recursion past a two-second timeout. The inhabitant is therefore the expected live infinite loop at runtime.

    A three-instance cycle also checks:

    def hop2(~f: Unit -> Empty, u: Unit) -> Empty:
      f(u)
    
    def hop1(~f: Unit -> Empty, u: Unit) -> Empty:
      hop2(~f, u)
    
    def loop(~k: Key, u: Unit) -> Empty:
      hop1(~loop(~k), u)
    
    def boom() -> Empty:
      loop(~Key{}, Unit{})
    
    All terms check.
    

    The checked instances form:

    loop~0(u) = hop1~0(u)
    hop1~0(u) = hop2~0(u)
    hop2~0(u) = loop~0(u)
    

    So rejecting re-entry into any in-progress instance would close this reproducer directly. A more general termination check needs to cover the whole instantiated call-graph SCC rather than only the current lhs or a caller/callee pair.

  3. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Templates are the one part I didn't formalize (yet) and they're full of holes. Thanks for the report, will fix this one now, but likely there will be more, sadly.

  4. jasisz commented on Sep 20, 2026

    @jasisz
    ContributorAuthor

    I’ll take that as a hint and keep poking at templates ;)

  5. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.21 (bend update).

    The cause was where you placed it. def_inst mints an instance as a bodiless placeholder while its body is checked. When bounce~0's body reached loop(~Key{}, u), the key already named loop~0, and the placeholder came back unchecked: the "filled definition" guard had looked at the template loop, which has a body, and the self-call guard compares against the current lhs, which was bounce~0.

    Now a reuse of an instance whose body is still being checked, from any other lhs, is refused as a self-call that does not decrease: it is a call back into an instance on the check stack, and no column of the current lhs can show it shrinking. Your two-node and three-node files both stop at the loop inside ~loop(~k):

    - expected : a decreasing self-call (arguments are read left to right: each passed unchanged until one shrinks)
    - observed : loop
    

    An instance that calls itself directly with a shrinking argument still checks. Instances are minted depth first, so with re-entry refused the live call graph of instances is a tree plus descending self-loops, the same shape plain defs already have (a forward reference is undefined, so mutual recursion between defs is refused too). Your two-node file is pinned as tests/check/template_inst_cycle.bend.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions