update week1 notes
authorJim <jim.pryor@nyu.edu>
Sun, 1 Feb 2015 05:29:34 +0000 (00:29 -0500)
committerJim <jim.pryor@nyu.edu>
Sun, 1 Feb 2015 05:29:34 +0000 (00:29 -0500)
week1.mdwn

index 9af4054..56b05d1 100644 (file)
@@ -319,10 +319,106 @@ rather than:
 
 ### Patterns ###
 
 
 ### Patterns ###
 
+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 &lambda;-expressions, too. So you can write:
+
+&lambda;`(x, y).` &phi;
+
+as well as:
+
+&lambda;`x.` &phi;
+
+You can even write:
+
+&lambda; `[x, 10].` &phi;
+
+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`  
+&nbsp;&nbsp;`f match` &lambda;`(x, y). (x, x + y, x + 2*y, x + 3*y);`  
+&nbsp;&nbsp;`(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`  
+&nbsp;&nbsp;`f match` &lambda;`(x, y). (x, x + y, x + 2*y, x + 3*y);`  
+&nbsp;&nbsp;`(_, 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.
 
 
-*More coming*
 
 ### Recursive let ###
 
 
 ### Recursive let ###