anon_swe
anon_swe

Reputation: 9345

SML: If -> is right associative

Ullman's book, Elements of ML Programming, introduces a simple version of the map function defined as follows:

fun simpleMap(F, nil) = nil
|   simpleMap(F, x::xs) = F(x)::simpleMap(F, xs);

val map = fn : ('a -> 'b) * 'a list -> 'b list

Given that -> is right associative, wouldn't you parenthesize like this:

('a -> 'b) * ('a list -> 'b list) ?

But this is incorrect since the domain type is a tuple consisting of the function and a list and the range type is just a list.

Where am I going wrong?

Thanks!

Upvotes: 2

Views: 601

Answers (1)

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68152

In SML, the type operator * binds more tightly than ->: it has a higher precedence just like * has a higher precedence than + in arithmetic.

This is why string * string -> string is the same as (string * string) -> string and not string * (string -> string). To read your example, we'd need to put parentheses around the * before worrying about how -> associates:

(('a -> 'b) * 'a list) -> 'b list

Upvotes: 6

Related Questions