update week1 notes
[lambda.git] / week1.mdwn
index be1b714..7a6050e 100644 (file)
-Here's what we did in seminar on Monday 9/13, (Sometimes these notes will expand on things mentioned only briefly in class, or discuss useful tangents that didn't even make it into class.)
+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.
 
-Applications
-============
+We begin with a decidable fragment of arithmetic. Our language has some primitive literal values:
 
-We mentioned a number of linguistic and philosophical applications of the tools that we'd be helping you learn in the seminar. (We really do mean "helping you learn," not "teaching you." You'll need to aggressively browse and experiment with the material yourself, or nothing we do in a few two-hour sessions will succeed in inducing mastery of it.)
+    0, 1, 2, 3, ...
 
-From linguistics
-----------------
+In fact we could get by with just the primitive literal `0` and the `succ` function, but we will make things a bit more convenient by allowing literal expressions of any natural number. We won't worry about numbers being too big for our finite computers to handle.
 
-*      generalized quantifiers are a special case of operating on continuations
+We also have some predefined functions:
 
-*      (Chris: fill in other applications...)
+    succ, +, *, pred, -
 
-*      expressives -- at the end of the seminar we gave a demonstration of modeling [[damn]] using continuations...see the linked summary for more explanation and elaboration
+Again, we might be able to get by with just `succ`, and define the others in terms of it, but we'll be a bit more relaxed. Since we want to stick with natural numbers, not the whole range of integers, we'll make `pred 0` just be `0`, and `2-4` also be `0`.
 
-From philosophy
----------------
+Here's another set of functions:
 
-*      the natural semantics for positive free logic is thought by some to have objectionable ontological commitments; Jim says that thought turns on not understanding the notion of a "union type", and conflating the folk notion of "naming" with the technical notion of semantic value. We'll discuss this in due course.
+    ==, <, >, <=, >=, !=
 
-*      those issues may bear on Russell's Gray's Elegy argument in "On Denoting"
+`==` 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.
 
-*      and on discussion of the difference between the meaning of "is beautiful" and "beauty," and the difference between the meaning of "that snow is white" and "the proposition that snow is white."
+As mentioned in class, we represent the truth-values like this:
 
-*      the apparatus of monads, and techniques for statically representing the semantics of an imperatival language quite generally, are explicitly or implicitly invoked in dynamic semantics
+    'true, 'false
 
-*      the semantics for mutation will enable us to make sense of a difference between numerical and qualitative identity---for purely mathematical objects!
+These are instances of a broader class of literal values that I called *symbolic atoms*. We'll return to them shortly. The reason we write them with an initial `'` will also be explained shortly. For now, it's enough to note that the expression:
 
-*      issues in that same neighborhood will help us better understand proposals like Kit Fine's that semantics is essentially coordinated, and that `R a a` and `R a b` can differ in interpretation even when `a` and `b` don't
+    1 + 2 == 3
 
+evaluates to `'true`, and the expression:
 
+    1 + 0 == 3
 
+evaluates to `'false`. Something else that evaluates to `'false` is the simple expression:
 
+    'false
 
-1.     Declarative vs imperatival models of computation.
-2.     Variety of ways in which "order can matter."
-3.     Variety of meanings for "dynamic."
-4.     Schoenfinkel, Curry, Church: a brief history
-5.     Functions as "first-class values"
-6.     "Curried" functions
+That is, literal values are a limiting case of expression, that evaluate to just themselves. More complex expressions like `1 + 0` don't evaluate to themselves, but rather down to literal values.
 
-1.     Beta reduction
-1.     Encoding pairs (and triples and ...)
-1.     Encoding booleans
+The functions `succ` and `pred` come before their arguments, like this:
 
+    succ 1
 
+On the other hand, the functions `+`, `*`, `-`, `==`, and so on come in between their arguments, like this:
 
+    x < y
 
+Functions of this latter sort are said to have an "infix" syntax. This is just a convenience for how we write them. Our language will have to keep rigorous track of which functions have infix syntax and which don't, but we'll just rely on context and our brains to make sense of this for now. Functions with the ordinary, non-infix syntax can take two arguments, as well. If we had defined the less-than relation (boolean function) in that style, we'd write it like this instead:
 
+    lessthan? (x, y)
 
-       Order matters
+or perhaps like this:
 
-Declarative versus imperative:
+    lessthan? x y
 
-In a pure declarative language, the order in which expressions are
-evaluated (reduced, simplified) does not affect the outcome.
+We'll get more acquainted with the difference between these next week. For now, I'll just stick to the first form.
 
-(3 + 4) * (5 + 11) = 7 * (5 + 11) = 7 * 16 = 112
-(3 + 4) * (5 + 11) = (3 + 4) * 16 = 7 * 16 = 112
+Another set of operations we have are:
 
-In an imperative language, order makes a difference.
+    and, or, not
 
-x := 2
-x := x + 1
-x == 3
-[true]
+The first two of these are infix functions that expect two boolean arguments, and gives a boolean result. The third is a function that expects only one boolean argument. Our earlier function `!=` means "doesn't equal", and:
 
-x := x + 1
-x := 2
-x == 3
-[false]
+    x != y
 
+will in general be just another way to write:
 
-Declaratives: assertions of statements.
-No matter what order you assert true facts, they remain true:
+    not (x == y)
 
-The value is the product of x and y.
-x is the sum of 3 and 4.
-y is the sum of 5 and 11.
-The value is 112.
+You see that you can use parentheses in the standard way.
 
-Imperatives: performative utterances expressing a deontic or bouletic
-modality  ("Be polite", "shut the door")
-Resource-sensitive, order sensitive: 
+I've started throwing in some variables. We'll say variables are any expression that starts with a lower-case letter, then is followed by a sequence of 0 or more upper- or lower-case letters, or underscores (`_`). Then at the end you can optionally have a `?` or `!` or a sequence of `'`s, understood as "prime" symbols. Hence, all of these are legal variables:
 
-Make x == 2.
-Add one to x.
-See if x == 3.
+    x
+    x1
+    x_not_y
+    xUBERANT
+    x'
+    x''
+    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?` 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. 
 
-Untype (monotyped) lambda calculus
+We also conventionally reserve variables ending in `!` for a different special class of functions, that we will explain later in the course.
 
-Syntax:
+In fact you can think of `succ` and `pred` and `not` and all the rest as also being variables; it's just that these variables have been pre-defined in our language to be bound to special functions we designated in advance. You can even think of `==` and `<` as being variables, too, bound to other functions. But I haven't given you rules yet which would make them legal variables, because they don't start with a lower-case letter. We can make the rules more liberal later.
 
-Variables: x, x', x'', x''', ...
-(Cheat:    x, y, z, x1, x2, ...)
+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.)
 
-Each variable is a term.
-For all terms M and N and variable a, the following are also terms:
+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.
 
-(M N)   The application of M to N
-(\a M)  The abstraction of a over M
+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.
 
-Examples of terms:
+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.
 
-x
-(y x)
-(x x)
-(\x y)
-(\x x)
-(\x (\y x))
-(x (\x x))
-((\x (x x))(\x (x x)))
+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.
 
-Reduction/conversion/equality:
+(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.)
 
-Lambda terms express recipes for combining terms into new terms.
-The key operation in the lambda calculus is beta-conversion.
+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.)
 
-((\a M) N) ~~>_beta M{a := N}
+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.
 
-The term on the left of the arrow is an application whose first
-element is a lambda abstraction.  (Such an application is called a
-"redex".)  The beta reduction rule says that a redex is
-beta-equivalent to a term that is constructed by replacing every
-(free) occurrence of a in M by a copy of N.  For example, 
+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.)
 
-((\x x) z) ~~>_beta z
-((\x (x x)) z) ~~>_beta (z z)
-((\x x) (\y y)) ~~>_beta (\y y)
+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:
 
-Beta reduction is only allowed to replace *free* occurrences of a variable.
-An occurrence of a variable a is BOUND in T if T has the form (\a N).
-If T has the form (M N), and the occurrence of a is in M, then a is
-bound in T just in case a is bound in M; if the occurrence of a is in
-N, than a is bound in T just in case a is bound in N.  An occurrence
-of a variable a is FREE in a term T iff it is not bound in T.
-For instance:
+    let x be 5 in x + 1
 
-T = (x (\x (\y (x (y z)))))
+which evaluates to `6`. Okay, fair enough, so I am moving beyond the &forall; `x==5.` &phi; idea when I do this. But the rule 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.
 
-The first occurrence of x in T is free.  The second occurrence of x
-immediately follows a lambda, and is bound.  The third occurrence of x
-occurs within a form that begins with "\x", so it is bound as well.
-Both occurrences of y are bound, and the only occurrence of z is free.
+We can have multiple `let`-expressions embedded, as in:
 
-Lambda terms represent functions.
-All (recursively computable) functions can be represented by lambda
-terms (the untyped lambda calculus is Turning complete).
-For some lambda terms, it is easy to see what function they represent:
+    let y be (let x be 5 in x + 1) in 2 * y
 
-(\x x) the identity function: given any argument M, this function
-simply returns M: ((\x x) M) ~~>_beta M.
+    let x be 5 in let y be x + 1 in 2 * y
 
-(\x (x x)) duplicates its argument:
-((\x (x x)) M) ~~> (M M)
+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:
 
-(\x (\y x)) throws away its second argument:
-(((\x (\y x)) M) N) ~~> M
+    let
+      x be 5;
+      y be x + 1
+    in 2 * y
 
-and so on.
+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:
 
-It is easy to see that distinct lambda terms can represent the same
-function.  For instance, (\x x) and (\y y) both express the same
-function, namely, the identity function.
+    let x be 0 in (let x be 5; y be x + 1 in 2 * y)
 
------------------------------------------
-Dot notation: dot means "put a left paren here, and put the right
-paren as far the right as possible without creating unbalanced
-parentheses".  So (\x(\y(xy))) = \x\y.xy, and \x\y.(z y) x =
-(\x(\y((z y) z))), but (\x\y.(z y)) x = ((\x(\y(z y))) x).
+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.
 
------------------------------------------
-
-Church figured out how to encode integers and arithmetic operations
-using lambda terms.  Here are the basics:
-
-0 = \f\x.fx
-1 = \f\x.f(fx)
-2 = \f\x.f(f(fx))
-3 = \f\x.f(f(f(fx)))
-...
-
-Adding two integers involves applying a special function + such that 
-(+ 1) 2 = 3.  Here is a term that works for +:
-
-+ = \m\n\f\x.m(f((n f) x))
-
-So (+ 0) 0 =
-(((\m\n\f\x.m(f((n f) x))) ;+
-  \f\x.fx)                 ;0
-  \f\x.fx)                 ;0
-
-~~>_beta targeting m for beta conversion
-
-((\n\f\x.[\f\x.fx](f((n f) x)))
- \f\x.fx)
-
-\f\x.[\f\x.fx](f(([\f\x.fx] f) x))
-
-\f\x.[\f\x.fx](f(fx))
-
-\f\x.\x.[f(fx)]x
-
-\f\x.f(fx)
-
-
-
-----------------------------------------------------
-
-A concrete example: "damn" side effects
-
-1. Sentences have truth conditions.
-2. If "John read the book" is true, then
-   John read something,
-   Someone read the book,
-   John did something to the book, 
-   etc.
-3. If "John read the damn book",
-   all the same entailments follow.
-   To a first approximation, "damn" does not affect at-issue truth
-   conditions.  
-4. "Damn" does contribute information about the attitude of the speaker
-   towards some aspect of the situation described by the sentence.
-
-
-
------------------------------------------
-Old notes, no longer operative:
-
-1. Theoretical computer science is beautiful.
-
-   Google search for "anagram": Did you mean "nag a ram"?
-   Google search for "recursion": Did you mean "recursion"?
-
-   Y = \f.(\x.f (x x)) (\x.f (x x))
-
-
-1. Understanding the meaning(use) of programming languages
-   helps understanding the meaning(use) of natural langauges
-
-      1. Richard Montague. 1970. Universal Grammar. _Theoria_ 34:375--98.
-         "There is in my opinion no important theoretical difference
-         between natural languages and the artificial languages of
-         logicians; indeed, I consider it possible to comprehend the
-         syntax and semantics of both kinds of languages within a
-         single natural and mathematically precise theory."
-
-      2. Similarities: 
-
-         Function/argument structure: 
-             f(x)    
-             kill(it)
-         pronominal binding: 
-             x := x + 1
-             John is his own worst enemy
-         Quantification:
-             foreach x in [1..10] print x
-             Print every number from 1 to 10
-
-      3. Possible differences:
-
-         Parentheses:
-             3 * (4 + 7)
-             ?It was four plus seven that John computed 3 multiplied by
-             (compare: John computed 3 multiplied by four plus seven)
-         Ambiguity:
-             3 * 4 + 7
-             Time flies like and arrow, fruit flies like a banana.
-         Vagueness:
-             3 * 4
-             A cloud near the mountain
-         Unbounded numbers of distinct pronouns:
-             f(x1) + f(x2) + f(x3) + ...
-             He saw her put it in ...
-             [In ASL, dividing up the signing space...]
-         
-         
-2. Standard methods in linguistics are limited.
-
-   1. First-order predicate calculus
-
-        Invented for reasoning about mathematics (Frege's quantification)
-
-        Alethic, order insensitive: phi & psi == psi & phi
-        But: John left and Mary left too /= Mary left too and John left
-
-   2. Simply-typed lambda calculus 
-
-        Can't express the Y combinator
-
-
-3. Meaning is computation.
-
-   1. Semantics is programming 
-
-   2. Good programming is good semantics
-
-      1. Example 
-
-         1. Programming technique
-
-            Exceptions
-              throw (raise)
-              catch (handle)
-
-         2. Application to linguistics
-              presupposition
-              expressives
-
-              Develop application:
-                fn application
-                divide by zero
-                test and repair
-                raise and handle
-
-                fn application
-                presupposition failure
-                build into meaning of innocent predicates?
-                expressives
-                throw
-                handle
-                resume computation
+*More to come.*