18 Expressions

Prerequisites

To capture and compute on expressions, and to visualise them, we will load the rlang and the lobstr packages.

18.1 Abstract syntax trees

  1. Q: Reconstruct the code represented by the trees below:

    #> █─f 
    #> └─█─g 
    #>   └─█─h
    #> █─`+` 
    #> ├─█─`+` 
    #> │ ├─1 
    #> │ └─2 
    #> └─3
    #> █─`*` 
    #> ├─█─`(` 
    #> │ └─█─`+` 
    #> │   ├─x 
    #> │   └─y 
    #> └─z

    A: Let the source (of the codechunks above) be with you and show you how the ASTs were produced. :)

  2. Q: Draw the following trees by hand then check your answers with ast().

    A: Let us delegate the drawing to the lobstr package.

  3. Q: What’s happening with the ASTs below? (Hint: carefully read ?"^")

    A: ASTs start function calls with the name of the function, because of this the call in the first expression is translated into its prefix form. In the second case, ** is translated by R’s parser into ^. In the last AST, the expression is flipped when R parses it:

  4. Q: What is special about the AST below? (Hint: re-read Section 6.2.1)

    A: The last leaf of the AST is not explicitly specified in the expression. Instead the srcref attribute, which points to the functions source code, is automatically created by base R.

  5. Q: What does the call tree of an if statement with multiple else if conditions look like? Why?

    A: The AST of nested else if statements might look a bit confusing because it contains multiple curly braces. However, we can see that in the else part of the AST just another expression is being evaluated, which happens to be an if statement and so forth.

    We can see the structure more clearly if we avoid the curly braces:

18.2 Expressions

  1. Q: Which two of the six types of atomic vector can’t appear in an expression? Why? Similarly, why can’t you create an expression that contains an atomic vector of length greater than one?

    A: It is not possible to create an expression that evaluates to an atomic of length greater than one without using a function (e.g. the c() function). But expressions that include a function are calls.

    Let’s make this observation concrete via an example:

    As there is no way to create raws and complex atomics without using a function call (this is only possible for imaginary scalars like i, 5i etc.), both of these vector types can not appear in an expression.

  2. Q: What happens when you subset a call object to remove the first element, e.g. expr(read.csv("foo.csv", header = TRUE))[-1] Why?

    A: When the first element of a call object is removed, the second element moves to the first position, which is the function to call. Therefore, we get "foo.csv"(header = TRUE)

  3. Q: Describe the differences between the following call objects.

    A: The call objects differ in their first two elements, which are in some cases evaluated before the call is constructed. In the first one, both median() and x are evaluated and inlined into the call. Therefore, we can see in the constructed call that median is a generic and the x argument is 1:10.

    In the following calls we remain with differing combinations. Once, only x and once only median() gets evaluated.

    In the final call neither x nor median() are evaluated.

    Note that all these calls will generate the same result when evaluated. The key difference is when the values bound to the x and median symbols are found.

  4. Q: rlang::call_standardise() doesn’t work so well for the following calls. Why? What makes mean() special?

    A: The reason for this unexpected behaviour is that mean() uses the ... argument and therefore can not standardise the regarding arguments. Since mean() uses S3 dispatch (i.e., UseMethod()) and the underlying mean.default() method specifies some more arguments, rlang::call_standardise() can do much better with a specific S3 method.

  5. Q: Why does this code not make sense?

    A: As stated in the book

    The first element of a call is always the function that gets called.

    Let’s see what happens when we run the code

    So giving the first element a name just adds metadata that R ignores.

  6. Q: Construct the expression if(x > 1) "a" else "b" using multiple calls to call2(). How does the structure code reflect the structure of the AST?

    A: Similar to the prefix version we get

    When we read the AST from left to right, we get the same structure: Function to evaluate, expression, which is another function and is evaluated first, and two constants which will be evaluated next.

18.3 Parsing and grammar

  1. Q: R uses parentheses in two slightly different ways as illustrated by these two calls:

    Compare and contrast the two uses by referencing the AST.

    A: The trick with these examples lies in the fact, that ( can be a part of R’s general prefix function syntax, but can also represent a call to the ( function.

    So in the AST of the first example, we will not see the outer (, since it is prefix function syntax and belongs to f(). In contrast, the inner ( is a function (represented as a symbol in the AST):

    In the second example, we can see that the outer ( is a function and the inner ( belongs to its syntax:

    For the sake of clarity, let’s also create a third example, where none of the ( is part of another function’s syntax:

  2. Q: = can also be used in two ways. Construct a simple example that shows both uses.

    A: = is used both for assignment, and for naming arguments in function calls:

    So when we play with ast(), we can directly see that the following is not possible

    We get an error, because b = makes R looking for an argument called b. Since x is the only argument of ast(), we get an error.

    The easiest way around this problem is to wrap in {}.

    When we ignore the braces and compare the trees, we can see, that the first = is used for assignment, second = is part of the syntax of function calls.

  3. Q: Does -2^2 yield 4 or -4? Why?

    A: It yields -4, because ^ has a higher operator precedence than -, which we can verify by looking at the AST:

  4. Q: What does !1 + !1 return? Why?

    A: The answer is a little surprising:

    To answer the “why?”, we take a look at the AST.

    The right !1 is evaluated first. It evaluates to FALSE, because R coerces every non 0 numeric to TRUE, when a logical operator is applied. The negation of TRUE then equals FALSE.

    Next 1 + FALSE is evaluated to 1, since FALSE is coerced to 0.

    Finally !1 is evaluated to FALSE.

    Please note that if ! had a higher precedence, the intermediate result would be FALSE + FALSE, which would evalutate to 0.

  5. Q: Why does x1 <- x2 <- x3 <- 0 work? Describe the two reasons.

    A: One reason is that <- is right-associative, i.e. evaluation takes place from right to left:

    The other reason is that <- invisibly returns the value on the right hand side.

  6. Q: Compare the ASTs of x + y %+% z and x ^ y %+% z. What have you learned about the precedence of custom infix functions?

    A: Let’s take a look at the syntax trees:

    Here y %+% z will be calculated first and the result will be added to x.

    Here x ^ y will be calculated first, and the result will be used as first argument to %+%().

    We can conclude that custom infix functions have precedence between addition and exponentiation.

  7. Q: What happens if you call parse_expr() with a string that generates multiple expressions? e.g. parse_expr("x + 1; y + 1")

    A: In this case parse_expr() notices that more than one expression would have to be generated and throws an error.

  8. Q: What happens if you attempt to parse an invalid expression? e.g. "a +" or "f())".

    A: Invalid expressions will lead to an error from the underlying parse() function.

  9. Q: deparse() produces vectors when the input is long. For example, the following call produces a vector of length two:

    What does expr_text() do instead?

    A: expr_text() will paste the results from deparse(expr) together and use a linebreak \n as separator.

  10. Q: pairwise.t.test() assumes that deparse() always returns a length one character vector. Can you construct an input that violates this expectation? What happens?

A: We can pass an expression to one of pairwise.t.test()’s data input arguments, which exceeds the default cutoff width in deparse(). The expression will be split into a character vector of length greater 1.

The deparsed data inputs are directly pasted (you may take a look at the source code) with “and” as separator and the result is just used to be displayed in the output. Just the data.name output will change (it will include more than one “and”).

```r
d <- 1
pairwise.t.test(2, d + d + d + d + d + d + d + d + 
  d + d + d + d + d + d + d + d + d)
#> 
#>  Pairwise comparisons using t tests with pooled SD 
#> 
#> data:  2 and d + d + d + d + d + d + d + d + d + d + d + d + d + d + d + d +  2 and     d 
#> 
#> <0 x 0 matrix>
#> 
#> P value adjustment method: holm
```

18.4 Walking AST with recursive functions

  1. Q: logical_abbr() returns TRUE for T(1, 2, 3). How could you modify logical_abbr_rec() so that it ignores function calls that use T or F?

    A: We can apply a similar logic as in the multiple assignment example from the textbook. We just treat it as a special case handled within a sub function called find_T_call(), which finds T() calls and “bounces them out”:

    Now lets test our new logical_abbr() function:

  2. Q: logical_abbr() works with expressions. It currently fails when you give it a function. Why not? How could you modify logical_abbr() to make it work? What components of a function will you need to recurse over?

    A: The function currently fails, because "closure" is not handled in switch_expr() within logical_abbr_rec(). If we want to make it work, we have to write a function to also iterate over the formals and the body of the input function.

  3. Q: Modify find_assign to also detect assignment using replacement functions, i.e. names(x) <- y.

    A: Let`s see what the AST of such an assignment looks like:

    So we need to catch the case where the first two elements are both calls. Further the first call is identical to <- and we must return only the second call to see which objects got new values assigned.

    This is why we add the following block within another else statement in find_assign_call():

    Let us finish with the whole code, followed by some tests for our new function:

  4. Q: Write a function that extracts all calls to a specified function.

    A: Here we need to delete the previously added else statement and check for a call (not necessarily <-) within the first if() in find_assign_call(). We save a call when we found one and return it later as part of our character output. Everything else stays the same: