0

Scala 3 has a powerful mechanism of expressing type constructors via type lambdas.

Even simple type lambdas can do powerful things like expressing partial application of a type constructor (see for ex https://stackoverflow.com/a/75428709/336184 ).

Docs mention "Curried Type Parameters" like

type TL = [X] =>> [Y] =>> (X, Y)

this looks like even more abstract thing.

Question:

Can anyone give a working example with an implementation of such a type lambda? Also - what is a practical purpose of such an abstraction? Any parallels in Haskell?

Max
  • 1,741
  • 3
  • 23
  • 40

1 Answers1

2
type TL = [X] =>> [Y] =>> ...

is the same as

type TL[X] = [Y] =>> ...

and should be the same as

type TL[X][Y] = ...

if there were multiple type-parameter lists (MTPL).

So [X] =>> [Y] =>> ... should be a way to introduce such type anonymously.

MTPL along with named type parameters could be useful for specifying some type parameters and inferring the rest of them.

Cannot prove equivalence with a path dependent type

Curried type "does not take type parameters"

https://contributors.scala-lang.org/t/multiple-type-parameter-lists-in-dotty-si-4719/2399

https://github.com/scala/bug/issues/4719

https://docs.scala-lang.org/scala3/reference/experimental/named-typeargs.html

For example specifying some type parameters and inferring the rest can be necessary for type-level calculations. Currently for type-level calculations people either make type parameters nested or use type members instead of type parameters.

When are dependent types needed in Shapeless?

In Haskell you can write

foo :: forall (f :: * -> * -> *) . ()
foo = ()

but in Scala without MTPL implemented, currently you can't write

def foo[F[_][_]]: Unit = ()

you can only write

def foo[F[_,_]]: Unit = ()

If there were MTPL then for a definition like def foo[F[_][_]]... it would be convenient having curried type lambdas [X] =>> [Y] =>> ..., you could use them at a call site foo[[X] =>> [Y] =>> ...].

In Haskell all type constructors are curried and there are no type lambdas

Lambda for type expressions in Haskell?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • Dmytro, you keep rocking ! The part "and should be the same as... if there were multiple type-parameter lists (MTPL)" is very very interesting. is it possible you write any factual __implementation__ for the type alias `type TL = ...` for illustration, to make it more tangible and not so abstract? – Max Feb 13 '23 at 15:54
  • 1
    @Max Well, maybe `TL[K][V] = Map[K, V]` (we'd like to specify `K` and infer `V`) or `TL[K, +V][P] = CircularMap[K, V, P]` in https://stackoverflow.com/questions/69120836/cannot-prove-equivalence-with-a-path-dependent-type or `TL[In][O] = MyTypeclass[In] { type Out = O }` for any type class in type-level calculations, for example https://stackoverflow.com/questions/75418623/express-function-of-arbitrary-arity-in-vanilla-scala-3 I guess `[X] =>> [Y] =>> ...` are currently a little premature and not so useful without `type TL[X][Y]`, `def foo[X][Y]`, `def foo[F[_][_]]` – Dmytro Mitin Feb 14 '23 at 06:10
  • @Max and it's written *"no special provision is made to infer type arguments to such curried type lambdas"* in https://docs.scala-lang.org/scala3/reference/new-types/type-lambdas-spec.html – Dmytro Mitin Feb 14 '23 at 06:10