Cs776 (Prasad)L16rem1 Remaining Features of SML97 Records Exceptions Reference Types Arrays.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Modern Programming Languages, 2nd ed.
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
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.
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.
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 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
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.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
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 박성우.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
1 Functional Programming Lecture 6 - Algebraic Data Types.
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.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
ML programming Effects. Contents Effects Exceptions References I/O.
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.
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Type Checking and Type Inference
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
SML-97 Specifics SML/NJ 110 cs7120(Prasad) L4-SML.
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.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Abstract Syntax cs7100 (Prasad) L7AST.
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Presentation transcript:

cs776 (Prasad)L16rem1 Remaining Features of SML97 Records Exceptions Reference Types Arrays

cs776 (Prasad)L16rem2 Records type component = { key : int, info : string }; val part = { key = 3, info = “nail” }; (* val part = {info="nail",key=3} : {info:string, key:int} *) #key part = 3; part = {info = “na” ^ “il”, key = 3-0 }; (* val it = true : bool *) Order of fields is irrelevant when matching. (2, “two”) = { 1 = 2, 2 = “two” }; Tuples are special records with numbers as field labels.

cs776 (Prasad)L16rem3 Pattern Matching for Records fun info_compare (p1: component ) (p2: component ) = String.compare (#info p1, #info p2); fun key_compare ({key=k1,info=_}: component ) ({key=k2,... }: component ) = Int.compare (k1, k2); (* Wildcards : Type name mandatory with ellipses. *) fun check_info (c as {key,info}: component ) = (#info c = info) ; (* Using a field name to refer to value *) (* maps a component to true *)

cs776 (Prasad)L16rem4 Exceptions An exception name is a constructor of the built-in type exn. This datatype is special in that the set of constructors can be extended. - exception Overflow; - exception message of string; - exception outOfRange of int*int; - Overflow; (* val it = Overflow( - ) : exn *) - message; (* val it = fn : string -> exn *) - outOfRange; (* val it = fn : int * int -> exn *) Predefined exceptions : Div, Empty, Match,... etc.

cs776 (Prasad)L16rem5 Operations on exception raise raise exn-name ; handle expression handle exn_name1 => … | exn_name2 => … ; Organization “Trigger” exception Handle exception Raise exception Define exception CLIENTSERVER

cs776 (Prasad)L16rem6 Motivation for incorporating exception Robustness –Language/application specified behavior on error Context-sensitive handling –Retry to overcome transient errors –Attempt another solution strategy (heuristic) on failure –Rescue reinstate any required invariant (consistency) Dynamic Handling via run-time call stack

cs776 (Prasad)L16rem7 Examples exception fail of string; fun search name [] = raise (fail name) | search name ((x,v)::xvs) = if (name = x) then v else search name xvs; datatype employers = wsu | wpafb | ncr | lexis_nexis ; val name_emp = [(“john”,wsu),(“jane”,ncr)]; (search “jill” name_emp) handle (fail elem) => wpafb; Server Client

cs776 (Prasad)L16rem8 (cont’d) val name_car = [(“john”, “acura”),(“jane”, “lexus”)]; (search “jill” name_car) handle (fail elem) => elem ^ “ owns Dodge Stealth!”; val name_tel = [(“john”,8501),(“jane”,1359)]; (search “jill” name_tel) handle (fail elem) => elem ^ “’s phone number is unlisted.”; (* type error *)

cs776 (Prasad)L16rem9 References and Assignments

cs776 (Prasad)L16rem10 (ML) Redefinition val x = [5]; val x = 2; (* The first list is inaccessible (garbage). *) (ML) Shadowing let val x = 5 in (let val x = [2] in (hd x) end) + x end; (Pascal) Assignment var i: int; begin i := 0; i := i + 1 end; Redefinition and shadowing are different from assignment.

cs776 (Prasad)L16rem11 ML supports a separate sub-language to deal with variables and assignments (that can change value bound to a variable). val x = ref 5; x := 6; x := 1 + !x; l-value r-value (address of location) (contents of location) 5 References x

cs776 (Prasad)L16rem12 Iteration : Pascal vs ML var i, sum : int := 0; var n : int := 5; while i < n do begin i := i + 1; sum := sum + i end; The l-value of a variable on the rhs of an assignment is automatically coerced to its r-value. val i = ref 0; val sum = ref 0; val n = 5; while !i < n do ( i := !i + 1 ; sum := !sum + !i ); A reference variable on the rhs of an assignment must be explicitly dereferenced, to get its r-value.

cs776 (Prasad)L16rem13 Aliasing problem fun rot3 (a,b,c) = let val t = !a in a := !b; b := !c; c := t end; val i = ref 0; val j = ref 1; val k = ref 2; (!i,!j,!k); rot3 (i,j,k); (!i,!j,!k); (* (0,1,2), () : unit, (1,2,0) *) val i = ref 0; val j = ref 1; val k = ref 2; (!i,!j,!i); rot3 (i,j,i); (!i,!j,!i); (* (0,1,0), () : unit, (0,1,0) *)

cs776 (Prasad)L16rem14 Encapsulating State : Objects fun new_counter () = let val cnt = ref 0 fun tick () = ( cnt := !cnt + 1; !cnt ) fun reset () = ( cnt := 0 ) in { tick = tick, reset = reset } end; val c1 = new_counter(); val c2 = new_counter(); (#tick c1) (); (* 1 *) (#tick c2) (); (* 1 *) (#tick c1) (); (* 2 *) (#tick c2) (); (* 2 *) (#reset c1) (); (#tick c1) (); (* 1 *) (#tick c2) (); (* 3 *) (* Object-based programming *)

cs776 (Prasad)L16rem15 Mutable Data Structure : structure Array open Array; val n = 3; val M = array(n, array(n,0)); (* Constructs array whose top- level elements share the same inner array. *) val i = ref 0; while (!i < n) do ( update(M,!i,array(n,0)); i := !i + 1 ); (* creates n x n locations initialized to 0 *) i := 0; val j = ref 0; while (!i < n) do ( while (!j < n) do ( update(sub(M,!i),!j,!i + !j); j := !j + 1 ); j := 0; i := !i + 1 ); (* initializes M[i,j] to i+j. *) (* M = [|[|0,1,2|],[|1,2,3|],[|2,3,4|]|] : int array array *)

cs776 (Prasad)L16rem16 Interaction with polymorphism polymorphicNormally, variables/functions can be polymorphic, but not values. fun id x = x; id 5; ( (fn x => (id x)) 5 ); ( (fn f => (f 5)) id ); (* (fn f => (f 5)) : (int -> 'a) -> 'a *) (id 5, id true); ( (fn f => (f 5, f true)) id ); (* Type error *)

cs776 (Prasad)L16rem17 Polymorphic references are banned. val fp = ref id; (* potential type: ’a->’a ref *) fp := not; !fp 5; Polymorphic exceptions are banned. exception fail of ’a; (raise fail true) handle (fail x) => 0 * x; Value Restriction for polymorphism