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.

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.
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 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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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 =
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.
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.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
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.
CSE 341 : Programming Languages Lecture 6 Fancy Patterns, Exceptions, Tail Recursion Zach Tatlock Spring 2014.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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.
Chapter 9: Functional Programming in a Typed Language.
1 Functional Programming Lecture 6 - Algebraic Data Types.
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.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
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:
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.
CSE 341 : Programming Languages Lecture 5 More Datatypes and Pattern Matching Zach Tatlock Spring 2014.
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.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
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 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching 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 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
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 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 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2017.
Adapted from slides by Nicholas Shahan and Dan Grossman
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
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 5 More Datatypes and Pattern-Matching Doug Woos Winter 2018.
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 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
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2019.
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 can return an answer, or fail to find one, or signal that a solution does not exist.  We can try to use ML datatypes:’ datatype int sol = Success of int | Failure | Impossible  Using special return values can be tedious, and requires explicit handling every time case methodA problem of Success s => Int.toString s | Failure => (case methodB problem of Success s => Int.toString s | Failure => "Both failed" | Impossible => "No Good") | Impossible => "No Good“  Sometimes we don’t really know what to do with the error, so we we’ll return a sol…

ML Exceptions.3  Separates the exceptional path from the normal path  Upon error, we raise an exception fun hd [] = raise Empty | hd (x::xs) = x  A raised exception is called packet  The packet propagates … fun add_hd xs ys = (hd xs) + (hd ys)  … up to the first point where a handler for it exists val x = add_hd [1] [] handle Empty => 1  The caller of a function doesn’t have to check any of the error values  The whole expression is a single expression Using Exceptions

ML Exceptions.4  We can raise only a specific type: the built-in type exn  The type exn is a special datatype with an extendable set of constructors and values  Declaring exceptions - values of type exn - exception Failure; - Failure; val it = Failure(-) : exn  Declaring exceptions – Constructors: - exception Problem of int; - Problem; val it = fn : int -> exn The Exception type exn

ML Exceptions.5  In normal expressions, exn behaves pretty much like a regular datatype (though it is not an equality type)  Values of type exn have all the privileges of values of other types - val x = Failure ; (* Failure is a value *) val x = Failure(-) : exn - val p = Problem 1; (* Problem is a constructor *) val p = Problem(-) : exn - map Problem [0, 1, 2]; val it = [Problem(-),Problem(-),Problem(-)] : exn list - fun what's_the_problem (Problem p) = p; Warning: match nonexhaustive Problem x =>... val what's_the_problem = fn : exn -> int The Exception type exn

ML Exceptions.6  raise Exp The expression Exp of type exn is evaluated to e raise Exp evaluates to an exception packet containing e Packets are not ML values  Packets propagates under the call-by-value rule  Which means that all of the following evaluate to raise Exp f (raise Exp) (raise Exp) arg raise (Exp1 (raise Exp)) (* Exp1 is constructor *) (raise Exp, raise Exp1) (* or {a=Exp, b=Exp1} *) let val name = raise Exp in some_expression end local val name = raise Exp in some_declaration end Raising Exceptions - Semantics

ML Exceptions.7 The Computation of take fun take (0, _) = [] | take (n, x::xs) = x :: take (n-1, xs) | take (n, nil) = raise Empty fun reverse3 xs = rev (take (3, xs)) - reverse3 [1,2]  rev (take (3, [1, 2]))  rev (1::take (2, [2]))  rev (1::(2::take (1, [])))  rev (1::(2::raise Empty))  rev (1::raise Empty)  rev (raise Empty)  raise Empty

ML Exceptions.8  General form: Exp_0 handle P1 => Exp_1 |... | Pn => Exp_n  Assuming Pis are disjoint, this is equivalent to (((Exp_0 handle P1 => Exp_1) … ) handle Pn => Exp_n)  All Exp_i must be type-compatible  All Pi must be valid pattern for the type exn  Calculating length using exceptions - fun len l = 1 + len (tl l) handle Empty => 0; Handling Exceptions - Syntax

ML Exceptions.9 Exp_0 handle Cons1 x => Exp_1  Assume Exp_0 evaluates to some value V Then the value of this expression is: Exp1 If Exp_0 evaluate to raise Cons1 x V otherwise. V may be either a normal value, or a non-matching raised exception.  Handle is a short-circuit operator, like if-then-else  All this is exactly equivalent to the familiar notions from C++ Handling Exceptions - Semantics

ML Exceptions.10 The Computation of len fun tl [] = raise Tl | tl (x::xs) = xs fun len xs = 1 + (len (tl xs)) handle Tl => 0; - len [3]  1+(len (tl [3])) handle Tl=>0  1+(len []) handle Tl=>0  1+(1+(len (tl [])) handle Tl=>0) handle Tl=>0  1+(1+(len (raise Tl)) handle Tl=>0) handle Tl=>0  1+(1+(raise Tl) handle Tl=>0) handle Tl=>0  1+(raise Tl handle Tl=>0) handle Tl=>0  1+0 handle Tl=>0  1 handle Tl=>0  1

ML Exceptions.11  The expression raise Exp is of type ‘a  Technically described as “t for some arbitrary type t”  It is NOT an expression of type exn!  Within context, it simply puts no restriction on other parts of the expression: - fun throw _ = raise Empty; val throw = fn : 'a -> 'b - fun bar x = if x>0 then x else raise Underflow; val bar = fn : int -> int  Theoretically, It may be type-checked as type None (aka Bot) or something similar to datatype ‘a Ex = ‘a The Type of raise Exp

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

ML Exceptions.13 - raise Problem; Error: argument of raise is not an exception [tycon mismatch] raised: int -> exn in expression: raise Problem -hd [“good”] handle nil => “bad” ; Error: handler domain is not exn [tycon mismatch] handler domain: 'Z list in expression: hd ("good" :: nil) handle nil => "bad" | exn => raise exn Error Messages

ML Exceptions.14 Exam Questions  What is the response 1. local exception E of string; in fun f (E “Hello”, E x) = x; end 2. let exception Exy of int in (fn 2 => raise Exy 4 | x => x+x) 2 handle Exy n => n end

ML Exceptions.15 Standard Exceptions Some 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 matches 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