CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Similar documents
Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

The Environment Model

The Environment Model. Nate Foster Spring 2018

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

The Substitution Model

To figure this out we need a more precise understanding of how ML works

Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Data Types. Guest Lecture: Andrew Myers Spring 2018

The Substitution Model. Nate Foster Spring 2018

To figure this out we need a more precise understanding of how ML works

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Functions. Nate Foster Spring 2018

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS4215 Programming Language Implementation. Martin Henz

CS153: Compilers Lecture 15: Local Optimization

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Async. Prof. Clarkson Fall Today s music: It's Gonna be Me by *NSYNC

Recap: Functions as first-class values

CS153: Compilers Lecture 16: Local Optimization II

Programming Languages and Compilers (CS 421)

CS558 Programming Languages. Winter 2013 Lecture 3

Introduction to OCaml

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS422 - Programming Language Design

Types, Expressions, and States

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CSE 341: Programming Languages

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

A Functional Evaluation Model

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CS 11 Haskell track: lecture 1

Some Advanced ML Features

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Shared Variables and Interference

CSCI-GA Scripting Languages

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

CSE 505: Concepts of Programming Languages

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Fall Lecture 3 September 4. Stephen Brookes

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CS 11 Ocaml track: lecture 2

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

CS422 - Programming Language Design

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

CS558 Programming Languages

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CMSC 330: Organization of Programming Languages

Lecture 5: The Untyped λ-calculus

(Refer Slide Time: 4:00)

Typing Data. Chapter Recursive Types Declaring Recursive Types

ob-ject: to feel distaste for something Webster's Dictionary

SML A F unctional Functional Language Language Lecture 19

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Syntax Errors; Static Semantics

Lecture 2: Big-Step Semantics

CS 320: Concepts of Programming Languages

CS558 Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CMSC 330: Organization of Programming Languages. Operational Semantics

Shared Variables and Interference

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Lecture 2: SML Basics

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

Softwaretechnik. Lecture 18: Featherweight Java. Peter Thiemann. University of Freiburg, Germany

Type Checking and Type Inference

CS 242. Fundamentals. Reading: See last slide

Variables and Bindings

Func+on applica+ons (calls, invoca+ons)

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Software Engineering

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

1 Introduction. 3 Syntax

Lecture Notes on Aggregate Data Structures

Contents. Softwaretechnik. Type Safety of Java. Featherweight Java. Lecture 21: Featherweight Java

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

CMSC 330: Organization of Programming Languages

15 150: Principles of Functional Programming. More about Higher-Order Functions

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Transcription:

CS 3110 Lecture 7: The dynamic environment Prof. Clarkson Spring 2015 Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Review Course so far: Syntax and semantics of (most of) OCaml Today: Different semantics

Question #1 How much of PS1 have you finished? A. None B. About 25% C. About 50% D. About 75% E. I m done!!!

Semantics Dynamic semantics How expressions evaluate Dynamic: execution is in motion Evaluation rules e --> e' --> e'' Static semantics How expressions type check (among other things) Static: execution is not yet moving Type checking rules e : t

Dynamic semantics Today: change our model of evaluation: Small-step substitution model: substitute value for variable in body of let expression & in body of function What we ve done doing so far Good mental model, not really what OCaml does Big-step environment model: keep a data structure around that binds variables to values What we ll do now Also a good mental model, much closer to what OCaml really does

The core of OCaml Essential sublanguage of OCaml: e ::= v C e (e1,..., en) e1 + e2 x e1 e2 let x = e1 in e2 match e0 with pi -> ei v ::= c fun x -> e C v (v1,..., vn) Missing, unimportant: records, lists, options, declarations, patterns in function arguments and let bindings, if Missing, important: rec Extraneous: all we really need is e ::= x e1 e2 fun x -> e

Review: evaluation Expressions step to new expressions e --> e1 --> e2 -->... Long arrow means steps to Star means reflexive, transitive closure: 0, 1, or more steps Values have no further computation to do So they don't take a single step: v-/->! But they could take zero steps: v-->*v! Small-step semantics: we model each small step the evaluation takes

New kind of evaluation Big-step semantics: we model just the reduction from the original expression to the final value Suppose e --> e' -->... --> v We'll just record the fact that e v! new notation means e evaluates (down) to v in ASCII: e v!

Values Values are already done: Evaluation rule: v v Constants are already values 42 is already a value 3110 is already a value () is already a value same for C v and (v1,..., vn)! Functions are already values heads-up: we'll reconsider this choice next lecture fun x -> e is already a value, no matter what e is

Operator evaluation rule e1 + e2 v if e1 v1 and e2 v2 and v is the result of primitive operation v1 + v2 e.g., 1 + 2 3 3.110 *. 1.0 3.11 0 < 1 true "zar" ^ "doz" "zardoz"

Tuples To evaluate (e1,...,en), Evaluate the subexpressions: e1 v1 en vn Return (v1,...,vn) In which case, (e1,...,en) (v1,...,vn)

Tuple evaluation rule (e1,...,en) (v1,...,vn) if e1 v1 and... and en vn e.g., so (1+1, 2+2) (2,4) because 1+1 2 and 2+2 4

Question #2 If we changed evaluation order to be en first, then then e2, then e1, which of the following expressions would evaluate to a different value? A. (0+1,2*3)! B. (let x = 3 in x, hi )! C. ((), (fun x -> x+1) 1)! D. All the above E. None of the above

Question #2 If we changed evaluation order to be en first, then then e2, then e1, which of the following expressions would evaluate to a different value? A. (0+1,2*3)! B. (let x = 3 in x, hi )! C. ((), (fun x -> x+1) 1)! D. All the above E. None of the above

Tuple evaluation order Q: What order are the ei evaluated in? A: It doesn t matter. Pure programs can't distinguish the order of evaluation. Pure = no side effects: no printing, no exceptions,... A: OCaml language specification says order is unspecified. A: OCaml compiler on VM does right to left: e2 then e1. ((print_string left\n ; 0), (print_string right\n ; 1))

Constructors To evaluate C e, Evaluate the subexpression: e v Return C v In which case, C e C v

Constructor evaluation rule C e C v if e v e.g., Some (1+1) Some 2 because 1+1 2 Multiple arguments: C e1... en. Rule easily extends. Constructors that carry no data behave like constants true is already a value [] is already a value

Progress e ::= v C e (e1,..., en) e1 + e2 x e1 e2 let x = e1 in e2 match e0 with pi -> ei

Variables What does a variable name evaluate to? x??? Trick question: we don t have enough information to answer it Need to know what value variable was bound to

Question #3 What do these evaluate to? let x = 2 in x+1 (fun x -> x+1) 2 match 2 with x -> x+1 A. 2, 2, and 2 B. 3, 3, and 3 C. 3, 2, and 3 D. 3, 3, and 2 E. 2, 3, and 3

Question #3 What do these evaluate to? let x = 2 in x+1 (fun x -> x+1) 2 match 2 with x -> x+1 A. 2, 2, and 2 B. 3, 3, and 3 C. 3, 2, and 3 D. 3, 3, and 2 E. 2, 3, and 3

Variables What does a variable name evaluate to? x??? Trick question: we don t have enough information to answer it Need to know what value variable was bound to e.g., let x = 2 in x+1 e.g., (fun x -> x+1) 2 e.g., match 2 with x -> x+1 All evaluate to 3, but we reach a point where we need to know binding of x Until now, we've never needed this, because we always substituted before we ever get to a variable name

Variables OCaml doesn't actually do substitution (fun x -> 42) 0! waste of runtime resources to do substitution inside 42 Instead, OCaml lazily substitutes by maintaining dynamic environment

Dynamic environment Set of bindings of all current variables Changes throughout evaluation: No bindings at $: $ let x = 42 in let y = "3110" in e One binding {x=42} at $: let x = 42 in $ let y = "3110" in e Two bindings {x=42,y="3110"} at $: let x = 42 in let y = "3110" in $ e

Variable evaluation To evaluate x in environment env Look up value v of x in env Return v Type checking guarantees that variable is bound, so we can t ever fail to find a binding in dynamic environment

Variable evaluation rule env :: x v if v = env(x) New notation: env :: e v meaning: in dynamic environment env, expression e evaluates down to value v! env(x) meaning: the value to which env binds x

Redo: rules with environment Values: env :: v v Operators: env :: e1 + e2 v if env :: e1 v1 and env :: e2 v2 and v is the result of primitive operation v1+v2 Tuples: env :: (e1,...en) (v1,...vn) if env :: e1 v1 and... and env :: en vn Constructors: env :: C e C v if env :: e v Why the same environment for each component of tuple?

Scope Bindings are in effect only in the scope (the block ) in which they occur Exactly what you re used to from (say) Java Bindings inside elements of tuples are not in scope outside that element ((let x = 1 in x+1), (let y=2 in y+2)) x is not in scope in second component y is not in scope in first component so dynamic environment stays the same from one component to another env :: ei vi

Progress e ::= v C e (e1,..., en) e1 + e2 x e1 e2 let x = e1 in e2 match e0 with pi -> ei

Let expressions To evaluate let x = e1 in e2 in environment env Evaluate the binding expression e1 to a value v1 in environment env env :: e1 v1 Extend the environment to bind x to v1 env = env + {x=v1} Evaluate the body expression e2 to a value v2 in environment env env :: e2 v2 Return v2

Let expression evaluation rule env :: let x=e1 in e2 v2 if env :: e1 v1 and env+{x=v1} :: e2 v2 Example: {} :: let x = 42 in x 42 Why? Because... {} :: 42 42! and {}+{x=42} :: x 42 Why? because if env is {x=42} then env(x)=42!

Initial environment Can add an entire file s worth of bindings to the dynamic environment with open Name You ve been doing that in unit test files OCaml always does open Pervasives at the beginning (+), (=), int_of_string, (@), print_string, fst,... The environment is never really empty it s always polluted? :) But we write {} anyway

Extending the environment What does env+{x=v} really mean? Illuminating example: let x = 0 in let x = 1 in x 1 Environment extension can t just be set union We d get {x=0,x=1} and now we don t know what x is! Instead inner binding shadows outer binding Casts its shadow over it; temporarily replaces it Environments at particular places (abuse OCaml syntax here): let x = ({} 0) in ({x=0} let x = 1 in ({x=1} x))

Question #4 let x = 0 in x + (let x = 1 in x)??? A. 0 B. 1 C. 2 D. unspecified by language E. none of the above

Question #4 let x = 0 in x + (let x = 1 in x)??? A. 0 B. 1 C. 2 D. unspecified by language E. none of the above

Question #5 let x = 0 in (let x = 1 in x) + x??? A. 0 B. 1 C. 2 D. unspecified by language E. none of the above

Question #5 let x = 0 in (let x = 1 in x) + x??? A. 0 B. 1 C. 2 D. unspecified by language E. none of the above

Shadowing is not assignment let x = 0 in 1 x + (let x = 1 in x) let x = 0 in 1 (let x = 1 in x) + x

Progress e ::= v C e (e1,..., en) e1 + e2 x e1 e2 let x = e1 in e2 match e0 with pi -> ei