effekt-lang / effekt

A research language with effect handlers and lightweight effect polymorphism
https://effekt-lang.org
MIT License
310 stars 14 forks source link

Typeclasses #66

Open b-studios opened 2 years ago

b-studios commented 2 years ago

We should consider adding typeclasses or similar to prevent repeating code such as sorting datastructures, etc.

serkm commented 9 months ago

This seems to me like a straightforward transformation to codata:

I guess the tricky part would be inferring the correct instance.

serkm commented 9 months ago

A suggestion for syntax:

class C[A] {
    // function declarations
}

instance C[T] {
    // function definitions
}

// type constraints
def f [A] <C[A]> (a: A) = {...}
serkm commented 9 months ago

Another idea would be to add implicit parameters to Effekt and use regular codata to express type classes:

// implicit parameter of type A
def f () <a: A> = {...}

def main() = {
   f() // passes implicit value iff there is exactly one value of type A in scope
}