Variables and Bindings

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

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: Functions as first-class values

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

Programming Languages

Programming Languages

Begin at the beginning

Programming Languages

Programming Languages

Programming Languages

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

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

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

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

Scope, Functions, and Storage Management

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

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

Programming Languages

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

Programming Languages

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

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

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

Programming Languages

Fall Lecture 3 September 4. Stephen Brookes

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Lambda Calculus.

CSE341 Autumn 2017, Final Examination December 12, 2017

Implementing Recursion

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

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

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

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

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

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

So what does studying PL buy me?

CSE341, Spring 2013, Final Examination June 13, 2013

Standard ML. Curried Functions. ML Curried Functions.1

(Refer Slide Time: 4:00)

Introduction to SML Getting Started

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Programming Languages and Compilers (CS 421)

Variables. Substitution

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Parsing Scheme (+ (* 2 3) 1) * 1

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Programming Languages

G Programming Languages - Fall 2012

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

CMSC 330: Organization of 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?

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

Comp 311: Sample Midterm Examination

CS558 Programming Languages

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Tracing Ambiguity in GADT Type Inference

CSE 341 Section 5. Winter 2018

Functional programming Primer I

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE 130 [Winter 2014] Programming Languages

Semantic Analysis and Type Checking

The role of semantic analysis in a compiler

FOFL and FOBS: First-Order Functions

CMSC330. Objects, Functional Programming, and lambda calculus

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

Functional Programming. Pure Functional Programming

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

Programming Project #3: Syntax Analysis

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Turtles All The Way Down

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Scope and Parameter Passing 1 / 19

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

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

Programs as data first-order functional language type checking

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

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

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS153: Compilers Lecture 14: Type Checking

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

Programming Language Concepts, cs2104 Lecture 04 ( )

Scope. Chapter Ten Modern Programming Languages 1

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS 314 Principles of Programming Languages

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 (!

CSCI-GA Scripting Languages

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Transcription:

Net: Variables

Variables and Bindings Q: How to use variables in ML? Q: How to assign to a variable? # let = 2+2;; val : int = 4 let = e;; Bind the value of epression e to the variable

Variables and Bindings # let = 2+2;; val : int = 4 # let y = * * ;; val y : int = 64 # let z = [;y;+y];; val z : int list = [4;64;68] Later declared epressions can use Most recent bound value used for evaluation Sounds like C/Java? NO!

Environments ( Phone Book ) How ML deals with variables Variables = names Values = phone number y z 4 : int 64 : int [4;64;68] : int list 8 : int

Environments and Evaluation ML begins in a top-level environment Some names bound let = e;; ML program = Sequence of variable bindings Program evaluated by evaluating bindings in order 1. Evaluate epr e in current env to get value v : t 2. Etend env to bind to v : t (Repeat with net binding)

Environments Phone book Variables = names Values = phone number 1. Evaluate: Find and use most recent value of variable 2. Etend: Add new binding at end of phone book

Eample # let = 2+2;; val : int = 4 4 : int # let y = * * ;; val y : int = 64 # let z = [;y;+y];; val z : int list = [4;64;68] # let = + ;; val : int = 8 New binding! y y z y z 4 : int 64 : int 4 : int 64 : int [4;64;68] : int list 4 : int 64 : int [4;64;68] : int list 8 : int

Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end How is this different from C/Java s store? # let = 2+2;; val : int = 4 4 : int # let f = fun y -> + y; val f : int -> int = fn # let = + ; val : int = 8 # f 0; val it : int = 4 4 : int f fn <code, >: int->int New binding: No change or mutation Old binding frozen in f

Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end How is this different from C/Java s store? # let = 2+2;; val : int = 4 4 : int # let f = fun y -> + y; val f : int -> int = fn # let = + ; val : int = 8 # f 0; val it : int = 4 4 : int f fn <code, >: int->int 4 : int f fn <code, >: int->int 8 : int

Environments 1. Evaluate: Use most recent bound value of var 2. Etend: Add new binding at end How is this different from C/Java s store? # let = 2+2;; val : int = 4 # let f = fun y -> + y; val f : int -> int = fn # let = + ; val : int = 8 # f 0; val it : int = 4 Binding used to eval (f ) 4 : int f fn <code, >: int->int 8 : int Binding for subsequent

Cannot change the world Cannot assign to variables Can etend 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 = 2+2;; val : int = 4 # let f = fun y -> + y;; val f : int -> int = fn # let = + ;; val : int = 8; # f 0;; val it : int = 4 Binding used to eval (f ) 4 : int f fn <code, >: int->int 8 : int Binding for subsequent

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

Eamples of no sharing Remember: No addresses, no sharing. Each variable is bound to a fresh instance of a value Tuples, Lists Efficient implementation without sharing? There is sharing and pointers but hidden from you Compiler s job is to optimize code Efficiently implement these no-sharing semantics Your job is to use the simplified semantics Write correct, cleaner, readable, etendable systems

Recap: Environments Phone book Variables = names Values = phone number 1. Evaluate: Find and use most recent value of variable 2. Etend: let = e ;; Add new binding at end of phone book

Net: Functions Epressions Values Types

Functions epr Functions are values, can bind using let let fname = fun -> e ;; Problem: Can t define recursive functions! fname is bound after computing rhs value no (or old ) binding for occurences of fname inside e let rec fname = e ;; Occurences of fname inside e bound to this definition let rec fac = if <=1 then 1 else *fac (-1)

Functions Type f : T1 -> T2 F takes a value of type T1 and returns a value of type T2

Functions Values Two questions about function values: What is the value: 1. of a function? 2. of a function application (call)? (e1 e2)

Values of functions: Closures Body epression not evaluated until application but type-checking takes place at compile time i.e. when function is defined Function value = <code + environment at definition> closure # let = 2+2;; val : int = 4 # let f = fun y -> + y;; val f : int -> int = fn # let = + ;; val : int = 8 # f 0;; val it : int = 4 Binding used to eval (f ) 4 : int f fn <code, >: int->int 8 : int Binding for subsequent

Values of function application Application: fancy word for call (e1 e2) apply the argument e2 to the (function) e1 Application Value: 1. Evaluate e1 in current env to get (function) v1 v1 is code + env code is (formal + body e), env is E 2. Evaluate e2 in current env to get (argument) v2 3. Evaluate body e in env E etended by binding to v2

Eample 1 let = 1;; let f y = + y;; let = 2;; let y = 3;; f ( + y);;

Eample 1 let = 1;; let f y = + y;; let = 2;; let y = 3;; f ( + y);;

Eample 2 let = 1;; let f y = let = 2 in fun z -> + y + z ;; let = 100;; let g = (f 4);; let y = 100;; (g 1);;

Eample 2 let = 1;; let f y = let = 2 in fun z -> + y + z ;; let = 100;; let g = (f 4);; let y = 100;; (g 1);;

Eample 3 let f g = let = 0 in g 2 ;; let = 100;; let h y = + y;; f h;;

Static/Leical Scoping For each occurrence of a variable, Unique place in program tet where variable defined Most recent binding in environment Static/Leical: Determined from the program tet Without eecuting the programy Very useful for readability, debugging: Don t have to figure out where a variable got assigned Unique, statically known definition for each occurrence

Alternative: dynamic scoping let = 100 let f y = + y let g = f 0 let z = g 0 (* value of z? *)