Programming Languages

Similar documents
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?

Programming Languages

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

Programming Languages

x = e Python tries to avoid overwrites i Python tries to avoid overwrites next Monday Programming Assignment #7 on Prolog quarter Assignment Revisited

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

Programming Languages

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Programming Languages

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

CS558 Programming Languages

Programming Languages

Recap: Functions as first-class values

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CS558 Programming Languages

Programming Languages

Variables and Bindings

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

Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

CMSC 330: Organization of Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS558 Programming Languages

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

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

CS558 Programming Languages

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

Syntax Errors; Static Semantics

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

The role of semantic analysis in a compiler

Ruby: Introduction, Basics

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CS558 Programming Languages

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Python debugging and beautification

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Try and Error. Python debugging and beautification

CS558 Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Programming Languages

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

CSE 130 : Fall Programming Languages. Lecture 11: Ranjit Jhala UC San Diego. programming

CSE341, Spring 2013, Final Examination June 13, 2013

Begin at the beginning

Overview of Semantic Analysis. Lecture 9

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

Programming Languages

CS61A Notes Week 13: Interpreters

Programming language design and analysis

Lecture 16: Static Semantics Overview 1

Scope. Chapter Ten Modern Programming Languages 1

Programming Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

CSE : Python Programming

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 360 Programming Languages Interpreters

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Scope, Functions, and Storage Management

CS 415 Midterm Exam Spring 2002

DRAWING ENVIRONMENT DIAGRAMS

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

Environments

Programming Languages

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Programming Languages

Types for Flexible Objects

Guaranteeing memory safety in Rust

CSE 130: Spring 2012

CS558 Programming Languages. Winter 2013 Lecture 3

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

Programming Languages Lecture 15: Recursive Types & Subtyping

Ruby: Introduction, Basics

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CSE : Python Programming

Lecture 02, Fall 2018 Friday September 7

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Names, Bindings, Scopes

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

Transcription:

CSE 130 : Spring 2011 Programming Languages Lecture 13: What s in a Name? Ranjit Jhala UC San Diego

Next: What s in a name? More precisely: How should programmer think of data What does a variable x really mean?

What s in a name? ML (or Functional Languages) Name refers to a Value Binding maps Names to Values Environment list of bindings Environment can be extended Environment can t be changed

Data model in functional PL Vars = names in phonebook Evaluation = Most recent Environment frozen in function value behavior of function cannot be changed easier reasoning

Data model in OO languages Variables point to objects Objects = boxes with data inside X pumpkin Y 3.142 Z [1,2,3]

Namespaces Manage variable names in Python Similar to, but different from Environments Core PL concept, unifies many ideas We will see very important differences

Ok, but what IS a namespace? A mapping from names to objects X pumpkin Y 3.142 Z [1,2,3]

Namespaces vs. Environments Both are maps from variables to something Namespace Environment What s the difference? 1.Assignment 2.Updates/Mutation

1. Assignment Basic operation in Imperative PL: x = e 1.Compute object corresponding to e 2.Change the name x to refer to object

Simple example i,s = 0,0 while (i <= 3): i,s = i+1, s+i Iter 0 i s 0 0 Same name s - points to different objects - namespace is not extended

Simple example i,s = 0,0 while (i <= 3): i,s = i+1, s+i Iter 0 i s 0 0 Iter 1 1 0 Same name s - points to different objects - namespace is not extended

Simple example i,s = 0,0 while (i <= 3): i,s = i+1, s+i Iter 0 i s 0 0 Iter 1 1 0 Iter 2 Same name s - points to different objects - namespace is not extended 2 1

1. Assignment Basic operation in Imperative PL: x = e 1.Compute object corresponding to e 2.Change the name x to refer to object Assignment: changes box that name refers to

2. Update/Mutation Change what s inside the box (object) - Not with immutable objects - eg. integers - But with mutable objects - eg. arrays, lists, dictionaries >>> x = [100,200] >>> x [100, 200] >>> x[0] = "gobble gobble" >>> x ['gobble gobble', 200] How is it different from build a new box with updated value inside?

Aliasing Two or more names refer to same object Peter Parker Spider-Man

Aliasing Two or more names refer to same object >> x = [100,200] >> y = x X Y [100,200]

Aliasing and Update Two or more names refer to same object >> x = [100,200] >> y = x >> y[0] = gobble gobble" >> x X Y [ gobble,200] [100,200]

Aliasing Two or more names refer to same object >> x = [100,200] >> y = x >> y[0] = gobble gobble" >> x [ gobble gobble,200] X Y [ gobble,200] [100,200] If multiple names refer to same object, update affects values of all names

Aliasing Does not happen in Ocaml/Functional PLs actually it does happen (where?) but not exposed to the (130) programmer Does happen in every imperative PL Java, Python: names point to objects C: names point to memory cells

Aliasing Good because? Bad because?

Namespaces everywhere Namespace = map from names to objects Notion of namespace pervades Python Can create namespace, Can name a namespace, Can peep inside a namespace (see whats bound) Go to code!

Creating Namespaces a.py x = 22 y = this sentence is false X 22 Y this a b.py x = pumpkin y = 3.142 X pumpkin Y 3.142 b >>> import a >>> a.x 22

Namespaces X a Y this 22 pumpkins X Y 3.142 b Different names can point to same object!

Namespaces For two namespaces a, b: names inside unrelated names in different spaces a.x : attribute/name x in space a b.x : attribute/name x in space a X 22 Y X this pumpkin Y 3.142 a b Different names can point to same object!

Creating Namespaces: Fun Calls x = 10 def f(y): y = y + x return y X 10 f <fun> y 20 f(x) f(x) Call-by-Value: globals New local namespace for call y bound to same object (value) as arg x x binding unchanged by call In this case, after call, local namespace disappears

Creating Namespaces: Fun Calls x = 10 def f(y): y = y + x return y X 10 f <fun> y 20 f(x) f(x) globals Questions: Why new local namespace (not just stack)? What s the deal with x not declared/bound in f? When do we need to freeze a namespace?

Creating Namespaces: Fun Calls 2 y = 0 x = [10] def f(y): z = len(x)+y return z f(5) Static Scoping Lookup at runtime Not compile time Missing z added len <fun> y 0 x [10] f <fun> y 5 z 6 f(5) globals builtins

Creating Namespaces: Fun Calls 3 >>> def g(y): >>> return y + n >>> g(5) NameError: global name 'n' is not defined What happened? Looks for n at run-time,when g is called Can t find n in local, global, builtins Throws run-time error

Creating Namespaces: Fun Calls 3 >>> def g(y): >>> return y + n >>> g(5) NameError >>> n = 10 >>> g(5) 15 What happened? Looks for n at run-time,when g is called Finds n in global, returns 15 Here n is a free variable of g Needs to be bound in some enclosing scope

Aaargh! >>> def g(y): >>> return y + n >>> g(5) NameError >>> n = 10 >>> g(5) 15 >>> n = 100 >>> g(5) 105 Changed behavior after definition whether or not fun works depends on what we did after func-definition Change I/O behavior too Unlike ML, no new binding: Just change what n is bound to Be careful with free variables!

Python tries to avoid overwrites >>> n 100 >>> def f(): >>> n = smash >>> print n >>> >>> f() smash >>> n 100 Python tries to ensure you don t overwrite outer variables How? - unlike C/Java - assignment different from reads - no variable declarations - assignment = declaration!

Python tries to avoid overwrites Assignment Revisited x = e 1.Compute object corresponding to e 2.Change the name x to refer to object in the current namespace (added if missing)

Python tries to avoid overwrites >>> n 100 >>> def f(): >>> n = smash >>> print n >>> >>> f() smash >>> n 100

What happens? >>> x = 10 >>> def g(): >>> x = x + 1 >>> print x >>> >>> g() >>> x

What happens? >>> x = 10 >>> def g(): >>> global x >>> x = x + 1 >>> print x >>> >>> g() >>> x

What happens? >>> x = [10] >>> def g(): >>> x[0] = abc >>> print x >>> >>> g() >>> x Remember: Assignment v. Mutation!

What happens? (Hint: Closures!) >>> x = [10] >>> def f(y): >>> def h(z): >>> return (y+x[0]+z) >>> return h >>> >>> foo = f(5) >>> foo <function object> >>> foo(100) >>> 115 >>> foo1 = f(-5) >>> foo1(100) 105