OCaml Datatypes. COS 326 David Walker Princeton University

Similar documents
OCaml Datatypes. COS 326 David Walker* Princeton University

Lecture 19: Functions, Types and Data Structures in Haskell

COS 326 David Walker Princeton University

CSE 3302 Programming Languages Lecture 8: Functional Programming

Thinking Induc,vely. COS 326 David Walker Princeton University

A Func'onal Space Model

CMSC 330: Organization of Programming Languages

Inductive Data Types

CMSC 330: Organization of Programming Languages. OCaml Data Types

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

CSCE 314 Programming Languages

CS 457/557: Functional Languages

Programming Languages and Techniques (CIS120)

Topic 7: Algebraic Data Types

Thinking Induc,vely. COS 326 David Walker Princeton University

Haskell Overview II (2A) Young Won Lim 8/9/16

Standard ML. Data types. ML Datatypes.1

Advanced features of Functional Programming (Haskell)

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 320: Concepts of Programming Languages

Programming Languages and Techniques (CIS120)

Haskell Overview III (3A) Young Won Lim 10/4/16

Func%onal Decomposi%on

CS 11 Ocaml track: lecture 2

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

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

Haskell Overview II (2A) Young Won Lim 8/23/16

List Functions, and Higher-Order Functions

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

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

Modules and Representation Invariants

An introduction introduction to functional functional programming programming using usin Haskell

Programming Languages Fall 2013

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

Algebraic Types. Chapter 14 of Thompson

Programming Languages and Techniques (CIS120)

Exercise 1 ( = 24 points)

Functional Programming in Haskell Part I : Basics

Exercise 1 ( = 22 points)

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

Introduction to OCaml

A Functional Evaluation Model

Advanced Programming Handout 3

Review. Advanced Programming Handout 3. Review. Review. Review. Trees. What are the types of these functions? How about these? f1 x y = [x] : [y]

Trees and Inductive Definitions

Typed Racket: Racket with Static Types

Programming Languages and Techniques (CIS120)

02157 Functional Programming Tagged values and Higher-order list functions

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

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

Programming Languages and Techniques (CIS120)

CMSC 330, Fall 2013, Practice Problems 3

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Introduction to Objective Caml

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

CS 2336 Discrete Mathematics

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

Tree Equality: The Problem

Parallelism; Parallel Collections

Haskell Overview II (2A) Young Won Lim 9/26/16

A general introduction to Functional Programming using Haskell

Type-indexed functions in Generic Haskell

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Haskell An Introduction

CSE399: Advanced Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

CSC324 Principles of Programming Languages

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

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Exercise 1 ( = 18 points)

A Functional Space Model. COS 326 David Walker Princeton University

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Case by Case. Chapter 3

Exercise 1 (2+2+2 points)

CS 320: Concepts of Programming Languages

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Structural polymorphism in Generic Haskell

ICS 161 Algorithms Winter 1998 Final Exam. 1: out of 15. 2: out of 15. 3: out of 20. 4: out of 15. 5: out of 20. 6: out of 15.

2009 HMMT Team Round. Writing proofs. Misha Lavrov. ARML Practice 3/2/2014

Problem Set CVO 103, Spring 2018

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Programming Languages and Techniques (CIS120e)

Data Structures (INE2011)

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

Programming in Omega Part 1. Tim Sheard Portland State University

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Module 01 Processing Recap. CS 106 Winter 2018

Geometry. Zachary Friggstad. Programming Club Meeting

Programming Languages and Techniques (CIS120)

OBJECT ORIENTED PROGRAMMING

Sum-of-Product Data Types

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

Programming Languages

Sum-of-Product Data Types

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

The Typed Racket Guide

Transcription:

OCaml Datatypes COS 326 David Walker Princeton University slides copyright 2013-2015 David Walker and Andrew W. 1 Appel permission granted to reuse these slides for non-commercial educagonal purposes

OCaml So Far 2 We have seen a number of basic types: int float char string bool We have seen a few structured types: pairs tuples opgons lists In this lecture, we will see some more general ways to define our own new types and data structures

Type AbbreviaGons We have already seen some type abbreviagons: 3 type point = float * float

Type AbbreviaGons We have already seen some type abbreviagons: 4 type point = float * float These abbreviagons can be helpful documentagon: let distance (p1:point) (p2:point) : float = let square x = x *. x in let (x1,y1) = p1 in let (x2,y2) = p2 in sqrt (square (x2 -. x1) +. square (y2 -. y1)) But they add nothing of substance to the language they are equal in every way to an exisgng type

Type AbbreviaGons 5 We have already seen some type abbreviagons: type point = float * float As far as OCaml is concerned, you could have writen: let distance (p1:float*float) (p2:float*float) : float = let square x = x *. x in let (x1,y1) = p1 in let (x2,y2) = p2 in sqrt (square (x2 -. x1) +. square (y2 -. y1)) Since the types are equal, you can subs)tute the definigon for the name wherever you want we have not added any new data structures

DATA TYPES 6

Data types OCaml provides a general mechanism called a data type for defining new data structures that consist of many alternagves 7 type my_bool = Tru Fal a value with type my_bool is one of two things: Tru, or Fal read the " " as "or"

Data types OCaml provides a general mechanism called a data type for defining new data structures that consist of many alternagves 8 type my_bool = Tru Fal Tru and Fal are called "constructors" a value with type my_bool is one of two things: Tru, or Fal read the " " as "or"

Data types OCaml provides a general mechanism called a data type for defining new data structures that consist of many alternagves 9 type my_bool = Tru Fal type color = Blue Yellow Green Red there's no need to stop at 2 cases; define as many alternagves as you want

Data types OCaml provides a general mechanism called a data type for defining new data structures that consist of many alternagves 10 type my_bool = Tru Fal type color = Blue Yellow Green Red CreaGng values: let b1 : my_bool = Tru let b2 : my_bool = Fal let c1 : color = Yellow let c2 : color = Red use constructors to create values

Data types 11 type color = Blue Yellow Green Red let c1 : color = Yellow let c2 : color = Red Using data type values: let print_color (c:color) : unit = match c with Blue -> Yellow -> Green -> Red -> use patern matching to determine which color you have; act accordingly

Data types 12 type color = Blue Yellow Green Red let c1 : color = Yellow let c2 : color = Red Using data type values: let print_color (c:color) : unit = match c with Blue -> print_string "blue" Yellow -> print_string "yellow" Green -> print_string "green" Red -> print_string "red"

Data types 13 type color = Blue Yellow Green Red let c1 : color = Yellow let c2 : color = Red Using data type values: let print_color (c:color) : unit = match c with Blue -> print_string "blue" Yellow -> print_string "yellow" Green -> print_string "green" Red -> print_string "red" Why not just use strings to represent colors instead of defining a new type?

Data types 14 type color = Blue Yellow Green Red oops!: let print_color (c:color) : unit = match c with Blue -> print_string "blue" Yellow -> print_string "yellow" Red -> print_string "red" Warning 8: this patern-matching is not exhausgve. Here is an example of a value that is not matched: Green

Data types 15 type color = Blue Yellow Green Red oops!: let print_color (c:color) : unit = match c with Blue -> print_string "blue" Yellow -> print_string "yellow" Red -> print_string "red" Warning 8: this patern-matching is not exhausgve. Here is an example of a value that is not matched: Green OCaml's datatype mechanism allow you to create types that contain precisely the values you want!

Data Types Can Carry AddiGonal Values 16 Data types are more than just enumeragons of constants: type point = float * float type simple_shape = Circle of point * float Square of point * float Read as: a simple_shape is either: a Circle, which contains a pair of a point and float, or a Square, which contains a pair of a point and float (x,y) s (x,y) r

Data Types Can Carry AddiGonal Values Data types are more than just enumeragons of constants: 17 type point = float * float type simple_shape = Circle of point * float Square of point * float let origin : point = (0.0, 0.0) let circ1 : simple_shape = Circle (origin, 1.0) let circ2 : simple_shape = Circle ((1.0, 1.0), 5.0) let square : simple_shape = Square (origin, 2.3)

Data Types Can Carry AddiGonal Values Data types are more than just enumeragons of constants: 18 type point = float * float type simple_shape = Circle of point * float Square of point * float let simple_area (s:simple_shape) : float = match s with Circle (_, radius) -> 3.14 *. radius *. radius Square (_, side) -> side *. side

Compare Data types are more than just enumeragons of constants: 19 type point = float * float type simple_shape = Circle of point * float Square of point * float let simple_area (s:simple_shape) : float = match s with Circle (_, radius) -> 3.14 *. radius *. radius Square (_, side) -> side *. side type my_shape = point * float let simple_area (s:my_shape) : float = (3.14 *. radius *. radius)?? or?? (side *. side)

More General Shapes 20 type point = float * float type shape = Square of float Ellipse of float * float RtTriangle of float * float Polygon of point list Square s = s RtTriangle (s1, s2) = s1 s2 v2 Ellipse (r1, r2) = r1 r2 Polygon [v1;...;v5] = v1 v5 v3 v4

More General Shapes 21 type point = float * float type radius = float type side = float type shape = Square of side Ellipse of radius * radius RtTriangle of side * side Polygon of point list Type abbreviagons can aid readability Square s = s RtTriangle (s1, s2) = s1 s2 v2 Ellipse (r1, r2) = r1 r2 RtTriangle [v1;...;v5] = v1 v5 v3 v4

More General Shapes 22 type point = float * float type radius = float type side = float type shape = Square of side Ellipse of radius * radius RtTriangle of side * side Polygon of point list Square builds a shape from a single side RtTriangle builds a shape from a pair of sides let sq : shape = Square 17.0 let ell : shape = Ellipse (1.0, 2.0) let rt : shape = RtTriangle (1.0, 1.0) let poly : shape = Polygon [(0., 0.); (1., 0.); (0.; 1.)] they are all shapes; they are constructed in different ways Polygon builds a shape from a list of points (where each point is itself a pair)

More General Shapes 23 type point = float * float type radius = float type side = float a data type also defines a patern for matching type shape = Square of side Ellipse of radius * radius RtTriangle of side * side Polygon of point list let area (s : shape) : float = match s with Square s -> Ellipse (r1, r2)-> RtTriangle (s1, s2) -> Polygon ps ->

More General Shapes 24 type point = float * float type radius = float type side = float a data type also defines a patern for matching type shape = Square of side Ellipse of radius * radius RtTriangle of side * side Polygon of point list let area (s : shape) : float = match s with Square s -> Ellipse (r1, r2)-> RtTriangle (s1, s2) -> Polygon ps -> Square carries a value with type float so s is a patern for float values RtTriangle carries a value with type float * float so (s1, s2) is a patern for that type

More General Shapes 25 type point = float * float type radius = float type side = float a data type also defines a patern for matching type shape = Square of side Ellipse of radius * radius RtTriangle of side * side Polygon of point list let area (s : shape) : float = match s with Square s -> s *. s Ellipse (r1, r2)-> pi *. r1 *. r2 RtTriangle (s1, s2) -> s1 *. s2 /. 2. Polygon ps ->???

CompuGng Area 26 How do we compute polygon area? For convex polygons: Case: the polygon has fewer than 3 points: it has 0 area! (it is a line or a point or nothing at all) Case: the polygon has 3 or more points: Compute the area of the triangle formed by the first 3 vergces Delete the second vertex to form a new polygon Sum the area of the triangle and the new polygon v2 v1 v5 v3 v4 = +

CompuGng Area 27 How do we compute polygon area? For convex polygons: Case: the polygon has fewer than 3 points: it has 0 area! (it is a line or a point or nothing at all) Case: the polygon has 3 or more points: Compute the area of the triangle formed by the first 3 vergces Delete the second vertex to form a new polygon Sum the area of the triangle and the new polygon Note: This is a beaugful inducgve algorithm: the area of a polygon with n points is computed in terms of a smaller polygon with only n-1 points! v2 v1 v5 v3 v4 = +

CompuGng Area 28 let area (s : shape) : float = match s with Square s -> s *. s Ellipse (r1, r2)-> r1 *. r2 RtTriangle (s1, s2) -> s1 *. s2 /. 2. Polygon ps -> poly_area ps This patern says the list has at least 3 items let poly_area (ps : point list) : float = match ps with p1 :: p2 :: p3 :: tail -> tri_area p1 p2 p3 +. poly_area (p1::p3::tail) _ -> 0. v1 v5 v2 v3 v4 = +

CompuGng Area 29 let tri_area (p1:point) (p2:point) (p3:point) : float = let a = distance p1 p2 in let b = distance p2 p3 in let c = distance p3 p1 in let s = 0.5 *. (a +. b +. c) in sqrt (s *. (s -. a) *. (s -. b) *. (s -. c)) let rec poly_area (ps : point list) : float = match ps with p1 :: p2 :: p3 :: tail -> tri_area p1 p2 p3 +. poly_area (p1::p3::tail) _ -> 0. let area (s : shape) : float = match s with Square s -> s *. s Ellipse (r1, r2)-> pi *. r1 *. r2 RtTriangle (s1, s2) -> s1 *. s2 /. 2. Polygon ps -> poly_area ps

INDUCTIVE DATA TYPES 30

InducGve data types 31 We can use data types to define inducgve data A binary tree is: a Leaf containing no data a Node containing a key, a value, a leq subtree and a right subtree

InducGve data types 32 We can use data types to define inducgve data A binary tree is: a Leaf containing no data a Node containing a key, a value, a leq subtree and a right subtree type key = string type value = int type tree = Leaf Node of key * value * tree * tree

InducGve data types 33 type key = int type value = string type tree = Leaf Node of key * value * tree * tree let rec insert (t:tree) (k:key) (v:value) : tree =

InducGve data types 34 type key = int type value = string type tree = Leaf Node of key * value * tree * tree let rec insert (t:tree) (k:key) (v:value) : tree = match t with Leaf -> Node (k', v', left, right) -> Again, the type definigon specifies the cases you must consider

InducGve data types 35 type key = int type value = string type tree = Leaf Node of key * value * tree * tree let rec insert (t:tree) (k:key) (v:value) : tree = match t with Leaf -> Node (k, v, Leaf, Leaf) Node (k', v', left, right) ->

InducGve data types 36 type key = int type value = string type tree = Leaf Node of key * value * tree * tree let rec insert (t:tree) (k:key) (v:value) : tree = match t with Leaf -> Node (k, v, Leaf, Leaf) Node (k', v', left, right) -> if k < k' then Node (k', v', insert left k v, right) else if k > k' then Node (k', v', left, insert right k v) else Node (k, v, left, right)

InducGve data types 37 type key = int type value = string type tree = Leaf Node of key * value * tree * tree let rec insert (t:tree) (k:key) (v:value) : tree = match t with Leaf -> Node (k, v, Leaf, Leaf) Node (k', v', left, right) -> if k < k' then Node (k', v', insert left k v, right) else if k > k' then Node (k', v', left, insert right k v) else Node (k, v, left, right)

InducGve data types 38 type key = int type value = string type tree = Leaf Node of key * value * tree * tree Note on memory use let rec insert (t:tree) (k:key) (v:value) : tree = match t with Leaf -> Node (k, v, Leaf, Leaf) Node (k', v', left, right) -> if k < k' then Node (k', v', insert left k v, right) else if k > k' then Node (k', v', left, insert right k v) else Node (k, v, left, right)

InducGve data types: Another Example 39 Recall, we used the type "int" to represent natural numbers but that was kind of broken: it also contained negagve numbers we had to use a dynamic test to guard entry to a funcgon: let double (n : int) : int = if n < 0 then raise (Failure "negative input!") else double_nat n it would be nice if there was a way to define the natural numbers exactly, and use OCaml's type system to guarantee no client ever atempts to double a negagve number

InducGve data types 40 Recall, a natural number n is either: zero, or m + 1 We use a data type to represent this definigon exactly:

InducGve data types 41 Recall, a natural number n is either: zero, or m + 1 We use a data type to represent this definigon exactly: type nat = Zero Succ of nat

InducGve data types 42 Recall, a natural number n is either: zero, or m + 1 We use a data type to represent this definigon exactly: type nat = Zero Succ of nat let rec nat_to_int (n : nat) : int = match n with Zero -> 0 Succ n -> 1 + nat_to_int n

InducGve data types 43 Recall, a natural number n is either: zero, or m + 1 We use a data type to represent this definigon exactly: type nat = Zero Succ of nat let rec nat_to_int (n : nat) : int = match n with Zero -> 0 Succ n -> 1 + nat_to_int n let rec double_nat (n : nat) : nat = match n with Zero -> Zero Succ m -> Succ (Succ(double_nat m))

Summary 44 OCaml data types: a powerful mechanism for defining complex data structures: They are precise contain exactly the elements you want, not more elements They are general recursive, non-recursive (mutually recursive and polymorphic) The type checker helps you detect errors missing cases in your funcgons