writing up week2
authorJim Pryor <profjim@jimpryor.net>
Thu, 16 Sep 2010 15:05:52 +0000 (11:05 -0400)
committerJim Pryor <profjim@jimpryor.net>
Thu, 16 Sep 2010 15:05:52 +0000 (11:05 -0400)
Signed-off-by: Jim Pryor <profjim@jimpryor.net>
week2.mdwn

index c7e8230..9bb13d4 100644 (file)
@@ -1,3 +1,202 @@
+Syntactic equality, reduction, convertibility
+=============================================
+
+Define T to be `(\x. x y) z`. Then T and `(\x. x y) z` are syntactically equal, and we're counting them as syntactically equal to `(\z. z y) z` as well. We write:
+
+<pre>
+T &equiv; `(\x. x y) z` &equiv; `(\z. z y) z`
+</pre>
+
+This:
+
+<pre>
+T ~~> `z y`
+</pre>
+
+means that T beta-reduces to `z y`. This:
+
+<pre>
+M <~~> T
+</pre>
+
+means that M and T are beta-convertible, that is, that there's something they both reduce to in zero or more steps.
+
+Combinators and Combinatorial Logic
+===================================
+
+Lambda expressions that have no free variables are known as **combinators**. Here are some common ones:
+
+<pre>
+**I** is defined to be `\x x`<p>
+**K** is defined to be `\x y. x`, That is, it throws away its second argument. So `K x` is a constant function from any (further) argument to `x`. ("K" for "constant".) Compare K to our definition of **true**.<p>
+**get-first** was our function for extracting the first element of an ordered pair: `\fst snd. fst`. Compare this to K and true as well.<p>
+**get-second** was our function for extracting the second element of an ordered pair: `\fst snd. snd`. Compare this to our definition of false.<p>
+**&omega;** is defined to be: `\x. x x`<p>
+</pre>
+
+It's possible to build a logical system equally powerful as the lambda calculus (and straightforwardly intertranslatable with it) using just combinators, considered as atomic operations. Such a language doesn't have any variables in it: not just no free variables, but no variables at all.
+
+One can do that with a very spare set of basic combinators. These days the standard base is just three combinators: K and I from above, and also one more, S, which behaves the same as the lambda expression  `\f g x. f x (g x)`. behaves.But it's possible to be even more minimalistic, and get by with only a single combinator. (And there are different single-combinator bases you can choose.)
+
+These systems are Turing complete. In other words: every computation we know how to describe can be represented in a logical system consisting of only a single primitive operation!
+
+Here's more to read about combinatorial logic:
+
+MORE
+
+Evaluation strategies
+=====================
+
+In the assignment we asked you to reduce various expressions until it wasn't possible to reduce them any further. For two of those expressions, this was impossible to do. One of them was this:
+
+       (\x. x x) (\x. x x)
+
+As we saw above, each of the halves of this formula are the combinator &omega;; so this can also be written:
+
+<pre><code>&omega; &omega;</code></pre>
+
+This compound expression---the self-application of &omega;---is named &Omega;. It has the form of an application of an abstract (&omega;) to an argument (which also happens to be &omega;), so it's a redex and can be reduced. But when we reduce it, we get <code>&omega; &omega;</code> again. So there's no stage at which this expression has been reduced to a point where it can't be reduced any further. In other words, evaluation of this expression "never terminates." (This is standard language, however it has the unfortunate connotation that evaluation is a process or operation that is performed in time. You shouldn't think of it like that. Evaluation of this expression "never terminates" in the way that the decimal expansion of &pi; never terminates. This are static, atemporal facts about their mathematical properties.)
+
+There are infinitely many formulas in the lambda calculus that have this same property. &Omega; is the syntactically simplest of them. In our meta-theory, it's common to assign such formula a special value, <code>&perp;</code>, pronounced "bottom." When we get to discussing types, you'll see that this value is counted as belonging to every type. To say that a formula has the bottom value means that the computation that formula represents never terminates and so doesn't evaluate to any orthodox value.
+
+From a "Fregean" or "weak Kleene" perspective, if any component of an expression fails to be evaluable (to an orthodox, computed value), then the whole expression should be unevaluable as well.
+
+However, in some such cases it seems *we could* sensibly carry on evaluation. For instance, consider:
+
+<pre><code>
+(\x. y) (&omega; &omega;)
+</code></pre>
+
+Should we count this as unevaluable, because the reduction of <code>&omega; &omega;</code> never terminates? Or should we count it as evaluating to `y`?
+
+This question highlights that there are different choices to make about how evaluation or computation proceeds. It's helpful to think of three questions in this neighborhood:
+
+>      Q1. When arguments are complex, as <code>&omega; &omega;</code>, do we reduce them before or after substituting them into the abstracts to which they are arguments?
+
+>      Q2. Are we allowed to reduce inside abstracts? That is, can we reduce:
+
+>              (\x y. x z) (\x. x)
+
+>      only this far:
+
+>              \y. (\x. x) z
+
+>      or can we continue reducing to:
+
+>              \y. z
+
+>      Q3. Are we allowed to "eta-reduce"? That is, can we reduce expressions of the form:
+
+>              \x. M x
+
+>      where x does not occur free in `M`, to `M`? It should be intuitively clear that `\x. M x` and `M` will behave the same with respect to any arguments they are given. It can also be proven that no other functions can behave differently with respect to them. However, the logical system you get when eta-reduction is added to the proof theory is importantly different from the one where only beta-reduction is permitted.
+
+
+The evaluation strategy which answers Q1 by saying "reduce arguments first" is known as **call-by-value**. The evaluation strategy which answers Q1 by saying "substitute arguments in unreduced" is known as **call-by-name** or **call-by-need** (the difference between these has to do with efficiency, not semantics).
+
+When one has a call-by-value strategy that also permits reduction to continue inside unapplied abstracts, that's known as "applicative order" reduction. When one has a call-by-name stratehy that oermits reduction inside abstracts, that's known as "normal order" reduction. Consider an expression of the form:
+
+       ((A B) (C D)) (E F)
+
+Its syntax has the following tree:
+
+  ((A B) (C D)) (E F)
+       /     \
+      /       \
+((A B) (C D))  \
+    /\        (E F)
+   /  \        /\
+  /    \      E  F
+(A B) (C D)
+ /\    /\
+/  \  /  \
+A   B C   D
+
+Applicative order evaluation does what's called a "post-order traversal" of the tree: that is, we always go left and down whenever we can, and we process a node only after processing all its children. So `(C D)` gets processed before `((A B) (C D))` does, and `(E F)` gets processed before `((A B) (C D)) (E F)` does.
+
+Normal order evaluation, on the other hand, will substitute the expresion `(C D)` into the abstract that `(A B)` evaluates to, without first trying to compute what `(C D)` evaluates to. That computation may be done later.
+
+When we have an expression like:
+
+       (\x. y) (C D)
+
+the computation of `(C D)` won't ever have to be performed, on a normal order or call by name evaluation strategy. Instead, that reduces directly to `y`. This is so even if `(C D)` is the non-evaluable <code>(&omega; &omega;)</code>!
+
+Most programming languages, including Scheme and OCaml, use the call-by-value evaluation strategy. (But they don't permit evaluation to continue inside an unappplied function.) There are techniques for making them model the other sort of behavior.
+
+Some functional programming languages, such as Haskell, use the call-by-name evaluation strategy. Each has pros and cons.
+
+The lambda calculus can be evaluated either way. You have to decide what the rules shall be.
+
+One important advantage of the normal-order evaluation strategy is that it can compute an orthodox value for:
+
+<pre><code>
+(\x. y) (&omega; &omega;)
+</code></pre>
+
+Indeed, it's provable that if there's any reduction path that delivers a value for an expression, the normal-order evalutation strategy will terminate with that value.
+
+An expression is said to be in **normal form** when it's not possible to perform any more reductions. (EVEN INSIDE ABSTRACTS?) There's a sense in which you can't get anything more out of <code>&omega; &omega;</code>, but it's not in normal form because it still has the form of a redex.
+
+A computational system is said to be **confluent**, or to have the **Church-Rosser** or **diamond** property, if, whenever there are multiple possible evaluation paths, those that terminate always terminate in the same value. In such a system, the choice of which sub-expressions to evaluate first will only matter if some of them but not others might lead down a non-terminating path.
+
+The untyped lambda calculus is confluent. So long as a computation terminates, it always terminates in the same way. It doesn't matter which order the sub-expressions are evaluated in.
+
+A computational system is said to be **strongly normalizing** if every permitted evaluation path is guaranteed to terminate. The untyped lambda calculus is not strongly normalizing: <code>&omega; &omega;</code> doesn't terminate by any evaluation path; and <code>(\x. y) (&omega; &omega;)</code> terminates only by some evaluation paths but not by others.
+
+But the untyped lambda calculus enjoys some compensation for this weakness. It's Turing complete! It can represent any computation we know how to describe. (That's the cash value of being Turing complete, not the rigorous definition. We don't know how to rigorously define "any computation we know how to describe." There is however a rigorous definition for being Turing complete.) And in fact, it's been proven that you can't havee both. If a language is Turing complete, it cannot be strongly normalizing.
+
+A computational system is said to be **weakly normalizing** if there's always guaranteed to be *at least one* evaluation path that terminates. The untyped lambda calculus is not weakly normalizing either, as we've seen.
+
+The *typed* lambda calculus that linguists traditionally work with, on the other hand, is strongly normalizing. (And as a result, is not Turning complete.) It has expressive power that the untyped lambda calculus lacks, but it is also unable to represent some (terminating!) computations that the untyped lambda calculus can represent.
+
+Other more-powerful type systems we'll look at in the course will also fail to be Turing complete, though they will turn out to be pretty powerful.
+
+
+
+
+
+
+K
+omega
+true/get-first/K
+false/get-second
+make-pair
+S,B,C,W/dup,Omega
+
+(( combinatorial logic ))
+
+
+
+
+We'll write that like this:
+
+       ((\x (y x)) z) ~~> (y z)
+
+Different authors use different notations. Some authors use the term "contraction" for a single reduction step, and reserve the term "reduction" for the reflexive transitive closure of that, that is, for zero or more reduction steps. Informally, it seems easiest to us to say "reduction" for one or more reduction steps. So when we write:
+
+       M ~~> N
+
+We'll mean that you can get from M to N by one or more reduction steps. Hankin uses the symbol <code><big><big>&rarr;</big></big></code> for one-step contraction, and the symbol <code><big><big>&#8608;</big></big></code> for zero-or-more step reduction. Hindley and Seldin use <code><big><big><big>&#8883;</big></big></big><sub>1</sub></code> and <code><big><big><big>&#8883;</big></big></big></code>.
+
+When M and N are such that there's some P that M reduces to by zero or more steps, and that N also reduces to by zero or more steps, then we say that M and N are **beta-convertible**. We'll write that like this:
+
+       M <~~> N
+
+This is what plays the role of equality in the lambda calculus. Hankin uses the symbol `=` for this. So too do Hindley and Seldin. Personally, I keep confusing that with the relation to be described next, so let's use this notation instead. Note that `M <~~> N` doesn't mean that each of `M` and `N` are reducible to each other; that only holds when `M` and `N` are the same expression. (Or, with our convention of only saying "reducible" for one or more reduction steps, it never holds.)
+
+In the metatheory, it's also sometimes useful to talk about formulas that are syntactically equivalent *before any reductions take place*. Hankin uses the symbol <code>&equiv;</code> for this. So too do Hindley and Seldin. We'll use that too, and will avoid using `=` when discussing the metatheory. Instead we'll use `<~~>` as we said above. When we want to introduce a stipulative definition, we'll write it out longhand, as in:
+
+
+combinators as lambda expressions
+combinatorial logic
+
+tuples = possibly type-heterogenous ordered collections, different length -> different type
+lists = type-homogenous ordered collections, lists of different lengths >=0 can be of same type
+
+
+
+
 1.     Substitution; using alpha-conversion and other strategies
 1.     Conversion versus reduction
 
 1.     Substitution; using alpha-conversion and other strategies
 1.     Conversion versus reduction
 
@@ -463,16 +662,6 @@ confluence/Church-Rosser
 
 "combinators", useful ones:
 
 
 "combinators", useful ones:
 
-Useful combinators
-I
-K
-omega
-true/get-first/K
-false/get-second
-make-pair
-S,B,C,W/dup,Omega
-
-(( combinatorial logic ))
 
 composition
 n-ary[sic] composition
 
 composition
 n-ary[sic] composition