Programming Languages

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

Programming Languages

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

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

Programming Languages

Programming Languages

Programming Languages. Next: Building datatypes. Suppose I wanted. Next: Building datatypes 10/4/2017. Review so far. Datatypes.

Programming Languages. Datatypes

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

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

Recap: ML s Holy Trinity

CSE 130 [Winter 2014] Programming Languages

Programming Languages

Begin at the beginning

Programming Languages

Programming Languages

Variables and Bindings

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

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

Recap: Functions as first-class values

Programming Languages

Programming Languages

Programming Languages

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

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

Programming Languages

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

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

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

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

CSE 341 Section 5. Winter 2018

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

So what does studying PL buy me?

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

Project 2: Scheme Interpreter

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS558 Programming Languages

Mini-ML. CS 502 Lecture 2 8/28/08

CS558 Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

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

CS558 Programming Languages

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

CSE 341: Programming Languages

CSE 341: Programming Languages

sum: tree -> int sum_leaf: tree -> int sum_leaf: tree -> int Representing Trees Next: Lets get cosy with Recursion Sum up the leaf values. E.g.

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

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

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

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

Functional Programming

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Data Types The ML Type System

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

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

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

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

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

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

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

SML A F unctional Functional Language Language Lecture 19

Introduction to OCaml

Homework 3 COSE212, Fall 2018

Lecture 15 CIS 341: COMPILERS

CSE341 Autumn 2017, Final Examination December 12, 2017

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

CSE413 Midterm. Question Max Points Total 100

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

Programming Languages

Programming Languages

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

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

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?

CSE 341: Programming Languages

Metaprogramming assignment 3

Programming Languages and Compilers (CS 421)

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

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

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

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Lists. Michael P. Fourman. February 2, 2010

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

Intro. Scheme Basics. scm> 5 5. scm>

Type Checking and Type Inference

Fall Lecture 3 September 4. Stephen Brookes

Lecture #23: Conversion and Type Inference

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

SCHEME AND CALCULATOR 5b

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

Fall 2018 Lecture N

Transcription:

CSE 130 : Winter 2009 Programming Languages News PA 2 out, and due Mon 1/26 5pm Lecture 5: Functions and Datatypes t UC San Diego Recap: Environments Phone book Variables = names Values = phone number Next: Functions Expressions Values 1. Evaluate: Find and use most recent value of variable Types 2. Extend: let x = e Add new binding at end of phone book Q: What s the value of a function?

Functions Values Functions Values Two questions about function values: Two questions about function values: What is the value: What is the value: 1. of a function? 1. of a function? 2. of a function application (call)? (e11 e2) 2. of a function application (call)? (e11 e2) Values of functions: Closures Free (vs. Bound) Variables Body expression 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 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 Binding used to eval (f ) x 4 : int f fn <code, >: int->int x 8 : int Binding for subsequent x let a = 20 let f x = let y = 1 in let g z = y + z in a + (g x) f 0 Inside a function: A bound occurrence: 1. Formal variable 2. Variable bound in let-in x, y, z are bound inside f A free occurrence: Not bound occurrence a is free inside f Environment at definition, frozen inside closure, is used for values of free variables

Nested function bindings Nested function bindings let a = 20 let f x = let a = 1 in let g z = a + z in a + (g x) f 0; Inside a function: A bound occurrence: 1. Formal variable 2. Variable bound in let-in-end x, a, z are bound inside f A free occurrence: Not bound occurrence nothing is free inside f Environment at definition, frozen inside closure, is used for values of free variables let a = 20 let f x = let a = 1+1 in let g z = a + z in a + (g x) f 0; Q: Where do values of bound variables come from? Bound variable values determined when fun evaluated ( executed ) From arguments Local variable bindings Obtained by evaluation Static/Lexical Scoping Static/Lexical Scoping let a = 20 let f x = let y = 10 in let g z = y + z in a + (g x) f 0; For each var, unique place in prog. text where var is defined Static/Lexical Scope Determined by prog text Without executing prog Easy readability, debugging For each occurrence of a variable, there is a unique place in program text where the variable was defined Most recent binding in environment Static/Lexical: Determined from the program text Without executing the program Very useful for readability, debugging: Don t have to figure out where a variable got assigned Don t have to figure out where a variable got assigned Unique, statically known definition for each occurrence

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 x + body e), env is E 2. Evaluate e2 in current env to get (argument) v2 3. Evaluate body e in env E extended by binding x to v2 Example 1 let x = 1 let f y = x + y let x = 2 let y = 3 f (x + y) Example 1 let x = 1 let f y = x + y let x = 2 let y = 3 f (x + y) Example 2 let x = 1 let f y = let x = 2 in fun z -> x + y + z let x = 100 let g =(f 4) g let y = 100 (g 1)

Example 2 let x = 1 let f y = let x = 2 in fun z -> x + y + z let x = 100 let g =(f 4) let y = 100 (g 1) Example 3 let f g = let x = 0 in g 2 let x = 100 let h y = x + y f h Static/Lexical Scoping What about more complex data? For each occurrence of a variable, Unique place in program text where variable defined Most recent binding in environment Expressions Values Static/Lexical: Determined from the program text Without executing 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 Types Many kinds of expressions: l 1. Simple 2. Variables 3. Functions

What about more complex data? We ve seen some base types and values: Integers, Floats, Bool, String etc. Some ways to build up types: Products (tuples), records, lists Functions Design Principle: Orthogonality Don t clutter core language with stuff Few, powerful orthogonal building techniques Put derived types, values, functions in libraries Next: Building datatypes Three key ways to build complex types/values 1. Each-of types Value of T contains value of T1 and a value of T2 2. One-of types Value of T contains value of T1 or a value of T2 3. Recursive Value of T contains (sub)-value of same type T Suppose I wanted a program that processed lists of attributes Name (string) Age (integer) DOB (int-int-int) Address (string) Height (float) Alive (boolean) Phone (int-int) email (string) Many kinds of attributes: too many to put in a record can have multiple l names, addresses, phones, emails etc. Want to store them in a list. Can I? t Constructing Datatypes type t = C1 of t1 C2 of t2 Cn of tn is a new datatype. A value of type t is either: Or Or Or a value of type t1 placed in a box labeled C1 a value of type t2 placed in a box labeled C2 a value of type tn placed in a box labeled Cn

Suppose I wanted Attributes: type attrib = Name(string) Name of string Age (integer) Age of int DOB (int-int-int) DOB of int*int*int Address (string) Address of string Height (real) Height of float Alive (boolean) Alive of bool Phone (int-int) Phone of int*int email (string) Email of string Creating Values How to create values of type attrib? # let a1 = Name Ranjit val x : attrib = Name Ranjit type attrib = # let a2 = Height 5.83 Name of string val a2 : attrib = Height 5.83 # let year = 1977 Age of int val year : int = 1977 DOB of int*int*int # let a3 = DOB (9,8,year) Address of string val a3 : attrib = DOB (9,8,1977) Height of float # let a_l = [a1;a2;a3] Alive of bool val a3 : attrib list = Phone of int*int t Email of string One-of types We ve defined a one-of type named attrib Elements are one of: string, int, int*int*int, float, bool Can create uniform attrib lists Suppose I want a function to print attribs datatype attrib = Name of string Age of int DOB of int*int*int Address of string Height of real Alive of bool Phone of int*int Email of string; How to tell whats in the box? type attrib = match e with Name of string Name s -> e1 Age of int Age i -> e2 DOB of int*int*int DOB (m,d,y) -> e3 Address of string Address addr -> e4 Height of float Height h -> e5 Alive of bool Phone of int*int Alive b -> e6 Email of string Phone (a,n) -> e7 Email e -> e8 Pattern-match expression: check if e is of the form On match: value in box bound to pattern variable thi lt i i l t d matching result expression is evaluated Simultaneously test and extract contents of box

match-with is an Expression match e with Name s -> e1 Age i -> e2 DOB (m,d,y) -> e3 Address addr -> e4 Height h -> e5 Alive b -> e6 Phone (a,n) -> e7 Email e -> e8 match-with is an Expression match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en Type rules? e1, e2,,enen must have same type Which is type of whole expression Benefits of match-with match e with type t = C1 x1 -> e1 C1 of t1 C2 x2 -> e2 C2 of t2 Cn xn -> en Cn of tn What about Recursive types? type int_list = Nil Cons of int * int_list 1. Simultaneous test-extract-bind 2. Compile-time checks for: missed cases: ML warns if you miss a t value redundant cases: ML warns if a case never matches Think about this! What are values of int_list? Cons(1,Cons(2,Cons(3,Nil))) Cons(2,Cons(3,Nil)) Cons(3,Nil) Nil Cons Cons Cons 1, 2, 3, Nil

Lists aren t built-in in! datatype int_list = Nil Cons of int * int_list Some functions on Lists : Length Base pattern let rec len l = match l with Base Expression Ind pattern Nil -> 0 Cons(h,t) -> 1 + (len t) Inductive Expression Lists are a derived type: built using elegant core! 1. Each-of 2. One-of 3. Recursive :: is just a pretty way to say Cons [] is just a pretty way to say Nil let rec len l = match l with Nil -> 0 Cons(_,t) -> 1 + (len t) Matches everything, no binding let rec len l = match l with Cons(_,t) -> 1 + (len t) _ -> 0 Pattern-matching in order - Must match with Nil Some functions on Lists : Append let rec append (l1,l2) = Base pattern Base Expression Ind pattern Find the right induction strategy Base case: pattern + expression Induction case: pattern + expression Inductive Expression Well designed datatype gives strategy null, hd, tl are all functions Bad ML style: More than aesthetics! Pattern-matching better than test-extract: ML checks all cases covered ML checks no redundant cases at compile-time: fewer errors (crashes) during execution get the bugs out ASAP!

Another Example: Calculator We want an arithmetic calculator to evaluate expressions like: 4.0 + 2.9 = 6.9 3.78 592 5.92 = -2.14 (4.0 + 2.9) * (3.78-5.92) = -14.766 Q: Whats a ML datatype for such expressions? Another Example: Calculator We want an arithmetic calculator to evaluate expressions like: 4.0 + 2.9 = 6.9 3.78 592 5.92 = -2.14 (4.0 + 2.9) * (3.78-5.92) = -14.766 Whats a ML function for evaluating such expressions? Random Art from Expressions PA #2 Build more funky expressions, evaluate them, to produce: