std::derive

std/src/derive.mo

Derive macros: derive_lens, derive_debug, derive_beq, derive_bord, derive_copy, derive_clone — written using Monad's own macro system.

Each defmacro registers its #[derive ...] target with #[derive_handler <Target>] — the registration the derive-target registry resolves the attribute through, on both compilers (no hardcoded target list anywhere). Two different macros registering the same target is a compile error.

All six are ported to the reflection-as-data kernel (init/meta.mo's TypeInfo/Expr/MetaDecl values + rust/core/src/eval/meta_compile.rs's MetaEvalContext, invoked via the reflect_type_info! intrinsic) — see plans/review-and-reduce-the-greedy-nest.md: each derive_* stays an ordinary defmacro/decl-gen wrapper (so derive_*!/ #[derive ...] dispatch is completely unchanged), but its BODY is now just reflect_type_info! T derive_*_meta, splicing in the result of CALLING an ordinary TypeInfo -> List MetaDecl function, evaluated for real (arbitrary Monad computation — List.map/match/recursion), not template-substituted — rather than composing built-in per-constructor/ per-field dispatch intrinsics. The OLDER 5-intrinsic kernel these replaced (reflect_ctors!/reflect_ctor_fields!/ reflect_pairwise_ctors!/reflect_fields!/reflect_set_field!) has since been deleted outright — see rust/core/src/eval/macro_expand.rs's BUILTIN_INTRINSICS doc comment for the current (reflect_type_info!- only) design.

defderive_lens_meta#
pub def derive_lens_meta (info : TypeInfo) : List MetaDecl

derive_lens! Point (or #[derive Lens] struct Point {...}) generates Point.x : Lens Point I64/Point.y : Lens Point I64 — one field lens per field. T must have exactly one constructor (a struct, or a single-constructor type) — a lens focuses on exactly one field of exactly one shape; a multi-constructor type generates nothing (silent, matching this port's scope — see init.optics's Prism for sum-type variant access instead).

defderive_debug_meta#
pub def derive_debug_meta (info : TypeInfo) : List MetaDecl

#[derive Debug] struct Point { x : I64, y : I64 } (or, called directly, derive_debug! Point) generates a rust-style structural Debug Point instance: Debug.debug produces "Point { x: 1, y: 2 }" for a struct, or "TypeName::CtorName { field: repr, ... }" per variant for a multi-constructor type.

Every field's own type must have a Debug instance in scope (Debug is applied recursively, field by field) — std/debug.mo ships base instances for String/I64/Bool.

defderive_beq_meta#
pub def derive_beq_meta (info : TypeInfo) : List MetaDecl

#[derive BEq] struct Point { x : I64, y : I64 } (or, called directly, derive_beq! Point) generates a structural BEq Point instance: a == b iff a/b were built with the same constructor and every field is pairwise ==.

Every field's own type must have a BEq instance in scope (BEq is applied recursively, field by field) — init/prelude.mo and init/number.mo/init/string.mo ship base instances for the numeric types, Bool, String, and Nat.

defderive_bord_meta#
pub def derive_bord_meta (info : TypeInfo) : List MetaDecl

#[derive BOrd] struct Point { x : I64, y : I64 } (or, called directly, derive_bord! Point) generates a structural BOrd Point instance: constructors ordered by declaration position (first sorts lowest), fields within the same constructor compared lexicographically in declared order. All four BOrd methods are emitted because the class declares no defaults (lte/gte are required).

Every field's own type must have a BOrd instance in scope, same requirement as derive_beq's BEq above.

defderive_copy_meta#
pub def derive_copy_meta (info : TypeInfo) : List MetaDecl

#[derive Copy] struct T { ... } generates the structural instance: duplicate every field with Copy.dup and rebuild the constructor twice. Every field type must have a Copy instance; a #[linear]/#[proof] carrier is rejected by the instance-kind rule, and an #[opaque] carrier cannot rebuild its unreferenceable witness. A generic type is rejected here (TypeInfo.params is non-empty) because MetaDecl.d_instance cannot carry the [Copy A] constraints it would need.

Known limit: a monomorphic type whose field has no Copy instance is not caught by monad check. The generated match binders have no written type (QTT Known Gap #2), so the Copy.dup call fails open; the rejection is a codegen no instance found instead. The Copy.dup helper exists so the moment attribution improves, the call becomes a real check-time obligation.

defderive_clone_meta#
pub def derive_clone_meta (info : TypeInfo) : List MetaDecl

#[derive Clone] struct T { ... } generates the structural instance: match the borrowed source, and rebuild each constructor from Clone.clone_field (Shared.of field) — the same shape init/clone.mo's hand-written Pair/Option/Result/List instances use. Every field type must have a Clone instance. A generic type is rejected here for the same reason as derive_copy_meta, and a monomorphic non-Clone field is likewise a codegen-time no instance found until match-binder attribution improves (see derive_copy_meta's Known limit).