Programming Languages

Similar documents
Variables and Bindings

Programming Languages

Programming Languages

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

Programming Languages

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

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

Recap: Functions as first-class values

Begin at the beginning

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

Programming Languages

Programming Languages

Programming Languages

Programming Languages

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Preparing for Class: Watch le Videos! What is an ML program?

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Programming Languages

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Programming Languages

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

7.3.3 A Language With Nested Procedure Declarations

Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Programming Languages

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

COP-5555 PROGRAMMING LANGUAGEPRINCIPLES NOTES ON RPAL

An example DFA: reaching definitions

Programming Languages

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Lines and Their Slopes

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

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

Programming Languages

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

So what does studying PL buy me?

A Crash Course on Standard ML

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Programming Languages Lecture 14: Sum, Product, Recursive Types

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

About coding style Spring February 9, 2004

Programming Languages

CS 403 Compiler Construction Lecture 8 Syntax Tree and Intermediate Code Generation [Based on Chapter 6 of Aho2] This Lecture

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Lecture 12 Date:

The Phases of a Compiler. Course Overview. In Chapter 4. Syntax Analysis. Syntax Analysis. Multi Pass Compiler. PART I: overview material

A Programming Language. A different language g is a different vision of life. CSE 130 : Fall Two variables. L1: x++; y--; (y=0)?

Implementing Recursion

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

12.4 The Ellipse. Standard Form of an Ellipse Centered at (0, 0) (0, b) (0, -b) center

Using Characteristics of a Quadratic Function to Describe Its Graph. The graphs of quadratic functions can be described using key characteristics:

Programming Languages

CS 312 Problem Set 1: An Introduction to SML

Week 27 Algebra 1 Assignment:

CS 312, Fall Exam 1

CPSC W1 University of British Columbia

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

Today. Recap from last Week. Factorial. Factorial. How does it execute? How does it execute? More on recursion. Higher-order functions

Winter Compiler Construction T9 IR part 2 + Runtime organization. Announcements. Today. Know thy group s code

Systems of Linear Equations

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

CSci 4223 Principles of Programming Languages

Recap: ML s Holy Trinity

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

A Programming Language. A different language g is a different vision of life. CSE 130 : Spring Two variables. L1: x++; y--; (y=0)?

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

Simple example. Analysis of programs with pointers. Program model. Points-to relation

type environment updated subtype sound

Lambda Calculus.

Fall Lecture 3 September 4. Stephen Brookes

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSE 130 [Winter 2014] Programming Languages

Unit I - Chapter 3 Polynomial Functions 3.1 Characteristics of Polynomial Functions

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

CSE 341 Section 5. Winter 2018

Programming Languages Lecture 15: Recursive Types & Subtyping

Module Mechanisms CS412/413. Modules + abstract types. Abstract types. Multiple Implementations. How to type-check?

EECS1022 Winter 2018 Additional Notes Tracing Point, PointCollector, and PointCollectorTester

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Semantics. Names. Binding Time

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

Scope, Functions, and Storage Management

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Chapter 9: Rational Equations and Functions

1.5 LIMITS. The Limit of a Function

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Fall 2015

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Spring 2015

Determine Whether Two Functions Are Equivalent. Determine whether the functions in each pair are equivalent by. and g (x) 5 x 2

B + -trees. Kerttu Pollari-Malmi

CHECK Your Understanding

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Section 1.4 Limits involving infinity

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

Transcription:

CSE 130 : Fall 2008 Programmg Languages Lecture 4: Variables, Environments, Scope News PA 1 due Frida 5pm PA 2 out esterda (due net Fi) Fri) Ranjit Jhala UC San Diego Variables and Bdgs Q: How to use variables L? Q: How to assign to a variable? # = 2+2 val : t = 4 = e Bd the value of epression e to the variable Variables and Bdgs # = 2+2 val : t = 4 # = * * val : t = 64 # z = [;;+] val z : t list = [4;64;68] Later declared epressions can use ost recent bound value used for evaluation Sounds like C/Java? NO!

Environments ( Phone Book ) How L deals with variables Variables = names Values = phone number Environments and Evaluation L begs a top-level environment Some names bound = e z 4 : t 64 : t [4;64;68] : t list 8 : t L program = Sequence of variable bdgs Program evaluated b evaluatg bdgs order 1. Evaluate epr e current env to get value v : t 2. Etend env to bd to v : t (Repeat with net bdg) Environments Phone book Variables = names Values = phone number 1. Evaluate: Fd and use most recent value of variable 2. Etend: Add new bdg at end of phone book Eample # = 2+2 val : t = 4 # = * * val : t = 64 4 : t 4 : t 64 : t # z = [;;+] val z : t list = [4;64;68] 4 : t # = + val : t = 8 New bdg! z z 64 : t [4;64;68] : t list 4 : t 64 : t [4;64;68] : t list 8 : t

Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? # = 2+2 val : t = 4 4 : t # f = fun -> + ; val f : t -> t = fn 4 : t f fn <code, >: t->t # = + ; val : t = 8 # f 0; val it : t = 4 New bdg: No change or mutation Old bdg frozen f # = 2+2; val : t = 4 4 : t # f = fun -> + ; val f : t -> t = fn 4 : t f fn <code, >: t->t # = + ; val : t = 8; 4 : t # f 0; f fn <code, >: t->t val it : t = 4 8 : t Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new bdg at end How is this different from C/Java s store? # = 2+2; val : t = 4 # f = fun -> + val f : t -> t = fn Bdg used to eval (f ) # = + ; val : t = 8 4 : t f fn <code, >: t->t # f 0; 8 : t val it : t = 4 Bdg for subsequent Cannot change the world Cannot assign to variables Can etend the env b addg a fresh bdg Does not affect previous uses of variable Environment at fun declaration frozen side fun value Frozen env used to evaluate application (f ) Q: Wh is this a good thg? # = 2+2 Bdg used to eval (f ) val : t = 4 # f = fun -> + val f : t -> t = fn # = + val : t = 8; # f 0 val it : t = 4 4 : t f fn <code,, >: t->t 8 : t Bdg for subsequent

Cannot change the world Q: Wh is this a good thg? A: Function behavior frozen at declaration Nothg entered afterwards changes function Same puts alwas produce same outputs So? Localizes debuggg Localizes testg/reasong g about program No sharg means no evil aliasg effects Eamples of no sharg Remember: No addresses, no sharg. Each variable is bound to a fresh stance of a value Tuples, Lists Efficient implementation without sharg? There is sharg and poters but hidden from ou Compiler s job is to optimize code to efficientl implement these no-sharg sharg semantics Your job is to use the simplified no-sharg semantics to write correct, cleaner, readable, etendable sstems Function bdgs Functions are values, can bd usg fname = fun -> e Problem: Can t defe recursive functions! fname is bound after computg rhs value no (or old ) bdg for occurences of fname side id e rec fname = e Occurences of fname side e bound to this defition rec fac = if <=1 then 1 else *fac (-1) Local bdgs: - So far: bdgs that rema until a re-bdg ( global ) Local, temporar variables are useful side functions Avoid repeatg computations ake functions more readable = e1 e2 Let- is an epression! Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] (onl) to evaluate e2

Local bdgs Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 Let- is an epression! Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 = 10 * 10 : t = = 10 * 10 : t 100 : t Nested bdgs Evaluatg - env E: 1. Evaluate epr e1 env E to get value v : t 2. Use etended E [ a v : t] to evaluate e2 = 10 10 : t ( = 20 * ) 10 : t + 20 : t 10 : t Nested bdgs = 10 = 20 * = 10 = 20 * Correct Formattg

Eample Nested function bdgs rec filter f s = if s = [] then [] else h = hd s t = filter f (tl s) if (f h) then h::t else t a = 20 f = = 10 g z = + z a + (g ) Env frozen with function Used to evaluate fun appl. Vl Values appl. are frozen env at defition f 0; Recap Recap Variables are names for values Environment: dictionar/phonebook ost recent bdg used Entries never changed, new entries added Build comple epressions with local bdgs - epression The -bdg is visible ( scope) side -epression Elsewhere the bdg is not visible Environment frozen at fun defition Re-bdg variables cannot change a function Same I/O behavior at ever call

Static/Leical Scopg Net: Functions For each occurrence of a variable, there is a unique place program tet where the variable was defed ost recent bdg environment Static/Leical: Determed from the program tet Without eecutg the program Epressions Tpes Values Ver useful for readabilit, debuggg: Don t have to figure out where a variable got assigned Unique, staticall known defition for each occurrence Q: What s the value of a function?