r/ProgrammingLanguages • bruijn, effekt • Apr 27 '26

Discussion Effekt: Name-Based Implicits

https://effekt-lang.org/tour/name-based-implicits

We recently added name-based implicits to our language. It's based on the work by Daan Leijen and Tim Whiting "Syntactic Implicit Parameters with Static Overloading". Let us know of your thoughts!

29 Upvotes

17 comments sorted by

View all comments

Show parent comment

3

u/_marzipankaiser_ Effekt Apr 29 '26 edited Apr 29 '26

I would agree that this is not a good use for the feature (and it's mostly in the examples because it's the simplest use of it I could come up with), and I have yet to see a really good use case for non-function-types other than the ones treated specially (like sourcePosition).

As for the sorting example, the interesting part is how this works at polymorphic uses / you abstract over them. If I have some more complex algorithm that sorts in the middle (or, e.g., a tree map), but I don't want to write this algorithm for every type anew, then there needs to be some way to say "this works with any type, as long as < works on this type". Haskell would use typeclasses for this. You could explicitly pass < as another argument (and that's what Effekt did until now).

Now, at the call site, I would argue that it's not typing that I mostly save (I'd be fine with typing more). In the sorting example, I save on:

  • Building up the compare function from its components: So, generating a compare for List[(Int, Int)] with the "obvious" implementation made up from a compare[A](List[A], List[A]){(A,A) => Ordering), a compare[A,B]( (A,B), (A,B) ){ (A,A) => Ordering }{ (B,B) => Ordering } and a compare(Int, Int).
  • Reading the compare - if it is just the standard implementation, specifying it makes the code less readable IMHO, since this verbosity hides the interesting part of the code. This is even more the case if the only use of the comparison is to build up a tree map to use as a dictionary.

So then, why not "just" use typeclasses? For one, I think this is a better fit for Effekt. We also desugar, e.g., [1,2,3] into Cons(1, Cons(2, Cons(3, Nil()))) by-name (and you can define your own Cons if you want to). Additionally, it does not need an additional set of rules to resolve typeclasses (it's just overloading at the callsite, and potentially resolving implicits again), a new construct to declare a new typeclass etc. More interestingly, by using the context, we can have local definitions and have them used. So, if I want to sort Ints differently within a function, I can have a different comparison, which is locally scoped. It also (in a sense) answers the question about orphan instances: The definition is not in scope and thus will not be used. Finally, it also can be retrofitted to an existing language and stdlib beautifully: Without any changes in the standard library, I can already define a function def foo[A](...){?show: A => String }: ... and have it use the show function for, e.g., Int that is already defined. Changing a definition to now take the existing show parameter implicitly also does not require any of the use sites to change (since passing it explicitly is still allowed).