1

Given the following function in SML:

fun i a b = let fun j()=a+1 in j end;

The data type of the function is:

val i = fn : int -> 'a -> unit -> int

I don't understand why int -> 'a -> unit -> int?

And why not (int * a') -> unit -> int, since the function i receives two inputs.

Jesper.Reenberg
  • 5,944
  • 23
  • 31
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 2
    Perhaps [this](http://stackoverflow.com/questions/14550740/sml-why-functions-always-take-one-argument-make-language-flexible/14554122#14554122), [this](http://stackoverflow.com/questions/14432696/what-does-this-function-signature-mean-in-sml/14438864#14438864) and [this](http://stackoverflow.com/questions/8395564/what-are-curry-and-uncurry-in-high-order-functions-in-ml/8400194#8400194) will enlighten you a bit more on currying? – Jesper.Reenberg Feb 16 '13 at 23:01

2 Answers2

2

This function (as well, as any function in SML) is actually receive one input, because of currying.

Technically there is function that takes unit, that returns function that takes 'a that returns function that takes int.

So

fun foo a b = a + b;

is just an syntactic sugar to

fun foo a = fn b => a + b;

and so on.

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
2

If you defined the function as fun i (a,b) = ... then it would take a tuple as an argument and its type would indeed be (int * 'a) -> unit -> int. It would be called as i (23,42) () ((23,42) being a tuple and () being the unit value).

However you defined it as fun i a b = ..., which defines a function that takes an argument and then returns another function that takes the next argument. It is a shortcut for fun i a = fn b => .... It can be called as i a b ().

sepp2k
  • 363,768
  • 54
  • 674
  • 675