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

Similar documents
COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Standard ML. Data types. ML Datatypes.1

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

Functional Paradigm II

CSE 3302 Programming Languages Lecture 8: Functional Programming

Exercises on ML. Programming Languages. Chanseok Oh

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Overloading, Type Classes, and Algebraic Datatypes

Lists. Michael P. Fourman. February 2, 2010

Data Types The ML Type System

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CSCE 314 Programming Languages

Functional Programming and Modeling

int f(char a, char b) {

CS558 Programming Languages

CSCI-GA Scripting Languages

Handout 2 August 25, 2008

Functional Paradigm II

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

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

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

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

CS558 Programming Languages

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

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

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

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

Datatype declarations

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

CS 11 Ocaml track: lecture 2

SML A F unctional Functional Language Language Lecture 19

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

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

Polymorphism and Type Inference

Inductive Data Types

Functional Programming. Lecture 2: Algebra

Lecture #23: Conversion and Type Inference

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Higher-Order Functions

CSE 341 Section 5. Winter 2018

Lecture 19: Functions, Types and Data Structures in Haskell

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

List Processing in SML

Lecture 12: Data Types (and Some Leftover ML)

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

List Processing in SML

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CSCI-GA Final Exam

"Object Oriented" Programming using ML

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

DC69 C# &.NET JUNE C# is a simple, modern, object oriented language derived from C++ and Java.

CSCC24 Functional Programming Typing, Scope, Exceptions ML

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

Introduction to OCaml

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

Chapter 3 Linear Structures: Lists

Programming in Haskell Aug-Nov 2015

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

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

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Object Orientation. Chapter Sixteen Modern Programming Languages, 2nd ed. 1

G Programming Languages - Fall 2012

CS 457/557: Functional Languages

Imperative languages

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Polymorphism and Type Inference

CMSC 330: Organization of Programming Languages

CSE 341 : Programming Languages Midterm, Spring 2015

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Scope. Chapter Ten Modern Programming Languages 1

CSE3322 Programming Languages and Implementation

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

Introduction to Objective Caml

COP4020 Programming Assignment 1 - Spring 2011

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

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

CSE 307: Principles of Programming Languages

Introduc)on To Standard ML

UMBC CMSC 331 Final Exam

CSE 307: Principles of Programming Languages

Software System Design and Implementation

CS 320: Concepts of Programming Languages

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

A LISP Interpreter in ML

Topic 7: Algebraic Data Types

CSC324 Principles of Programming Languages

CSCE 314 Programming Languages

Programming Paradigms

CS558 Programming Languages

Parallel Haskell on MultiCores and Clusters

More non-primitive types Lesson 06

Two approaches to writing interfaces

Lecture: Functional Programming

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

Transcription:

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

Type Definitions Predefined, but not primitive in ML: datatype bool = true false; Type constructor for lists: datatype 'element list = nil :: of 'element * 'element list Defined for ML in ML Chapter Eleven Modern Programming Languages, 2nd ed. 2

Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 3

Defining Your Own Types New types can be defined using the keyword datatype These declarations define both: type constructors for making new (possibly polymorphic) types data constructors for making values of those new types Chapter Eleven Modern Programming Languages, 2nd ed. 4

Example - datatype day = Mon Tue Wed Thu Fri Sat Sun; datatype day = Fri Mon Sat Sun Thu Tue Wed - fun isweekday x = not (x = Sat orelse x = Sun); val isweekday = fn : day -> bool - isweekday Mon; val it = true : bool - isweekday Sat; val it = false : bool day is the new type constructor and Mon, Tue, etc. are the new data constructors Why constructors? In a moment we will see how both can have parameters Chapter Eleven Modern Programming Languages, 2nd ed. 5

No Parameters - datatype day = Mon Tue Wed Thu Fri Sat Sun; datatype day = Fri Mon Sat Sun Thu Tue Wed The type constructor day takes no parameters: it is not polymorphic, there is only one day type The data constructors Mon, Tue, etc. take no parameters: they are constant values of the day type Capitalize the names of data constructors Chapter Eleven Modern Programming Languages, 2nd ed. 6

Strict Typing - datatype flip = Heads Tails; datatype flip = Heads Tails - fun isheads x = (x = Heads); val isheads = fn : flip -> bool - isheads Tails; val it = false : bool - isheads Mon; Error: operator and operand don't agree [tycon mismatch] operator domain: flip operand: day ML is strict about these new types, just as you would expect Unlike C enum, no implementation details are exposed to the programmer Chapter Eleven Modern Programming Languages, 2nd ed. 7

Data Constructors In Patterns fun isweekday Sat = false isweekday Sun = false isweekday _ = true; You can use the data constructors in patterns In this simple case, they are like constants But we will see more general cases next Chapter Eleven Modern Programming Languages, 2nd ed. 8

Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 9

Wrappers You can add a parameter of any type to a data constructor, using the keyword of: datatype exint = Value of int PlusInf MinusInf; In effect, such a constructor is a wrapper that contains a data item of the given type Some things of type exint: PlusInf MinusInf Value 38 Value 26 Value 36 Chapter Eleven Modern Programming Languages, 2nd ed. 10

- datatype exint = Value of int PlusInf MinusInf; datatype exint = MinusInf PlusInf Value of int - PlusInf; val it = PlusInf : exint - MinusInf; val it = MinusInf : exint - Value; val it = fn : int -> exint - Value 3; val it = Value 3 : exint Value is a data constructor that takes a parameter: the value of the int to store It looks like a function that takes an int and returns an exint containing that int Chapter Eleven Modern Programming Languages, 2nd ed. 11

A Value Is Not An int - val x = Value 5; val x = Value 5 : exint - x+x; Error: overloaded variable not defined at type symbol: + type: exint Value 5 is an exint It is not an int, though it contains one How can we get the int out again? By pattern matching Chapter Eleven Modern Programming Languages, 2nd ed. 12

Patterns With Data Constructors - val (Value y) = x; val y = 5 : int To recover a data constructor s parameters, use pattern matching So Value is no ordinary function: ordinary functions can't be pattern-matched this way Note that this example only works because x actually is a Value here Chapter Eleven Modern Programming Languages, 2nd ed. 13

An Exhaustive Pattern - val s = case x of = PlusInf => "infinity" = MinusInf => "-infinity" = Value y => Int.toString y; val s = "5" : string An exint can be a PlusInf, a MinusInf, or a Value Unlike the previous example, this one says what to do for all possible values of x Chapter Eleven Modern Programming Languages, 2nd ed. 14

Pattern-Matching Function - fun square PlusInf = PlusInf = square MinusInf = PlusInf = square (Value x) = Value (x*x); val square = fn : exint -> exint - square MinusInf; val it = PlusInf : exint - square (Value 3); val it = Value 9 : exint Pattern-matching function definitions are especially important when working with your own datatypes Chapter Eleven Modern Programming Languages, 2nd ed. 15

Exception Handling (A Peek) - fun square PlusInf = PlusInf = square MinusInf = PlusInf = square (Value x) = Value (x*x) = handle Overflow => PlusInf; val square = fn : exint -> exint - square (Value 10000); val it = Value 100000000 : exint - square (Value 100000); val it = PlusInf : exint Patterns are also used in ML for exception handling, as in this example We ll see it in Java, but skip it in ML Chapter Eleven Modern Programming Languages, 2nd ed. 16

Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 17

Type Constructors With Parameters Type constructors can also use parameters: datatype 'a option = NONE SOME of 'a; The parameters of a type constructor are type variables, which are used in the data constructors The result: a new polymorphic type NONE SOME 1.5 Values of type real option SOME 123.4 NONE SOME "Hello" SOME "world" Values of type string option Chapter Eleven Modern Programming Languages, 2nd ed. 18

Parameter Before Name - SOME 4; val it = SOME 4 : int option - SOME 1.2; val it = SOME 1.2 : real option - SOME "pig"; val it = SOME "pig" : string option Type constuctor parameter comes before the type constructor name: datatype 'a option = NONE SOME of 'a; We have types 'a option and int option, just like 'a list and int list Chapter Eleven Modern Programming Languages, 2nd ed. 19

Uses For option Predefined type constructor in ML Used by predefined functions (or your own) when the result is not always defined - fun optdiv a b = = if b = 0 then NONE else SOME (a div b); val optdiv = fn : int -> int -> int option - optdiv 7 2; val it = SOME 3 : int option - optdiv 7 0; val it = NONE : int option Chapter Eleven Modern Programming Languages, 2nd ed. 20

Longer Example: bunch datatype 'x bunch = One of 'x Group of 'x list; An 'x bunch is either a thing of type 'x, or a list of things of type 'x As usual, ML infers types: - One 1.0; val it = One 1.0 : real bunch - Group [true,false]; val it = Group [true,false] : bool bunch Chapter Eleven Modern Programming Languages, 2nd ed. 21

Example: Polymorphism - fun size (One _) = 1 = size (Group x) = length x; val size = fn : 'a bunch -> int - size (One 1.0); val it = 1 : int - size (Group [true,false]); val it = 2 : int ML can infer bunch types, but does not always have to resolve them, just as with list types Chapter Eleven Modern Programming Languages, 2nd ed. 22

Example: No Polymorphism - fun sum (One x) = x = sum (Group xlist) = foldr op + 0 xlist; val sum = fn : int bunch -> int - sum (One 5); val it = 5 : int - sum (Group [1,2,3]); val it = 6 : int We applied the + operator (through foldr) to the list elements So ML knows the parameter type must be int bunch Chapter Eleven Modern Programming Languages, 2nd ed. 23

Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 24

Recursively Defined Type Constructors The type constructor being defined may be used in its own data constructors: datatype intlist = INTNIL INTCONS of int * intlist; Some values of type intlist: INTNIL the empty list INTCONS 1 INTNIL the list [1] INTCONS INTCONS 2 INTNIL Chapter Eleven Modern Programming Languages, 2nd ed. 25 1 the list [1,2]

Constructing Those Values - INTNIL; val it = INTNIL : intlist - INTCONS (1,INTNIL); val it = INTCONS (1,INTNIL) : intlist - INTCONS (1,INTCONS(2,INTNIL)); val it = INTCONS (1,INTCONS (2,INTNIL)) : intlist INTNIL the empty list INTCONS 1 INTNIL 1 INTCONS INTCONS 2 INTNIL the list [1,2] the list [1] Chapter Eleven Modern Programming Languages, 2nd ed. 26

An intlist Length Function fun intlistlength INTNIL = 0 intlistlength (INTCONS(_,tail)) = 1 + (intlistlength tail); fun listlength nil = 0 listlength (_::tail) = 1 + (listlength tail); A length function Much like you would write for native lists Except, of course, that native lists are not always lists of integers Chapter Eleven Modern Programming Languages, 2nd ed. 27

Parametric List Type datatype 'element mylist = NIL CONS of 'element * 'element mylist; A parametric list type, almost like the predefined list ML handles type inference in the usual way: - CONS(1.0, NIL); val it = CONS (1.0,NIL) : real mylist - CONS(1, CONS(2, NIL)); val it = CONS (1,CONS (2,NIL)) : int mylist Chapter Eleven Modern Programming Languages, 2nd ed. 28

Some mylist Functions fun mylistlength NIL = 0 mylistlength (CONS(_,tail)) = 1 + mylistlength(tail); fun addup NIL = 0 addup (CONS(head,tail)) = head + addup tail; This now works almost exactly like the predefined list type constructor Of course, to add up a list you would use foldr Chapter Eleven Modern Programming Languages, 2nd ed. 29

A foldr For mylist fun myfoldr f c NIL = c myfoldr f c (CONS(a,b)) = f(a, myfoldr f c b); Definition of a function like foldr that works on 'a mylist Can now add up an int mylist x with: myfoldr (op +) 0 x One remaining difference: :: is an operator and CONS is not Chapter Eleven Modern Programming Languages, 2nd ed. 30

Defining Operators (A Peek) ML allows new operators to be defined Like this: - infixr 5 CONS; infixr 5 CONS - 1 CONS 2 CONS NIL; val it = 1 CONS 2 CONS NIL : int mylist Chapter Eleven Modern Programming Languages, 2nd ed. 31

Polymorphic Binary Tree datatype 'data tree = Empty Node of 'data tree * 'data * 'data tree; Some values of type int tree: Empty the empty tree Node Node Empty 1 Empty 2 Empty Node 2 Empty the tree 2 Empty Node 3 Empty the tree 2 1 3 Chapter Eleven Modern Programming Languages, 2nd ed. 32

Constructing Those Values - val treeempty = Empty; val treeempty = Empty : 'a tree - val tree2 = Node(Empty,2,Empty); val tree2 = Node (Empty,2,Empty) : int tree - val tree123 = Node(Node(Empty,1,Empty), = 2, = Node(Empty,3,Empty)); Chapter Eleven Modern Programming Languages, 2nd ed. 33

Increment All Elements fun incall Empty = Empty incall (Node(x,y,z)) = Node(incall x, y+1, incall z); - incall tree123; val it = Node (Node (Empty,2,Empty), 3, Node (Empty,4,Empty)) : int tree Chapter Eleven Modern Programming Languages, 2nd ed. 34

Add Up The Elements fun sumall Empty = 0 sumall (Node(x,y,z)) = sumall x + y + sumall z; - sumall tree123; val it = 6 : int Chapter Eleven Modern Programming Languages, 2nd ed. 35

Convert To List (Polymorphic) fun listall Empty = nil listall (Node(x,y,z)) = listall x @ y :: listall z; - listall tree123; val it = [1,2,3] : int list Chapter Eleven Modern Programming Languages, 2nd ed. 36

Tree Search fun isintree x Empty = false isintree x (Node(left,y,right)) = x=y orelse isintree x left orelse isintree x right; - isintree 4 tree123; val it = false : bool - isintree 3 tree123; val it = true : bool Chapter Eleven Modern Programming Languages, 2nd ed. 37

Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 38

That's All That s all the ML we will see There is, of course, a lot more A few words about the parts we skipped: records (like tuples with named fields) arrays, with elements that can be altered references, for values that can be altered exception handling Chapter Eleven Modern Programming Languages, 2nd ed. 39

More Parts We Skipped support for encapsulation and data hiding: structures: collections of datatypes, functions, etc. signatures: interfaces for structures functors: like functions that operate on structures, allowing type variables and other things to be instantiated across a whole structure Chapter Eleven Modern Programming Languages, 2nd ed. 40

More Parts We Skipped API: the standard basis predefined functions, types, etc. Some at the top level but most in structures: Int.maxInt, Real.Math.sqrt, List.nth, etc. Chapter Eleven Modern Programming Languages, 2nd ed. 41

More Parts We Skipped exene: an ML library for applications that work in the X window system the Compilation Manager for building large ML projects Other dialects besides Standard ML Ocaml F# (in Visual Studio, for the.net platform) Concurrent ML (CML) extensions Chapter Eleven Modern Programming Languages, 2nd ed. 42

Functional Languages ML supports a function-oriented style of programming If you like that style, there are many other languages to explore, like Lisp and Haskell Chapter Eleven Modern Programming Languages, 2nd ed. 43