Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

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 Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
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.
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.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the 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 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.
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.
Scope Chapter Ten Modern Programming Languages.
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 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction 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 =
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
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.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Lecture #2, Jan. 10, 2007 Evaluating expressions Defining functions
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
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,
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.
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.
SE424 Languages with Context A Block Structured Language.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CSC 212 Object-Oriented Programming and Java Part 2.
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.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
An introduction to functional programming using Haskell CENG242 –Recitation 1.
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
Introduction to C++ Programming
SML-97 Specifics SML/NJ 110 cs7120(Prasad) L4-SML.
FP Foundations, Scheme In Text: Chapter 14.
CSE 341 Section 5 Winter 2018.
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
ML’s Type Inference and Polymorphism
Chapter 7: Expressions and Assignment Statements Sangho Ha
ML’s Type Inference and Polymorphism
PROGRAMMING IN HASKELL
Presentation transcript:

cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110

cs776(Prasad)L6sml972 Getting Started Invoking interpreter (actually a compiler in interactive mode!) % sml Loading ML program - use “~/eg.sml”; Accessing a structure element –Int.max; Opening a structure –open Real; Top-level environment contains a selected set of useful functions. A more complete set of standard functions is organized into structures ( Int, Real, String, etc) that forms the standard basis. - Real.Math.sqrt(4.0) - List.take(2,[1,2,3])

cs776(Prasad)L6sml973 Expressions –~ ; val it = 20 : int; –“1\t\na\t\n”; tab, newline,… –“\\\”” ^ “abc”; Slash, double quotes, concatenation - #“a”; character –#“Z” < #“a” ; –“ab” < “acd”; –1 <> 2; –1 < 2 orelse 0/0; –if true andthen not (1>=2) then 3.0 else ; if-then control structure is legal, but not expression.

cs776(Prasad)L6sml974 “Tip of the” Type Errors — ; coercion —real(1) + 2.0; —1 + floor(2.0); —1/2; —#“a” ^ “bb”; —if true then #“a” else “bb”; —1::[2.0]; real is not an equality type. –Portability reasons. (Cf. (int->int) is not an equality type for computability reasons.) In the absence of rounding errors, (r=s) is equivalent to (r<=s) andalso (s<=r).

cs776(Prasad)L6sml975 Identifiers Alphanumeric Identifiers a’1_b2, B2_bomber, …, etc –ordinary variables ’a’b’c, ’’a, etc –type variables Symbolic Identifiers |?:&, +, /, etc Separators (, ), [,],{,}, ”,.,,, ; ML is case-sensitive.

cs776(Prasad)L6sml976 Variables Identifiers vs Variables vs Values Environment vs Store Adding new variable and binding val i = 10; val i = [1,2,3]; (Cf., “ := ” in Pascal and “ = ” in C.) Static scoping (Closure) val a = 2; fun ax(x) = a * x; val a = 5; ax 10; (* ax 10 = 20 *)

cs776(Prasad)L6sml977 Patterns fun merge (L as x::xs, M as y::ys) = … fun comb(_,0) = … fun f ((x,y)::z::xs) = … Illegal cases: fun length [x]) = … fun f (x+1) = … fun g (0.0) = … Term Matching (cf. Unification)

cs776(Prasad)L6sml978 fun same (n,n) = 0 | same _ = 1; Error : duplicate variable in pattern (* requires equational reasoning *) fun fp nil = 0 | fp (x::xs) = 1; val fp = fn : 'a list -> int fun feq xs = if (xs = nil) then 0 else 1; val feq = fn : ''a list -> int

cs776(Prasad)L6sml979 Match Expressions val rec reverse = fn nil => nil | x::xs => [x] ; rule : pattern => expression case x < y of true => x | false => y ; if x < y then #“1” else 1; Error: Type of rules do not agree.

cs776(Prasad)L6sml9710 Limitations on the use of polymorpic functions fun id x = x; (* id :’a -> ’a *) id 0; (* id : int -> int *) id id 0; (* id :(int -> int) -> (int -> int) *) val id = fn x => x; (* id :’a -> ’a *) To ensure that a bound variable has the same meaning as its binding, the variable introduced by a val- declaration is allowed to be polymorphic only if the right-hand side is a value (and not a computation).

cs776(Prasad)L6sml9711 val J = id id; (*illegal*) Motivation: Contexts (such as (J 5, J “a”) ) that replicate the binding may impose conflicting requirements on the type. (cf. Multiple evaluation of side-effect causing expressions.) (cf. Referential transparency violated if two occurrences of an expression is not equivalent to two occurrences of its value.) Value Restriction is too conservative. val k = nil; (* illegal *)

cs776(Prasad)L6sml9712 Value Restriction on Polymorphic Declaration ILLEGAL: id id; id foldr; LEGAL: fun J x = id id x; id id “a”; id Int.max; let val v = id id in v “a” end;

cs776(Prasad)L6sml9713 Polymorphic type variable: ’a Generalizable ( universal quantification ) for every type T, there is an instance of this object Non-generalizable ( existential quantification ) there exists a fixed but arbitrary type T for this object For compile-time guarantees, SML-97 requires that the expressions at the top- level be such that the generalizable interpretation of type variables is appropriate.

cs776(Prasad)L6sml9714 Non-expansive Expressions A constant or a variable is non-expansive. A function definition is non-expansive. A tuple of non-expansive expressions is non- expansive. A non-expansive expression may be preceded by a data/exception constructor. Expressions that are not of these forms are expansive and are not allowed to have type variables. –Generates a “type variable not generalizable” error.

cs776(Prasad)L6sml9715 Non-generalizable type variable error All the following conditions must be met for the error to occur: –The expression is at the top-level. –The type of the expression involves at least one type variable. –The form of the expression does not meet the conditions for it to be non-expansive.

cs776(Prasad)L6sml9716 Examples —id id ; (* error *) —(id, id) ; val it = : ('a -> 'a)*('b -> 'b) —id (id: int-> int ) ; —[id, id] val it = [fn,fn] : ('a -> 'a) list (* Allowed by SML97 even though according to Ullman’s book it is illegal? *)

cs776(Prasad)L6sml9717 (cont’d) —let val x = (id id) in x(1) end; (* Legal. (id id) is not top-level. *) —let val x = (id id) in (x 1, x “a”) end; (* Error: operator and operand don't agree.*) —(id 1, id “a”) ; (* val it = (1,"a") : int * string *) —let val x = id in (x 1, x “a”) end;