5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A First Look at ML.
A Fourth Look At ML 1. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML datatype bool = true | false;
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter 11 Type-Safe Data Structures Chapter ElevenModern Programming Languages1.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
A First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Polymorphism Chapter EightModern Programming Languages1.
Chapter EightModern Programming Languages1 Polymorphism.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
Chapter 2 A Module of Shapes: Part I. Defining New Datatypes  The ability to define new data types in a programming language is important.  Kinds of.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
A lightening tour in 45 minutes
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Types and Classes in Haskell
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
Review Previously User-defined data types: records, variants
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not primitive in ML: Type constructor for lists: We are going to learn what is that!

5/11/2015IT 3272 Defining Your Own Types New types can be defined using the keyword datatype n These declarations define both: – type constructors for making new (possibly polymorphic) types – data constructors for making values of those new types

5/11/2015IT 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 Mon, Tue, etc. are the new data constructors n Why call them “constructors”? Since both can have parameters… Enumeration examples

5/11/2015IT 3274 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 - datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun; datatype day = Fri | Mon | Sat | Sun | Thu | Tue | Wed No Parameters involved

5/11/2015IT 3275 n ML is strict about types, Unlike C enum, no implementation details are exposed to the programmer - 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 Strict Typing

5/11/2015IT 3276 n You can use the data constructors in patterns n They are like constants fun isWeekDay Sat = false | isWeekDay Sun = false | isWeekDay _ = true; Data Constructors in Patterns

5/11/2015 IT Data constructors with parameters Wrappers Add a parameter of any type to a data onstructor, using the keyword of datatype exint = Value of int | PlusInf | MinusInf; Such a constructor is a wrapper that contains a data item of the given type PlusInf Value 36 MinusInf Value 26 Value 38 Exint examples: 3 data constructors type constructor

5/11/2015IT 3278 Value is a data constructor that takes a parameter that is a value of int It is a kind of function int -> exint - 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

5/11/2015IT 3279 Value 5 is an exint It is not an int, though it contains one How can we get the int out? - val x = Value 5; val x = Value 5 : exint - x+x; Error: overloaded variable not defined at type symbol: + type: exint A Value is Not an int

5/11/2015IT So Value is no ordinary function: ordinary functions can't be pattern-matched - val (Value y) = x; Warning: binding not exhaustive Value y =... val y = 5 : int Patterns With Data Constructors - val x = Value 5; exint Pattern matching can retrieve data constructor’s parameters

5/11/2015IT An Exhaustive Pattern n You get a warning if it is not exhaustive An exint can be a PlusInf, a MinusInf, or a Value n all cases should be considered - val s = case x of = PlusInf => "infinity" | = MinusInf => "-infinity" | = Value y => Int.toString y; val s = "5" : string - val (Value y) = x; Warning: binding not exhaustive Value y =... val y = 5 : int This one does not Consider all cases

5/11/2015IT val x = Value 5; - val (Value y) = x; Warning: binding not exhaustive Value y =... val y = 5 : int Similar bindings (matching) that are not exhaustive - val [a,b] = [1,2]; stdIn: Warning: binding not exhaustive a :: b :: nil =... val a = 1 : int val b = 2 : int

5/11/2015IT 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

5/11/2015IT Exception Handling -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 : exint - square (Value ); val it = PlusInf : exint

5/11/2015IT Type Constructors With Parameters datatype 'a option = NONE | SOME of 'a; n The parameters of a type constructor are type variables, which are used in the data constructors n A polymorphic type NONE SOME "Hello" string option SOME "world" NONE SOME 1.5 real option SOME Using Java/C++’s language, what are these all about?

5/11/2015IT Type constuctor parameter comes before the type constructor name: datatype 'a option = NONE | SOME of 'a; 'a option, int option, just like 'a list, int list - 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 option is predefined - SOME; val it = fn : 'a -> 'a option

5/11/2015IT Applications of option n Predefined type constructor in ML n 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

5/11/2015IT n As usual, ML infers types: bunch datatype 'x bunch = One of 'x | Group of 'x list; - One 1.0; val it = One 1.0 : real bunch - Group [true,false]; val it = Group [true,false] : bool bunch type constructors data constructors

5/11/2015IT Examples val a = One 3.0; val b = Group [1.0, 2.0, 5.0]; val c = Group [1, 2, 5]; val d = One 3; datatype 'x bunch = One of 'x | Group of 'x list; val a = One 3.0 : real bunch; val b = Group [1.0, 2.0, 5.0] : real bunch; val c = Group [1, 2, 5] : int bunch; val d = One 3 : int bunch;

5/11/2015IT ML can infer bunch types, but does not always have to resolve them, just as with list types - 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 Example: Polymorphism

5/11/2015IT Example: No Polymorphism We applied the + operator (through foldr ) to the list elements So ML knows the parameter type must be int bunch - 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 use + to resolve

5/11/2015IT Recursively defined type constructors Definition of `a list 1) nil is a ‘a list 2) If e is ‘a and L is ‘a list, then e::L is ‘a list datatype 'x = of 'x | of 'x ; type constructors data constructors

5/11/2015IT Recursively Defined Type Constructors datatype intlist = INTNIL | INTCONS of int * intlist; i.e., using its own type constructor - INTNIL; val it = INTNIL : intlist - INTCONS; val it = fn : int * intlist -> intlist -

5/11/2015IT datatype intlist = INTNIL | INTCONS of int * intlist; INTCONS 1 INTNIL INTCONS 2 INTCONS 1 INTNIL INTCONS 2 INTCONS 1 INTNIL INTCONS 3

5/11/2015IT 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 Constructing intlist INTCONS 1 INTNIL INTCONS 1 INTCONS 2 INTNIL

5/11/2015IT fun intlistLength INTNIL = 0 | intlistLength (INTCONS(_,tail)) = 1 + (intListLength tail); An intlist Length Function: fun listLength nil = 0 | listLength (_::tail) = 1 + (listLength tail); pattern matching

5/11/2015IT Parametric List Type like ‘a list datatype 'element mylist = NIL | CONS of 'element * 'element mylist; - 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 n datatype intlist = INTNIL | INTCONS of int * intlist; fixed

5/11/2015IT This now works almost exactly like the predefined list type constructor add up a list you may use foldr … fun myListLength NIL = 0 | myListLength (CONS(_,tail)) = 1 + myListLength(tail); Some mylist functions fun addup NIL = 0 | addup (CONS(head,tail)) = head + addup tail;

5/11/2015IT Recall the foldr Function foldr f c [x 1, …, x n ] computes: - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b

5/11/2015IT So we can apply it to mylist x like : myfoldr (op +) 0 x; Or myfoldr (op ^) “” x; fun myfoldr f c NIL = c | myfoldr f c (CONS(a,b)) = f(a, myfoldr f c b); Define myfoldr for mylist

5/11/2015IT CONS is not an operator datatype 'element mylist = NIL | CONS of 'element * 'element mylist; val ML = NIL; CONS (1, CONS(2, ML)); infixr 5 CONS; 1 CONS 2 CONS NIL; val it = 1 CONS 2 CONS NIL : int mylist Now, it is

5/11/2015IT datatype 'data tree = Empty | Node of 'data tree * 'data * 'data tree; Polymorphic Binary Tree Empty Node 2 Empty Node 2 Node 3 Empty Node 1 Empty

5/11/2015IT 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)); Constructing 'a tree

5/11/2015IT Increment All Elements | incAll (Node(x,y,z)) = - incAll tree123; val it = Node (Node (Empty,2,Empty), 3, Node (Empty,4,Empty)) : int tree Pattern matching fun incAll Empty = Empty Node(incAll x, y+1, incAll z);

5/11/2015IT Add Up The Elements fun sumAll Empty = 0 | sumAll (Node(x,y,z)) = sumAll x + y + sumAll z; - sumall tree123; val it = 6 : int

5/11/2015IT Convert To List (Polymorphic) fun listAll Empty = nil | listAll (Node(x,y,z)) = listAll y :: listAll z; - listAll tree123; val it = [1,2,3] : int list

5/11/2015IT 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

5/11/2015IT That's all, folks! n What we have skipped: – records (like tuples with named fields) – arrays, with elements that can be altered – references, for values that can be altered – exception handling – 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 – predefined functions, types, etc. – eXene: an ML library for applications that work in the X window system – the Compilation Manager for building large ML projects

5/11/2015IT Other Functional Languages n ML supports a function-oriented style of programming n Other popular functional languages: 1. Lisp 2. Haskell