So what does studying PL buy me?

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

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. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Programming Languages

Programming Languages

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

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

Programming Languages

Programming Languages

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2013

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE : Fall 2017

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

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

Programming Languages

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2014

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

CSE 130 [Spring 2014] Programming Languages

CSE 130 [Winter 2014] Programming Languages

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 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

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

Programming Languages

Begin at the beginning

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

Recap: ML s Holy Trinity

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

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

CSCI-GA Scripting Languages

Recap: Functions as first-class values

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Programming Languages

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

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

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

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

Programming Languages

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

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

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

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

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

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

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

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

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Functional Programming. Pure Functional Programming

Chapter 15. Functional Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

CS558 Programming Languages

Exercises on ML. Programming Languages. Chanseok Oh

Functional Programming

Example Scheme Function: equal

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Variables and Bindings

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

The Environment Model. Nate Foster Spring 2018

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

The Environment Model

Functional Programming. Pure Functional Programming

Data Types The ML Type System

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Chapter 15. Functional Programming Languages

Functional Programming and Haskell

CS 360 Programming Languages Interpreters

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 11 Haskell track: lecture 1

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

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

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

Programming Languages, Summary CSC419; Odelia Schwartz

Scope. Chapter Ten Modern Programming Languages 1

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CSC 533: Organization of Programming Languages. Spring 2005

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

SOFTWARE ARCHITECTURE 6. LISP

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Programmiersprachen (Programming Languages)

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Functional Programming. Introduction To Cool

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CS558 Programming Languages. Winter 2013 Lecture 3

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

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

Chapter 11 :: Functional Languages

Transcription:

So what does studying PL buy me? Enables you to better choose the right language but isn t that decided by libraries, standards, and my boss? Yes. Chicken-and-egg. My goal: educate tomorrow s tech leaders & bosses So you ll make considered, informed choices

So what does studying PL buy me? Makes you look at things in different ways, think outside of the box Knowing language paradigms other than traditional ones will give you new tools to approach problems, even if you are programming in Java

PL Dimensions Wide variety of programming languages How do they differ? along certain dimensions... What are these dimensions?

Dimension: Syntax Languages have different syntax But the difference in syntax can be superficial C# and Java have different syntax, but are very similar In this class, will look beyond superficial syntax to understand the underlying principles

Dimension: Computation model Functional: Lisp, OCaml, ML Imperative: Fortran, Pascal, C Object oriented: Smalltalk, C++, Java, C# Constraint-based: Prolog, CLP(R)

Dimension: Memory model Explicit allocation-deallocation: C, C++ Garbage collection: Smalltalk, Java, C# Regions: safe versions of C (e.g. Cyclone) allocate in a region, deallocate entire region at once more efficient than GC, but no dangling ptrs

Dimension: Typing model Statically typed: Java, C, C++, C# Dynamically typed: Lisp, Scheme, Perl, Smalltalk Strongly typed (Java) vs. weakly typed (C, C++)

Dimension: Execution model Compiled: C, C++ Interpreted: Perl, shell scripting PLs Hybrid: Java Is this really a property of the language? Or the language implementation? Depends...

So many dimensions Yikes, there are so many dimensions! How to study all this! One option: study each dimension in turn In this course: explore the various dimensions by looking at a handful of PLs

Weekly Programming Assignments Unfamiliar languages + Unfamiliar environments Start Early!

Weekly Programming Assignments Forget Java, C, C++ other 20 th century PLs Don t complain that Ocaml is hard that Ocaml is @!#@%

Immerse yourself in new language It is not. Free your mind.

Enough with the small talk?

Say hello to OCaml void sort(int arr[], int beg, int end){ if (end > beg + 1){ int piv = arr[beg]; int l = beg + 1; int r = end; while (l!= r-1){ if(arr[l] <= piv) l++; else swap(&arr[l], &arr[r--]); } if(arr[l]<=piv && arr[r]<=piv) l=r+1; else if(arr[l]<=piv && arr[r]>piv) {l++; r--;} else if (arr[l]>piv && arr[r]<=piv) swap(&arr[l++], &arr[r--]); else r=l-1; swap(&arr[r--], &arr[beg]); sort(arr, beg, r); sort(arr, l, end); } } Quicksort in C let rec sort l = match l with [] -> [] (h::t) -> let(l,r)= List.partition ((<=) h) t in (sort l)@h::(sort r) Quicksort in Ocaml

Why readability matters sort=:(($:@(<#[),(=#[),$:@(>#[))({~?@#))^: (1:<#) Quicksort in J

Say hello to OCaml let rec sort l = match l with [] -> [] (h::t) -> let (l,r)= List.partition ((<=) h) t in (sort l)@h::(sort r) Quicksort in OCaml

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

History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language to manipulate Theorems/Proofs Several dialects: Standard ML (of New Jersey) Original syntax O Caml: The PL for the discerning hacker French dialect with support for objects State-of-the-art Extensive library, tool, user support (.NET)

ML s holy trinity Expression Value Type Everything is an expression Everything has a value Everything has a type

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?

Base type: Integers 2 2 2+2 4 2 * (9+10) 38 2 * (9+10) -12 26 int Complex expressions using operators :(why the quotes?) +, -, * div, mod

Base type: Strings ab ab ^ xy ab abxy string Complex expressions using operators :(why the quotes?) Concatenation ^

Base type: Booleans true false true false 1 < 2 true aa = pq ( aa = pq ) && (1<2) ( aa = pq ) && (1<2) false false true bool Complex expressions using operators : Relations : =, <, <=, >= &&,, not

Type Errors (2+3) ( a = b ) pq ^ 9 (2 + a ) Untypable expression is rejected No casting or coercing Fancy algorithm to catch errors ML s single most powerful feature

Complex types: Product (tuples) (2+2, 7>8); (4,false) int * bool

Complex types: Product (tuples) (9-3, ab ^ cd,(2+2,7>8)) (6, abcd,(4,false)) (int * string * (int * bool)) Triples, Nesting: Everything is an expression, nest tuples in tuples

Complex types: Lists []; [] a list Unbounded size Can have lists of anything But [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, c )] (int*string) list [[1];[2;3];[4;5;6]]; [[1];[2;3];[4;5;6]]; (int list) list

Complex types: Lists [1; pq ]; All elements must have same type

Complex types: Lists List operator Cons :: 1::[]; [1] int list 1::[2]; [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 @ [1;2]@[3;4;5]; [1;2;3;4;5] int list [ a ]@[ b ]; [ a ; b ] string list []@[1]; [1] string list Can only append two lists 1 @ [2;3]; of the same type [1] @ [ a ; b ];

Complex types: Lists List operator head hd hd [1;2]; 1 int hd ([ a ]@[ b ]); a string Only take the head a nonempty list hd [];

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?

Recap: Tuples vs. Lists? What s the difference? Tuples: Different types, but fixed number: Lists: pair = 2 elts triple = 3 elts Same type, unbounded number: Syntax: (3, abcd ) (int * string) (3, abcd,(3.5,4.2)) (int * string * (real * real)) [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 # 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] #

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 int tempvar is bound only inside expr body from in ;; Not visible ( in scope ) outside

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 : functions, but remember Expression Value Type Everything is an expression Everything has a value Everything has a type A function is

Complex types: Functions! Parameter (formal) Body Expr fun x -> x+1;; fn int -> int # let inc = fun x -> x+1 ; val inc : int -> int = fn # 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 Body expr

A Problem Parameter (formal) Body Expr fun x -> x+1;; fn int -> int Can functions only have a single parameter? How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr

A Solution: Simultaneous Binding Parameter (formal) Body Expr fun (x,y) -> x<y; fn (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 Body expr

Another Solution Parameter (formal) Body Expr fun x -> fun y -> x<y; fn int -> (int -> bool) Whoa! A function can return a function # let lt = fun x -> fn y -> x < y ; val lt : int -> int -> bool = fn # let is5lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false;

And how about Parameter (formal) Body Expr fun f -> fun x -> not(f x); fn ( 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 = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; (* 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 = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn; # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true;

Put it together: a filter function If arg matches this pattern then use this Body Expr - 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 list = fn # 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,2,10] # 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 list * a list = fn # 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 = fn # lt 2 3;; val it : bool = true; # lt ba ab ;; val it : bool = false; # let is5lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false;

Put it together: a quicksort function let rec sort l = match l with [] -> [] (h::t) -> let (l,r) = partition ((<) h) t in (sort l)@(h::(sort r)) ;;