lang::typecheck::copy_class

lang/src/typecheck/copy_class.mo

Is a type Copy? — the gate that decides whether a binder may be used more than once.

Milestone 2 of the affine-by-default experiment (Design B: Viability Experiment). Under Design B every binder is affine by default and Copy A resolving is the only route to ω, so this one predicate is what stands between a program and a use-after-move. It is deliberately small and deliberately paranoid.

Real instances, not a table

The verdict comes from resolving a genuine Copy instance (init/src/copy.mo) through the scope's own instance registry, so a user type can opt in with instance Copy T { ... } and the gate is extensible rather than closed.

This module deliberately does NOT import init::copy. Importing it here looks like it should put the instances in scope and does not: they reach a scope only via the elaborated dependency closure of whatever is being checked, and lang/src/lib.mo re-exports a small surface that never reaches this file. Measured, not assumed — see bench/src/affine_target.mo, which records the experiment that established it.

A concern was raised and checked before this was built: the corpus documents a "first-registered-instance-wins" fragility in instance resolution. That is real, but it is the evaluator's resolve_class_method_instance — a different path. find_matching_instance (lang/src/scope.mo) does match on the carrier, preferring fully-concrete instances and falling back to wildcards. It is sound to build on. What it does not do is notice when two instances both match, which is why this module counts matches itself rather than taking the first.

Two rules that make it safe

Fail closed. Anything this cannot resolve confidently — an unknown head, a bare type variable, a hole, a function type — registers as NOT Copy. The asymmetry is the whole point: a missed Copy costs one redundant release at run time, while a wrongly granted Copy is a double free. Every uncertain case must fall on the cheap side.

Ambiguity is an answer, not a coin flip. Two concrete instances matching the same carrier is reported as cv_ambiguous, never silently resolved to whichever the scan reached first. mk-style collisions are endemic here — lang/src/typecheck/usage.mo's own attribution had to fail closed on exactly this — and a wrong pick in this predicate frees memory that is still live.

typeCopyVerdict#
pub type CopyVerdict {
    /// A single instance matched: the binder may be used freely.
    cv_copy,
    /// No instance matched. Affine.
    cv_not_copy,
    /// Two or more concrete instances matched the same carrier.
    /// Affine, and worth reporting — it means the program has a real
    /// instance collision that some other resolution path is currently
    /// deciding arbitrarily.
    cv_ambiguous,
}

The gate's verdict. Three outcomes, not two, because "I could not tell" and "no" need to be distinguishable by the caller even though both deny ω.

defcopy_verdict_grants_omega#
pub def copy_verdict_grants_omega (v : CopyVerdict) : Bool
defis_copy#
pub def is_copy (scope : Scope) (typ : Term) : Bool

Does typ have a Copy instance in scope?

defcopy_verdict#
pub def copy_verdict (scope : Scope) (typ : Term) : CopyVerdict

The full verdict, for callers that want to distinguish "no" from "could not tell".