1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
CS314 – Section 5 Recitation 9
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
CS314 – Section 5 Recitation 10
Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Original material by Eric Grimson
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Closures and Streams cs7100(Prasad) L11Clos
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson

2 Starting Point Informal Scheme Semantics  To evaluate (E1 E2... En), recursively evaluate E1, E2,...,En - E1 should evaluate to a function - and then apply the function value of E1 to the arguments given by the values of E2,...,En.  In the base case, there are self evaluating expressions (e.g. numbers and symbols). In addition, various special forms such as quote and if must be handled separately.

3 Theme  Programs are functions and their semantics involve function application. Programs may also produce function by returning functions as values. In pure functional programming, this is it, there are no variables, side effects, nor loops. This simplifies semantics but does not reduce computational power.  We will investigate the style of programming this implies, and how to model the semantics of such programs. We conclude with a scheme interpreter [SICP – “Structure and Interpretation of Computer Programs by Abelson and Sussman].

4 Outline  Review scheme syntax and semantics  Functional programming  Programs are functions – for every input there is a unique output  No variables  no assignment and no loops  Use recursion for control  Functions are first class objects  Pass as arguments and return as values  Simple semantics [value semantics] (no side effects, referential transparency)

5 Outline  Substitution model of computation  Modeling state with assignment  Environments  Streams and delayed evaluation  Scheme interpreter (meta-circular)

6 Scheme Syntax and Semantics  S expressions (E 1 … E n )  Special forms  Self evaluating: numbers, strings, …  (quote E)  (if exp E1 E2)  (cond ((P1 E1) … (Pt Et)))  (lambda (p1 … pn) E1 … Et)  (define name E)  (let ((b1 v1) … (bt vt) E)

7 Higher Order Functions  sort: (sort '( ) ( )  map: (map sqr '( )) => ( )  filter: (keep-matching-items '( ) odd?) => (1 3 5)  reduce: (reduce * 1 '( )) => 24  Functions that return functions  (define (make-adder x) (lambda (y) (+ x y)))  Function composition  (define (compose g f) (lambda (x) (g (f x))))  Currying  (define (curry f a) (lambda (b) (f a b)))  ((curry + x) y)

8 Substitution Model of Computation  function application corresponds to substituting the argument expressions into the formal parameters of the function body  Order of evaluation  Applicative vs. normal order  lambda calculus  Church-Rosser

9 Substitution Example  (define (square x) (* x x))  (define (sum-of-squares x y) (+ (square x) (square y)))  (define (f a) (sum-of-squares (+ a 1) (* a 2))) [applicative order]  (f 5)  (sum-of-squares (+ 5 1) (* 5 2))  (+ (square 6) (square 10))  (+ (* 6 6) (* 10 10))  ( )  136 [normal order]  (f 5)  (sum-of-squares (+ 5 1) (* 5 2))  (+ (square (+ 5 1)) (square (* 5 2)) )  (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))  (+ (* 6 6) (* 10 10))  ( )

10 Order Matters (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) (test 0 (p))

11 Environments  When assignment is introduced the substitution model falls apart and a different, more complicated model, must be used.  Environments used to store variables and bindings.  Values can change  assignment supported  List of frames to support nested scope

12 Modeling State with Assignment (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) Make-withdraw can be used as follows to create two objects W1 and W2: (define W1 (make-withdraw 100)) (define W2 (make-withdraw 100)) (W1 50) 50 (W2 70) 30 (W2 40) "Insufficient funds" (W1 40) 10

13 Modeling State with Assignment (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE- ACCOUNT" m)))) dispatch) (define acc (make-account 100)) ((acc 'withdraw) 50) 50 ((acc 'withdraw) 60) "Insufficient funds" ((acc 'deposit) 40) 90 ((acc 'withdraw) 60) 30

14 Cost of Assignment (define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) balance)) (define W (make-simplified-withdraw 25)) (W 10) 15 (W 10) 5 (define (make-decrementer balance) (lambda (amount) (- balance amount))) (define D (make-decrementer 25)) (D 10) 15 (D 10) 15

Substitution Model Fails (make-decrementer 25) 20)  ((lambda (amount) (- 25 amount)) 20)  ( )  5 ((make-simplified-withdraw 25) 20)  ((lambda (amount) (set! balance (- 25 amount)) 25) 20)  (set! balance ( )) 25

16 Environment Model Solves Problem (define W1 (make-withdraw 100)) (W1 50) (define W2 (make-withdraw 100)) Global Env Balance = 50Balance = 100 E1 E2 Parameters body W1W2 Make-withdraw

17 Streams  Sequence of elements  (cons-stream x y)  (stream-car s)  (stream-cdr s)  (stream-null? s)  the-empty-stream

18 Streams Motivation and E.G. (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) ((prime? count) (iter (+ count 1) (+ count accum))) (else (iter (+ count 1) accum)))) (iter a 0))

19 Streams (define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) Enumeratefilteraccumulate Prime?+,0a,b

20 Streams are Not Lists  Consider the following example (car (cdr (filter prime? (enumerate-interval ))))  This would be extremely inefficient if implemented with lists  Do not build the entire stream of elements  Get the next element from the stream when needed  Necessary for potentially infinite streams

21 Delayed Binding (cons-stream )  (cons (delay )) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream))) (delay )  (lambda () ) (define (force delayed-object) (delayed-object))

22 Delayed Binding in Action (stream-car (stream-cdr (stream-filter prime? (stream-enumerate-interval )))) (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (cons (delay (stream-enumerate-interval )))

23 Delayed Binding in Action (define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))) (else (stream-filter pred (stream-cdr stream))))) stream-cdr forces (cons (delay (stream-enumerate-interval )))

24 Delayed Binding in Action (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream))) which in this case is (cons (delay (stream-filter prime? (cons (delay (stream-enumerate-interval ))))))

25 Scheme Interpreter 1.To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions. 2.To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied.

26 Scheme Interpreter: eval (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp))))

27 Scheme Interpreter: apply (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure))))

28 Dynamic vs. Static Scope  Static scope (define (make-adder x) (lambda (y) (+ x y))) (define add1 (make-adder 1)) (define x 2) (add1 2) 3  Dynamic scope 4

29 Scheme Interpreter: dynamic eval (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ; don't need to store env - not used. JRJ ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env) env)) ; added env as an argument to apply. JRJ (else (error "Unknown expression type -- EVAL" exp))))

30 Scheme Interpreter: dynamic apply (define (apply procedure arguments env) ; added env parameter. JRJ (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments env ))) ; extend runtime env instead of procedure-environment (else (error "Unknown procedure type -- APPLY" procedure))))

31 Practice Exercises  Implement map and reduce  Trace scheme interpreter from SICP using as input the following two expressions [you will have to add =, *, - as primitive procedures for this to work]. (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) (fact 3)

32 Practice Exercises  Add the let expression to the interpreter  How can you convert (let ((n1 v1) … (nt vt)) E) to an equivalent scheme expression already handled by the scheme interpreter  Modify the SICP interpreter to use dynamic instead of static scope. Provide an example where the same code provides two different answers depending on which scoping rule is used.

33 Practice Exercises  Try out the streams example in MIT- scheme (streams are supported)  the-empty-stream  stream-enumerate-interval  stream-filter  prime?  (stream-car (stream-cdr (stream-filter prime? (stream-enumerate-interval ))))

34 Practice Exercises  Modify the SICP interpreter to support streams  cons-stream  delay  Force  stream-car  stream-cdr  Run the previous example using your modified interpreter

35 Assignment 6  Re-implement assignment 2 or 3 (mini language with lists) to support procedures as first class objects  Proc values  proc(...) … end should be an expression which can be assigned to variables, passed as an argument, and returned by a procedure  Nested scope  You should support nested scope  Version 1 uses static scope  Version 2 uses dynamic scope  Test with make-adder [both static and dynamic scope]