Programming Languages

Similar documents
Programming Languages

Programming Languages

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

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

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

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

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?

Programming Languages

Programming Languages

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

CSE 130 [Winter 2014] Programming Languages

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

Programming Languages

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

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

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

Programming Languages

Recap: ML s Holy Trinity

Begin at the beginning

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

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

Programming Languages

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

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

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

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

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

Recap: Functions as first-class values

CSE 130 [Spring 2014] Programming Languages

Programming Languages

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

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

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

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

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

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

Programming Languages

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

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Which of the following expressions has the type int list?

Programming Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Introduction to OCaml

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

Lecture #23: Conversion and Type Inference

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

Variables and Bindings

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

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

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

Programming Languages

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

Exercises on ML. Programming Languages. Chanseok Oh

Note that pcall can be implemented using futures. That is, instead of. we can use

CMSC 330, Fall 2013, Practice Problems 3

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

Functional Programming

Programming Languages and Compilers (CS 421)

CSc 372 Comparative Programming Languages

CSCI-GA Scripting Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Programming in Standard ML: Continued

Thinking Induc,vely. COS 326 David Walker Princeton University

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

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

CSE 341 Section 5. Winter 2018

Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Functional Programming. Pure Functional Programming

SML A F unctional Functional Language Language Lecture 19

Chapter 15. Functional Programming Languages

CS 11 Haskell track: lecture 1

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Fall Lecture 3 September 4. Stephen Brookes

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.

CS 321 Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

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

Chapter 15. Functional Programming Languages

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

CS 320: Concepts of Programming Languages

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

F28PL1 Programming Languages. Lecture 14: Standard ML 4

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

Case by Case. Chapter 3

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

Data Types The ML Type System

Type Checking and Type Inference

Transcription:

CSE 130: Fall 2009 Programming Languages Lecture 2: A Crash Course in ML News On webpage: Suggested HW #1 PA #1 (due next Fri 10/9) Technical issues installing Ocaml - should be resolved soon! Ranjit Jhala UC San Diego Please post questions to WebCT Today: A crash course in ML contd Type Errors Complex types: Product (tuples) (2+3) ( a = b ) pq ^ 9 (2+2, 7>8); (4,false) (2 + a ) Untypable expression is rejected No casting, No coercing Fancy algorithm to catch errors ML s single most powerful feature (why?) int * bool

Complex types: Product (tuples) Recap: ML s holy trinity ession Value (9-3, ab ^ cd,(2+2,(,7>8)) (6, abcd,(4,false)) Triples, Nesting: (int * string * (int * bool)) Everything is an expression Nest tuples in tuples Type Everything is an expression Everything has a value Everything has a type Recap: Interacting with ML Read-Eval-Print Loop Repeat: 1. System reads expression e 2. System evaluates e to get value v 3. System prints value v and type t What are these expressions, values and types? - int, bool, products (tuples), lists Complex types: Lists []; [] a list [1;2;3]; [1;2;3] int list [1+1;2+2;3+3;4+4]; [2;4;6;8] int list [ a ; b ; c ^ d ]; [ a ; b ; cd ] string list [(1, a ^ b );(3+4, c )]; )]; [(1, ab );(7 );(7, c )] (int*string) list [[1];[2;3];[4;5;6]]; [[1];[2;3];[4;5;6]]; (int list) list Unbounded size Can have lists of anything (e.g. lists of lists) But

Complex types: Lists [1; pq ]; All elements must have same type Complex types: Lists List operator Cons :: 1::[]; [1] int list 1::[2;3]; [1;2;3] int list a ::[ b ; c ]; [ a ; b ; c ] string list Can only cons element to a list of same type 1::[ b ; cd ]; Complex types: Lists List operator Append @ Complex types: Lists List operator head hd [1;2]@[3;4;5]; [1;2;3;4;5] int list hd [1;2]; 1 int [ a ]@[ b ]; [ a ; b ] string list hd ([ a ]@[ b ]); a string []@[1]; [1] int list Can only append two lists 1 @ [2;3]; Only take the head a nonempty list hd []; of the same type [1] @ [ a ; b ];

Complex types: Lists List operator tail tl tl [1;2;3]; [2;3] int list tl ([ a ]@[ b ]); [ b ] string list Only take the tail of nonempty list tl []; Recap: Tuples vs. Lists? What s the difference? Tuples: Different types, but fixed number: (3, abcd ) (int * string) pair = 2 elts (3, abcd,(3.5,4.2)) (int * string * (float * float)) triple = 3 elts Lists: Same type, unbounded number: Syntax: [3;4;5;6;7] int list Tuples = comma Lists = semicolon So far, a fancy calculator what do we need next? Variables and bindings let x = e; Bind the value of expression e to the variable x expression e to the variable x # let x = 2+2;; val x : int = 4

Variables and bindings Later declared expressions can use x Most recent bound value used for evaluation # let x = 2+2;; val x : int = 4 # let y = x * x * x;; val y : int = 64 # let z = [x;y;x+y];; val z : int list = [4;64;68] 68] # Variables and bindings Undeclared variables (i.e. without a value binding) are not accepted! # let p = a + 1; Characters 8-9: let p = a + 1 ;; ^ Unbound value a Catches many bugs due to typos Local bindings for expressions using temporary variables let tempvar = x + 2 * y in tempvar * tempvar ;; 17424 tempvar is bound only inside expr body from in ;; Not visible ( not in scope ) outside int Binding by Pattern-Matching Simultaneously bind several variables # let (x,y,z) = (2+3, a ^ b, 1::[2]);; val x : int = 5 val y : string = ab val z : int list = [1;2]

Binding by Pattern-Matching But what of: # let h::t = [1;2;3];; Warning P: this pattern-matching not exhaustive. val h : int = 1 val t : int list = [2;3] Why is it whining? # let h::t = []; Exception: Match_failure # let l = [1;2;3]; val l = [1;2;3]: list - val h::t = l; Warning: Binding not exhaustive val h = 1 : int val t = [2;3] : int In general l may be empty (match failure!) Another useful early warning Next class: functions, but remember ession Everything is an expression Everything has a value Everything has a type Type Value A function is a value! Complex types: Functions! A Problem fun x -> x+1;; fun x -> x+1;; int -> int int -> int # let inc = fun x -> x+1 ; val inc : int -> int = # inc 0; val it : int = 1 # inc 10; val it : int = 11 How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate expr Can functions only have a single parameter? How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate expr

A Solution: Simultaneous Binding fun (x,y) -> x<y; (int * int) -> bool Can functions only have a single parameter? How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate expr Another Solution fun x -> fun y-> x<y; int -> (int -> bool) Whoa! A function can return a function # let lt = fun x -> y -> x < y ; val lt : int -> int -> bool = # let is5lt = lt 5; val is5lt : int -> bool = ; # is5lt 10; # is5lt 2; And how about fun f -> fun x -> not(f x); ( a ->bool) -> ( a -> bool) A function can also take a function argument # let neg = fun f -> fun x -> not (f x); val lt : int -> int -> bool = # let is5gte = neg is5lt; val is5gte : int -> bool = # is5gte 10; # is5gte 2; (* odd, even *) A shorthand for function binding # let neg = fun f -> fun x -> not (f x); # let neg f x = not (f x); val neg : int -> int -> bool = # let is5gte = neg is5lt; val is5gte : int -> bool = ; # is5gte 10; # is5gte 2;

Put it together: a filter function If arg matches this pattern then use this - let rec filter f l = match l with [] -> [] (h::t)-> if f h then h::(filter f t) else (filter f t);; val filter : ( a->bool)-> a list-> a lisi) = # let list1 = [1;31;12;4;7;2;10];; # filter is5lt list1 ;; val it : int list = [31;12;7;10] # filter is5gte list1;; val it : int list = [1;4;2] # filter even list1;; val it : int list = [12;4;2;10] Put it together: a partition function # let partition f l = (filter f l, filter (neg f) l); val partition :( a->bool)-> a list-> a lisi * a list = # let list1 = [1,31,12,4,7,2,10]; - # partition is5lt list1 ; val it : (int list * int list) = ([31,12,7,10],[1,2,10] # partition even list1; val it : (int list * int list) = ([12,4,2,10],[1,31,7]) A little trick # 2 <= 3;; val it : bool = true # ba <= ab ;; val it : bool = false # let lt = (<) ;; val it : a -> a -> bool = # lt 2 3;; # lt ba ab ;; Put it together: a quicksort function let rec sort xs = match xs with [] -> [] (h::t) -> let (l,r) = partition ((<) h) t in (sort l)@(h::(sort r)) ;; # let is5lt = lt 5; val is5lt : int -> bool = ; # is5lt 10; # is5lt 2; Now, lets begin at the beginning