0

I have this function:

fun x (u,v,w) = (u::[v])::w;

Which gets a return type of

fn: 'a * 'a * 'a list list -> 'a list list

Could anyone explain to me how this type is generated from the function? Thank you!

Edit: Also, how would I call this function?

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
user2796815
  • 465
  • 2
  • 11
  • 18
  • 3
    You should not replace your question with a completely different question after someone has answered it. – newacct Dec 12 '13 at 23:42
  • I have rolled this back to the original question. If you want to ask a new question, please do that instead of editing this one. – molbdnilo Jan 10 '14 at 12:49
  • @OP: That's what is called type inference. You may have just asked people to explain you what type inference is. – Hibou57 Feb 04 '14 at 19:01

1 Answers1

2

:: takes two arguments -- one on the left and one on the right. If its left argument is of type t, then its right argument must be of type t list.

Thus, :: has type ('a * 'a list) -> 'a list, as the t is arbitrary, and is therefore represented by an 'a.

When you have the code (u::[v]), therefore, you are telling SML that u : t and 'v : t for some type t. This expression is then a t list, and so to use it as the left argument to ::, the right argument, w, must have type t list list.

This then gives that the type of (u::[v])::w is t list list for some t.

In summary:
u : t
v : t
w : t list
(u::[v])::w : t list list,

all for some type t.

Thus, the type of x is ('a * 'a * 'a list) -> 'a list list, as t is arbitrary.

To call this function, you could do something like x(1,2,[3,4]), which would give the list [[1,2],[3,4]].

qaphla
  • 4,707
  • 3
  • 20
  • 31