Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially apply function n times

Tags:

haskell

Assume

f x y z = x*y*z

Then I expect to return the application of f three times with each member of the list

foldl ($) f [1,2,3]

Something like (((f 1) 2) 3) = 1*2*3 = 6

The function f would be the accumulator and each iteration of the fold would apply one argument and return a partially applied function as the next accumulator.

Why doesn't this work? Is it because f changes type while iterating?

As an aside: is there any other way to accomplish this type of function application?

like image 924
Juan Pablo Santos Avatar asked Jan 28 '26 17:01

Juan Pablo Santos


1 Answers

The types look like

foldl :: (a -> b -> a) -> a -> [b] -> a

($) :: (x -> y) -> x -> y

To apply foldl to ($) requires matching (technically, unifying) the type of the first argument to foldl with the type of ($). That is, solving the equation

a -> b -> a  =  (x -> y) -> x -> y

This leads immediately to

a = x -> y
b = x
a = y

Substituting the second and third equations into the first:

a = b -> a

The problem is that Haskell has no type that solves this equation. In particular, it is impossible to write down a solution with a finite number of symbols! It expands first to

a = b -> b -> a

then

a = b -> b -> b -> a

and on forever. So there is no way to choose types for the type variables to make them match, and GHC will complain loudly.

like image 125
dfeuer Avatar answered Jan 30 '26 09:01

dfeuer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!