Standard ML. Data types. ML Datatypes.1

Similar documents
Inductive Data Types

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists

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

Exercises on ML. Programming Languages. Chanseok Oh

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

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Programming Paradigms

CS 320: Concepts of Programming Languages

Fall Lecture 3 September 4. Stephen Brookes

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

CSE 3302 Programming Languages Lecture 8: Functional Programming

Handout 2 August 25, 2008

Datatype declarations

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CS 457/557: Functional Languages

Lists. Michael P. Fourman. February 2, 2010

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

CSE3322 Programming Languages and Implementation

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

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

SML A F unctional Functional Language Language Lecture 19

The type checker will complain that the two branches have different types, one is string and the other is int

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CSCI-GA Scripting Languages

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Standard ML. Curried Functions. ML Curried Functions.1

CSC324 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Topic 7: Algebraic Data Types

CS558 Programming Languages

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

CMSC 330: Organization of Programming Languages. OCaml Data Types

Higher-Order Functions

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

CSC/MAT-220: Lab 6. Due: 11/26/2018

Flang typechecker Due: February 27, 2015

Advanced features of Functional Programming (Haskell)

Data Types The ML Type System

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

Recap: ML s Holy Trinity

CSC324 Functional Programming Typing, Exceptions in ML

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Programming Languages

Chapter 6 Abstract Datatypes

CSE341, Fall 2011, Midterm Examination October 31, 2011

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Functional Paradigm II

A list is a finite sequence of elements. Elements may appear more than once

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Lecture 19: Functions, Types and Data Structures in Haskell

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

Consider the following EBNF definition of a small language:

CSE341 Autumn 2017, Midterm Examination October 30, 2017

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Overloading, Type Classes, and Algebraic Datatypes

Types and Programming Languages. Lecture 5. Extensions of simple types

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

Typed Scheme: Scheme with Static Types

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

"Object Oriented" Programming using ML

CSE341 Spring 2016, Midterm Examination April 29, 2016

(Refer Slide Time: 4:00)

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

Programming Languages and Techniques (CIS120)

Typed Racket: Racket with Static Types

CSE341, Fall 2011, Midterm Examination October 31, 2011

Exercise 1 (2+2+2 points)

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

List Functions, and Higher-Order Functions

Lecture 12: Data Types (and Some Leftover ML)

Why Standard ML? A language particularly suited to compiler implementation.

Programming Language Concepts, cs2104 Lecture 04 ( )

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

02157 Functional Programming Tagged values and Higher-order list functions

CSE 130 [Winter 2014] Programming Languages

Begin at the beginning

Programming in Standard ML: Continued

Polymorphism and Type Inference

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Combining Programming with Theorem Proving

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Algebraic Types. Chapter 14 of Thompson

CS 440: Programming Languages and Translators, Spring 2019 Mon

15 212: Principles of Programming. Some Notes on Continuations

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Introduction to SML Getting Started

Sample Exam; Solutions

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

Transcription:

Standard ML Data types ML Datatypes.1

Concrete Datatypes The datatype declaration creates new types These are concrete data types, not abstract Concrete datatypes can be inspected - constructed and taken apart ML s datatypes has two kinds of values: atomic and composite The only operations supported: Use atomic values Construct a composite value Take a composite value apart: pattern matching ML Datatypes.2

Enumeration Types Simplest example: single valued type datatype single = only; - only; val it = only : single only denotes the only value in the type single Isomorphic to unit Consisting of a finite number of constants - datatype bool = true false; datatype bool = false true - true; val it = true : bool - false; val it = false : bool ML Datatypes.3

Enumeration Types No order on the elements (unlike Pascal, C) - datatype piece = king queen rook bishop knight pawn; datatype piece = bishop king knight pawn queen rook - fun value king = Real.posInf (* infinity *) value queen = 9.0 value rook = 5.0 value (bishop knight) = 3.0 value pawn = 1.0; val value = fn : piece -> real - value bishop; val it = 3.0 : int ML Datatypes.4

Branding Newton s second law: - fun a m f = f/m; val a = fn : real -> real -> real - val (body, engine) = (0.0122, 50.0); - a engine body; (* oops *) val it = 4098.36065574 : real Type alias will not suffice here type mass = real and force = real and acceleration = real; - fun a (m:mass) (f:force) : acceleration = f/m; val a = fn : mass -> force -> acceleration - a engine body; (* still oops *) val it = 4098.36065574 : acceleration ML Datatypes.5

Constructors Simulate branding using datatype: datatype mass = Kg of real; datatype force = Newton of real; datatype acceleration = m_s'2 of real; Constructors are functions - Kg; val it = fn : real -> mass - Newton; val it = fn : real -> force - val body = Kg 0.0122; val engine = Newton 50.0; val body = Kg 0.0122 : mass val engine = Newton 50.0 : force May be passed to other functions - map Kg [1.2, 5.3]; ML Datatypes.6

Constructors Simulate branding using datatype: datatype mass = Kg of real; datatype force = Newton of real; datatype acceleration = m_s'2 of real; Constructor names are the building blocks of patterns - fun a (Kg m) (Newton f) = m_s'2 (f / m); val a = fn : mass -> force -> acceleration - a body engine; val it = m_s'2 25.0 : acceleration - a engine body; Error: operator and operand don't agree [tycon mismatch] operator domain: mass operand: force in expression: ML Datatypes.7

We ve already seen an example: enumerated types Shapes datatype shape = point Line of real Circle of real Rectangle of real*real; A tagged union Calculate area: Variant Types fun area (point Line _) = 0.0 area (Circle r) = Math.pi*r*r area (Rectangle (w, h)) = w * h; val area = fn : shape -> real ML Datatypes.8

Pattern Matching In val binding: val line = Line 5.3; - val Line length = line; val length = 5.3 : real - val Circle radius = line; uncaught exception Bind [nonexhaustive binding failure] What will happen for the following: - val point = point; - val point = 5.3; (* OK. *) val declaration fails if the matching fails (* FAIL - types mismatch *) Constructors cannot be rebound in a binding declaration ML Datatypes.9

Recursive Datatypes Let s recall the definition of lists: Every list is either nil or has the form x::xs where x is its head and the list xs is its tail. We can do it ourselves! datatype intlist = nil :: of int * intlist; What if we omit the nil part in the datatype definition? Defining functions as usual: - fun length nil = 0 length (_::xs) = 1 + length xs; We can t have the [] syntax. That s a built-in syntactic sugar ML Datatypes.10

Polymorphic Recursive Datatypes intlist? Seriously? What s so special about ints? Well, nothing, of course. Guess what comes next? datatype 'a list = nil :: of 'a * ('a list); - "hello" :: "world" :: nil; val it = "hello" :: "world" :: nil : string list In OOP terminology, it is called generics ML Datatypes.11

Polymorphic Datatypes We can denote optional parameters in function: datatype 'a option = NONE SOME of 'a; We can use datatypes to unite two different types. We can abstract on this idea, and create a datatype uniting any two types: a and b datatype ('a,'b) union = type1 of 'a Example in the next slide type2 of 'b; ML Datatypes.12

Union - Example datatype ('a,'b) union = type1 of 'a type2 of 'b; - val five = type1 5; val five = type1 5 : (int,'a) union - val hello = type2 "hello"; val hello = type2 "hello" : ('a,string) union - val five_or_hello = if true then five else hello; val five_or_hello = type1 5 : (int,string) union - val int_char_list = [type1 5, type2 # a ]; val five_or_hello = [type1 5, type2 # a ] : (int,char) union list ML Datatypes.13

Trees datatype 'a tree = Nil Br of 'a * ('a tree) * ('a tree); fun Leaf x = Br (x, Nil, Nil); - val tree2 = Br (2, Leaf 1, Leaf 3); val tree2 = Br (2,Br (1,Nil,Nil),Br (3,Nil,Nil)) : int tree - val tree5 = Br (5, Leaf 6, Leaf 7); - val tree4 = Br (4, tree2, tree5); val tree4 = Br (4,Br (2,Br #,Br #),Br (5,Br #,Br #)) : int tree - fun size Nil = 0 size (Br (v,t1,t2)) = 1 + size t1 + size t2; val count = fn : a tree -> int ML Datatypes.14

Binary Search Trees Implement an associative array using trees The tree will be of type (int * 'a) tree int is the key, a is the data in the node. Value may be anything Assumption: The tree is sorted Any key on the left subtree is smaller than the current key The current key is larger than any of the keys in the right subtree We will define two functions: insert = fn : (int * 'a) tree -> int * 'a -> (int * 'a) tree get = fn : (int * 'a) tree -> int -> 'a Two helpers: datatype order = LESS EQUAL GREATER (* predefined *) val compare : int*int->order = Int.compare ML Datatypes.15

Binary Search Trees fun get (Br ((node_k, v), left, right)) k = case compare (node_k, k) of EQUAL => v GREATER => get right k LESS => get left k ; local in end fun compare (k1,_) (k2,_) = compare (k1, k2) fun insert Nil item = Br (item, Nil, Nil) insert (Br (node, left, right)) item = case cmp node item of EQUAL => Br (item, left, right) GREATER => Br (node, left, insert right item) LESS => Br (node, insert left item, right) ML Datatypes.16