Arity-generic functions

I’ve been trying to assess whether Dotty would allow for implementing arity-generic functions without relying on code generation (or macros). Tuples and match types seem to solve the type-level problem, but I have to admit I get stuck on the value-level side of things .

For instance, it feels like the construct below should be implementable without relying on metaprogramming

trait Zipper[F[_]]{

  def zip[A, B](fa : F[A], fb : F[B]) : F[(A, B)]

  // Can I implement this generically, applying zip recursively 
  // on tuple of arbitrary sizes ?
  def zipAll[Args <: Tuple](args : Tuple.Map[Args, F]) : F[Args] = ???

}

What am I missing here ? I feel like match types are a very elegant to the typelevel programming problem, but the parallel between types and values (for the case of tuples) does not appear obvious to me.

Maybe it is a case that fold should exist on tuples to allow for such a usecase, and its implementation should rely on the dynamic nature of the underlying structure, not shying away from instanceOf ?

4 Likes

This zipAll is actually sequence, right? Then I guess you would need some kind of fold. And an Applicative[F].

The problem I have with the current generic programming stuff in dotty is that AFAICT once you need to implement something yourself (anything that’s not as straightforward as a zip b or a map f) you need to resort to pretty low level stuff with casts all over the place. Shapeless has a pretty high entry barrier but once you get it you can fairly easily combine all the basic operations to build pretty powerful things in completely typesafe ways.

Perhaps once all the standard combinators are implemented it will be more feasible to build something interesting without having to resort to yucky lowlevel code. But I’m still not really seeing it.

3 Likes

It’s not really sequence because F is not necessary applicative: it could be a contravariant structure (an encoder of some sort, for instance). But what F is is irrelevant to the topic.

And I am very aware that shapeless can help solving the problem, but it’s my impression that Dotty aims at giving better UX to the sort of solutions that shapeless helps building, without having to rely on extensive knowledge related to implicit resolution.

Perhaps once all the standard combinators are implemented it will be more feasible to build something interesting without having to resort to yucky lowlevel code

That is another formulation of what I’m asking :smile:

3 Likes