ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,

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.
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]
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
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.
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 Data  Consists of constructions that can be inspected, taken apart, or joined to form.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
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.
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.
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.
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.
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 박성우.
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.
A Binary Search Tree Binary Search Trees.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
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.
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.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
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.
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.
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
CMSC 330: Organization of Programming Languages Object Oriented Programming with OCaml.
String is a synonym for the type [Char].
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
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
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSE 341 Section 2 Nick Mooney Spring 2017
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Review Previously User-defined data types: records, variants
Presentation transcript:

ML Datatypes.1 Standard ML Data types

ML Datatypes.2 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.3 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.4 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.5 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 = : 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 = : acceleration

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;  Constructors are functions - Kg;val it = fn : real -> mass - Newton;val it = fn : real -> force - val body = Kg ; val engine = Newton 50.0; val body = Kg : mass val engine = Newton 50.0 : force May be passed to other functions - map Kg [1.2, 5.3];

ML Datatypes.7 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' : acceleration - a engine body; Error: operator and operand don't agree [tycon mismatch] operator domain: mass operand: force in expression:

ML Datatypes.8  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: fun area (point | Line _) = 0.0 | area (Circle r) = Math.pi*r*r | area (Rectangle (w, h)) = w * h; val area = fn : shape -> real Variant Types

ML Datatypes.9  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; val declaration fails if the matching fails (* OK. *) (* FAIL - types mismatch *) Constructors cannot be rebound in a binding declaration Pattern Matching

ML Datatypes.10  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 Recursive Datatypes

ML Datatypes.11  “ intlist ”? Seriously? What’s so special about int s?  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” Polymorphic Datatypes Recursive Datatypes

ML Datatypes.12  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 | type2 of 'b;  Example – in the next slide Polymorphic Datatypes

ML Datatypes.13 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 Union - Example

ML Datatypes.14 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.15 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.16 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 fun compare (k1,_) (k2,_) = compare (k1, k2) in 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) end