update week1 notes
[lambda.git] / week1.mdwn
index f6595ba..d461a89 100644 (file)
@@ -1,5 +1,7 @@
 These notes will recapitulate, make more precise, and to some degree expand what we did in the last hour of our first meeting, leading up to the definitions of the `factorial` and `length` functions.
 
+### Getting started ###
+
 We begin with a decidable fragment of arithmetic. Our language has some primitive literal values:
 
     0, 1, 2, 3, ...
@@ -16,7 +18,7 @@ Here's another set of functions:
 
     ==, <, >, <=, >=, !=
 
-`==` is just what we non-programmers normally express by `=`. It's a relation that holds or not between two values. Here we'll treat it as a function that takes two values as arguments and returns a *boolean* value, that is a truth-value, as a result. The reason for using the doubled`=` symbol is that the single `=` symbol tends to get used in lots of different roles in programming, so we reserve `==` to express this meaning. I will deliberately try to minimize the uses of single `=` in this made-up language (but not eliminate it entirely), to reduce ambiguity and confusion. The `==` relation, or as we're treating it here, the `==` function that returns a boolean value, can at least take two numbers as arguments. Probably it makes sense for it to take other kinds of values as arguments, too. For example, it should operate on two truth-values as well. Maybe we'd want it to operate on a number and a truth-value, too? and always return false in that case? What about operating on two functions? Here we encounter the difficulty that the computer can't in general *decide* when two functions are equivalent. Let's not try to sort this all out just yet. We'll suppose that `==` can at least take two numbers as arguments, or two truth-values.
+`==` is just what we non-programmers normally express by `=`. It's a relation that holds or not between two values. Here we'll treat it as a function that takes two values as arguments and returns a *boolean* value, that is a truth-value, as a result. The reason for using the doubled `=` symbol is that the single `=` symbol tends to get used in lots of different roles in programming, so we reserve `==` to express this meaning. I will deliberately try to minimize the uses of single `=` in this made-up language (but not eliminate it entirely), to reduce ambiguity and confusion. The `==` relation---or as we're treating it here, the `==` *function* that returns a boolean value---can at least take two numbers as arguments. Probably it makes sense for it to take other kinds of values as arguments, too. For example, it should operate on two truth-values as well. Maybe we'd want it to operate on a number and a truth-value, too? and always return false in that case? What about operating on two functions? Here we encounter the difficulty that the computer can't in general *decide* when two functions are equivalent. Let's not try to sort this all out just yet. We'll suppose that `==` can at least take two numbers as arguments, or two truth-values.
 
 As mentioned in class, we represent the truth-values like this:
 
@@ -52,7 +54,7 @@ or perhaps like this:
 
     lessthan? x y
 
-We'll get more acquainted with the difference and relation between these next week. For now, I'll just stick to the first form.
+We'll get more acquainted with the difference between these next week. For now, I'll just stick to the first form.
 
 Another set of operations we have are:
 
@@ -79,7 +81,7 @@ I've started throwing in some variables. We'll say variables are any expression
     x?
     xs
 
-We'll follow a *convention* of using variables with short names and a final `s` to represent collections like sequences (to be discussed below). But this is just a convention to help us remember what we're up to, not a strict rule of the language. We'll also follow a convention of only using variables ending in `?` to represent functions that return a boolean value. Thus, for example, `zero?` is a function that expects a single number argument and returns a boolean corresponding to whether that number is `0`. `odd?` is a function tha expects a single number argument and returns a boolean corresponding to whether than number is odd. Above, I suggested we might use `lessthan?` to represent a function that expects *two* number arguments, and again returns a boolean result. 
+We'll follow a *convention* of using variables with short names and a final `s` to represent collections like sequences (to be discussed below). But this is just a convention to help us remember what we're up to, not a strict rule of the language. We'll also follow a convention of only using variables ending in `?` to represent functions that return a boolean value. Thus, for example, `zero?` will be a function that expects a single number argument and returns a boolean corresponding to whether that number is `0`. `odd?` will be a function that expects a single number argument and returns a boolean corresponding to whether than number is odd. Above, I suggested we might use `lessthan?` to represent a function that expects *two* number arguments, and again returns a boolean result. 
 
 We also conventionally reserve variables ending in `!` for a different special class of functions, that we will explain later in the course.
 
@@ -87,16 +89,89 @@ In fact you can think of `succ` and `pred` and `not` and all the rest as also be
 
 Only a few things in our language aren't variables. These include the **keywords** like `let` and `case` and so on that we'll discuss below. You can't use `let` as a variable, else the syntax of our language would become too hard to mechanically parse. (And probably too hard for our meager brains to parse, too.)
 
-The rules for symbolic atoms are that a single quote `'` followed by any single word that could be a legal variable is a symbolic atom. Thus `'false` is a symbolic atom, but so too are `'x` and `'succ`. For the time being, I'll restrict myself to only talking about the symbolic atoms `'true` and `'false`. These are a special subgroup of symbolic atoms that we call the *booleans* or *truth-values*. Nothing deep hangs on these being a subclass of a larger category in this way; it just seems elegant. Other languages sometimes make booleans their own special type, not a subclass of any other limited type. Others make them a subclass of the numbers (yuck). We will think of them this way.
+The rule for symbolic atoms is that a single quote `'` followed by any single word that could be a legal variable is a symbolic atom. Thus `'false` is a symbolic atom, but so too are `'x` and `'succ`. For the time being, I'll restrict myself to only talking about the symbolic atoms `'true` and `'false`. These are a special subgroup of symbolic atoms that we call the *booleans* or *truth-values*. Nothing deep hangs on these being a subclass of a larger category in this way; it just seems elegant. Other languages sometimes make booleans their own special type, not a subclass of any other limited type. Others make them a subclass of the numbers (yuck). We will think of them this way.
 
 Note that in symbolic atoms there is no closing `'`, just a `'` at the beginning. That's enough to make the whole word, up to the next space (or whatever) count as naming a symbolic atom.
 
 We call these things symbolic *atoms* because they aren't collections. Thus numbers are also atoms, just not symbolic ones. And functions are also atoms, but again, not symbolic ones.
 
-Functions are another class of values we'll have in our language. They aren't "literal" values, though. Numbers and symbolic atoms are simple expressions in the language that evaluate to themselves. Functions aren't expressions in the language; they have to be generated from the evaluation of more complex expressions.
+Functions are another class of values we'll have in our language. They aren't "literal" values, though. Numbers and symbolic atoms are simple expressions in the language that evaluate to themselves. That's what we mean by calling them "literals." Functions aren't expressions in the language at all; they have to be generated from the evaluation of more complex expressions.
 
 (By the way, I really am serious about thinking of *the numbers themselves* as being expressions in this language; rather than some "numerals" that aren't themselves numbers. We can talk about this down the road. For now, don't worry about it too much.)
 
-I said we wanted to be starting with a fragment of arithmetic, so we'll keep the function values off-stage for the moment, and also all the symbolic atoms except for `'true` and `'false`. So we've got numbers, truth-values, and some functions and relations (that is, boolean functions) defined on them. We also help ourselves to a notion of bounded quantification, as in &forall;`x < M.` &phi;, where `M` and &phi; are (simple or complex) expressions that evaluate to a number and a boolean, respectively.
+I said we wanted to be starting with a fragment of arithmetic, so we'll keep the function values off-stage for the moment, and also all the symbolic atoms except for `'true` and `'false`. So we've got numbers, truth-values, and some functions and relations (that is, boolean functions) defined on them. We also help ourselves to a notion of bounded quantification, as in &forall;`x < M.` &phi;, where `M` and &phi; are (simple or complex) expressions that evaluate to a number and a boolean, respectively. We limit ourselves to *bounded* quantification so that the fragment we're dealing with can be "effectively" or mechanically decided. (As we extend the language, we will lose that property, but it will be a topic for later discussion exactly when that happens.)
+
+As I mentioned in class, I will sometimes write &forall; x : &psi; . &phi; in my informal metalanguage, where the &psi; clause represents the quantifier's *restrictor*. Other people write this like `[`&forall; x : &psi; `]` &phi;, or in various other ways. My notation is meant to parallel the notation some linguists (for example, Heim &amp; Kratzer) use in writing &lambda; x : &psi; . &phi;, where &psi;  clause restricts the range of arguments over which the function designated by the &lambda;-expression is defined. Later we will see the colon used in a somewhat similar (but also somewhat different) way in our programming languages. But that's just foreshadowing.
+
+
+### Let and lambda ###
+
+So we have bounded quantification as in &forall; `x < 10.` &phi;. Obviously we could also make sense of &forall; `x == 5.` &phi; in just the same way. This would evaluate &phi; but with the variable `x` now bound to the value `5`, ignoring whatever it may be bound to in broader contexts. I will express this idea in a more perspicuous vocabulary, like this: `let x be 5 in` &phi;. (I say `be` rather than `=` because, as I mentioned before, it's too easy for the `=` sign to get used for too many subtly different jobs.)
+
+As one of you was quick to notice in class, though, when I shift to the `let`-vocabulary, I no longer restricted myself to just the case where &phi; evaluates to a boolean. I also permitted myself expressions like this:
+
+    let x be 5 in x + 1
+
+which evaluates to `6`. Okay, fair enough, so I am moving beyond the &forall; `x==5.` &phi; idea when I do this. But the rules for how to interpret this are just a straightforward generalization of our existing understanding for how to interpret bound variables. So there's nothing fundamentally novel here.
+
+We can have multiple `let`-expressions embedded, as in:
+
+    let y be (let x be 5 in x + 1) in 2 * y
+
+    let x be 5 in let y be x + 1 in 2 * y
+
+both of which evaluate to `12`. When we have a stack of `let`-expressions as in the second example, I will write it like this:
+
+    let
+      x be 5;
+      y be x + 1
+    in 2 * y
+
+It's okay to also write it all inline, like so: `let x be 5; y be x + 1 in 2 * y`. The `;` represents that we have a couple of `let`-bindings coming in sequence. The earlier bindings in the sequence are considered to be in effect for the later right-hand expressions in the sequence. Thus in:
+
+    let x be 0 in (let x be 5; y be x + 1 in 2 * y)
+
+The `x + 1` that is evaluated to give the value that `y` gets bound to uses the (more local) binding of `x` to `5`, not the (previous, less local) binding of `x` to `0`. By the way, the parentheses in that displayed expression were just to focus your attention. It would have parsed and meant the same without them.
+
+Now we can allow ourselves to introduce &lambda;-expressions in the following way. If a &lambda;-expression is applied to an argument, as in: `(`&lambda; `x.` &phi;`) M`, for any (simple or complex) expressions &phi; and `M`, this means the same as: `let x be M in` &phi;. That is, the argument to the &lambda;-expression provides (when evaluated) a value for the variable `x` to be bound to, and then the result of the whole thing is whatever &phi; evaluates to, under that binding to `x`.
+
+If we restricted ourselves to only that usage of &lambda;-expressions, that is when they were applied to all the arguments they're expecting, then we wouldn't have moved very far from the decidable fragment of arithmetic we began with.
+
+However, it's tempting to help ourselves to the notion (at least partly) *unapplied* &lambda;-expressions, too. If I can make sense of what:
+
+`(`&lambda; `x. x + 1) 5`
+
+means, then I can make sense of what:
+
+`(`&lambda; `x. x + 1)`
+
+means, too. It's just *the function* that waits for an argument and then returns the result of `x + 1` with `x` bound to that argument.
+
+This does take us beyond our (first-order) fragment of arithmetic, at least if we allow the bodies and arguments of &lambda;-expressions to be any expressible value, including other &lambda;-expressions. But we're having too much fun, so why should we hold back?
+
+So now we have a new kind of value our language can work with, alongside numbers and booleans. We now have function values, too. We can bind these function values to variables just like other values:
+
+`let id be` &lambda; `x. x; y be id 5 in y`
+
+will evaluate to `5`. In reaching that result, the variable `id` was temporarily bound to the identity function, that expects an argument, binds it to the variable `x`, and then returns the result of evaluating `x` under that binding.
+
+This is what is going on, behind the scenes, with all the expressions like `succ` and `+` that I said could really be understood as variables. They have just been pre-bound to certain agreed-upon functions rather than others.
+
+
+### Containers ###
+
+*More coming*
+
+### Patterns ###
+
+*More coming*
+
+### Recursive let ###
+
+*More coming*
+
+### Comparing recursive-style and iterative-style definitions ###
+
+*More coming*
+
 
-*More to come*