Com S 342 meeting -*- Outline -*- * reasoning about procedures (4.1) (omit) more transformation rules for programming this is one advantage of functional programming: that can reason about procedures this way ------------------------------------------ REASONING ABOUT FUNCTIONAL PROGRAMS (define caadr (lambda (ls) (car (car (cdr ls))))) (caadr (list (list 1 2) (list 3 4))) => ((lambda (ls) (car (car (cdr ls)))) (list (list 1 2) (list 3 4))) => (car (car (cdr (list (list 1 2) (list 3 4))))) ------------------------------------------ Write in the reasons for each ... ... Can continue this... Q: How did we proceed? summarize it in words This process (beta reduction) is useful when it applies, but isn't always accurate (more later about that) Sometimes omit steps like the first These are reductions, because they are oriented. ------------------------------------------ FOR YOU TO DO (define square-first (lambda (n1 n2) (* n1 n1))) Complete the following simplification: (square-first (+ 5 3) (/ (* 10567 (+ 93 5)) 7)) => ------------------------------------------ Q: Is it better to evaluate the arguments first? can do either order here, but the second one isn't needed. Q: What strategy does Scheme use? evaluates arguments first ------------------------------------------ EVALUATING ARGUMENTS FIRST (define caadr (lambda (ls) (car (car (cdr ls))))) (caadr (list (list 1 2) (list 3 4))) => ((lambda (ls) (car (car (cdr ls)))) (list (list 1 2) (list 3 4))) => ((lambda (ls) (car (car (cdr ls)))) '((1 2) (3 4))) => (car (car (cdr '((1 2) (3 4))))) ------------------------------------------ These strategies are called applicative-order = evaluate arguments first normal order, leftmost = evaluate leftmost redex first See section 4.3 for details ------------------------------------------ REDUCTION RULES AS A GENERAL SEMANTIC MECHANISM (OPERATIONAL SEMANTICS) 1. for procedures (define c+ (lambda (n) (lambda (m) (+ n m)))) ((c+ 4) 3) => ((lambda (m) (+ 4 m)) 3) => (+ 4 3) => 7 2. for syntactic sugars (let ((x 3) (add5 (c+ 5))) (add5 x)) => ((lambda (x add5) (add5 x)) 3 (c+ 5)) => ((lambda (x add5) (add5 x)) 3 (lambda (m) (+ 5 m))) => ------------------------------------------ This kind of semantics is widely used in computer science to describe programming languages Idea: say how to successively reduce expressions, statements, etc to some final form (a value for expressions) Q: How is this kind of semantics like an interpreter? Q: What happens if the program being reduced is an infinite loop? (define oh-no (lambda (n) (oh-no n))) (oh-no 1) This kind of reasoning is sometimes called "equational reasoning", because it replaces equals by equals