Skip to content

Templates: a ~-argument that is a plain def parameter fails to check with "a defined name" #784

Closed

Description

@docxology

What you did

bend localvar.bend
# localvar.bend
import Base

def inc(x: Nat) -> Nat:
  1n + x

def apply_map(g: Nat -> Nat, xs: List<Nat>) -> List<Nat>:
  List.map(~Nat, ~Nat, ~g, xs)

def total(xs: List<Nat>) -> Nat:
  match xs:
    case Nil{}:
      0n
    case h <> t:
      h + total(t)

def main() -> IO(Unit):
  do IO<Unit>:
    IO.print(Nat.show(total(apply_map(inc, [1n, 2n]))))

What happened

Error:
- expected : a defined name
- observed : List.map
Context:
- g  : @_:Nat -> Nat
- xs : List<&1, Nat>
Location: apply_map
6 | def apply_map(g: Nat -> Nat, xs: List<Nat>) -> List<Nat>:
7>|   List.map(~Nat, ~Nat, ~g, xs)

Calling a template with a ~ argument that is a plain def parameter never checks. Closed ~ arguments work (List.map(~Nat, ~Nat, ~inc, xs) is fine, and tests/comptime/* pass), but the moment a wrapper receives the function as a runtime parameter, the whole templated stdlib (List.map, List.sort, List.foldl, ...) becomes unreachable from higher-order code.

Mechanism

  • parse_term_ops (bend.ts:2039-2047) builds the instantiation key via term_higher(x, env) where env maps every scope-bound variable to Ref("\0"). A ~ argument that is a def parameter makes the key contain \0, and the if (!key.includes("\0")) guard skips minting entirely, leaving the call as a plain Ref("List.map").
  • Template defs are stored only in book.tmps (bend.ts:2487), never in book.tlds/book.order, so infer-ref (bend.ts:3274) throws a defined name for the template name itself.

Note the error message is also misleading: List.map is a defined name, it is a template.

Expected

Either mint the instance treating the open ~ argument as an ordinary erased parameter, or produce an error that says the ~ argument must be closed (a global/comptime value), naming the offending argument.

Version / system

  • bend 2.0.5 (main @ e6676b0), bun main.ts from a source checkout
  • Darwin arm64

Activity

  1. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    The rejection is by design: templates are comptime, so a ~ argument must be closed; a def parameter is not. The message was wrong, though. In 2.0.6 it reads "expected: a template applied to closed ~ arguments (a def parameter is not comptime)". Accepting open ~ arguments (making ~f a runtime closure when open) would change what ~ means and stays a design question for later.

    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.

  2. added a commit that references this issue on Sep 18, 2026
    88d6864
  3. added a commit that references this issue on Sep 27, 2026
    d80f06a
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions