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
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Modern Programming Languages, 2nd ed.
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 [3,4]
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 Fourth Look At ML 1. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML datatype bool = true | false;
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.
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 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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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 =
Module Language. Module language The Standard ML module language comprises the mechanisms for structuring programs into separate units. –Program units.
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 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures 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 =
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
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,
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
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.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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 341 : Programming Languages Lecture 5 More Datatypes and Pattern Matching Zach Tatlock Spring 2014.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
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 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 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 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
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 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
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 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  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  Can be declared locally using let/local 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  Fixing hd and tl - exception Empty; - fun hd (x::_) = x | hd [] = raise Empty; val hd = fn : 'a list -> 'a - fun tl (_::xs) = xs | tl [] = raise Empty; val tl = fn : 'a list -> 'a list Raising Exceptions - Example

ML Exceptions.8  General form: Exp_0 handle P1 => Exp_1 |... | 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 expression raise Exp is of type ‘a  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 The Type of raise Exp

ML Exceptions.11  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.12 - 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 - hd [“good”] handle Empty => false ; Error: expression and handler don't agree [tycon mismatch] body: string handler range: bool in expression: hd ("good" :: nil) handle Empty => false | exn => raise exn Error Messages

ML Exceptions.13 Exam Questions  What is the response 1. local exception E of string; in fun f (E “Hello”, E x) = x; end stdIn: Warning: match nonexhaustive (E "Hello",E x) =>... val f = fn : exn * exn -> string 2. let exception Exy of int in (fn 2 => raise Exy 4 | x => x+x) 2 handle Exy n => n end val it = 4 : int

ML Exceptions.14 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