How does this (`unsafePartial`) function application work?

This is from chapter 6 in the PureScript by example book:

unsafePartial head [1, 2, 3]

Why does this work? I’d have expected to have to write something like:

unsafePartial (head [1 2 3])
-- Or maybe:
(unsafePartial head) [1 2 3]

For reference:

:t unsafePartial
forall (a :: Type). (Partial => a) -> a

So I’m thinking unsafePartial is a function taking a single argument and dropping the Partial constraint from that. But in the example above it seems it is getting applied to two arguments.

What am I missing?

Welcome @joncol!

I think the only piece missing in your understanding is that function application is always left associative. In other words f a b c is equivalent to ((f a) b) c. This is possible because of “function currying.”

So you say

I’m thinking unsafePartial is a function taking a single argument and dropping the Partial constraint from that.

And that’s totally correct. The “single argument” in this case is Partial => a1 or specifically Partial => Array a2 -> a2 and the returned value is Array a2 -> a2 (I numbered the a’s to avoid mixing them up). Then it takes that and applies it to the next argument [1, 2, 3].

More examples of the same concept:

fn :: Int -> String
fn i = "This is #" <> show i

> fromMaybe show (Just fn) 5
"This is #5"

> fromMaybe show Nothing 5
"5"

> const fn "doesn't matter" 5
"This is #5"
2 Likes

Of course, thanks for pointing it out.