Reductions

Remember:

(foo (+ 4 1) 7)

((lambda (x y) (+ (* x 3) y))
 (+ 4 1)
 7)

(+ (* (+ 4 1) 3) 7)

22

IN GENERAL

	((lambda (x) E) M) = E[M/x]

where E[M/x] is defined by our substitution rules
1 - 4. 

This  is also called a BETA-CONVERSION and an 
expression of the form 

	((lambda (x) E) M) 

to which this rule can be applied is called a
Beta-Redux 

When the Beta-conversion rule is used in
left to right direction to transform an Beta-redux 
it is called a Beta-reduction.


Can expressions be reduced to more than one constant?

((lambda (x y) (+ (* x 3) y))
 (+ 4 1)
 7)

which do we reduce first?
(+ 4 1) or the lambda expression's beta reduction?

Church-Rosser Theorem:

If an expression E can be reduced to either E1 or
E2, using different reduction sequences, then
there is some expression N that can be reached
from both E1 and E2

Church-Rosser property
Confluence property
Diamond property

Can expressions be reduced to more than one
constant?

If an expression reduced to two different
constants then there has to be some other terms N
to which both constants reduced. 

But constants cannot be reduced.

Therefore there cannot be two different constants
to which an expression is reducible to.


Can we blindly apply Beta-reduction and be
guaranteed to find an answer?



Beta-Reduce:

((lambda (x) (x x))
 (lambda (x) (x x)))



Beta-Reduce

((lambda (y) 3)
 ((lambda (x) (x x))
  (lambda (x) (x x))))



Can we blindly apply Beta-reduction and be
guaranteed to find an answer?


Ans: NO

Because you can still have infinite sequences

Thus: Different reduction strategies may or may
not find an answer, but they will agree on the
answer they find.



What reduction strategy best models the behavior 
of scheme?

Applicative order-reduction.
Also called the lambda-value calculus.

Answer: 
  Constant, 
  Variable, or
  Abstraction (a lambda exp)

That is an Answer is anything but an application

1. In applicative-order reduction, Beta reduction
can only be applied if both the operator and
operand are already Answers.

2. If they are not, then either the operator or the
operand can be reduced.

3. A redex in the body of an abstraction is never
reduced!

4. eta-reduction is never applied

What is eta-reduction?



Remember:
(define extend-ff
 (lambda (sym val ff)
  (make-extended-ff sym val ff)))

same as

(define extend-ff make-extended-ff)

This is called eta-reduction:

Eta-reduction:
	(lambda (x) (E x) ) = E 

where x does not occur free in E
E is a procedure of one variable.



Beta rule for scheme:

((lambda (x) E) M) = E[M/x]
where M is an answer



Procedure reduce-once-appl
takes a parsed lamabda calculus expression and
reduces the first redex it finds according to 
applicative-order reduction, if there is such a
redex and returns the reduced expression.

1. If constant, varref, lambda
	cannot be reduced...

2. If applicative Beta-redex then reduce
using beta-reduce (ex 4.2.4)

3. If application but not beta-redex, try to
first reduce the operator
   if this fails because the operator is
   already an answer
	then
   try to reduce operand


Whenever we succeed in reducing a subexpression,
we must construct a new expression that contains
the reduced form of the subexpression.



How do we know whether we succeeded or failed?

ribassoc returned a special failure value

This is not good design for reduce-once-appl
since
(let ([retval (reduce-once-appl exp)])
  (if (eq? retval 'fail)
      (failed ...)
      (succeeded ...)))

cumbersone and not elegant.

Instead we pass two additional arguments to
reduce-once-appl:

1. a PROCEDURE succeed that is passed the reduced
expression in the event of success
2. a PROCEDURE fail of no argumnets that is
invoked if the reduction fails.

That is

(reduce-once-appl exp succeed fail) =

   (succeed exp') if exp contains an applicative
order beta-redux, in which case exp' is the result 
of performing ONE applicative order reduction on
exp

	OR

   (fail) is exp has no applicative-order beta
redex. 



(define answer?
  (lambda (exp)
    (not (app? exp))))

(define reduce-once-appl
  (lambda (exp succeed fail)
    (variant-case exp
      [lit (datum) (fail)]
      [varref (datum) (fail)]
      [lambda (formal body) (fail)]
      [app (rator rand)
	(if (and (beta-redex? exp)
		 (answer? rand))
	    (succeed
	      (beta-reduce exp))
	    (reduce-once-appl rator
	      (lambda (reduced-rator)
		(succeed
		  (make-app reduced-rator rand)))
	      (lambda ()
		(reduce-once-appl rand
		  (lambda (reduced-rand)
		    (succeed
		      (make-app rator reduced-rand)))
		  fail))))]
      )))



Sushil Louis
Last modified: Mon Oct 11 12:26:37 PDT 1999