Skip to content

quantity: @linear exactly-once not enforced across loops (consumed per iteration) or branches (dropped on one path) #858

Backlog

Description

@hyperpolymath

The quantity checker (lib/quantity.ml) accepts two ways of breaking the exactly-once rule for @linear bindings. This is independent of lib/borrow.ml. First recorded in issues-drafts/09-round3-borrow-affine-soundness-probe.adoc (holes 2 and 3); not filed until now.

Reproduce

Re-verified on main 464f7c6 with main.exe check:

fn consume(@linear r: Int) -> Int = r + 1;

// Hole 2: consumed once per iteration, so up to n times
fn loop_param(@linear x: Int, n: Int) -> Int {
  let mut i = 0;
  while i < n { consume(x); i = i + 1; }
  0
}

// Hole 3: never consumed when c is false
fn drop_on_else(@linear x: Int, c: Bool) -> Int {
  if c { consume(x) } else { 0 }
}
Program check Should be
while loop (hole 2) Type checking passed rejected: used many times
for i in [1, 2, 3] { consume(x); } (hole 2) Type checking passed rejected
if c { consume(x) } else { 0 } (hole 3) Type checking passed rejected: unused on one path
match c { true => consume(x), _ => 0 } (hole 3) Type checking passed rejected
control: consume(x); consume(x) Quantity error: … used multiple times (correct)
control: fn f(@linear x: Int) -> Int { 0 } Quantity error: … never used (correct)

Cause (from the draft; line numbers approximate)

  • Hole 2. StmtWhile/StmtFor merge the two passes with env_join (a per-variable max). join_usage UOne UOne = UOne, so a once-per-iteration use is never promoted to many. The comment there ("any variable used in the loop body is used >= 2 times") says the opposite of what the code does.
  • Hole 3. join_usage takes the max of the branch usages. Max is right for at-most-once, but @linear means exactly-once: the checker already rejects zero uses on a straight path, but not on one branch.

Fix (suggestions, unverified)

  • In loops, scale the per-iteration usage to many (or add it to itself), as the comment intends.
  • For QOne bindings, branch merge must flag any branch that leaves the variable unused. Keep max for QOmega so affine bindings are not over-rejected.
  • Add both controls and all four programs above as tests.

This bears on #516 (proof: @linear consumed exactly once). That proof cannot hold for the implementation while either hole is open. Refs #563, #513.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: lib/quantity.ml accepts @linear bindings that break exactly-once. In loops (while/for), env_join merges with max, so UOne per iteration stays UOne instead of becoming many. In branches (if/match), max hides a path where the variable is never used. Reproduced on main 464f7c6.

    Expected: check rejects all four programs: "used multiple times" for the loops and "unused on one path" for the branches. Affine (QOmega) branch merging stays permissive. Both controls keep their current errors.


    Clarity: 95%. Clear enough to work on.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  2. added
    architecture
    bindings
    bug
    priority:p1
    scope:repo
    on Dec 1, 2026
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

    architecturebindingsbugpriority:p1scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions