f6a480306a595912daf5e6933a03ab02cc4dcdaa
[lambda.git] / exercises / _assignment6.mdwn
1 # Assignment 6 (week 7)
2
3 ## Evaluation order in Combinatory Logic
4
5 1. Give a term that the lazy evaluators (either [[the Haskell
6 evaluator|code/ski_evaluator.hs]], or the lazy version of [[the OCaml
7 evaluator|code/ski_evaluator.ml]]) do not evaluate all the way to a
8 normal form, i.e., that contains a redex somewhere inside of it after
9 it has been reduced.
10
11 <!-- reduce3 (FA (K, FA (I, I))) -->
12
13 2. One of the [[criteria we established for classifying reduction
14 strategies|topics/week3_evaluation_order]] strategies is whether they
15 reduce subexpressions hidden under lambdas.  That is, for a term like
16 `(\x y. x z) (\x. x)`, do we reduce to `\y.(\x.x) z` and stop, or do
17 we reduce further to `\y.z`?  Explain what the corresponding question
18 would be for CL. Using either the OCaml CL evaluator or the Haskell
19 evaluator developed in the wiki notes, prove that the evaluator does
20 reduce expressions inside of at least some "functional" CL
21 expressions.  Then provide a modified evaluator that does not perform
22 reductions in those positions. (Just give the modified version of your
23 recursive reduction function.)
24
25 <!-- just add early no-op cases for Ka and Sab -->
26
27 ## Evaluation in the untyped lambda calculus: substitution
28
29 Once you grok reduction and evaluation order in Combinatory Logic,
30 we're going to begin to construct an evaluator for a simple language
31 that includes lambda abstraction.  We're going to work through the
32 issues twice: once with a function that does substitution in the
33 obvious way.  You'll see it's somewhat complicated.  The complications
34 come from the need to worry about variable capture.  (Seeing these
35 complications should give you an inkling of why we presented the
36 evaluation order discussion using Combinatory Logic, since we don't
37 need to worry about variables in CL.)  
38
39 We're not going to ask you to write the entire program yourself.
40 Instead, we're going to give you [[the complete program, minus a few
41 little bits of glue|code/reduction.ml]].  What you need to do is
42 understand how it all fits together.  When you do, you'll understand
43 how to add the last little bits to make functioning program.  Here's a
44 first target for when you get it working:
45
46     # reduce (App (Abstract ("x", Var "x"), Constant (Num 3)));;
47     - : lambdaTerm = Constant (Num 3)
48
49 1. In the previous homework, you built a function that took an
50 identifier and a lambda term and returned a boolean representing
51 whether that identifier occured free inside of the term.  Your first
52 task is to adapt your previous solution as necessary to work with the
53 code base.  Once you have your function working, you should be able to
54 run queries such as this:
55
56