RFC-011: Generic Type System Design - Zero-Cost Abstractions and Macro Alternatives
Abstract
This document defines the generic type system design of the YaoXiang language. It uses powerful generics to implement zero-cost abstractions, reduces reliance on macros through compile-time optimization, and provides a dead-code elimination mechanism.
Core design:
- Unified signature syntax: Generic parameters and ordinary parameters are unified as
(T: Type, R: Type) -> ... Typeself-description mechanism:Typeis a language-level special entity, andTypepositions in signatures can be inferred and filled automatically- Type constraints: Multiple constraints such as
T: Dup + Add, as well as function type constraints - Associated types:
Iterator: (Item: Type) -> Type = { next: () -> Option(Item), has_next: () -> Bool } - Compile-time generics:
N: Intgeneric value parameters, instantiated with compile-time constants - Conditional types: Type-level computation with
If: (C: Bool, T: Type, E: Type) -> Type, forming type families
Benefits:
- Zero-cost abstraction: Compile-time monomorphization with no runtime overhead
- Dead-code elimination: Instantiation graph analysis + LLVM optimization
- Macro replacement: Generics replace 90% of macro use cases
- Type safety: Compile-time checks and IDE-friendly diagnostics
- Explicit over implicit: Self-describing
Typewith automatic compiler inference
Reference Documents
This design is based on the following documents:
| Document | Relationship | Description |
|---|---|---|
| RFC-010: Unified Type Syntax | Syntax basis | Integrates generic syntax with the unified name: type = value model |
| RFC-010: Unified Type Syntax | Call syntax | Section 6: Generic call syntax—unified () application and complete removal of [] |
| RFC-009: Ownership Model | Type system | Natural integration of move semantics and generics |
| RFC-024: spawn-Based Concurrent Runtime Semantics | Execution model | DAG analysis and generic type checking |
| RFC-008: Runtime Model | Compiler architecture | Generic monomorphization and compile-time optimization strategies |
| The Type Universe Idea (see the identically named section below) | Theoretical core | Type universe hierarchy model and value-dependent type design |
| RFC-027: Compile-Time Predicates and Unified Static Verification | Termination checking | Automatic measure synthesis and compile-time evaluation safety guarantees |
The Type Universe Idea and Value-Dependent Types
YaoXiang's generic type system is built on the Type Universe Idea. This mental model unifies all concepts in the language into a layered structure, with its core innovation being the elevation of value-dependent types to first-class citizens at the Type2 layer.
What Are Value-Dependent Types?
A value-dependent type is a type that depends on one or more values, rather than only on other types. These values can be evaluated at compile time, providing type-safety guarantees during compilation.
# Traditional generics: type parameters
List: (T: Type) -> Type
# Value-dependent types: value parameters
Array: (T: Type, N: Int) -> Type # The array type depends on the length value N
Matrix: (T: Type, Rows: Int, Cols: Int) -> Type # The matrix type depends on the number of rows and columnsLayering of Container Type Names
The language layer has three container concepts. The fundamental distinction between them is where length information belongs:
| Type | Length | Semantics | Underlying implementation |
|---|---|---|---|
Array(T, N) | In the type | Fixed-length array with N in the type | Core primitive (stack/inline preferred) |
Vec(T) | Runtime value | Primitive buffer with a runtime length, capable of growth | Core primitive (contiguous heap buffer) |
List(T) | Runtime value | Growable-list annotation whose source name is List | Same as Vec(T) (source-name landing is normalized) |
The division of responsibility among the three is as follows:
Array(T, N)is the only form that places the length in the type—the length is a compile-time constant, so out-of-bounds failures can be rejected at compile time (a[5]produces a direct compile-time error whena: Array(Int, 3); see “Compile-Time Dimension Validation” below).Vec(T)is the minimal foundation for runtime lengths—it provides only four capabilities: allocation, length retrieval, reading and writing, and growth. Capacity policy, growth factors, and whether to shrink are all deliberately omitted. It serves as the raw material for building other containers.List(T)is not a type independent ofVec(T)—since commit 22a67d93,std/list.yxis built entirely onVec(T)and no longer defines aListrecord; the source nameListis normalized to theVecside in the type domain (landing normalization for #403). All growable-semantics policies—when to grow, how much to grow, and whether sharing is possible—live in the library. The compiler is not involved.
The construction form of Vec(T) (two layers: type parameters first, then construction arguments):
# Empty construction—length 0, with elements appended later
v = Vec(Int)()
# Element construction—length determined by the number of elements
w = Vec(Int)(1, 2, 3) # Length 3
# Slot allocation—allocate n zero-valued slots
buf = Vec(Int)(len=64) # Length 64, with all elements set to zeroSlot allocation uses a field-name form (
len=) rather than a positional form: a positional single integer would be ambiguous with a “single-element vector” (Vec(Int)(64)cannot distinguish “length 64” from “one element whose value is 64”). This is consistent with the unified rules for generic construction: field-name arguments are bound by name and are unaffected by positional inference.This is the only primitive required for library-level growth—
std.list'spushwrites directly at the end of the buffer (Vecgrowth leaves room), while growth and relocation are completed by the library on top of theVecprimitive:yaoxiangnew_data = Vec(T)(len=self.length * 2)
std.listdetermines when to grow, how much to grow, and whether to shrink.Vecdoes not implement a capacity policy.
From bottom to top, performance decreases while flexibility increases: Array > Vec.
Naming rationale:
Vec/vectordenotes a runtime-length growable sequence in mainstream languages (Rust/C++), whereasArrayspecifies a fixed length.
Core Advantages of Value-Dependent Types
Compared with traditional generics, YaoXiang's value-dependent types offer the following core advantages:
| Feature | Traditional Generics (C++/Rust) | YaoXiang Value-Dependent Types |
|---|---|---|
| Values on which types depend | Depend only on type parameters | May depend on any value, including the result of a function call |
| Compile-time evaluation | C++ templates require manual specialization; Rust has none | Automatic compile-time evaluation with guaranteed termination |
| Type-level computation | Template metaprogramming (complex/dangerous) | Unified type-level computation engine |
| Type safety | Unavailable in C++; limited in Rust | Complete type safety with compile-time checks |
| Dimension validation | Runtime checks or manual specialization | Compile-time dimension validation with no runtime overhead |
Type Universe Hierarchy and Value-Dependent Types
The Type Universe Idea divides language concepts into different levels according to their semantic roles. Value-dependent types reside at the Type2 layer:
| Level | Role | Examples |
|---|---|---|
| Type-1 | Values | 42, factorial(5), and functions themselves |
| Type0 | Metatype keyword | Type |
| Type1 | Concrete types | Int, String, Array(Int, 3) |
| Type2 | Functions/type constructors/value-dependent types | add: (Int, Int) -> Int, Array: (T: Type, N: Int) -> Type, Matrix: (T: Type, Rows: Int, Cols: Int) -> Type |
Key design: Functions, type constructors, and value-dependent types at the Type2 layer use unified syntax. All have the form (params) -> result:
- Ordinary function:
(Int, Int) -> Int→ the result is a value - Type constructor:
(T: Type) -> Type→ the result is a type - Value-dependent type:
(T: Type, N: Int) -> Type→ the result is a type and depends on the value parameter N
Curry-Howard correspondence: This unification is not coincidental. The Curry-Howard correspondence states that “types are propositions, and programs are proofs”—the function type
A → Bcorresponds to the logical implication “if A, then B”; the generic type(T: Type) -> Typecorresponds to universal quantification, “for every type T”; and the value-dependent type(n: Int) -> Typecorresponds to “for every integer n, there exists a type.” YaoXiang unifies functions, type constructors, and value-dependent types at the Type2 layer, essentially unifying “proof” and “computation” as the same concept—constructive proof. This is precisely how the Curry-Howard correspondence manifests directly in language design: one form ((params) -> result) carries both logical propositions and computational processes.
Compile-Time Determinism Guarantees
YaoXiang's Type Universe Idea requires that everything at the Type level be determined at compile time.
# Compile-time dimension validation example
Matrix: (T: Type, Rows: Int, Cols: Int) -> Type = {
data: Array(Array(T, Cols), Rows),
# Compile-time check: dimensions must be positive
_assert: Assert(Rows > 0),
_assert: Assert(Cols > 0),
}
# Create a 3x3 identity matrix - performed at compile time
identity: (T: Add + Zero + One, N: Int) -> ((size: N) -> Matrix(T, N, N)) = {
matrix = Matrix(T, N, N)()
# ...
}
# Compile-time computation: factorial(3) = 6; the array size is determined at compile time
arr: Array(Int, factorial(3)) = Array(Int, 6)()The compiler automatically:
- Detects function calls in type positions
- Performs compile-time termination checks on functions (see the termination-checking mechanism below)
- Evaluates them at compile time
- Embeds the results in the generated type
Use Cases for Value-Dependent Types
Compile-Time Dimension Validation
# Matrix multiplication: dimensions are validated at compile time
multiply: (T: Add + Multiply + Zero,
Rows: Int, Cols: Int, M: Int) -> ((
a: Matrix(T, Rows, Cols),
b: Matrix(T, Cols, M)
) -> Matrix(T, Rows, M)) = {
# Compile-time check: a.Cols == b.Rows; otherwise, a compile error is reported
result = Matrix(T, Rows, M)()
# ...
}
# The error is caught at compile time:
# multiply(matrix_2x3, matrix_4x2) # Compile error: 2 != 4Type-Safe Array Sizes
# The array size is a compile-time constant
Array: (T: Type, N: Int) -> Type = {
data: Array(T, N),
length: N,
}
# N is a compile-time constant and can be used in type-level computation
first_three: Array(Int, 3) = Array(Int, 3)(1, 2, 3)
# first_three.length == 3 (known at compile time)Goal of Covering Boundary Failures at Compile Time
Implementation status: Container types have been de-specialized—
Array(T, N)is a const generic constructor, and literal-context landing andinmembership predicates have both been implemented. The N and element type of anArray(T, N)literal landing are enforced by compile-time validation (E1002), so N is trusted. The mechanism described in this section can therefore be built on “the annotation N == the runtime length.” At present, out-of-bounds[]indexing (E6003) and missing Dict keys (E6008) are in a transitional runtime-error state. Value-dependent types are the target mechanism for moving these boundary failures to compile time:
- Const index:
a[5](where 5 is a compile-time constant) is rejected directly at compile time whena: Array(Int, 3);- Value index:
a[i]requires the preconditioni < len(a), proven by a value-dependent type contract;- The
inpredicate forms the basis of Hoare-logic preconditions:n in 1..10andx in some_setare both compile-time-provable propositions.The complete design of refined types will be added to this section separately when implemented.
Conditional Types
# Type-level If
If: (C: Bool, T: Type, E: Type) -> Type = match C {
True => T,
False => E,
}
# Type family
AsString: (T: Type) -> Type = match T {
Int => String,
Float => String,
Bool => String,
_ => String,
}Generic Functions
# map: a generic function whose type parameters T and R are determined at compile time
map: (T: Type, R: Type) -> (
(list: List(T), f: (x: T) -> R) -> List(R)
) = (list, f) => {
result = List(R)()
for x in list {
result.push(f(x))
}
return result
}
# Usage is completely transparent, with types inferred automatically
numbers = List(Int)() # Value construction uses the two-layer form (see §9.1); populate elements with push
numbers.push(1)
numbers.push(2)
numbers.push(3)
doubled = map(numbers, (x) => x * 2) # Inferred as map[Int, Int]Comparison with Other Languages
| Feature | C++ Templates | Rust Generics | Haskell GADT | YaoXiang |
|---|---|---|---|---|
| Type parameters | ✅ | ✅ | ✅ | ✅ |
| Value-dependent types | ❌ | ❌ | ✅ | ✅ |
| Compile-time evaluation | Template instantiation | ❌ | ✅ | ✅ |
| Termination guarantee | ❌ | ❌ | ❌ (dangerous) | ✅ (automatic measure exploration + explicit measures, RFC-027) |
| Type safety | ❌ (macro expansion) | ✅ | ✅ | ✅ |
| Unified syntax | ❌ | ❌ | ❌ | ✅ |
| Compile-time dimension validation | Manual specialization | Runtime checks | Type families | Automatic compile-time validation |
| Semi-automatic termination annotations (decreases/invariant) | ❌ | ❌ | ❌ | ❌ (no annotation syntax; explicit measures are written in type position) |
Termination Checking Mechanism (Unified with RFC-027)
Compile-time evaluation of value-dependent types must guarantee termination; otherwise, the type system could enter an infinite loop. Termination checking is performed by RFC-027's compile-time proof pipeline with fully automatic operation preferred: the compiler first explores measures automatically, allowing recursive functions and loops that can be proven to terminate; if it cannot find a measure and no explicit measure was supplied, it reports a compile error (RFC-027 §6.9 specifies the explicit measure fallback for type positions). No provision is made for separate annotation syntax: RFC-022's //! decreases and /*! invariant !*/ were deprecated together with RFC-022, and the specification is the type annotation itself.
Trigger criterion (RFC-027 §7): Termination obligations are triggered by refined types—a type enters verification mode as soon as it is refined. Ordinary types that are not refined do not enter verification mode and generate no termination obligation.
Termination Checking for Recursive Functions
For recursive functions with refined signatures, the compiler checks whether recursive-call arguments strictly decrease along every recursive path (RFC-027 §6.7). No specification comments are required:
# A recursive function with a refined signature: no //! requires/ensures/decreases; the compiler automatically explores the decrease
factorial: (n: NonNegative(n)) -> Int = {
if n <= 1 { return 1 }
return n * factorial(n - 1) # The compiler explores: n-1 < n → decreases → termination is proven
}| Scenario | Behavior |
|---|---|
The compiler can discover a decrease (such as n-1) | Pass |
| Exploration fails, but the type position supplies a measure (§6.9) | Pass if proven by SMT |
| Exploration fails and there is no explicit measure / the measure is disproved by SMT | Compile error |
| The signature has no refinement (does not enter verification mode) | No termination obligation is generated |
Termination Checking for Loops
Loops do not require : Invariant(...) or : decreases(...) annotations. A refined type annotation on a variable (such as UpTo(n)) simultaneously provides a loop invariant and a measure bound. The compiler tries four measure-exploration strategies in priority order and stops as soon as it finds one (RFC-027 §6.1–6.5):
- Automatic synthesis of a linear rank function—extract variable bounds from type annotations, enumerate linear combinations, and use SMT to verify m ≥ 0 and m' < m on every path
- Predicate-violation counting (experimental)—extract
violation_countfrom the target type definition (such asSorted) and cover adjacent swaps/moves - Bounded increment/decrement patterns—
v += const→ measureupper - v(a degenerate form of Strategy 1 and the fastest path) - Multiplicative scaling measure template—
v *= const(const > 1) → measureceil(log_const(upper / v))
sum: (arr: Array(Int, n)) -> Int = {
mut i: UpTo(arr.len) = 0 # The type annotation provides the upper bound arr.len and lower bound 0 → verification mode is entered
while i < arr.len {
# The compiler automatically explores: measure arr.len - i, strictly decreasing by 1 on each iteration → termination is proven
s += arr[i]; i += 1
}
return s
}When exploration fails, a name may be bound to the loop and a measure supplied in the type position (RFC-027 §6.9):
loop: (n: Int) -> Int = {
mut i = 0
// The loop body is a `{}` block, whose value is given by the tail expression (spec §2.9): after the assignment statement at the end of the body, `i` must also be written;
// otherwise, the block value is `Void`, and `return acc` returning `Void` from `-> Int` is invalid.
acc: Terminates(n - i) = while i < n { i = i + 1; i }
return acc
}Termination Checking Workflow
┌─────────────────────────────────────────────────────────────┐
│ 类型检查阶段 │
│ 遇到带精化类型的位置(参数精化、返回精化、变量精化) │
└─────────────────────────┬───────────────────────────────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ 1. 终止检查(RFC-027 证明管道,全自动优先) │
│ - 递归函数:检查参数在每条递归路径上严格递减 │
│ - 循环:四种度量探索策略(线性秩/违反计数/有界模式/ │
│ 乘法缩放),SMT 验证递减 │
│ - 探索不出 → 可由程序员在类型位给出测度(Terminates) │
│ - 无测度 / 测度被 SMT 判伪 → 编译错误(硬边界) │
└─────────────────────────┬───────────────────────────────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ 2. 编译期求值(由内置解释器执行) │
│ - 纯函数:直接求值 │
│ - 副作用:编译错误(类型位置必须无副作用) │
└─────────────────────────┬───────────────────────────────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ 3. 结果嵌入类型 │
│ - Array(Int, factorial(5)) → Array(Int, 120) │
│ - Matrix(Float, 3, 3) → 具体类型 │
└─────────────────────────────────────────────────────────────┘Advantages
- Safety: Ensures that compile-time evaluation must terminate, preventing the type system from entering an infinite loop
- Unification: Termination checking and correctness verification (VC generation) share one compile-time proof pipeline (RFC-027), with no independent specification syntax
- Fully automatic operation preferred: The compiler automatically explores measures from type annotations and passes the check whenever it can prove termination; when exploration fails, a measure can be supplied in the type position (
Terminates) and is still evaluated by SMT—there is no need for programmers to writedecreasessyntax manually
Motivation
Why Is a Strong Generic Type System Needed?
Generics in current mainstream languages have limitations:
| Language | Generic Capabilities | Problem |
|---|---|---|
| Java | Bounded types | Compile-time monomorphization without generic specialization |
| C# | Generic constraints | Runtime type checks with performance overhead |
| Rust | Generics + Trait | The Trait system is complex and has a steep learning curve |
| C++ | Templates | Template specialization is complex, and compile errors are poor |
| YaoXiang | Value-dependent types | Types can depend on values, with compile-time dimension validation and termination guarantees |
Core Tensions
- Performance vs. flexibility: Runtime flexibility vs. compile-time optimization
- Complexity vs. simplicity: A powerful type system vs. ease of use
- Macros vs. generics: Macro code generation vs. generic type safety
- Value dependence vs. type safety: Traditional generics cannot validate dimensions at compile time
Core Advantages of Value-Dependent Types
YaoXiang's value-dependent types are its core advantage over traditional generics:
| Advantage | Description |
|---|---|
| Types depend on values | Array: (T: Type, N: Int) -> Type lets a type depend on a concrete value |
| Compile-time evaluation | Function calls in type positions are evaluated at compile time, and the results are embedded directly in types |
| Dimension validation | Matrix(Float, 3, 3) validates matrix dimensions at compile time |
| Type-level computation | Conditional types such as If and Match support type-level computation |
| Termination guarantee | Compile-time termination checking (automatic measure synthesis) ensures that compile-time evaluation must terminate |
# Compile-time validation that C++/Rust cannot perform
matrix: Matrix(Float, factorial(3), factorial(2)) = ...
# Compile-time computation: factorial(3) = 6, factorial(2) = 2
# The type is Matrix(Float, 6, 2)
# Dimension mismatches are caught at compile time
identity: Matrix(Float, 3, 3) = ...
# multiply(matrix_2x3, identity_3x3) # Compile error: 2 != 3Value of the Generic Type System
# Example: unified API design
# map operations for different container types
# Traditional approach: implement each type separately
map_int_array: (array: Vec(Int), f: Fn(Int) -> Int) -> Vec(Int) = ...
map_string_array: (array: Vec(String), f: Fn(String) -> String) -> Vec(String) = ...
map_int_list: (list: List(Int), f: Fn(Int) -> Int) -> List(Int) = ...
map_string_list: (list: List(String), f: Fn(String) -> String) -> List(String) = ...
# Generic approach: one generic function covers every type
map: (T: Type, R: Type)(container: Container(T), f: Fn(T) -> R) -> Container(R) = {
for item in container {
result.push(f(item))
}
result
}Design Goals
Core Goals
- Zero-cost abstraction - A generic call is equivalent to a call with a concrete type
- Dead-code elimination - Compile-time analysis instantiates only the generics that are used
- Macro replacement - Generics replace 90% of macro use cases
- Type safety - Compile-time checks with no runtime type overhead
- IDE-friendly - Smart suggestions and clear error messages
- Value-dependent types - Types can depend on values, supporting compile-time dimension validation
- Safe compile-time evaluation - RFC-027's automatic measure synthesis guarantees that compile-time evaluation terminates
Design Principles
- Compile-time determination: Generic parameters are determined at compile time
- Monomorphization first: Generate concrete code and avoid virtual-function calls
- Constraint-driven: Type constraints guide instantiation
- Platform optimization: Specialization supports platform-specific optimizations
- Unified type universe: Functions, type constructors, and value-dependent types are unified at the Type2 layer
- Termination guarantee: Function calls in type positions must prove termination
Proposal
1. Basic Generics
1.1 Generic Type Parameters
Key rule: Generic type definitions must explicitly annotate
: Type; otherwise, HM infers them as functions.
Form Meaning List: (T: Type) -> Type = {...}✅ Type constructor List = {...}❌ Inferred by HM as a function, not a type
# Generic type definitions (must have : Type)
Option: (T: Type) -> Type = {
some: (T) -> Self,
none: () -> Self
}
Result: (T: Type, E: Type) -> Type = {
ok: (T) -> Self,
err: (E) -> Self
}
List: (T: Type) -> Type = {
data: Vec(T),
length: Int,
push: (self: List(T), item: T) -> Void, # self is merely a conventional name, not a keyword
get: (self: List(T), index: Int) -> Option(T),
}
# Generic functions (without : Type; inferred by HM as functions)
map: (T: Type, R: Type) -> ((opt: Option(T), f: Fn(T) -> R) -> Option(R)) = {
return match opt {
some => Option.some(f(some)),
none => Option.none(),
}
}
# Generic constraints (direct expressions; return may be omitted on a single line)
clone: (T: Clone)(value: T) -> T = value.clone()
# Multiple type parameters
combine: (T: Type, U: Type) -> ((a: T, b: U) -> (T, U)) = (a, b)Generic Function Call Syntax
1.1 Unified Signature Syntax
# Generic functions use the unified (T: Type, R: Type) signature syntax
map: (T: Type, R: Type) -> ((list: List(T), f: (x: T) -> R) -> List(R)) = ...
# Multiple type parameters
combine: (T: Type, U: Type) -> ((a: T, b: U) -> (T, U)) = (a, b)1.2 The Self-Describing Type Mechanism
Type is a language-level special entity. The compiler can inherently recognize Type positions in signatures and automatically infer and fill them from the types of the actual arguments.
# The compiler automatically infers generic parameters
numbers: List(Int) = List(Int)()
# ^^^^^^^^ ^^^^^^^^
# Type declaration and construction call: Int fills T, and () constructs the value
# Function-call inference
numbers: List(Int) = List(Int)()
f: (x: Int) -> String = (x) => x.to_string()
strings: List(String) = map(numbers, f)
# The compiler infers: T=Int, R=String1.3 Monomorphization
# Source code
map: (T: Type, R: Type) -> ((list: List(T), f: (x: T) -> R) -> List(R)) = {
result: List(R) = List(R)()
for x in list {
result.push(f(x))
}
return result
}
# Use sites
int_list: List(Int) = List(Int)()
doubled: List(Int) = map(int_list, (x: Int) => x * 2) # Instantiate map[Int, Int]
string_list: List(String) = List(String)()
uppercased: List(String) = map(string_list, (s: String) => s.to_uppercase()) # Instantiate map[String, String]
# After compilation (equivalent code)
map_Int_Int: (list: List(Int), f: (Int) -> Int) -> List(Int) = {
result: List(Int) = List(Int)()
for x in list {
result.push(f(x))
}
return result
}
map_String_String: (list: List(String), f: (String) -> String) -> List(String) = {
result: List(String) = List(String)
for s in list {
result.push(f(s))
}
return result
}1.4 Explicit Population (When Inference Fails)
# Omit Type parameters when inference is possible
numbers: List(Int) = List(Int)()
strings: List(String) = map(numbers, (x: Int) => x.to_string())
# Type parameters must be supplied explicitly when inference fails
# map(numbers, (x) => x) # ❌ Error: Cannot infer R
### 2. Type Constraint System
#### 2.1 Single Constraint
```yaoxiang
# Basic trait definition (interface type)
Clone: Type = {
clone: (Self) -> Self,
}
Display: Type = {
fmt: (Self, Formatter) -> Result,
}
Debug: Type = {
fmt: (Self, Formatter) -> Result,
}
# Using constraints: declare type constraints directly in the signature
clone: (T: Clone) -> (value: T) -> T = value.clone()
debug_print: (T: Debug)(value: T) -> Void = {
formatter = Formatter.new()
value.fmt(formatter)
print(formatter.to_string())
}2.2 Multiple Constraints
# Multiple-constraint syntax
combine: (T: Clone + Add)(a: T, b: T) -> T = {
a.clone() + b
}
# Sorting a generic container
sort: (T: Clone + PartialOrd)(list: List(T)) -> List(T) = {
# Implement the sorting algorithm
result: List(T) = list.clone()
quicksort(&mut result)
return result
}
# Function type constraints
map: (T: Type, R: FnMut(T))(array: Vec(T), f: R) -> Vec(R) = {
result: Vec(R) = Vec()
for item in array {
result.push(f(item))
}
return result
}
# Usage
doubled: Vec(Int) = map(Vec(1, 2, 3), (x: Int) => x * 2) # Inferred by the compilerOrigin of constraint names (note dated 2026-09-22): Operator constraints such as
Add/Subtract/Multiply/Divide/Moduloare defined and implemented by RFC-011b: Operator Overloading and Interface-Driven Operators—T: Add≜ means that anAdd(T, T, T)interface instance has been registered (three type parameters, with the result typeOexplicit).Zero/One/PartialOrd/Fn/FnMutcurrently have no defined source and are dangling constraint names awaiting implementation in later RFCs. Until then, examples involving these names are illustrative only.
2.3 Function Type Constraints
# Higher-order function constraints
call_twice: (T: Type, F: Fn() -> T)(f: F) -> (T, T) = (f(), f())
call_with_arg: (T: Type, U: Type, F: Fn(T) -> U)(arg: T, f: F) -> U = f(arg)
compose: (A: Type, B: Type, C: Type, F: Fn(A) -> B, G: Fn(B) -> C)(a: A, f: F, g: G) -> C = g(f(a))
# Usage examples
result: Int = call_with_arg(42, (x: Int) => x * 2) # result = 84
composed: String = compose(
"hello",
(s: String) => s.to_uppercase(),
(s: String) => s + " WORLD"
) # composed = "HELLO WORLD"2.4 Built-in marker traits: Dup and Clone
Three kinds of copy semantics:
| Type | Meaning | Trigger | Applicable scenarios |
|---|---|---|---|
| Primitive value copy | Values are copied automatically on assignment, and the two values are fully independent | Automatic on assignment/argument passing | Int, Float, Bool, Char |
| Dup | Shallow copy: copies the handle/token while sharing the underlying data | Automatic on assignment/argument passing | &T tokens, ref T, String/Bytes |
| Clone | Deep copy: creates a complete, independent copy | value.clone() | Any type that implements Clone |
Semantics of Dup: A type that implements Dup does not transfer ownership on assignment or argument passing—the compiler copies the handle/token, allowing multiple holders to refer to the same underlying data. This complements the default move semantics of the RFC-009 ownership model.
Dup and Clone are orthogonal concepts:
Dup = 复制句柄,共享数据(修改互相影响)
Clone = 复制数据,副本独立(修改互不影响)Rules:
1. 原语值类型(Int, Float, Bool, Char) — 编译器内置值复制,不属于 Dup
2. Dup — 只适用于引用/令牌类型和内部引用计数的类型
3. Clone — 显式深拷贝,任何类型可实现
4. 默认 Move — 其他类型保持默认 Move 语义Which types are Dup:
| Type | Dup | Reason |
|---|---|---|
&T (borrowed token) | ✅ | A zero-sized token; copying the token gives multiple views of the same data |
ref T | ✅ | Copying an Rc/Arc increments the reference count and shares the heap data |
| String, Bytes | ✅ | Internal reference counting; copying the handle shares the underlying buffer |
&mut T (mutable token) | ❌ | Linear and exclusive; cannot be copied |
| struct | Derived | All fields ∈ (primitive value types ∪ Dup) → Dup; otherwise Move (RFC-009 §derivation rules, #398) |
| enum | Derived | All fields of all variants are copyable → the enum is Dup (design stage; not yet implemented) |
| tuple | Derived | Determined element by element, using the same rules as struct (#398) |
| Fn (closure) | ❌ | The captured environment may not be Dup |
*T (raw pointer) | ❌ | unsafe and outside the ownership system |
Int/Float/Bool/Char are not Dup—they are value types, and the compiler automatically copies their values on assignment (the two values are fully independent). This is not a “shallow copy”; it is built-in compiler behavior for primitives and neither needs nor should be expressed through a Dup type attribute.
# Primitive value types: the compiler copies values automatically (not Dup)
x: Int = 42
y = x # Value copy; x and y are fully independent
print(x) # ✅
# Dup: shallow copy; copying the handle shares the data
view: &Point = &point
view2 = view # ✅ Dup: copies the token; both refer to the same point
print(view.x) # ✅
# Clone: explicit deep copy that creates an independent copy
backup = big_struct.clone() # Explicit call
# Generic constraints
dup_use: (T: Dup) -> T = x # T: Dup → shallow copies are permitted
clone_use: (T: Clone) -> T = x.clone() # T: Clone → deep copies are permittedNote:
Send/Syncare not user-visible traits. Cross-task safety is guaranteed through therefkeyword and fully automatic compiler handling—refautomatically selects Rc or Arc, so users need not understand Send/Sync.
3. Associated Types
3.1 Associated Type Definition
# Iterator trait (using the (Item: Type) -> Type syntax)
Iterator: (Item: Type) -> Type = {
next: (Self) -> Option(Item),
has_next: (Self) -> Bool,
collect: (T: Type)(Self) -> List(T),
}
# Usage
collect_all: (T: Type, I: Iterator(T))(iter: I) -> List(T) = {
result: List(T) = List(T)
while iter.has_next() {
if let Some(item) = iter.next() {
result.push(item)
}
}
return result
}
# Vec's Iterator implementation
# Method-call syntactic sugar: Vec.Item, Vec.next, Vec.has_next
# The iteration position is carried by the wrapper record (Vec itself is a raw buffer with no index field)
VecIter: (T: Type) -> Type = {
data: &Vec(T),
index: Int,
}
VecIter.has_next: (T: Type)(self: &VecIter(T)) -> Bool = {
return self.index < self.data.length
}
VecIter.next: (T: Type)(self: &mut VecIter(T)) -> Option(T) = {
if self.index < self.data.length {
item = self.data[self.index]
self.index = self.index + 1
return Option.some(item)
} else {
return Option.none()
}
}
VecIter.Item: (T: Type)(arr: &VecIter(T)) -> T = {
return arr.data[arr.index]
}3.2 Generic Associated Types (GATs)
# More complex associated types
Producer: (Item: Type) -> Type = {
Item: T,
produce: (Self) -> Option(Item),
}
# Associated types may be generic
Container: (Item: Type) -> Type = {
Item: T,
IteratorType: Iterator(Item), # Associated types may also be generic
iter: (Self) -> IteratorType,
}
# Usage
process_container: (T: Type, C: Container(T))(container: C) -> List(T) = {
container.iter().collect()
}4. Compile-Time Generics
4.1 Compile-Time Value Parameters
Core design: The Type marker in a generic signature denotes a type parameter. Whether a parameter annotated with a concrete type (Int/Bool/Float and so on) enters compile-time processing depends on whether its value is referenced in a type position (value dependence). No const keyword is required.
The decision is driven by locally readable strength declarations within the signature: when the author references a value parameter in a type position ((k: N), Array(T, N), Assert(N > 0)), that reference is a strength declaration; the quantity is its mechanical reading. This is not implicit—when the author writes n: N, the appearance of the value name in a type position is itself a declaration, just like writing b: Positive(b) (RFC-027 §3.1).
Each binding infers a quantity q ∈ {0, 1, ω}:
- q = 0 (zero quantity): The value is computed at compile time and erased at runtime, occupying no slot. The parameter is referenced in a type position ⇒ q=0.
- q = 1 (linear): Move—ownership is transferred on assignment or argument passing (RFC-009).
- q = ω (free): Dup—the value may be copied and shared (RFC-009).
Slot allocation uses q as its sole input. The call-site argument sequence and the implementation-head binding sequence thus become two derived views of the same function, rather than two independent ledgers:
- Call-site argument view = formal parameters − type parameters (in declaration order). A q=0 parameter still occupies an argument slot:
factorial(k - 1)places the compile-time value in N's position, and the argument slot records “how many values the author filled in declaration order.” - Implementation-head binding view = formal parameters − type parameters − q=0 parameters (in declaration order). The implementation head binds only names for which runtime values exist:
= (k) => kis valid, whereas= (N, k) => kreports E1112.
E1011 and E1112 are both counted according to the implementation-head binding view.
The decision for type-position references traverses all Type subtrees in the declaration (formal-parameter annotations, return types, type-body fields, the types of inner Fns, Assert predicates, and type-constructor argument positions), resolving names to confirm the reference target (resolution to a declared type or type parameter ⇒ independent of the formal parameter; failure to resolve when the signature has a candidate with the same name ⇒ the strength declaration is matched). Self-reference is handled with a monotonic fixed point (the set only grows, for at most |candidates| rounds).
Call-site obligations are checked according to declaration strength: the validity of f(y) depends on whether the actual argument y carries compile-time evidence (a literal, a value environment, a single-value pin, a Γ equality—see RFC-027 §12), rather than on a global analysis of f's declaration. When there is no evidence, the error is reported at the call site and points to the q=0 slot visible in f's type. Classical types (without strength declarations) do not enter the proof pipeline and undergo no quantity inference, so their cost scales with declaration strength.
| Form | Decision | Reason |
|---|---|---|
add: (a: Int, b: Int) -> Int | a/b are runtime value parameters | They appear only in value positions and do not participate in type construction |
Array: (T: Type, N: Int) -> Type = { data: Array(T, N) } | N is a compile-time value parameter | N appears in the type-constructor argument position of Array(T, N) |
factorial: (N: Int) -> (k: N) -> Int | N is a compile-time value parameter | N is the type of the inner parameter k |
Foo: (T: Type, N: Int) -> Type = { x: T } | N is an ordinary parameter (see below) | N is not referenced in the type body, so there is no strength declaration and q=1/ω |
Essential nature of value dependence: A compile-time value parameter is a value-dependent type—only values used to construct types need to be determined at compile time. The form (
: Int) determines candidate eligibility, while its use (whether it appears in a type position) determines whether it is a compile-time value parameter. This follows the same criterion as “function calls in type positions are evaluated at compile time” in “Compile-Time Determinism Guarantees.”
# ════════════════════════════════════════════════════════
# Compile-time value parameter: N is referenced in a type position (the Measure length slot)
# ════════════════════════════════════════════════════════
Measure: (T: Type, N: Int) -> Type = {
data: Array(T, N), # N appears in a type-constructor argument position → compile-time value parameter
length: N,
}
# Usage: factorial(5) is evaluated in a type position (at compile time), and the result 120 is embedded in the type
m: Measure(Int, factorial(5)) # Measure(Int, 120)
# ════════════════════════════════════════════════════════
# Value dependence: N is the type of the inner parameter k
# ════════════════════════════════════════════════════════
# N is a compile-time value parameter (it appears in the type position of (k: N), q=0);
# k is a runtime value parameter whose type is the literal type N (a single-value type).
# Single-value pin: k: N and N is a compile-time value ⇒ k's value is pinned by its single-value type and is known at compile time,
# so k - 1 carries compile-time evidence and can fill N's position in a recursive call.
factorial: (N: Int) -> (k: N) -> Int = {
return match k {
0 => 1,
_ => k * factorial(k - 1)(k - 1)
}
}Handling of candidates that do not qualify: A candidate annotated with a concrete type but not referenced in a type position (such as
Foo'sNabove) is an ordinary parameter without a strength declaration and is processed normally with q=1/ω: the function-level path occupies a runtime value slot; for the type-constructor path, a candidate without a type-body reference has no compile-time semantics. The declaration side reports “the type-constructor parameter is not referenced in the type body,” suggesting that a reference be added or that it be changed to an ordinary function. There is no longer a separate code path for candidates that do not qualify.
4.2 Compile-Time Computation
# ════════════════════════════════════════════════════════
# Compile-time computation example
# ════════════════════════════════════════════════════════
# The compiler computes function calls on literal types at compile time
SIZE: Int = factorial(5) # 120 at compile time
# Matrix type usage
Matrix: (T: Type, Rows: Int, Cols: Int) -> Type = {
data: Array(Array(T, Cols), Rows),
}
# Compile-time dimension validation
identity_matrix: (T: Add + Zero + One, N: Int)(size: N) -> Matrix(T, N, N) = {
matrix: Matrix(T, N, N) = Matrix(T, N, N)()
for i in 0..size {
for j in 0..size {
if i == j {
matrix.data[i][j] = One::one()
} else {
matrix.data[i][j] = Zero::zero()
}
}
}
matrix
}
# Usage: compile-time computation generates Matrix(Float, 3, 3)
identity_3x3: Matrix(Float, 3, 3) = identity_matrix(Float, 3)(3)Never and Void: ⊥ and ⊤ in the Type System
YaoXiang's type system provides both ⊥ (false/empty type) and ⊤ (true/Unit) under the Curry-Howard correspondence, represented by the two built-in type names Never and Void:
Never (⊥) — three non-negotiable core properties:
- Zero constructors: No literal or expression can produce a value of type
Never. This is a meta-level property and must be built in. - Explosion principle:
Never <: Tholds for every typeT. ANevervalue may be used as any type—this is precisely why code afterassert(false)still passes type checking (although it is never executed). - Divergence marker:
f: (...) -> Neverindicates thatfis guaranteed not to return. The compiler uses this for dead-code analysis.
Never is a built-in type name, not a keyword, and is transparent to the parser. No empty-sum or type-literal syntax is exposed.
Void (⊤, namely Unit) — has exactly one resident (the default void value) and carries the true proposition “always true.” Void is the unit of the zero-field product type, while Never is the unit of the zero-variant sum type—the two are dual. x: Void = <default> is valid, whereas x: Never = ... has no right-hand side to write.
4.3 Compile-Time Validation (Standard Library Implementation)
# ════════════════════════════════════════════════════════
# Standard library implementation: using conditional types
# ════════════════════════════════════════════════════════
# Standard library definition
# IsTrue: a bridge from the value universe to the type universe—maps a Boolean truth value to a type
IsTrue: (b: Bool) -> Type = match b {
true => Void, # ⊤, has a value, and the program continues
false => Never, # ⊥, has no value, and diverges
}
# Assert: a compile-time refined-type primitive—the type-level expression of a Boolean proposition
Assert: (cond: Bool) -> Type = IsTrue(cond)
#
# cond is true → Assert(true) = Void (always true; erased)
# cond is false → Assert(false) = Never (always false; compile error/divergence)
# cond cannot be decided → the proof pipeline decides by dispatch mode:
# CompileTime → Unknown, requiring proof
# Runtime → inserts a check and injects a Γ assumption
# Usage 1: use as a constraint in a type definition
Bounded: (T: Type, N: Int) -> Type = {
data: Array(T, N),
# Compile-time check: N must be greater than 0 (Assert is in a type position)
length: Assert(N > 0),
}
# Usage 2: use in an expression
IntArray: (N: Int) -> Type = Array(Int, N)
# Validation: the size of IntArray(10) equals sizeof(Int) * 10
Assert(size_of(IntArray(10)) == sizeof(Int) * 10)4.4 Compile-Time Generic Specialization
# Small-array optimization: use function overloading to implement compile-time generic specialization
# Generic implementation
sum: (T: Type, N: Int) -> ((arr: Array(T, N)) -> T) = {
result = Zero::zero()
for item in arr.data {
result = result + item
}
return result
}
# N=1 specialization
sum: (T: Type) -> ((arr: Array(T, 1)) -> T) = arr.data[0]
# N=2 specialization
sum: (T: Type) -> ((arr: Array(T, 2)) -> T) = arr.data[0] + arr.data[1]
# Small-array loop unrolling (N <= 4)
sum: (T: Type, N: Int) -> ((arr: Array(T, N)) -> T) = {
# Compiler optimization: unroll the loop
return arr.data[0] + arr.data[1] + arr.data[2] + arr.data[3]
}5. Conditional Types
Curry-Howard correspondence: From the Curry-Howard perspective, conditional types are case analysis in logic. The
Booltype corresponds to a proposition with two possible values (True/False), andIfselects a different result according to the truth of that proposition—this is precisely case disjunction in logic.match C { True => T, False => E }actually expresses: “if the proposition C is known to be True, the conclusion is T; if it is known to be False, the conclusion is E.”
5.1 If Conditional Type
# Type-level If
If: (C: Bool, T: Type, E: Type) -> Type = match C {
True => T,
False => E,
}
# Example: compile-time branch
NonEmpty: (T: Type) -> Type = If(T != Void, T, Never)
Optional: (T: Type) -> Type = If(T != Void, T, Void)
# Compile-time validation (unified with the Assert definition in §4.3)
# Assert: (cond: Bool) -> Type = IsTrue(cond)
# Usage
# Type computation: If(True, Int, String) => Int
# Type computation: If(False, Int, String) => String5.2 Type Families
Curry-Howard correspondence: Type families are the most direct expression of “propositions as types.”
Add: (A: Type, B: Type) -> Typedoes not “write an addition function at the type level”; it constructs a proposition about natural-number addition.(Zero, B) => Bmeans “the proposition Add(Zero, B) is equivalent to B,” and(Succ(A'), B) => Succ(Add(A', B))means “if Add(A', B) holds, then Add(Succ(A'), B) also holds.” This is the definition of addition in the Peano axioms themselves. The type checker verifies that this match expression type-checks, which is equivalent to verifying the logical consistency of the definition.
# Compile-time type conversion
AsString: (T: Type) -> Type = match T {
Int => String,
Float => String,
Bool => String,
_ => String, # Default
}
# Type-level computation
Length: (T: Type) -> Type = match T.length {
0 => Zero,
1 => Succ(Zero),
2 => Succ(Succ(Zero)),
_ => TooLong,
}
# Type-level addition (Curry-Howard: case analysis + recursive call; termination checking is required for a complete induction)
Add: (A: Type, B: Type) -> Type = match (A, B) {
(Zero, B) => B,
(Succ(A'), B) => Succ(Add(A', B)),
}
# Example: compile-time computation of 2 + 3
Two: Type = Succ(Succ(Zero))
Three: Type = Succ(Succ(Succ(Zero)))
Five: Type = Add[Two, Three] # Succ(Succ(Succ(Succ(Succ(Zero)))))6. Function Overload Specialization
6.1 Basic Specialization
# Basic specialization: use function overloading (the compiler selects automatically)
sum: (arr: Vec(Int)) -> Int = {
# Compiled into more efficient code
return native_sum_int(arr.data, arr.length)
}
sum: (arr: Vec(Float)) -> Float = {
# Use SIMD instructions
return simd_sum_float(arr.data, arr.length)
}
# Generic implementation
sum: (T: Type) -> ((arr: Vec(T)) -> T) = {
result = Zero::zero()
for item in arr {
result = result + item
}
return result
}6.2 Conditional Specialization
# A specialization form fully compliant with RFC-010 syntax: function overloading
# Concrete-type specialization
sum: (arr: Vec(Int)) -> Int = {
return native_sum_int(arr.data, arr.length)
}
sum: (arr: Vec(Float)) -> Float = {
return simd_sum_float(arr.data, arr.length)
}
# Generic implementation (the compiler automatically selects the optimum)
sum: (T: Type) -> ((arr: Vec(T)) -> T) = {
result = Zero::zero()
for item in arr {
result = result + item
}
return result
}
# Usage is completely transparent
int_arr = Vec(Int)(1, 2, 3)
float_arr = Vec(Float)(1.0, 2.0, 3.0)
# The compiler automatically selects the optimal specialization
sum(int_arr) # Selects sum: (Vec(Int)) -> Int
sum(float_arr) # Selects sum: (Vec(Float)) -> Float6.3 Perfect Integration of Function Overloading and Inlining
Key feature: Function overloading and inline optimization integrate naturally to implement zero-cost abstractions.
# ======== Source code ========
sum: (arr: Vec(Int)) -> Int = {
return native_sum_int(arr.data, arr.length)
}
sum: (arr: Vec(Float)) -> Float = {
return simd_sum_float(arr.data, arr.length)
}
sum: (T: Type) -> ((arr: Vec(T)) -> T) = {
result = Zero::zero()
for item in arr {
result = result + item
}
return result
}
# Usage
int_arr = Vec(Int)(1, 2, 3, 4, 5)
result = sum(int_arr)
# ======== After compilation (equivalent code) ========
# The compiler automatically selects the optimal specialization and then inlines it
result = native_sum_int(int_arr.data, int_arr.length)
# Completely equivalent to hand-written optimized code, with no function-call overhead!Core advantages:
Intelligent compiler selection
yaoxiangsum(int_arr) # Automatically selects sum: (Vec(Int)) -> Int sum(float_arr) # Automatically selects sum: (Vec(Float)) -> Float sum(custom_arr) # Automatically selects sum: (T: Type) -> ((arr: Vec(T)) -> T)Inline optimization
- Small functions are automatically inlined at call sites
- Zero function-call overhead
- Completely equivalent to hand-written optimized code
Type safety
- Compile-time type checking
- Zero runtime overhead
- No virtual function table required
Perfect fit with RFC-010
yaoxiang# Uses the unified syntax throughout name: type = value # No new keywords such as impl or where are needed
Example of practical application:
# Performance-sensitive numerical computation
fibonacci: (n: Int) -> Int = {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
fibonacci: (n: Float) -> Float = {
# Use Binet's formula
phi = (1.0 + 5.0.sqrt()) / 2.0
return (phi.pow(n) - (-phi).pow(-n)) / 5.0.sqrt()
}
# The compiler automatically selects and inlines
fibonacci(10) # Selects the Int version and fully inlines it
fibonacci(10.5) # Selects the Float version and uses Binet's formulaWhat does this mean?
- ✅ Generic specialization → Function overloading solves it naturally
- ✅ Performance optimization → Inlining is performed automatically
- ✅ Code reuse → One function name with multiple implementations
- ✅ Zero-cost abstraction → Compile-time polymorphism with zero runtime overhead
- ✅ No new keywords → Perfectly compliant with RFC-010's unified syntax
### 7. Dead-Code Elimination Mechanism
#### 7.1 Instantiation Graph Analysis
```rust
// Compiler internals: construct the generic instantiation dependency graph
struct InstantiationGraph {
// Nodes: generic instantiations
nodes: HashMap<InstanceKey, InstanceNode>,
// Edges: usage relationships
edges: HashMap<InstanceKey, Vec<InstanceKey>>,
}
struct InstanceKey {
generic: FunctionId, // Generic function ID
type_args: Vec<TypeId>, // Type parameters
const_args: Vec<ConstId>, // Const parameters
}
// Algorithm: reachability analysis
fn eliminate_dead_instantiations(graph: &InstantiationGraph) {
let mut reachable = HashSet::new();
// Start from entry points (main, exported functions, etc.)
let entry_points = find_entry_points();
for entry in entry_points {
dfs_visit(entry, &graph, &mut reachable);
}
// Unvisited instantiations are dead code
for node in &graph.nodes {
if !reachable.contains(node.key) {
eliminate(node);
}
}
}7.2 Use-Site Analysis
# Source analysis
map: (T: Type, R: Type)(list: List(T), f: Fn(T) -> R) -> List(R) = ...
# Use site 1: instantiate map(Int, Int)
int_list = List(Int)()
int_list.push(1)
int_list.push(2)
int_list.push(3)
doubled = map(int_list, (x) => x * 2) # Requires map[Int, Int]
# Use site 2: instantiate map(String, String)
string_list = List(String)()
string_list.push("a")
string_list.push("b")
string_list.push("c")
uppercased = map(string_list, (s) => s.to_uppercase()) # Requires map[String, String]
# Unused: map[Float, Float], etc.
# These generic instances will not be generated
# The compiled output contains only the instances that are used
map_Int_Int: (list: List(Int), f: Fn(Int) -> Int) -> List(Int) = ...
map_String_String: (list: List(String), f: Fn(String) -> String) -> List(String) = ...7.3 Compile-Time Generic DCE
# Compile-time analysis: compile-time generic usage
Array: (T: Type, N: Int) -> Type = {
data: Array(T, N),
}
# Actual usage
arr_10_int = Array(Int, 10)(data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Two layers: type parameters + construction arguments
arr_100_int = Array(Int, 100)() # Empty construction; data is assigned later
# Only used sizes are generated after compilation
Array_Int_10: (Array(Int, 10)) = ...
Array_Int_100: (Array(Int, 100)) = ...
# Unused sizes are not generated
# Array(Int, 50) will not be generated7.4 Cross-Module DCE
# Module A
# A.yx
pub map: (T: Type, R: Type)(list: List(T), f: Fn(T) -> R) -> List(R) = ...
# Module B
# B.yx
use A.{map}
int_list = List(Int)()
int_list.push(1)
int_list.push(2)
int_list.push(3)
doubled = map(int_list, (x) => x * 2) # Instantiate map(Int, Int)
# Module C
# C.yx
use A.{map}
string_list = List(String)()
string_list.push("a")
string_list.push("b")
string_list.push("c")
uppercased = map(string_list, (s) => s.to_uppercase()) # Instantiate map(String, String)
# Compile analysis:
# - Module B uses map[Int, Int]
# - Module C uses map[String, String]
# - The compiled binary contains only these two instances7.5 LLVM-Level DCE
// Compilation pipeline
fn optimize_ir(ir: &mut IR) {
// 1. Monomorphization (YaoXiang compiler)
ir.monomorphize();
// 2. Inline optimization
ir.inline_small_functions();
// 3. Constant propagation
ir.constant_propagation();
// 4. Generate LLVM IR
let llvm_ir = ir.to_llvm();
// 5. LLVM optimization passes
llvm_ir.add_pass(Passes::DEAD_CODE_ELIMINATION);
llvm_ir.add_pass(Passes::INLINE_FUNCTION);
llvm_ir.add_pass(Passes::GLOBAL_DCE);
llvm_ir.add_pass(Passes::MERGE_FUNC);
// 6. Run optimization
llvm_ir.run_optimization_passes();
}8. Macro Replacement Strategy
8.1 Code Generation Replacement
# ❌ Macro approach: code generation
macro_rules! impl_debug {
($($t:ty),*) => {
$(impl Debug for $t {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{:?}", self)
}
})*
};
}
# ✅ Generic approach: automatic derivation
# Use the function-overloading approach for automatic derivation
debug_fmt: (T: fields...) -> ((self: Point(T)) -> String) = {
return "Point { x: " + self.x.to_string() + ", y: " + self.y.to_string() + " }"
}
# Usage
p = Point { x: 1, y: 2 }
p.debug_fmt(&formatter) # The call is generated automatically8.2 DSL Replacement
# ❌ Macro approach: HTML DSL
html! {
<div class="container">
<h1> { title } </h1>
<ul>
{ for item in items {
<li> { item } </li>
}}
</ul>
</div>
}
# ✅ Generic approach: type-safe builder
Element: Type = {
tag: String,
attrs: HashMap(String, String),
children: List(Element),
text: Option(String),
}
create_element: (tag: String) -> Element = {
return Element(tag, HashMap::new(), List::new(), None)
}
with_class: [E: Element](elem: E, class: String) -> E = {
elem.attrs.insert("class", class)
return elem
}
with_text: [E: Element](elem: E, text: String) -> E = {
return E { text: Some(text), ..elem }
}
# Build the DOM
container = create_element("div")
|> with_class("container")
|> with_children(List::new())
title_elem = create_element("h1") |> with_text(title)
items_li = items.map((item) =>
create_element("li") |> with_text(item)
)
root = container |> with_children(List::new() + [title_elem, ul_elem])8.3 Type-Level Programming Replacement
# ❌ Macro approach: type-level computation
macro_rules! add_types {
($a:ty, $b:ty) => {
($a, $b)
};
}
# ✅ Generic approach: conditional types
Add: (A: Type, B: Type) -> Type = match (A, B) {
(Int, Int) => Int,
(Float, Float) => Float,
(Int, Float) => Float,
(Float, Int) => Float,
_ => TypeError,
}
# Compile-time validation
AssertAddable: (A: Type, B: Type) -> Type = If(Add(A, B) != TypeError, (A, B), compile_error("Cannot add"))
# Usage
result_type = Add[Int, Float] # Inferred as FloatRelationship to RFC-011b (note dated 2026-09-22): The lifted type family
Add(A, B)in this section is the type-level view of the operator interface registry in RFC-011b—the core registry entryAdd(Int, Float, Float)and this table's(Int, Float) => Floatare the same rule, and each user interface instantiation adds a row to this table. The Peano type-levelAddin §5.2 is pure type-level computation (the same name, but a different concept) and does not interfere with the value-level operator interface—the operator query uses the implementation registry rather than name resolution.
9. Examples
9.1 Complete Generic Container Example
# ======== 1. Define the generic container ========
# Use the (T: Type) -> Type syntax
Result: (T: Type, E: Type) -> Type = {
ok: (T) -> Self,
err: (E) -> Self,
}
Option: (T: Type) -> Type = {
some: (T) -> Self,
none: () -> Self,
}
List: (T: Type) -> Type = {
data: Vec(T),
length: Int,
# Generic methods (T is automatically brought into scope by the outer List(T))
push: (self: List(T), item: T) -> Void,
pop: (self: List(T)) -> Option(T),
map: (R: Type) -> ((self: List(T), f: (T) -> R) -> List(R)),
filter: (self: List(T), predicate: (T) -> Bool) -> List(T),
fold: (U: Type) -> ((self: List(T), initial: U, f: (U, T) -> U) -> U),
}
# ======== 2. Implement the generic methods ========
# The function definition is under the List namespace (the List. prefix = namespace ownership)
# To make call syntax such as list.push(item) work, an explicit binding is required: List.push = push[0]
# self is merely a conventional parameter name; the compiler uses types rather than names
List.push: (T: Type) -> ((self: List(T), item: T) -> Void) = {
if self.length >= self.data.length {
# Grow
new_data = Vec(T)(len=self.data.length * 2)
for i in 0..self.length {
new_data[i] = self.data[i]
}
self.data = new_data
}
self.data[self.length] = item
self.length = self.length + 1
}
List.pop: (T: Type) -> ((self: List(T)) -> Option(T)) = {
if self.length > 0 {
self.length = self.length - 1
return Option.some(self.data[self.length])
} else {
return Option.none()
}
}
List.map: (T: Type, R: Type) -> ((self: List(T), f: (T) -> R) -> List(R)) = {
result = List(R)()
for i in 0..self.length {
result.push(f(self.data[i]))
}
return result
}
List.filter: (T: Type) -> ((self: List(T), predicate: (T) -> Bool) -> List(T)) = {
result = List(T)()
for i in 0..self.length {
if predicate(self.data[i]) {
result.push(self.data[i])
}
}
return result
}
List.fold: (T: Type, U: Type) -> ((self: List(T), initial: U, f: (U, T) -> U) -> U) = {
result = initial
for i in 0..self.length {
result = f(result, self.data[i])
}
return result
}
# ======== 3. Use type constraints ========
# Implement Clone for List
List.clone: (T: Clone) -> ((self: List(T)) -> List(T)) = {
result = List(T)()
for i in 0..self.length {
result.push(self.data[i].clone())
}
return result
}
# ======== 4. Usage examples ========
# Create a generic List
numbers = List(Int)()
numbers.push(1)
numbers.push(2)
numbers.push(3)
# Use generic methods
doubled = numbers.map((x) => x * 2)
evens = numbers.filter((x) => x % 2 == 0)
# Use fold to compute
sum = numbers.fold(0, (acc, x) => acc + x) # sum = 6
# Generic composition
sum_of_evens = numbers
.filter((x) => x % 2 == 0)
.map((x) => x * 2)
.fold(0, (acc, x) => acc + x) # sum_of_evens = 89.2 Generic Algorithm Example
# ======== 1. Generic sorting algorithm ========
Comparator: (T: Type) -> Type = {
compare: (T, T) -> Int, # -1 if a < b, 0 if a == b, 1 if a > b
}
# Generic quicksort
quicksort: (T: Clone) -> ((array: Vec(T), cmp: Comparator(T)) -> Vec(T)) = {
if array.length <= 1 {
return array.clone()
}
pivot = array[array.length / 2]
left = Vec(T)()
right = Vec(T)()
for i in 0..array.length {
if i == array.length / 2 {
continue
}
item = array[i]
comparison = cmp.compare(item, pivot)
if comparison < 0 {
left.push(item)
} else {
right.push(item)
}
}
sorted_left = quicksort(left, cmp)
sorted_right = quicksort(right, cmp)
result = sorted_left.clone()
result.push(pivot)
result.extend(sorted_right)
return result
}
# ======== 2. IntComparator implementation ========
# Use function overloading
compare: (a: Int, b: Int) -> Int = {
if a < b {
return -1
} else if a > b {
return 1
} else {
return 0
}
}
# ======== 3. Usage examples ========
# Sort an Int array
numbers = Vec(Int)(3, 1, 4, 1, 5, 9, 2, 6)
sorted = quicksort(numbers, Comparator(Int)())
# Sort a String array (StringComparator is required)
strings = Vec(String)("hello", "world", "foo", "bar")
sorted_strings = quicksort(strings, Comparator(String)())9.3 Compile-Time Generic Example
# ======== 1. Compile-time matrix type ========
Matrix: (T: Type, Rows: Int, Cols: Int) -> Type = {
data: Array(Array(T, Cols), Rows),
# Compile-time dimension validation: use the Assert standard-library type
_assert: Assert(Rows > 0), # Rows > 0; otherwise, a compile error is reported
_assert: Assert(Cols > 0), # Cols > 0; otherwise, a compile error is reported
# Matrix operations
multiply: (M: Int) -> ((self: Matrix(T, Rows, Cols), other: Matrix(T, Cols, M)) -> Matrix(T, Rows, M)) = {
result = Matrix(T, Rows, M)()
for i in 0..Rows {
for j in 0..M {
sum = Zero::zero()
for k in 0..Cols {
sum = sum + self.data[i][k] * other.data[k][j]
}
result.data[i][j] = sum
}
}
return result
}
}
# ======== 2. Compile-time matrix creation ========
identity: (T: Add + Multiply + One, N: Int) -> ((size: N) -> Matrix(T, N, N)) = {
matrix = Matrix(T, N, N)()
for i in 0..N {
for j in 0..N {
if i == j {
matrix.data[i][j] = One::one()
} else {
matrix.data[i][j] = Zero::zero()
}
}
}
return matrix
}
# ======== 3. Usage examples ========
# Create a matrix whose size is known at compile time
# 2x3 matrix
matrix_2x3 = Matrix(Float, 2, 3)()
matrix_2x3.data[0][0] = 1.0
matrix_2x3.data[0][1] = 2.0
matrix_2x3.data[0][2] = 3.0
matrix_2x3.data[1][0] = 4.0
matrix_2x3.data[1][1] = 5.0
matrix_2x3.data[1][2] = 6.0
# 3x2 matrix
matrix_3x2 = Matrix(Float, 3, 2)()
matrix_3x2.data[0][0] = 7.0
matrix_3x2.data[0][1] = 8.0
matrix_3x2.data[1][0] = 9.0
matrix_3x2.data[1][1] = 10.0
matrix_3x2.data[2][0] = 11.0
matrix_3x2.data[2][1] = 12.0
# Matrix multiplication: 2x3 * 3x2 = 2x2
result = matrix_2x3.multiply(matrix_3x2)
# Compile-time validation: the type of result is Matrix(Float, 2, 2)
# 2x2 identity matrix
identity_3x3 = identity(Float, 3)()
# Dimension mismatch: compile error
# bad_multiply = matrix_2x3.multiply(identity_3x3) # Compile error: 3x3 != 2x3Trade-offs
Advantages
Zero-cost abstraction
- Compile-time monomorphization with no runtime overhead
- No virtual functions and no RTTI
Dead-code elimination
- Compile-time analysis instantiates only the generics that are used
- Manageable code growth
Macro replacement
- Type-safe code generation
- IDE-friendly with clear error messages
Compile-time computation
- Compile-time generics support compile-time computation
- Features such as dimension validation
- No
constkeyword; pure type constraints
Disadvantages
Compilation time
- Generic instantiation increases compilation time
- Constraint solving may be relatively slow
Memory usage
- Increased compiler memory usage
- The caching mechanism requires memory
Implementation complexity
- The constraint solver is complex
- The type-level computation engine is complex
Error diagnostics
- Generic errors may be complex
- Clear error messages are required
Mitigations
Caching strategy
- Cache instantiation results
- Use an LRU cache to limit memory usage
Incremental compilation
- Cache compilation results
- Instantiate incrementally
Error messages
- Clear error messages
- Suggestions for inferring generic parameters
Parallel compilation
- Instantiate generics in parallel
- Solve constraints with multiple threads
Alternatives
| Alternative | Why It Was Not Chosen |
|---|---|
| Basic generics only | Cannot replace complex macros |
| A pure macro system | No type safety and poor error messages |
| Constraints only | Insufficient flexibility |
| Runtime generics | Performance overhead |
Risks
| Risk | Impact | Mitigation |
|---|---|---|
| Constraint-solving complexity | Excessive compilation time | Incremental solving + caching |
| Code growth | Oversized binary files | DCE + threshold control |
| Implementation complexity | Extended development cycle | Phased implementation |
| Error diagnostics | Poor user experience | Detailed error messages |
Open Questions
Pending Decisions
| Topic | Description | Status |
|---|---|---|
| Instantiation strategy | Eager vs Lazy vs Threshold | Under discussion |
| Cache size | LRU cache capacity setting | Under discussion |
| Error diagnostics | Level of detail in generic error messages | Under discussion |
Future Optimizations
| Optimization | Value | Implementation difficulty |
|---|---|---|
| Instantiation graph analysis | High | Medium |
| Type-level programming DSL | Medium | High |
| Generic performance benchmarks | Medium | Low |
Appendix
Syntax BNF
# Generic parameters use the unified () syntax as part of the function type
# For example, map: (T: Type, R: Type) -> ((list: List(T), f: (T) -> R) -> List(R))
# Type constraints (in generic parameters)
type_bound ::= identifier
| identifier '+' identifier ('+' identifier)*
# Parameter declaration (type + name)
parameter ::= identifier ':' type
parameters ::= parameter (',' parameter)*
# Function declaration: name: type = expression
# Generic parameters are the first parameter group in the function type: (T: Type) -> ((params) -> return)
function ::= identifier ':' type '=' (expression | block)
# Method declaration: Type.method: type = expression
method ::= identifier '.' identifier ':' type '=' (expression | block)
# Type definition (unified Binding syntax)
# A generic type such as List: (T: Type) -> Type = { ... }
generic_type ::= identifier ':' type '=' type_expression
# Type in generic parameters is automatically filled by the compiler from the actual argument types
# For example, in map(numbers, f), T is extracted from numbers: List(Int), and R from f: (Int) -> StringLifecycle and Destination
┌─────────────┐
│ 草案 │ ← 当前状态
└──────┬──────┘
│
▼
┌─────────────┐
│ 审核中 │ ← 开放社区讨论和反馈
└──────┬──────┘
│
├──────────────────┐
▼ ▼
┌─────────────┐ ┌─────────────┐
│ 已接受 │ │ 已拒绝 │
└──────┬──────┘ └──────┬──────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ accepted/ │ │ rfc/ │
│ (正式设计) │ │ (保留原位) │
└─────────────┘ └─────────────┘References
Official YaoXiang Documentation
- RFC-010: Unified Type Syntax
- RFC-009: Ownership Model
- RFC-024: spawn-Based Concurrent Runtime Semantics
- RFC-008: Runtime Model
- tutorial/ Tutorial
