{10, 20, 30}
-Whereas the sequences `[10, 20, 10]`, `[10, 20]`, and `[20, 10]` are three different sequences, `{10, 20, 10}`, `{10,20}`, and `{20, 10}` would just be different ways of expressing a single set.
+Whereas the sequences `[10, 20, 10]`, `[10, 20]`, and `[20, 10]` are three different sequences, `{10, 20, 10}`, `{10, 20}`, and `{20, 10}` would just be different ways of expressing a single set.
We can let the `&` operator do extra-duty, and express the "consing" relation for sets, too:
or a sequence of sequences of numbers:
- [[10,20], [], [30]]
+ [[10, 20], [], [30]]
An excellent question that came up in class is "How do we tell whether `[]` expresses the empty sequence of numbers or the empty sequence of something else?" We will discuss that question in later weeks. It's central to some of the developments we'll be exploring. For now, just put that question on a mental shelf and assume that somehow this just works out right.
-Now whereas sequences expect homogenously-typed elements, and their length is irrelevant to their own type, tuples are the opposite in both respects. Tuples may have elements of heterogenous type, as our example:
+Now whereas sequences expect homogenously-typed elements, and their length is irrelevant to their own type, mulivalues or tuples are the opposite in both respects. They may have elements of heterogenous type, as our example:
`(0, 'true,` λ`x. x)`
-did. They need not, but they may. Also, the type of a tuple does depend on its length, and moreover on the specific types of each of its elements. A tuple of length 2 (also called a "pair") whose first element is a number and second element is a boolean is a different type of thing that a tuple whose first element is a boolean and whose second element is a number. Most functions expecting the first as an argument will crash if you give them the second instead.
+did. They need not, but they may. Also, the type of a multivalue or tuple does depend on its length, and moreover on the specific types of each of its elements. A tuple of length 2 (also called a "pair") whose first element is a number and second element is a boolean is a different type of thing that a tuple whose first element is a boolean and whose second element is a number. Most functions expecting the first as an argument will crash if you give them the second instead.
-Earlier I said that we can call these things "tuples" or "multivalues". Here I'll make a technical comment, that in fact I'll understand these slightly differently. Really I'll understand the bare expression `(10, x)` to express a multivalue, and to express a tuple proper, you'll have to write `Pair (10, x)` or something like that. The difference between these is that only the tuple is itself a single value that can be bound to a single variable. The multivalue isn't a single value at all, but rather a plurality of values. This is a bit subtle, and other languages we're looking at this term don't always make this distinction. But the result is that they have to say complicated things elsewhere. If we permit ourselves this fine distinction here, many other things downstream will go more smoothly than they do in the languages that don't make it. Ours is just a made-up language, but I've thought this through carefully, so humor me. We haven't yet introduced the apparatus to make sense of expressions like `Pair (10, x)`, so for the time being I'll just restrict myself to multivalues, not to tuples proper. The result will be that while we can say:
+Earlier I said that we can call these things "multivalues or tuples". Here I'll make a technical comment, that in fact I'll understand these slightly differently. Really I'll understand the bare expression `(10, x)` to express a multivalue, and to express a tuple proper, you'll have to write `Pair (10, x)` or something like that. The difference between these is that only the tuple is itself a single value that can be bound to a single variable. The multivalue isn't a single value at all, but rather a plurality of values. This is a bit subtle, and other languages we're looking at this term don't always make this distinction. But the result is that they have to say complicated things elsewhere. If we permit ourselves this fine distinction here, many other things downstream will go more smoothly than they do in the languages that don't make it. Ours is just a made-up language, but I've thought this through carefully, so humor me. We haven't yet introduced the apparatus to make sense of expressions like `Pair (10, x)`, so for the time being I'll just restrict myself to multivalues, not to tuples proper. The result will be that while we can say:
let x be [10, 20] in ...
but in other examples it will be substantially more convenient to be able to bind `x` and `y` simultaneously. Here's an example:
`let`
-` f be` λ `x. (x, 2*x)`
-` (x, y) be f 10`
+ `f be` λ `x. (x, 2*x)`
+ `(x, y) be f 10`
`in [x, y]`
which will evaluate to `[10, 20]`. Note that we have the function `f` returning two values, rather than just one, just by having its body evaluate to a multivalue rather than to a single value.
+It's a little bit awkward to say `let (x, y) be ...`, so I propose we instead always say `let (x, y) match ...`. (This will be even more natural as we continue generalizing what we've done here, as we will in the next section.) For consistency, we'll say `match` instead of `be` in all cases, so that we write even this:
+ let
+ x match 10
+ in ...
+rather than:
+
+ let
+ x be 10
+ in ...
-*More coming*
### Patterns ###
-*More coming*
+What we just introduced is what's known in programming circles as a "pattern". Patterns can look superficially like expressions, but the context in which they appear determines that they are interpreted as patterns not as expressions. The left-hand sides of the binding lists of a `let`-expression are always patterns. Simple variables are patterns. Interestingly, literal values are also patterns. So you can say things like this:
+
+ let
+ 0 match 0;
+ [] match [];
+ 'true match 'true
+ in ...
+
+(`[]` is also a literal value, like `0` and `'true`.) This isn't very useful in this example, but it will enable us to do interesting things later. So variables are patterns and literal values are patterns. Also, a multivalue of any pattern is a pattern. That's why we can have `(x, y)` on the left-hand side of a `let`-binding: it's a pattern, just like `x` is. Notice that `(x, 10)` is also a pattern. So we can say this:
+
+ let
+ (x, 10) match (2, 10)
+ in x
+
+which will evaluate to `2`. What if you did, instead:
+
+ let
+ (x, 10) match (2, 100)
+ in x
+
+or, more perversely:
+
+ let
+ (x, 10) match 2
+ in x
+
+Those will be pattern-matching failures. The pattern has to "fit" the value its being matched against, and that requires having the same structure, and also having the same literal values in whatever positions the pattern specifies literal values. A pattern-matching failure in a `let`-expression makes the whole expression crash. Shortly though we'll consider `case`-expressions, which can recover from pattern-match failures in a useful way.
+
+We can also allow ourselves some other kinds of complex patterns. For example, if `p` and `ps` are two patterns, then `p & ps` will also be a pattern, that can match non-empty sequences and sets. When this pattern is matched against a non-empty sequence, we take the first value in the sequence and match it against the pattern `p`; we take the rest of the sequence and match it against the pattern `ps`. (If either of those results in a pattern-matching failure, then `p & ps` fails to match too.) For example:
+
+ let
+ x & xs match [10, 20, 30]
+ in (x, xs)
+
+will evaluate to the multivalue `(10, [20, 30])`.
+
+When the pattern `p & ps` is matched against a non-empty set, we just arbitrarily choose one value in the set match it against the pattern `p`; and match the rest of the set, with that value removed, against the pattern `ps`. You cannot control what order the values are chosen in. Thus:
+
+ let
+ x & xs match {10, 20, 30}
+ in (x, xs)
+
+might evaluate to `(20, {10, 30})` or to `(30, {10, 20})` or to `(10, {30, 20})`, or to one of these on Mondays and another on Tuesdays, and never to the third. You cannot control it or predict it. It's good style to only pattern match against sets when the final result will be the same no matter in what order the values are selected from the set.
+
+A question that came up in class was whether 'x + y` could also be a pattern. In this language (and most languages), no. The difference between `x & xs` and `x + y` is that `&` is a *constructor* whereas `+` is a *function*. We will be talking about this more in later weeks. For now, just take it that `&` is special. Not every way of forming a complex expression corresponds to a way of forming a complex pattern.
+
+Since as we said, `x & xs` is a pattern, we can let `x1 & x2 & xs` be a pattern as well, the same as `x1 & (x2 & xs)`. And since when we're dealing with expressions, we said that:
+
+ [x1, x2]
+
+is the same as:
+
+ x1 & x2 & []
+
+we might as well allow this for patterns, too, so that:
+
+ [x1, x2]
+
+is a pattern, meaning the same as `x1 & x2 & []`. Note that while `x & xs` matches *any* non-empty sequence, of length one or more, `[x1, x2]` only matches sequences of length exactly two.
+
+For the time being, these are the only patterns we'll allow. But since the definition of patterns is recursive, this permits very complex patterns. What would this evaluate to:
+
+ let
+ [(x, y), (z:zs, w)] match [([], 'true), ([10, 20, 30], 'false)]
+ in (z, y)
+
+Also, we will permit complex patterns in λ-expressions, too. So you can write:
+
+λ`(x, y).` φ
+
+as well as:
+
+λ`x.` φ
+
+You can even write:
+
+λ `[x, 10].` φ
+
+just be sure to always supply that function with arguments that are two-element sequences whose second element is `10`. If you don't, you will have a pattern-matching failure and the interpretation of your expression will "crash".
+
+Thus, you can now do things like this:
+
+`let`
+ `f match` λ`(x, y). (x, x + y, x + 2*y, x + 3*y);`
+ `(a, b, c, d) match f (10, 1)`
+`in (b, d)`
+
+which will evaluate `f (10, 1)` to `(10, 11, 12, 13)`, which it will match against the complex pattern `(a, b, c, d)`, binding all four of the contained variables, and then evaluate `(b, d)` under those bindings, giving us the result `(11, 13)`.
+
+Notice that in the preceding expression, the variables `a` and `c` were never used. We're allowed to do that, but there's also a special syntax to indicate that we want to throw away a value like this. We use the special pattern `_`:
+
+`let`
+ `f match` λ`(x, y). (x, x + y, x + 2*y, x + 3*y);`
+ `(_, b, _, d) match f (10, 1)`
+`in (b, d)`
+
+The role of `_` here is just to occupy a slot in the complex pattern `(_, b, _, d)`, to make it a multivalue of four values, rather than one of only two.
+
+One last wrinkle. What if you tried to make a pattern like this: `[x, x]`, where some variable occurs multiple times. This is known as a "non-linear pattern". Some languages permit these (and require that the values being bound against `x` in the two positions be equal). Many languages don't permit that. Let's agree not to do this.
+
### Recursive let ###