A code-along is a story about coding in which you are encouraged to “code up” all the examples gray kangarooin the story. In a code-along you should not cut-and-paste.
In this code along, we will explore val-declarations and let-blocks. These are language constructs that can be used to help us organize and improve the structure of Bricklayer programs.
Bricklayer programs are written in the functional programming language SML. The primary elements used in SML programs are declarations and expressions.
Bricklayer programs begin with a bricklayer-doc comment followed by a sequence of declarations which is followed by a sequence of expressions. SML comments can occur anywhere within a Bricklayer program.
So far we have learned about the the following kinds of declarations and expressions.
- open id
- fun id args = expression
- arithmetic expressions – expressions containing arithmetic operators
- string expressions – expressions containing the string concatenation operator
- function calls
- expression sequences
The val-declaration is a construct provided by the language SML as a mechanism for binding variables (i.e., identifiers) to values. Syntactically, a val-declaration consists of the keyword val followed by a special kind of equation. In general, this equation has a pattern as its left-hand side and an expression as its right-hand side.
Here, we will restrict our attention to a special kind of pattern consisting of a single identifier (other kinds of patterns will be discussed in higher Bricklayer levels). We will use the term “restricted val-declaration” when referring to a val-declaration whose equation has a left-hand side that is an identifier.
The execution of a restricted val-declaration involves the following steps:
- The expression on the right-hand side of the equation is reduced (through computation) to a value.
- The variable (i.e., id) on the left-hand side of the val-declaration’s equation is bound to the value produced in step 1.
All references to a variable bound by a val-declaration denote the value to which it is bound.
From a syntactic perspective, restricted val-declarations are similar to function declarations.
We now have learned about three kinds of declarations. The keywords used by each kind of declaration is listed below.
Let-blocks – a new kind of expression
The let-block is a very useful programming language construct. A let-block is an expression that can contain declarations which are only visible within the let-block. This is a nice way to manage/organize the variables/names that can be referenced at a given point in a program.
Syntactically, a let-block consists of the keyword “let” followed by a list of zero or more declarations, followed by the keyword “in”, followed by a semicolon-separated sequence of expressions, followed by the keyword “end”. We will refer to the semicolon-separated sequence of expressions (between the keyword in and the keyword end) as the body of the let-block.
A let-block is executed by first evaluating the declaration list in lexical order. This will create a set of variable bindings. Next, the expressions in the body of the let-block are evaluated in lexical order (meaning from top to bottom and from left to right).
Computationally speaking, a let-block reduces to the value of the last expression in its body.
The let-block below reduces to the value 33.
The let-block below reduces to the value 242.
The code examples that follow show how let-blocks can be introduced into Bricklayer programs in order to make programs more readable, organized, and to eliminate redundant computations.
We begin with a program that uses a geometric algorithm to construct a grid whose “squares” contain a cross artifact. Notice that the cross artifact is created by a function called, seed. The cross fits into a 3×3 box (its true bounding box).
The function cross1 is simply a renaming of the seed function. This renaming is done to help the reader understand the difference between the seed pattern and its geometric repetition. The coordinates in the geometric repetitions are related. However, the coordinates in the seed pattern have no relation to the coordinates in the geometric repetition.
A 4-cross pattern can be created by placing four individual crosses, and a 16-cross pattern can be created by placing four 4-cross patterns.
The positioning of the crosses in the geometric pattern is such that the individual crosses are separated by a single space. This is accomplished by code that (in effect) assumes the bounding box for the cross is a 4×4 box.
Transforming Expression Sequences into (Empty) Let-blocks
In this version of the program, the expression sequences that formed the bodies of the cross4 and cross16 function declarations are transformed into equivalent let-block expressions.
It is worth noting that the body of the cross4 function contains four occurrences of the expression “2*2”. Technically speaking, this expression could be evaluated statically (e.g., by the programmer). However, in its current form, the expression reveals how coordinates change over the course of the geometric algorithm.
Similarly, the body of the cross16 function contains four occurrences of the expression “2*2*2”.
Factoring out Repeated Computations from Let-block Bodies
In this version of the program, redundant computations are factored out of the bodies of the cross4 and cross16 functions. This is done by introducing a restricted val-declaration in which the variable named offsest is bound to the expression 2*2 in the function cross4 and 2*2*2 in the function cross16.
In cross4, the introduction of the variable offset is accompanied by a replacement of all occurrences of 2*2 in the body of the let-block by the variable offset. This completes the factoring for the function cross4.
A similar refactoring is applied to the body of the function cross16.
Note that in the bodies of cross4 and cross16 the variable introduced is named offset. The reason for using this same name in both functions is because, conceptually speaking, it serves the same purpose in both functions.
Normalizing Repeated Function Calls
This next transformation involves renaming the functions called in the body of the cross4 and cross16 functions. Specifically, the body of the let-block in cross4 consists of four calls to the function cross1. Similarly, the body of the let-block in cross16 consists of four calls to the function cross4.
A val declaration can be used to essentially rename a function. For example, the declaration
val f = cross1;
binds the variable f to the function cross1. Technically speaking, the variable f is bound to the function value to which cross1 is bound. However, at this point it sufficies to simply think of f as another name for the function cross1. The val-declaration above has the same effect as the following fun declaration.
fun f (x,z) = cross1 (x,z);
As was the case with the choice of offset as our variable name, we will use the same name to rename our functions in both function bodies. We do this because the renamed functions are serving conceptually equivalent purposes. Specifically, in the body of cross4, the function cross1 will be renamed to f. In the body of cross16, the function cross4 will be renamed to f as well.
To complete the renaming normalization, all occurrences of cross1 in the the body of the let-block associated with the function cross4 must be renamed to f. Similarly, all occurrences of cross4 in the the body of the let-block associated with the function cross16 must be renamed to f.
Notice the syntactic similarity of the bodies of cross4 and cross16. We are precisely trying to achieve such similarity. Furthermore, this similarity is no accident.
Moving a Top-Level Declaration into a Let-block
This last version of the program is mainly to show that declarations in a let-block can also be function declarations. In this version, the declaration of the function cross4 is moved into the declaration list of the let-block which forms the body of the cross16 function declaration. Note that, as a result, the function cross4 is not visible (i.e., is not defined) outside of the body of the cross16 function declaration.