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:
76
src/ast.rs
76
src/ast.rs
@@ -160,6 +160,8 @@ pub enum WhereClause {
|
||||
},
|
||||
/// Result refinement: where result > 0
|
||||
ResultRefinement { predicate: Box<Expr>, span: Span },
|
||||
/// Trait constraint: where T: Show, where T: Eq + Ord
|
||||
TraitConstraint(TraitConstraint),
|
||||
}
|
||||
|
||||
/// Module path: foo/bar/baz
|
||||
@@ -215,6 +217,10 @@ pub enum Declaration {
|
||||
Handler(HandlerDecl),
|
||||
/// Let binding at top level
|
||||
Let(LetDecl),
|
||||
/// Trait declaration: trait Name { fn method(...): T, ... }
|
||||
Trait(TraitDecl),
|
||||
/// Trait implementation: impl Trait for Type { ... }
|
||||
Impl(ImplDecl),
|
||||
}
|
||||
|
||||
/// Function declaration
|
||||
@@ -342,6 +348,76 @@ pub struct LetDecl {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// Trait declaration: trait Show { fn show(self): String }
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitDecl {
|
||||
pub visibility: Visibility,
|
||||
pub name: Ident,
|
||||
/// Type parameters: trait Functor<F> { ... }
|
||||
pub type_params: Vec<Ident>,
|
||||
/// Super traits: trait Ord: Eq { ... }
|
||||
pub super_traits: Vec<TraitBound>,
|
||||
/// Method signatures
|
||||
pub methods: Vec<TraitMethod>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A trait method signature
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitMethod {
|
||||
pub name: Ident,
|
||||
pub type_params: Vec<Ident>,
|
||||
pub params: Vec<Parameter>,
|
||||
pub return_type: TypeExpr,
|
||||
/// Optional default implementation
|
||||
pub default_impl: Option<Expr>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A trait bound: Show, Eq, Ord<T>
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TraitBound {
|
||||
pub trait_name: Ident,
|
||||
pub type_args: Vec<TypeExpr>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// Trait implementation: impl Show for Int { ... }
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImplDecl {
|
||||
/// Type parameters: impl<T: Show> Show for List<T> { ... }
|
||||
pub type_params: Vec<Ident>,
|
||||
/// Trait constraints on type parameters
|
||||
pub constraints: Vec<TraitConstraint>,
|
||||
/// The trait being implemented
|
||||
pub trait_name: Ident,
|
||||
/// Type arguments for the trait: impl Functor<List> for ...
|
||||
pub trait_args: Vec<TypeExpr>,
|
||||
/// The type implementing the trait
|
||||
pub target_type: TypeExpr,
|
||||
/// Method implementations
|
||||
pub methods: Vec<ImplMethod>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A trait constraint: T: Show, T: Eq + Ord
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitConstraint {
|
||||
pub type_param: Ident,
|
||||
pub bounds: Vec<TraitBound>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// A method implementation in an impl block
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImplMethod {
|
||||
pub name: Ident,
|
||||
pub params: Vec<Parameter>,
|
||||
pub return_type: Option<TypeExpr>,
|
||||
pub body: Expr,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// Type expressions
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TypeExpr {
|
||||
|
||||
Reference in New Issue
Block a user