blog-static/content/blog/12_compiler_let_in_lambda/index.md

32 KiB

title date tags description draft
Compiling a Functional Language Using C++, Part 12 - Let/In and Lambdas 2020-04-20T20:15:16-07:00
C and C++
Functional Languages
Compilers
In this post, we extend our language with let/in expressions and lambda functions. true

Now that our language's type system is more fleshed out and pleasant to use, it's time to shift our focus to the ergonomics of the language itself. I've been mentioning let/in expressions and lambda expressions for a while now. The former will let us create names for expressions that are limited to a certain scope (without having to create global variable bindings), while the latter will allow us to create functions without giving them any name at all.

Let's take a look at let/in expressions first, to make sure we're all on the same page about what it is we're trying to implement. Let's start with some rather basic examples, and then move on to more complex examples. The most basic use of a let/in expression is, in Haskell:

let x = 5 in x + x

In the above example, we bind the variable x to the value 5, and then refer to x twice in the expression after the in. The whole snippet is one expression, evaluating to what the in part evaluates to. Additionally, the variable x does not escape the expression - {{< sidenote "right" "used-note" "it cannot be used anywhere else." >}} Unless, of course, you bind it elsewhere; naturally, using x here does not forbid you from re-using the variable. {{< /sidenote >}}

Now, consider a slightly more complicated example:

let sum xs = foldl (+) 0 xs in sum [1,2,3]

Here, we're defining a function sum, {{< sidenote "right" "eta-note" "which takes a single argument:" >}} Those who favor the point-free programming style may be slightly twitching right now, the words eta reduction swirling in their mind. What do you know, fold-based sum is even one of the examples on the Wikipedia page! I assure you, I left the code as you see it deliberately, to demonstrate a principle. {{< /sidenote >}} the list to be summed. We will want this to be valid in our language, as well. We will soon see how this particular feature is related to lambda functions, and why I'm covering these two features in the same post.

Let's step up the difficulty a bit more, with an example that, {{< sidenote "left" "translate-note" "though it does not immediately translate to our language," >}} The part that doesn't translate well is the whole deal with patterns in function arguments, as well as the notion of having more than one equation for a single function, as is the case with safeTail.

It's not that these things are impossible to translate; it's just that translating them may be worthy of a post in and of itself, and would only serve to bloat and complicate this part. What can be implemented with pattern arguments can just as well be implemented using regular case expressions; I dare say most "big" functional languages actually just convert from the former to the latter as part of the compillation process. {{< /sidenote >}} illustrates another important principle:

1 2 3 4 5 6 7 let safeTail [] = Nothing safeTail [x] = Just x safeTail (_:xs) = safeTail xs myTail = safeTail [1,2,3,4] in myTail

The principle here is that definitions in let/in can be recursive and polymorphic. Remember the note in [part 10]({{< relref "10_compiler_polymorphism.md" >}}) about let-polymorphism? This is it: we're allowing polymorphic variable bindings, but only when they're bound in a let/in expression (or at the top level).

The principles demonstrated by the last two snippets mean that compiling let/in expressions, at least with the power we want to give them, will require the same kind of dependency analysis we had to go through when we implemented polymorphically typed functions. That is, we will need to analyze which functions calls which other functions, and typecheck the callees before the callers. We will continue to represent callee-caller relationships using a dependency graph, in which nodes represent functions, and an edge from one function node to another means that the former function calls the latter. Below is an image of one such graph:

{{< figure src="fig_graph.png" caption="Example dependency graph without let/in expressions." >}}

Since we want to typecheck callees first, we effectively want to traverse the graph in reverse topological order. However, there's a slight issue: a topological order is only defined for acyclic graphs, and it is very possible for functions in our language to mutually call each other. To deal with this, we have to find groups of mutually recursive functions, and and treat them as a single unit, thereby eliminating cycles. In the above graph, there are two groups, as follows:

{{< figure src="fig_colored_ordered.png" caption="Previous depndency graph with mutually recursive groups highlighted." >}}

As seen in the second image, according to the reverse topological order of the given graph, we will typecheck the blue group containing three functions first, since the sole function in the orange group calls one of the blue functions.

Things are more complicated now that let/in expressions are able to introduce their own, polymorphic and recursive declarations. However, there is a single invariant we can establish: function definitions can only depend on functions defined at the same time as them. That is, for our purposes, functions declared in the global scope can only depend on other functions declared in the global scope, and functions declared in a let/in expression can only depend on other functions declared in that same expression. That's not to say that a function declared in a let/in block inside some function f can't call another globally declared function g - rather, we allow this, but treat the situation as though f depends on g. In contrast, it's not at all possible for a global function to depend on a local function, because bindings created in a let/in expression do not escape the expression itself. This invariant tells us that in the presence of nested function definitions, the situation looks like this:

{{< figure src="fig_subgraphs.png" caption="Previous depndency graph augmented with let/in subgraphs." >}}

In the above image, some of the original nodes in our graph now contain other, smaller graphs. Those subgraphs are the graphs created by function declarations in let/in expressions. Just like our top-level nodes, the nodes of these smaller graphs can depend on other nodes, and even form cycles. Within each subgraph, we will have to perform the same kind of cycle detection, resulting in something like this:

{{< figure src="fig_subgraphs_colored_all.png" caption="Augmented dependency graph with mutually recursive groups highlighted." >}}

When typechecking a function, we must be ready to perform dependency analysis at any point. What's more is that the free variable analysis we used to perform must now be extended to differentiate between free variables that refer to "nearby" definitions (i.e. within the same let/in expression), and "far away" definitions (i.e. outside of the let/in expression). And speaking of free variables...

What do we do about variables that are captured by a local definition? Consider the following snippet:

1 2 3 addToAll n xs = map addSingle xs where addSingle x = n + x

In the code above, the variable n, bound on line 1, is used by addSingle on line 3. When a function refers to variables bound outside of itself (as addSingle does), it is said to be capturing these variables, and the function is called a closure. Why does this matter? On the machine level, functions are represented as sequences of instructions, and there's a finite number of them (as there is finite space on the machine). But there is an infinite number of addSingle functions! When we write addToAll 5 [1,2,3], addSingle becomes 5+x. When, on the other hand, we write addToAll 6 [1,2,3], addSingle becomes 6+x. There are certain ways to work around this - we could, for instance, dynamically create machine code in memory, and then execute it (this is called just-in-time compilation). This would end up with a collections of runtime-defined functions that can be represented as follows:

1 2 3 4 5 6 7 -- Version of addSingle when n = 5 addSingle5 x = 5 + x -- Version of addSingle when n = 6 addSingle6 x = 6 + x -- ... and so on ...

But now, we end up creating several functions with almost identical bodies, with the exception of the free variables themselves. Wouldn't it be better to perform the well-known strategy of reducing code duplication by factoring out parameters, and leaving only instance of the repeated code? We would end up with:

1 2 addToAll n xs = map (addSingle n) xs addSingle n x = n + x

Observe that we no longer have the "infinite" number of functions - the infinitude of possible behaviors is created via currying. Also note that addSingle {{< sidenote "right" "global-note" "is now declared at the global scope," >}} Wait a moment, didn't we just talk about nested polymorphic definitions, and how they change our typechecking model? If we transform our program into a bunch of global definitions, we don't need to make adjustments to our typechecking.

This is true, but why should we perform transformations on a malformed program? Typechecking before pulling functions to the global scope will help us save the work, and breaking down one dependency-searching problem (which is O(n^3) thanks to Warshall's) into smaller, independent problems may even lead to better performance. Furthermore, typechecking before program transformations will help us come up with more helpful error messages. {{< /sidenote >}} and can be transformed into a sequence of instructions just like any other global function. It has been pulled from its where (which, by the way, is pretty much equivalent to a let/in) to the top level.

Now, see how addSingle became (addSingle n)? If we chose to rewrite the program this way, we'd have to find-and-replace every instance of addSingle in the function body, which would be tedious and require us to keep track of shadowed variables and the like. Also, what if we used a local definition twice in the original piece of code? How about something like this:

1 2 3 fourthPower x = square * square where square = x * x

Applying the strategy we saw above, we get:

1 2 fourthPower x = (square x) * (square x) square x = x * x

This is valid, except that in our evaluation model, the two instances of (square x) will be built independently of one another, and thus, will not be shared. This, in turn, will mean that square will be called twice, which is not what we would expect from looking at the original program. This isn't good. Instead, why don't we keep the where, but modify it as follows:

1 2 3 fourthPower x = square * square where square = square' x square' x = x * x

This time, assuming we can properly implement where, the call to square' x should only occur once. Though I've been using where, which leads to less clutter in Haskell code, the exact same approach applies to let/in, and that's what we'll be using in our language.

This technique of replacing captured variables with arguments, and pulling closures into the global scope to aid compilation, is called Lambda Lifting. Its name is no coincidence - lambda functions need to undergo the same kind of transformation as our nested definitions (unlike nested definitions, though, lambda functions need to be named). This is why they are included in this post together with let/in!

Implementation

Now that we understand what we have to do, it's time to jump straight into doing it. First, we need to refactor our current code so allow for the changes we're going to make; then, we can implement let/in expressions; finally, we'll tackle lambda functions.

Infrastructure Changes

When finding captured variables, the notion of free variables once again becomes important. Recall that a free variable in an expression is a variable that is defined outside of that expression. Consider, for example, the expression:

let x = 5 in x + y

In this expression, x is not a free variable, since it's defined in the let/in expression. On the other hand, y is a free variable, since it's not defined locally.

The algorithm that we used for computing free variables was rather biased. Previously, we only cared about the difference between a local variable (defined somewhere in a function's body, or referring to one of the function's parameters) and a global variable (referring to a function name). This shows in our code for find_free. Consider, for example, this segment:

{{< codelines "C++" "compiler/11/ast.cpp" 33 36 >}}

We created bindings in our type environment whenever we saw a new variable being introduced, which led us to only count variables that we did not bind anywhere as 'free'. This approach is no longer sufficient. Consider, for example, the following Haskell code:

1 2 3 4 5 someFunction x = let y = x + 5 in x*y

We can see that the variable x is introduced on line 1. Thus, our current algorithm will happily store x in an environment, and not count it as free. But clearly, the definition of y on line 3 captures x! If we were to lift y into global scope, we would need to pass x to it as an argument. To fix this, we have to separate the creation and assignment of type environments from free variable detection. Why don't we start with ast and its descendants? Our signatures become:

void ast::find_free(std::set<std::string>& into);
type_ptr ast::typecheck(type_mgr& mgr, type_env_ptr& env);

For the most part, the code remains unchanged. We avoid using env (and this->env), and default to marking any variable as a free variable:

{{< codelines "C++" "compiler/12/ast.cpp" 39 41 >}}

Since we no longer use the environment, we resort to an alternative method of removing bound variables. Here's ast_case::find_free:

{{< codelines "C++" "compiler/12/ast.cpp" 169 181 >}}

For each branch, we find the free variables. However, we want to avoid marking variables that were introduced through pattern matching as free (they are not). Thus, we use pattern::find_variables to see which of the variables were bound by that pattern, and remove them from the list of free variables. We can then safely add the list of free variables in the pattern to the overall list of free variables. Other ast descendants experience largely cosmetic changes (such as the removal of the env parameter).

Of course, we must implement find_variables for each of our pattern subclasses. Here's what I got for pattern_var:

{{< codelines "C++" "compiler/12/ast.cpp" 402 404 >}}

And here's an equally terse implementation for pattern_constr:

{{< codelines "C++" "compiler/12/ast.cpp" 417 419 >}}

We also want to update definition_defn with this change. Our signatures become:

void definition_defn::find_free();
void definition_defn::insert_types(type_mgr& mgr, type_env_ptr& env, visibility v);

We'll get to the visiblity parameter later. The implementations are fairly simple. Just like ast_case, we want to erase each function's parameters from its list of free variables:

{{< codelines "C++" "compiler/12/definition.cpp" 13 18 >}}

Since find_free no longer creates any type bindings or environments, this functionality is shouldered by insert_types:

{{< codelines "C++" "compiler/12/definition.cpp" 20 32 >}}

Now that free variables are properly computed, we are able to move on to bigger and better things.

Nested Definitions

At present, our code for typechecking the whole program is located in main.cpp:

{{< codelines "C++" "compiler/11/main.cpp" 43 61 >}}

This piece of code goes on. We now want this to be more general. Soon, let/in expressions with bring with them definitions that are inside other definitions, which will not be reachable at the top level. The fundamental topological sorting algorithm, though, will remain the same. We can abstract a series of definitions that need to be ordered and then typechecked into a new struct, definition_group:

{{< codelines "C++" "compiler/12/definition.hpp" 73 83 >}}

This will be exactly like a list of defn/data definitions we have at the top level, except now, it can also occur in other places, like let/in expressions. Once again, ignore for the moment the visibility field.

The way we defined function ordering requires some extra work from definition_group. Recall that conceptually, functions can only depend on other functions defined in the same let/in expression, or, more generally, in the same definition_group. This means that we now classify free variables in definitions into two categories: free variables that refer to "nearby" definitions (i.e. definitions in the same group) and free variables that refer to "far away" definitions. The "nearby" variables will be used to do topological ordering, while the "far away" variables can be passed along further up, perhaps into an enclosing let/in expression (for which "nearby" variables aren't actually free, since they are bound in the let). We implement this partitioning of variables in definition_group::find_free:

{{< codelines "C++" "compiler/12/definition.cpp" 94 105 >}}

Notice that we have added a new nearby_variables field to definition_defn. This is used on line 101, and will be once again used in definition_group::typecheck. Speaking of typecheck, let's look at its definition:

{{< codelines "C++" "compiler/12/definition.cpp" 107 145 >}}

This function is a little long, but conceptually, each for loop contains a step of the process:

  • The first loop declares all data types, so that constructors can be verified to properly reference them.
  • The second loop creates all the data type constructors.
  • The third loop adds edges to our dependency graph.
  • The fourth loop performs typechecking on the now-ordered groups of mutually recursive functions.
    • The first inner loop inserts the types of all the functions into the environment.
    • The second inner loop actually performs typechecking.
    • The third inner loop makes as many things polymorphic as possible.

We can now adjust our parser.y to use a definition_group instead of two global vectors. First, we declare a global definition_group:

{{< codelines "C++" "compiler/12/parser.y" 10 10 >}}

Then, we adjust definitions to create definition_groups:

{{< codelines "text" "compiler/12/parser.y" 59 68 >}}

We can now adjust main.cpp to use the global definition_group. Among other changes (such as removing extern references to vectors, and updating function signatures) we also update the typecheck_program function:

{{< codelines "C++" "compiler/12/main.cpp" 41 49 >}}

Now, our code is ready for typechecking nested definitions, but not for compiling them. The main thing that we still have to address is the addition of new definitions to the global scope. Let's take a look at that next.

Global Definitions

We want every function (and even non-function definitions that capture surrounding variables), regardless of whether or not it was declared in the global scope, to be processed and converted to LLVM code. The LLVM code conversion takes several steps. First, the function's AST is translated into G-machine instructions, which we covered in [part 5]({{< relref "05_compiler_execution.md" >}}), by a process we covered in [part 6]({{< relref "06_compiler_compilation.md" >}}). Then, an LLVM function is created for every function, and registered globally. Finally, the G-machine instructions are converted into LLVM IR, which is inserted into the previously created functions. These things can't be done in a single pass: at the very least, we can't start translating G-machine instructions into LLVM IR until functions are globally declared, because we would otherwise have no means of referencing other functions. It makes sense to me, then, to pull out all the 'global' definitions into a single top-level list (perhaps somewhere in main.cpp).

Let's start implementing this with a new global_scope struct. This struct will contain all of the global function and constructor definitions:

{{< codelines "C++" "compiler/12/global_scope.hpp" 42 55 >}}

This struct will allow us to keep track of all the global definitions, emitting them as we go, and then coming back to them as necessary. There are also signs of another piece of functionality: occurence_count and mangle_name. These two will be used to handle duplicate names.

We cannot have two global functions named the same thing, but we can easily imagine a situation in which two separate let/in expressions define a variable like x, which then needs to be lifted to the global scope. We resolve such conflicts by slightly changing - "mangling" - the name of one of the resulting global definitions. We allow the first global definition to be named the same as it was originally (in our example, this would be x). However, if we detect that a global definition x already exists (we track this using occurence_count), we rename it to x_1. Subsequent global definitions will end up being named x_2, x_3, and so on.

Alright, let's take a look at global_function and global_constructor. Here's the former:

{{< codelines "C++" "compiler/12/global_scope.hpp" 11 27 >}}

There's nothing really surprising here: all of the fields are reminiscent of definition_defn, though some type-related variables are missing. We also include the three compilation-related methods, compile, declare_llvm, and generate_llvm, which were previously in definition_defn. Let's look at global_constructor now:

{{< codelines "C++" "compiler/12/global_scope.hpp" 29 40 >}}

This maps pretty closely to a single definition_data::constructor. There's a difference here that is not clear at a glance, though. Whereas the name in a definition_defn or definition_data refers to the name as given by the user in the code, the name of a global_function or global_constructor has gone through mangling, and thus, should be unique.

Let's now look at the implementation of these structs' methods. The methods add_function and add_constructor are pretty straightforward. Here's the former:

{{< codelines "C++" "compiler/12/global_scope.cpp" 39 43 >}}

And here's the latter:

{{< codelines "C++" "compiler/12/global_scope.cpp" 45 49 >}}

In both of these functions, we return a reference to the new global definition we created. This helps us access the mangled name field, and, in the case of global_function, inspect the ast_ptr that represents its body.

Next, we have global_scope::compile and global_scope::generate_llvm, which encapsulate these operations on all global definitions. Their implementations are very straightforward, and are similar to the gen_llvm function we used to have in our main.cpp:

{{< codelines "C++" "compiler/12/global_scope.cpp" 51 67 >}}

Finally, we have mangle, which takes care of potentially duplicate variable names:

{{< codelines "C++" "compiler/12/global_scope.cpp" 69 83 >}}

Let's move on to the global definition structs. The compile, declare_llvm, and generate_llvm methods for global_function are pretty much the same as those that we used to have in definition_defn:

{{< codelines "C++" "compiler/12/global_scope.cpp" 4 24 >}}

The same is true for global_constructor and its method generate_llvm:

{{< codelines "C++" "compiler/12/global_scope.cpp" 26 37 >}}

Recall that in this case, we need not have two methods for declaring and generating LLVM, since constructors don't reference other constructors, and are always generated before any function definitions.

Visibility

Should we really be turning all free variables in a function definition into arguments? Consider the following piece of Haskell code:

1 2 3 add x y = x + y mul x y = x * y something = mul (add 1 3) 3

In the definition of something, mul and add occur free. A very naive lifting algorithm might be tempted to rewrite such a program as follows:

1 2 3 4 add x y = x + y mul x y = x * y something' add mul = mul (add 1 3) 3 something = something' add mul

But that's absurd! Not only are add and mul available globally, but such a rewrite generates another definition with free variables, which means we didn't really improve our program in any way. From this example, we can see that we don't want to be turning reference to global variables into function parameters. But how can we tell if a variable we're trying to operate on is global or not? I propose a flag in our type_env, which we'll augment to be used as a symbol table. To do this, we update the implementation of type_env to map variables to values of a struct variable_data:

{{< codelines "C++" "compiler/12/type_env.hpp" 13 22 >}}

The visibility enum is defined as follows:

{{< codelines "C++" "compiler/12/type_env.hpp" 10 10 >}}

As you can see from the above snippet, we also added a mangled_name field to the new variable_data struct. We will be using this field shortly. We also add a few methods to our type_env, and end up with the following:

{{< codelines "C++" "compiler/12/type_env.hpp" 31 44 >}}

We will come back to find_free and find_free_except, as well as set_mangled_name and get_mangled_name. For now, we just adjust bind to take a visibility parameter that defaults to local, and implement is_global:

{{< codelines "C++" "compiler/12/type_env.cpp" 27 32 >}}

Remember the visibility::global in parser.y? This is where that comes in. Specifically, we recall that definition_defn::insert_types is responsible for placing function types into the environment, making them accessible during typechecking later. At this time, we already need to know whether or not the definitions are global or local (so that we can create the binding). Thus, we add visibility as a parameter to insert_types:

{{< codelines "C++" "compiler/12/definition.hpp" 44 44 >}}

Since we are now moving from manually wrangling definitions towards using definition_group, we make it so that the group itself provides this argument. To do this, we add the visibility field from before to it, and set it in the parser. One more thing: since constructors never capture variables, we can always move them straight to the global scope, and thus, we'll always mark them with visibility::global.

Managing Mangled Names

Just mangling names is not enough. Consider the following program:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 defn packOne x = { let { data Packed a = { Pack a } } in { Pack x } } defn packTwo x = { let { data Packed a = { Pack a } } in { Pack x } }

{{< sidenote "right" "lifting-types-note" "Lifting the data type declarations" >}} We are actually not quite doing something like the following snippet. The reason for this is that we don't mangle the names for types. I pointed out this potential issue in a sidenote in the previous post. Since the size of this post is already balooning, I will not deal with this issue here. Even at the end of this post, our compiler will not be able to distinguish between the two Packed types. We will hopefully get to it later. {{< /sidenote >}} and their constructors into the global scope gives us something like:

data Packed a = { Pack a }
data Packed_1 a = { Pack_1 a }
defn packOne x = { Pack x }
defn packTwo x = { Pack_1 x }

Notice that we had to rename one of the calls to Pack to be a call to be Pack_1. To actually change our AST to reference Pack_1, we'd have to traverse the whole tree, and make sure to keep track of definitions that could shadow Pack further down. This is cumbersome. Instead, we can mark a variable as referring to a mangled version of itself, and access this information when needed. To do this, we add the mangled_name field to the variable_data struct as we've seen above, and implement the set_mangled_name and get_mangled_name methods. The former:

{{< codelines "C++" "compiler/12/type_env.cpp" 34 37 >}}

And the latter:

{{< codelines "C++" "compiler/12/type_env.cpp" 39 45 >}}

We don't allow the set_mangled_name to affect variables that are declared above the receiving type_env, and use the empty string as a 'none' value. Now, when lifting data type constructors, we'll be able to use set_mangled_name to make sure constructor calls are made correctly. We will also be able to use this in other cases, like the translation of local function definitions.

New AST Nodes

Finally, it's time for us to add new AST nodes to our language. Specifically, these nodes are ast_let (for let/in expressions) and ast_lambda for lambda functions. We declare them as follows:

{{< codelines "C++" "compiler/12/ast.hpp" 131 166 >}}

In ast_let, the definitions field corresponds to the original definitions given by the user in the program, and the in field corresponds to the expression which uses these definitions. In the process of lifting, though, we eventually transfer each of the definitions to the global scope, replacing their right hand sides with partial applications. After this transformation, all the data type definitions are effectively gone, and all the function definitions are converted into the simple form x = f a1 ... an. We hold these post-transformation equations in the translated_definitions field, and it's them that we compile in this node's compile method.

In ast_lambda, we allow multiple parameters (like Haskell's \x y -> x + y). We store these parameters in the params field, and we store the lambda's expression in the body field. Just like definition_defn, the ast_lambda node maintains a separate environment in which its children have been bound, and a list of variables that occur freely in its body. The former is used for typechecking, while the latter is used for lifting. Finally, the translated field holds the lambda function's form after its body has been transformed into a global function. Similarly to ast_let, this node will be in the form f a1 ... an.

The observant reader will have noticed that we have a new method: translate. This is a new method for all ast descendants, and will implement the steps of moving definitions to the global scope and transforming the program. Before we get to it, though, let's quickly see the parsing rules for ast_let and ast_lambda:

{{< codelines "text" "compiler/12/parser.y" 107 115 >}}

This is pretty similar to the rest of the grammar, so I will give this no further explanation.

{{< todo >}} Explain typechecking for lambda functions and let/in expressions. {{< /todo >}}

{{< todo >}} Explain free variable detection for lambda functions and let/in expressions. {{< /todo >}}

Translation

While collecting all of the definitions into a global list, we can also do some program transformations. Let's return to our earlier example:

1 2 3 fourthPower x = square * square where square = x * x

We said it should be translated into something like:

1 2 3 fourthPower x = square * square where square = square' x square' x = x * x

In our language, the original program above would be:

1 2 3 4 5 6 7 defn fourthPower x = { let { defn square = { x * x } } in { square * square } }

And the translated version would be:

1 2 3 4 5 6 7 8 defn fourthPower x = { let { defn square = { square' x } } in { square * square } } defn square' x = { x * x }

Setting aside for the moment the naming of square' and square, we observe that we want to perform the following operations:

  1. Move the body of the original definition of square into its own global definition, adding all the captured variables as arguments.
  2. Replace the right hand side of the let/in expression with an application of the global definition to the variables it requires.