migea
migea

Reputation: 517

Type profiles for ML function definitions

I've searched on Google but I didn't find anything about type profiles for ML.

For example for: fun bar(a, b, c) = [a, b + hd(c)];

the type profile is: int * int * int list -> int list and for

fun weird x z = [x, tl(z)] the type profile is 'a list -> 'a list -> 'a list list

but I don't understand the logic behind it.

Upvotes: 1

Views: 78

Answers (1)

Andreas Rossberg
Andreas Rossberg

Reputation: 36088

"Type profile" isn't a thing. You probably mean type inference. It is easy in this case. First, note that a curried function definition like yours is equivalent to writing

val weird = fn x => fn z => [x, tl z]

Now:

  1. From the structure of the function it follows that weird : A -> B -> C for some types A, B, C.
  2. The result is a list, so C must be D list for some type D.
  3. You are invoking tl on z, so B must be E list for some type E.
  4. The result of that call is the same E list, so D must be the same as E list.
  5. x is used as an element of the same list, so A must be E list as well.
  6. There is no further constraint, so we can pick an arbitrary 'a for E.

Putting all this together, you get

weird : 'a list -> 'a list -> 'a list list

Upvotes: 4

Related Questions