Instances, constraints, and evidence

An instance supplies the members of a structure for particular arguments. requires asks the compiler to select that evidence coherently at a use site.

Declaring instances

instance Monoid(List(a)) requires Monoid(a) = {
    value empty = Nil;
    function combine(xs, ys) = append(xs, ys)
};

The member types are inherited from the matched structure after substitution. Missing or incompatible members are errors.

Compiler or target implementations may be intrinsic:

instance Additive(Int) = intrinsic;

Intrinsic evidence is still registered, typed, and resolved before the backend provider boundary.

Using constraints

function combineAll[a:Type](xs:List(a)) : a
    requires Monoid(a) = fold(combine, empty, xs);

The caller does not write a hidden dictionary argument when one coherent choice is available. Typed elaboration nevertheless records the selected evidence and compiled specialization closes over it explicitly.

Named alternatives

instance Format(Date) as iso8601 = { ... };

Tags permit intentional alternatives. Unnamed implicit selection must remain coherent in lexical and target context; the compiler does not guess between equally valid global candidates.

First-class evidence

Evidence can be packaged, passed, or closed over like other typed data when an API needs explicit control. requires is simply the concise common case.

This supports higher-order generic programming without performing semantic instance lookup inside a target backend. By RuntimeCore, calls and evidence identities are already closed.

Current native limitation found during guide testing

Pure generic evidence use is covered by the native path. A generic effectful wrapper with requires Show(a) currently exposes a native evidence-lowering gap in one tested shape even though front-end checking succeeds. The guide uses a pure generic rendering helper plus a concrete effectful wrapper until that gap is closed; the language rule itself is unchanged.

Common mistakes

An import order is not a coherence policy. Give intentional alternatives names, avoid two unnamed candidates for the same head, and do not expect a backend to repeat source-level instance search.

Recap

Instances are typed evidence values. requires requests one coherent value at a call site, and elaboration records that choice before target lowering.

Normative details: Language Reference §9.5.