feat: implement type classes / traits

Add support for type classes (traits) with full parsing, type checking, and
validation. The implementation includes:

- Trait declarations: trait Show { fn show(x: T): String }
- Trait implementations: impl Show for Int { fn show(x: Int) = ... }
- Super traits: trait Ord: Eq { ... }
- Trait constraints in where clauses: where T: Show + Eq
- Type parameters on traits: trait Functor<F> { ... }
- Default method implementations
- Validation of required method implementations

This provides a foundation for ad-hoc polymorphism and enables
more expressive type-safe abstractions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 04:51:06 -05:00
parent df5c0a1a32
commit 05a85ea27f
8 changed files with 675 additions and 10 deletions

View File

@@ -40,6 +40,9 @@ pub enum TokenKind {
As,
From, // from (for migrations)
Latest, // latest (for @latest version constraint)
Trait, // trait (for type classes)
Impl, // impl (for trait implementations)
For, // for (in impl Trait for Type)
// Behavioral type keywords
Is, // is (for behavioral properties)
@@ -118,6 +121,9 @@ impl fmt::Display for TokenKind {
TokenKind::As => write!(f, "as"),
TokenKind::From => write!(f, "from"),
TokenKind::Latest => write!(f, "latest"),
TokenKind::Trait => write!(f, "trait"),
TokenKind::Impl => write!(f, "impl"),
TokenKind::For => write!(f, "for"),
TokenKind::Is => write!(f, "is"),
TokenKind::Pure => write!(f, "pure"),
TokenKind::Total => write!(f, "total"),
@@ -550,6 +556,9 @@ impl<'a> Lexer<'a> {
"as" => TokenKind::As,
"from" => TokenKind::From,
"latest" => TokenKind::Latest,
"trait" => TokenKind::Trait,
"impl" => TokenKind::Impl,
"for" => TokenKind::For,
"is" => TokenKind::Is,
"pure" => TokenKind::Pure,
"total" => TokenKind::Total,