ML: A Strongly Typed Functional Language Interpreted (compilers available): -bash-3.00$ poly Poly/ML 5.4 Release > 1+3; val it = 4 : int >o ^D -bash-3.00.

Slides:



Advertisements
Similar presentations
Higher-order functions in ML
Advertisements

Type Variables in ML Until we know the type of a value (perhaps never!), we use a variable for its type Book uses, e.g., t1, tx, tf PL literature uses.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
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.
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.
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.
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.
RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
SEMINAL: Efficiently Searching for Type-Error Messages Benjamin Lerner Dan Grossman, Craig Chambers University of Washington.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
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 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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
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.
Functional programming Functional style makes heavy use of functions as values Hence, functional languages provide powerful constructs for manipulating.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
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.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Type Checking and Type Inference
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
Chapter 4: Types.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
Polymorphism Programming Language Design and Implementation
Functions, Patterns and Datatypes
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
ML’s Type Inference and Polymorphism
Polymorphism Programming Language Design and Implementation
ML’s Type Inference and Polymorphism
Modern Programming Languages
Polymorphism Programming Language Design and Implementation
Functions, Patterns and Datatypes
Polymorphism Programming Language Design and Implementation
Presentation transcript:

ML: A Strongly Typed Functional Language Interpreted (compilers available): -bash-3.00$ poly Poly/ML 5.4 Release > 1+3; val it = 4 : int >o ^D -bash-3.00

ML: A Strongly Typed Functional Language Strongly typed: > ; Error-Type error in function application. Function: + : real * real -> real Argument: (1.5, 3) : real * int Reason: Can't unify int (*In Basis*) with real (*In Basis*) (Different type constructors) Found near Static Errors

ML: A Strongly Typed Functional Language Functional: > fun square(x:int) = x*x; val square = fn : int -> int > map square [1,2,3]; val it = [1,4,9] : int list

ML: A Strongly Typed Functional Language Type-inferencing: > fun add(x:int, y) = x+y; val add = fn : int * int -> int > fun foo(a, b, c) = if a then b else 1+c; val foo = fn : bool * int * int -> int

ML: A Strongly Typed Functional Language Pattern-matching: > fun fact(n) = if n = 0 then 1 else n * fact(n-1); val fact = fn : int -> int > fact(5); val it = 120 : int > fun fact(0) = 1 | fact(n) = n * fact(n-1); val fact = fn : int -> int > fact(5); val it = 120 : int

ML: Lists > val a =[1,3,5,7,9]; val a = [1,3,5,7,9] : int list > hd(a); val it = 1 : int > tl(a); val it = [3,5,7,9] : int list > [11]; val it = [1,3,5,7,9,11] : int list

ML: Lists > fun rev(ls) = if null(ls) then nil else [hd(ls)]; val rev = fn : 'a list -> 'a list > rev([1,3,5,7,9]); val it = [9,7,5,3,1] : int list > > fun rev(nil) = nil | rev(x::t) = [x]; val rev = fn : 'a list -> 'a list > rev([1,3,5,7,9]); val it = [9,7,5,3,1] : int list

ML: Tuples (single, double, triple, quadruple, quintuple, sextuple, heptuple, octuple,..., -tuple): > [1,2,3]; val it = [1,2,3] : int list > (1,2,3); val it = (1,2,3) : int * int * int > [1,2,3.5]; Error-Elements in a list have different types. > (1,2,3.5); val it = (1,2,3.5) : int * int * real

ML: Tuples So f(3,4) is a fun of one arg: tuple of type int*int Recall imaginary numbers / complex arithmetic: C = {ai + b}, i = √-1 So can represent C as real*real: - fun cmag (a, b) = sqrt(a*a + b*b); val cmag = fn : real * real -> real % how inferred? - cmag (3.0, 4.0); val it = 5.0 : real

ML: Tuples - fun cadd(a1:real, b1:real) (a2, b2) = (a1+a2, b1+b2); val cadd = fn : real * real -> real * real -> real * real - cadd (3.3, 4.4) (5.5, 6.6); val it = (8.8,11.0) : real * real - fun cadd (a1,b1) (a2,b2) = (a1+a2,b1+b2):real*real; val cadd = fn : real * real -> real * real -> real * real