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:
64
src/types.rs
64
src/types.rs
@@ -582,6 +582,10 @@ pub struct TypeEnv {
|
||||
pub effects: HashMap<String, EffectDef>,
|
||||
/// Handler types
|
||||
pub handlers: HashMap<String, HandlerDef>,
|
||||
/// Trait definitions
|
||||
pub traits: HashMap<String, TraitDef>,
|
||||
/// Trait implementations: (trait_name, type) -> impl
|
||||
pub trait_impls: Vec<TraitImpl>,
|
||||
}
|
||||
|
||||
/// Type definition
|
||||
@@ -615,6 +619,66 @@ pub struct HandlerDef {
|
||||
pub params: Vec<(String, Type)>,
|
||||
}
|
||||
|
||||
/// Trait definition
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitDef {
|
||||
pub name: String,
|
||||
/// Type parameters for the trait (e.g., Functor<F>)
|
||||
pub type_params: Vec<String>,
|
||||
/// Super traits that must be implemented
|
||||
pub super_traits: Vec<TraitBoundDef>,
|
||||
/// Method signatures
|
||||
pub methods: Vec<TraitMethodDef>,
|
||||
}
|
||||
|
||||
/// A trait bound in type definitions
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitBoundDef {
|
||||
pub trait_name: String,
|
||||
pub type_args: Vec<Type>,
|
||||
}
|
||||
|
||||
/// A method signature in a trait
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitMethodDef {
|
||||
pub name: String,
|
||||
pub type_params: Vec<String>,
|
||||
pub params: Vec<(String, Type)>,
|
||||
pub return_type: Type,
|
||||
/// Whether this method has a default implementation
|
||||
pub has_default: bool,
|
||||
}
|
||||
|
||||
/// A trait implementation
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TraitImpl {
|
||||
pub trait_name: String,
|
||||
pub trait_args: Vec<Type>,
|
||||
/// The type this impl is for
|
||||
pub target_type: Type,
|
||||
/// Type parameters on the impl (e.g., impl<T> Show for List<T>)
|
||||
pub type_params: Vec<String>,
|
||||
/// Constraints on type parameters (e.g., where T: Show)
|
||||
pub constraints: Vec<(String, Vec<TraitBoundDef>)>,
|
||||
/// Method implementations
|
||||
pub methods: HashMap<String, Type>,
|
||||
}
|
||||
|
||||
/// A trait constraint on a type variable
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TraitConstraintDef {
|
||||
pub type_var: String,
|
||||
pub bounds: Vec<TraitBoundDef>,
|
||||
}
|
||||
|
||||
impl PartialEq for TraitBoundDef {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.trait_name == other.trait_name && self.type_args == other.type_args
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for TraitBoundDef {}
|
||||
|
||||
impl TypeEnv {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
|
||||
Reference in New Issue
Block a user