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.

Slides:



Advertisements
Similar presentations
Higher-order functions in ML
Advertisements

Modern Programming Languages, 2nd ed.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
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.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
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 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
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.
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
CSE 341 : Programming Languages Lecture 6 Fancy Patterns, Exceptions, Tail Recursion Zach Tatlock Spring 2014.
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,
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.
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.
Cs776 (Prasad)L16rem1 Remaining Features of SML97 Records Exceptions Reference Types Arrays.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE 341 Lecture 3 let expressions; pattern matching Ullman
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
341 midterm review Xinrong Zhao Autumn 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

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 a problem solution (like int) or fail to find the solution or find that a solution does not exists.  We can try to use ML datatypes: datatype sol = Success of int | Failure | Impossible;  Using the failure return values can be tedious case methodA(problem) of Success s => show s | Failure => (case methodB(problem) of Success s => show s | Failure => "Both failed" | Impossible => "No Good") | Impossible => "No Good"

ML Exceptions.3 Exceptions  Instead of using the return value to report an error we will use a mechanism outside the return value: Exceptions.  When an error is discovered we will raise an exception  The exception will propagate up the stack until someone handles it.  The caller of a function doesn’t have to check all or any of the error values.  In VERY pseudo-code: fun inner = do calculation if local error raise local_error, if global error raise global_error fun middle = inner(…) handle local_error fun outer = middle(…) handle global_error

ML Exceptions.4 Exceptions in ML  We can raise only a specific type: the built-in type exn.  exn is a datatype with an extendable set of constructors.  Declaring exceptions exception Failure; exception Impossible; exception Problem of int;  Defines the following constructors: Failure : exn; Impossible : exn; Problem : int -> exn;  Can be declared locally using let/local  Values of type exn have all the privileges of values of other types, and in addition, a special role in the operations raise and handle

ML Exceptions.5 Raising Exceptions  raise Exp The expression Exp of type “ exn of ‘a”, is evaluated to e raise Exp evaluates to an exception packet containing e  Packets are not ML values  Packets propagate under the call-by-value rule If E returns a packet then that is the result of f(E) f(raise Ex) is equivalent to raise Ex raise(Badvalue(raise Failure)) is equivalent to raise Failure

ML Exceptions.6 Raising Exceptions  Expressions are evaluated from left to right.  If a packet is returned during the evaluation then it is returned as the result, and the rest of the expression is not evaluated.... let val D = E1 in E2 end; If E1 evaluates to an exception packet so does the entire let expression

ML Exceptions.7 Handling Exceptions  Block handle exp1 => Block1 |... | expN => BlockN  If the result of Block is a packet: The packet’s contents are examined. If no pattern matches, the exception is propagated  If the result is not a packet, the value is passed on as usual  Fixing hd and tl - exception Hd;- exception Tl; - fun hd (x::_) = x- fun tl (_::xs) = xs | hd [] = raise Hd; | tl [] = raise Tl; val hd = fn : 'a list -> 'aval tl = fn : 'a list -> 'a list  Calculating length using exceptions fun len l = 1 + len(tl l) handle Tl => 0;

ML Exceptions.8 Another Example Sum of a list’s elements in positions i,f(i),f(f(i)),... until the current position is illegal - exception illegalPos; - fun getNth(x::_,0) = x | getNth (x::xs,n) = if n>0 then getNth(xs,n-1) else raise illegalPos | getNth _ = raise illegalPos; val getNth = fn : 'a list * int -> 'a - fun sumchain (l,f,i) = getNth(l,i)+sumchain(l,f,f(i)) handle illegalPos => 0; > val sumchain = fn : int list * (int -> int) * int -> int

ML Exceptions.9 Using Exception Handling - More  Example: If methodA fails then methodB is tried case methodA(problem) of Success s => show s | Failure => (case methodB(problem) of Success s => show s | Failure => "Both failed" | Impossible => "No Good") | Impossible => "No Good"  Exceptions give a shorter and clearer program. Error propagation does not clutter the code. show ( methodA(problem) handle Failure => methodB(problem) ) handle Failure => "Both failed" | Impossible => "No Good"

ML Exceptions.10 Question from Exam  Given exception E1; exception E2; fun f(1) = raise E1 | f(2) = raise E2 | f(n) = ( let val x = f(n-2) handle E2 => 2 in f(n-1) + x end ) handle E1 => 1;  What will be returned for f(5)?

ML Exceptions.11 Question from Exam exception E1; exception E2; fun f(1) = raise E1 | f(2) = raise E2 | f(n) = ( let val x = f(n-2) handle E2 => 2 in f(n-1) + x end ) handle E1 => 1; f(1) = raise E1 f(2) = raise E2 f(3) = (let val x = ( f(1) )...) f(4) = (let val x = ( f(2) ) f(5) = (let val x = ( f(3) )... raise E1 handle E1 => 1 = 1 raise E2 handle E2 => 2 in f(3) + x end 12 = 3 in f(4) + x end 31 = 4 1

ML Exceptions.12 Standard Exceptions Built-in exceptions Chr is raised by chr(k) if k 255 Match is raised for failure of pattern-matching (e.g. when an argument matchs none of the function’s patterns, if a case expression has no pattern that matches, etc.) Bind is raised if the value of E does not match pattern P in the declaration val P = E