Try incoherent traits on stable Rust today with Context-Generic Programming
In March, three blog posts sparked a conversation about traits and coherence in Rust. Nadrieril's Elaborating Rust Traits to Explicit Trait Proofs and What If Traits Carried Values introduced the concept of trait implementations as values, while Boxy's An Incoherent Rust explored what Rust would look like with incoherent traits that can opt out of coherence rules.
These discussions gained traction and became a goal for the Rust project. Nadrieril and Niko Matsakis are working on the question of when trait implementations may depend on themselves. Context-Generic Programming (CGP) extends Rust with pluggable trait implementations that can be chosen at compile time based on context. It currently runs on stable Rust.
This post explains how CGP relates to the discussion on incoherent traits, which parts of the Incoherent Rust proposal it can express, and where its context type works similarly to dictionaries in dictionary-passing style. However, CGP falls short in certain aspects.
The post begins with a background and introduction to CGP, followed by desugaring several features of Incoherent Rust into CGP code. It compares CGP's context type with the dictionaries used in dictionary-passing style and states the limitations of expressing Incoherent Rust using CGP. Later sections discuss the capabilities of CGP's providers and contexts beyond the current designs, suggestions for Incoherent Rust, and how other languages handle similar trade-offs.
The discussion surrounding incoherent traits and coherence rules in Rust can be summarized as follows:
1. Nadrieril's post on explicit trait proofs presents traits as structs of methods, with trait bounds acting as values passed along.
2. Boxy's post explores the idea of incoherent traits, which can opt out of Rust's coherence rules, using the example of ecosystem evolution, such as serde for HashMap and associated types.
3. The incoherent traits discussion has led to three separate lines of exploration:
a. The types team's experiment aims to compute each trait proof once during type checking instead of solving the same bounds repeatedly during other phases of the compiler.
b. Nadrieril's vision for a formal specification translates Rust into an interpreter (PolyMiniRust) where every trait proof is explicit.
c. Feature posts demonstrate what explicit proofs would enable, such as incoherent traits and traits carrying values.
These three lines of exploration do not necessarily need to go together, as the compiler might compute proofs once without exposing incoherent traits, and an incoherent traits feature could be designed before the compiler supports it.
Rewritten by urgent.news; may contain errors — check the original.
Source: contextgeneric.dev