Is it possible to do a foldLeft
on a list of arguments, where the initial value supplied to the fold is a fully curried function, the operator is apply
, and the list is a list of arguments to be passed to function f
?
For example, let's say f is defined as:
scala> val f = (i: Int, j: Int, k: Int, l: Int) => i+j+k+l
f: (Int, Int, Int, Int) => Int = <function4>
Which we can of course use directly:
scala> f(1, 2, 3, 4)
res1: Int = 10
Or curry and apply the arguments one at a time:
scala> f.curried
res2: Int => Int => Int => Int => Int = <function1>
scala> f.curried.apply(1).apply(2).apply(3).apply(4)
res3: Int = 10
At first glance this looks like a job for foldLeft
.
My first attempt at describing this sequence of apply
using foldLeft
looks like:
scala> List(1, 2, 3, 4).foldLeft(f.curried)({ (g, x) => g.apply(x) })
However, that yields the following error:
<console>:9: error: type mismatch;
found : Int => Int => Int => Int
required: Int => Int => Int => Int => Int
List(1, 2, 3, 4).foldLeft(f.curried)({ (g, x) => g.apply(x) })
My reading of the error message is that type inference would need some hint for g
.
The solution I'm looking for leaves everything unmodified in my original expression except the type of g
:
List(1, 2, 3, 4).foldLeft(f.curried)({ (g: ANSWER, x) => g.apply(x) })
My first thought was that a union type would be useful here. I've seen Miles Sabin's derivation of union types using Curry-Howard, so if that first hunch is true, then I appear to have the basic machinery required to solve the problem.
However: Even if union types are the answer it would be useful if I could refer to "The union of all types from the fully curried type of a function to the type of the curried function with all but the last argument supplied". In other words, a way to turn the type:
T1 => ... => Tn
into the union type:
(T1 => ... => Tn) |∨| ... |∨| (Tn-1 => Tn)
would be useful as the type for g
above.
Doing a foldLeft
on a List
limits the discussion to case where T1
through Tn-1
are all the same. A notation like
(T1 =>)+ Tn
would describe the type I want to provide for g
.
The specific case I'm asking about doesn't require arbitrarily long chains, so we could provide bounds on the iterator using
(T1 =>){1,4} Tn
Looking ahead at wanting to do this for chains of types that are not equal, though, perhaps some magical function on types that chops up the chain into the set of all suffixes is more useful:
Suffixes(T1 => ... => Tn)
Implementing this is well beyond my Scala abilities at the moment. Any hints as to how to go about doing so would be appreciated. Whether this can be done with advanced usage of Scala's existing type system or through a compiler plugin or neither, I do not know.
As has been noted in the comments below, calling the result a "union type" is not a perfect fit for this use case. I don't know what else to call it, but that's the closest idea I have at the moment. Do other languages have special support for this idea? How would this work in Coq and Agda?
Naming this problem and understanding where it sits with respect to the bigger picture (of type theory, decidability, and so forth) is more important to me than having a working implementation of ANSWER
, though both would be nice. Bonus points to anyone who can draw connections to Scalaz, Monoids, or Category Theory in general.