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.

Slides:



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

Programming with Lists
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 [3,4]
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 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.
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.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
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 Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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 ElevenModern 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.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Com Functional Programming Algebraic Data Types Marian Gheorghe Lecture 12 Module homepage Mole & ©University of.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
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.
Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
Functional Design and Programming Lecture 4: Sorting.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Enumeration Datatypes  Enumeration types A datatype consisting of a finite number of constants.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Introduction and Chapter 8 of Programming Languages by Ravi Sethi Elements of Functional Programming.
Functional Programming Element of Functional Programming.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Chapter 9: Functional Programming in a Typed Language.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Introduction A sequence is an ordered list of numbers. The numbers, or terms, in the ordered list are determined by a formula that is a function of the.
Chapter SevenModern Programming Languages1 A Second Look At ML.
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.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
Type Definitions cs776 (prasad) L8tdef.
PROGRAMMING IN HASKELL
CSE 341 Lecture 3 let expressions; pattern matching Ullman
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
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
Functions, Patterns and Datatypes
Presentation transcript:

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 larger constructions  Examples of concrete data: Lists Test whether or not empty Divide (a non-empty list) into its head and tail Join in new elements Trees, Logical proposition, etc.  datatype Defines a new type along with its constructors A constructor In an expression: creates a value of a datatype In a pattern: describe how to take a value apart

ML Datatypes.3 Types of Drinks  Cataloguing all types of drinks Coffee has a brand, number of sugar lumps and w/o milk e.g. Elite, 2 sugars, with milk - type Coffee = string*int*bool; Wine has vineyard name. e.g Shato-Lablan - type Wine = string; Beer has brand name. e.g. Paulaner. - type Beer = string; Water is simply water. There is nothing more to say. - type Water = unit; The datatype declaration for the type drink: datatype drink = Water | Coffee of string*int*bool | Wine of string | Beer of string;

ML Datatypes.4 Values of a datatype  May belong to compound data structures - val drinks = [Water, Coffee (“Elite”,2,true), Beer “Paulaner"]; val drinks = [Water,Coffee ("Elite",2,true),Beer "Paulaner"] : drink list  May be arguments and results of functions  Remember Patterns?

ML Datatypes.5 Constructors in Patterns  Create titles for drinks: fun title Water = “Nature’s best Drink!" | title (Beer brand) = brand^“ Beer” | title (Wine brand) = brand^” Wine” | title (Coffee (brand, sugars, true)) = brand^” Coffee with “^ Int.toString(sugars)^” lumps, with milk” | title (Coffee (brand, sugars, false)) = brand^” Coffee with “^ Int.toString(sugars)^” lumps, no milk”; > val title = fn : drink -> string  Get brand names of drinks in a list : fun pname [] = [] | pname ((Beer s)::ps) = s::(pname ps) | pname ((Wine s) ::ps) = s::(pname ps) | pname ((Coffee (s,_,_)) ::ps) = s::(pname ps) | pname (p :: ps) = pname ps; > val pname = fn : drink list -> string list

ML Datatypes.6 Patterns in Value Declarations  val P = E Defines the variables in the pattern P to have the corresponding values of expression E.  Extracting a brand name - val p = Beer “Paulaner"; - val (Beer s) = p; val s = " Paulaner " : string - val (Wine s) = Beer "Paulaner"; uncaught exception nonexhaustive binding failure  What will happen for the following: - val Water = Water; - val Water = “Paulaner"; val declaration fails if the matching fails (* OK *) (* FAIL - types mismatch *) Can't ruin constructors

ML Datatypes.7 Enumeration Datatypes  Enumeration types A datatype consisting of a finite number of constants datatype bool = true | false; fun not true = false | not false = true  But no order on the elements like Pascal, C

ML Datatypes.8 Polymorphic Datatypes  We can use datatypes to “unite” two different types: datatype number = whole of int | fraction of real;  We can abstract on this idea, and create a datatype uniting any two types; ‘a and ‘b datatype ('a,'b)union = type1 of 'a | type2 of 'b;  Three things are declared as a result: the type operator union the two constructors type1 : 'a -> ('a,'b)union type2 : 'b -> ('a,'b)union

ML Datatypes.9 Datatype ('a,'b)union  ('a,'b) union is a disjoint union of 'a and 'b Contains a copy of 'a OR a copy of 'b type1 and type2 can be viewed as labels that distinguish 'a from 'b  Allows several types where only a single type is allowed: ((string,int)union)list comprises string and integers [type2 Water,type1 "Technion"] : ((string,drink)union) list [type1 "Audi",type2 80,type1 "Technion"] : ((string,int)union)list type1 "Technion" : (string, 'a) union

ML Datatypes.10 The Disjoint union  Pattern-matching can test whether type1 or type2 is present - fun concat1 [] = "" | concat1 ((type1 s)::l) = s ^ concat1 l | concat1 ((type2 _)::l) = concat1 l; val concat1 = fn:(string, 'a)union list -> string  The disjoint union can express any other non-recursive datatype The type drink can be represented by: ((unit,string*int*bool)union,(string,string)union)union With the following constructors: Water = type1(type1()) Coffee(b,s,m)= type1(type2(b,s,m)) Wine(b)= type2(type1(b)) Beer(b)= type2(type2(b))

ML Datatypes.11 Trees  datatype 'a tree = Lf | Br of 'a * 'a tree * 'a tree - val tree2 = Br(2,Br(1,Lf,Lf),Br(3,Lf,Lf)); val tree2 = Br (2,Br (1,Lf,Lf),Br (3,Lf,Lf)) : int tree - val tree5 = Br(5,Br(6,Lf,Lf),Br(7,Lf,Lf)); val tree5 =... : int tree - val tree4 = Br(4,tree2,tree5); val tree4 = Br(4,Br(2,Br(1,Lf,Lf),Br(3,Lf,Lf)), Br(5,Br(6,Lf,Lf),Br(7,Lf,Lf))) : int tree - fun count Lf = 0 | count (Br(v,t1,t2)) = 1 + count t1 + count t2; val count = fn : ‘a tree -> int

ML Datatypes.12 Binary Search Trees  The search tree will hold pairs of (key,value). The key will be an int.  The tree is sorted so any key on the left sub-tree is smaller than the current key (larger for the right sub-tree) Remember: We will work on trees of pairs

ML Datatypes.13 Binary Search Trees - Search & Insert exception Bsearch of string; fun blookup(Br((a,x),t1,t2), b) = if b<a then blookup(t1,b) else if a<b then blookup(t2,b) else x | blookup(Lf,b) = raise Bsearch("lookup: "^Int.toString(b)); > val blookup = fn:(int * ‘a) tree * int -> ‘a fun binsert(Lf, b, y) = Br((b,y), Lf, Lf) | binsert(Br((a,x),t1,t2),b,y) = if b<a then Br((a,x),binsert(t1,b,y),t2) else if a<b then Br((a,x),t1,binsert(t2,b,y)) else (*a=b*) raise Bsearch("insert: "^Int.toString(b)); > val binsert = fn:(int * ‘a)tree * int * ‘a -> (int * ‘a)tree

ML Datatypes.14 Question from a test (2004) שאלה 1 סעיף א' נתון: datatype ('a,'b) union = type1 of 'a | type2 of 'b; נכתוב פונקציה חד-חד-ערכית foo ב-ML שהטיפוס שלה הוא: ( unit, (bool->'a) * (bool->'b) ) union -> ( unit, ( (bool,bool) union -> ('a,'b) union ) ) union להלן הגדרת הפונקציה. השלימו את החסר בה: fun foo (type1___) = _____ | foo (type2 (f: bool->'a, g: bool->'b) ) = ____________________________ _____________________________________; fun foo (type1()) = type1() | foo (type2((f: bool->'a,g: bool->'b))) = type2( fn (type1(b)) => type1(f(b)) | (type2(b)) => type2(g(b)) );