lec5-2x2

# lec5-2x2 - Environments CSE 130 : Fall 2011 Programming...

This preview shows pages 1–4. Sign up to view the full content.

CSE 130 : Fall 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Environments and Closures Tuesday, October 11, 2011 Environments “Phone book” • Variables = “names” • Values = “phone number” 1. Evaluate : Find and use most recent value of variable 2. Extend : Add new binding at end of “phone book” Tuesday, October 11, 2011 # let x = 2+2;; val x : int = 4 # let y = x * x * x;; val y : int = 64 # let z = [x;y;x+y];; val z : int list = [4;64;68] # let x = x + x ;; val x : int = 8 Example x 4 : int y 64 : int z [4;64;68] : int list M M x 4 : int y 64 : int M M x 4 : int M M M M x 4 : int y 64 : int z [4;64;68] : int list M M x 8 : int New binding! Tuesday, October 11, 2011 Environments 1. Evaluate : Use most recent bound value of var 2. Extend : Add new binding at end How is this different from C/Java’s “store” ? # let x = 2+2;; val x : int = 4 # let f = fun y -> x + y; val f : int -> int = fn # let x = x + x ; val x : int = 8 # f 0; val it : int = 4 x 4 : int M M New binding: • No change or mutation • Old binding frozen in f x 4 : int f fn < code, >: int->int M M Tuesday, October 11, 2011

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Environments 1. Evaluate : Use most recent bound value of var 2. Extend : Add new binding at end How is this different from C/Java’s “store” ? # let x = 2+2; val : int x = 4 # let f = fun y -> x + y; val f : int -> int = fn # let x = x + x ; val x : int = 8; # f 0; val it : int = 4 x 4 : int M M x 4 : int f fn < code, >: int->int M M x 8 : int x 4 : int f fn < code, >: int->int M M Tuesday, October 11, 2011 Environments 1. Evaluate : Use most recent bound value of var 2. Extend : Add new binding at end How is this different from C/Java’s “store” ? # let x = 2+2; val x : int = 4 # let f = fun y -> x + y;; val f : int -> int = fn # let x = x + x ; val x : int = 8 # f 0; val it : int = 4 x 4 : int f fn < code, >: int->int M M x 8 : int Binding used to eval (f …) Binding for subsequent x Tuesday, October 11, 2011 Cannot change the world Cannot “assign” to variables • Can extend the env by adding a fresh binding • Does not affect previous uses of variable Environment at fun declaration frozen inside fun “value” • Frozen env used to evaluate application (f …) Q : Why is this a good thing ? # let x = 2+2;; val x : int = 4 # let f = fun y -> x + y;; val f : int -> int = fn # let x = val x : int = 8; # f 0;; val it : int = 4 x 4 : int f fn < code, >: int->int M M x 8 : int Binding used to eval (f …) Binding for subsequent x Tuesday, October 11, 2011 Cannot change the world Q : Why is this a good thing ? A: Function behavior frozen at declaration • Nothing entered afterwards affects function • Same inputs always produce same outputs – Localizes debugging – Localizes reasoning about the program – No “sharing” means no evil aliasing Tuesday, October 11, 2011
Examples of no sharing Remember: No addresses, no sharing.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/11/2011 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

### Page1 / 10

lec5-2x2 - Environments CSE 130 : Fall 2011 Programming...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online