ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
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.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
A First Look at ML.
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.
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.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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 First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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 Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
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.
Introduction to ML A Quasi-Functional Language With Strong Typing And Type Inference.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Approaches to Typing Programming Languages Robert Dewar.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Type Checking  Legality checks  Operator determination  Overload resolution.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Introduction to ML A Quasi-Functional Language With Strong Typing And Type Inference.
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.
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,
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.
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.
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.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
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.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Type Definitions cs776 (prasad) L8tdef.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Announcements Quiz 5 HW6 due October 23
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Presentation transcript:

ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis = if (null lis) then 0 else 1 + len (tl lis); va len = fn : ‘a list -> int Type inference for local entities - x * x * x; val it = 125: int (* it denotes the last computation*)

Operations on lists Similar to LISP, with conventional syntax: (hd, tl, ::) instead of (car, cdr, cons) - fun append (x, y) = if null (x) then y else hd (x) :: append (tl (x), y); val append = fn: ‘a list * a’list -> ‘a list (* a function that takes a pair of lists and yields a list *)

Currying: partial bindings a b c means (a b) c: (a b) yields a function that is applied to c - fun app2 x y = if null x then y = else (hd x) :: app2 (tl x) y; val app2 = fn : ‘a list -> ‘a list -> a’list (*a function that given a list yields a function that takes a list a yields a list *) - val ap123 = app2 [1, 2, 3]; val ap123 = fun : int list -> int list - ap123 [10, 20, 30]; val it = [1, 2, 3, 10, 20, 30]

The type system Primitive types: int, char, real, string Constructors: list, array, product (record), function an expression has a corresponding type expression the interpreter builds the type expression for each input type checking requires that type expression of functions and their arguments match, and that type expression of context match result of function

Type inference - fun incr x = x + 1; val incr = fn : int -> int because of its appearance in (x+1), x must be integer - fun add2 x = incr (incr x); val add2 = fn : int -> int incr returns an integer, so add2 does as well x is argument of incr, so must be integer val wrong = incr 7; Error: operator and operand don’t agree

Polymorphism - fun len x = if null x then 0 = else 1 + len (tl x); works for any kind of list. What is its type expression? val len : fn = ‘a list -> int ‘a denotes a type variable. Implicit universal quantification: for any a, len applies to a list of a’s. - fun copy lis = if null lis then nil = else hd (lis) :: copy (tl lis);

Type inference and unification Type expressions are built by solving a set of equations - fun reduce f lis init = if null lis then init = else f ((hd lis), reduce f (tl lis) init)) null : ‘a list -> bool hd : ‘b list -> ‘b tl : ‘c list -> ‘c list apply : (‘d -> e’) * ‘d -> ‘e (*function application*) let b be the type of init. Let a be the element type of lis. Then f takes an a and a b and returns a b. - val reduce = fn : (‘a -> b -> ‘b) -> (‘a list) -> ‘b -> ‘b

Unification algorithm A type variable can be unified with another variable ‘a unifies with ‘b => ‘a and ‘b are the same A type variable can be unified with a constant ‘a unifies with int => all occurences of a mean int A type variable can be with an expression ‘a unifies with ‘b list ‘a does not unify with ‘a list A constant can be unified with itself int is int An expression can be unified with another expression if the constructors are identical and if the arguments can be unified: (int -> int) list unifies with ‘a list, ‘a is a function on integers

Type system does not handle overloading well - fun plus x y = x + y; operator is overloaded, cannot be resolved from context (error in some versions, defaults to int in others) Can use explicit typing to select interpretation: - fun mix (x, y,z) = x * y + z:real; mix : (real * real * real) -> real

Parametric polymorphism vs. generics A function whose type expression has type variables applies to an infinite set of types. Equality of type expressions means structural equivalence. All applications of a polymorphic function use the same body: no need to instantiate. - let val ints = [1, 2, 3]; = val strs = [“this”, “that”]; = in = len ints + len strs (* int list -> int, string list -> int *) = end; val it = 5: int

User-defined types and inference A user-defined type introduces constructors: datatype tree = leaf of int | node of tree * tree leaf and node are type constructors can define functions by pattern: - fun sum (leaf (t)) = t = | sum (node (t1, t2)) = sum t1 + sum t2; val sum = fn : tree -> int

Parameterized datatypes - fun flatten (leaf (t)) = [t] = | flatten (node (t1, t2)) = flatten flatten (t2); flatten: tree -> int list - datatype ‘a gentree = leaf of ‘a = | node of ‘a gentree * ‘a gentree; - val names = node (leaf (“this”), leaf (“that”)); val names = … string gentree