Programming Languages

Similar documents
Programming Languages

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

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

Programming Languages

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

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

Programming Languages. Datatypes

Programming Languages

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

Programming Languages

Programming Languages

Programming Languages

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

Programming Languages

Programming Languages

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

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

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

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

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

Begin at the beginning

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

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

Homework 3 COSE212, Fall 2018

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.

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

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

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

CSE 341: Programming Languages

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

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

Programming Languages and Techniques (CIS120)

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

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

Programming Languages Lecture 15: Recursive Types & Subtyping

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

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

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

Programming Languages

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

So what does studying PL buy me?

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. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

CS558 Programming Languages

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CSE 341 Section 5. Winter 2018

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

Lecture #23: Conversion and Type Inference

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

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

Lecture 3: Recursion; Structural Induction

Programming Languages

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

CSE 341: Programming Languages

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

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?

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

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

ITT8060: Advanced Programming (in F#)

Which of the following expressions has the type int list?

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Fall 2015

CSE 341: Programming Languages

Pointers in C/C++ 1 Memory Addresses 2

Exercises on ML. Programming Languages. Chanseok Oh

Programming Languages

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

Logic - CM0845 Introduction to Haskell

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

User-defined Functions. Conditional Expressions in Scheme

Fall 2018 Lecture N

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

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Spring 2015

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

CS558 Programming Languages

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Lecture 5 Abstract Data Types

Functional Programming

Thinking Induc,vely. COS 326 David Walker Princeton University

Programming Languages

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

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

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Introduction to SML Getting Started

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

Data Types The ML Type System

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting

Programming Languages

Programming Assignment 2

Transcription:

CSE 130 : Fall 2008 Programming Languages Lecture 6: Datatypes t and Recursion Ranjit Jhala UC San Diego News PA 2 Due Fri (and PA3 up) PA3 is a bit difficult, but do it yourself, or repent in the final RJ out of town on Thu WM will lecture and deliver the quiz WM will hold RJ s regular OH (1-3p @ CSE 250) Quiz #1, #2 graded pickup in section tomorrow What about more complex data? What about more complex data? Expressions Values We ve seen some base types and values: Integers, Floats, Bool, String etc. Types Many kinds of expressions: 1. Simple 2. Variables 3. Functions Some ways to build up types: Products (tuples), records, lists Functions Design Principle: Orthogonality 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) Age (integer) DOB (int-int-int) DOB of int*int*int Address (string) Height (real) Height of float Alive (boolean) 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 # let a2 = Height 5.83;; val a2 : attrib = Height 5.83 # let year = 1977 ;; val year : int = 1977 # let a3 = DOB (9,8,year) ;; val a3 : attrib = DOB (9,8,1977) # let a_l = [a1;a2;a3];; val a3 : attrib list = type attrib = DOB of int*int*int Height of float Phone of int*int 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 = DOB of int*int*int Height of real Phone of int*int Email of string; How to tell whats in the box? type attrib = match e with Name s -> e1 Age i -> e2 DOB of int*int*int DOB (m,d,y) -> e3 Address addr -> e4 Height of float Height h -> e5 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 matching thi 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 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 C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en 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 Type rules? e1, e2,,enen must have same type T and T is type of whole expression 1. Simultaneous test-extract-bind 2. Compile-time checks for: missed cases: ML warns if you miss a case redundant cases: ML warns if a case never matches What about Recursive types? type int_list = Cons of int * int_list Think about this! What are values of int_list? Cons(1,Cons(2,Cons(3,))) Cons(2,Cons(3,)) Cons 1, Cons 2, Cons(3,) Cons 3, Lists aren t built-in in! datatype int_list = Cons of int * int_list 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

Some functions on Lists : Length Base pattern let rec len l = match l with Base Expression Ind pattern -> 0 Cons(h,t) -> 1 + (len t) Inductive Expression Some functions on Lists : Append let rec append (l1,l2) = Base pattern Base Expression Ind pattern Inductive Expression let rec len l = match l with -> 0 Cons(_,t) -> 1 + (len t) let rec len l = match l with Cons(_,t) -> 1 + (len t) _ -> 0 Find the right induction strategy Base case: pattern + expression Induction case: pattern + expression Matches everything, no binding Pattern-matching in order - Must match with 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 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? 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 Random Art from Expressions PA #2 Build more funky expressions, evaluate them, to produce: Whats a ML function for evaluating such expressions?