[MLton] associativity of &

Stephen Weeks MLton@mlton.org
Thu, 23 Mar 2006 15:51:34 -0800


In writing up the fold stuff, I wondered about the decision to make
the "&" of product types be infix left, rather than infix right.

  datatype ('a, 'b) prod = & of 'a * 'b
  infix  4 &

What was the reason behind that decision?  I think it might be better
to make "&" infix right.  By analogy with lists, in which :: is infix
right, it is easier to recur over products via a fold left if they
look like

  1 & (2 & (3 & (4 & ())))

rather than

  ((((1 & 2) & 3) & 4) & ())

I ran into a couple of cases where code using fold and products would
have been simplified had "&" been infix right.

With "&" infix right, constructing product via a fold left is slightly
trickier, because one has to construct a function that fills a "hole"
in the product, and apply that function at the very end.  But it's not
too bad.