Search code examples
haskellfunctional-programmingassociative

Is "associates to the right" equivalent to being left or right associative?


If I say an operation is left-associative, is that equivalent to saying it "associates from the left" and "associates to the right"?

My confusion comes from an example in my functional programming Haskell textbook. It states:

Function application associates to the left. e.g. mult x y z means ((mult x)y)z. i.e. mult takes an integer x, returns a function mult x, which takes an integer y, and returns a function mult x y, which takes an integer z and returns the result of x*y*z.

But if I say it "associates to the left", I think of it being right-associative, i.e. evaluation starts from the right and to the left. However, since evaluation of mult starts from the left and to the right, is this left-associative? Should the author have said function application "associates to the right"? Or am I missing something and the author is correct?


Solution

  • You just need to stop thinking about evaluation order.

    Bracketing is actually about expression structure, that is, which of these we mean when we say mult x y, not about how we may later decide to evaluate it.

          $           $
         / \         / \
        $   y     mult  $
       / \             / \
    mult  x           x   y
    

    Yes, we were taught in school that brackets are about the order you do things in. That's because we learned it in the context of arithmetic operators. Since these are all strict, there's less freedom to decide how to evaluate a given expression and the bracketing mostly determines an order. Plus we probably never thought much about expressions as abstract things distinct from the way they are written down.

    In the more general context of Haskell we can't conflate parsing and evaluation. When we say something "associates left" or "to the left" we're only talking about how it's parsed. It tells you that the x belongs in a subexpression with the mult on its left and not with the y on its right.

    (I haven't seen anyone use the phrase "associates from" and it doesn't really make sense unless maybe you read it as "associates away from".)