Standard ML. Exceptions

Similar documents
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Inductive Data Types

A Brief Introduction to Standard ML

Mini-ML. CS 502 Lecture 2 8/28/08

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS 345. Exceptions. Vitaly Shmatikov. slide 1

Sample Exam; Solutions

CSE3322 Programming Languages and Implementation

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSE341 Spring 2016, Midterm Examination April 29, 2016

Products and Records

Chapter 3 Linear Structures: Lists

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010

CSE3322 Programming Languages and Implementation

Part I: Written Problems

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Chapter 3 Linear Structures: Lists

Scope, Functions, and Storage Management

Functional Paradigm II

Lecture 19: Signatures, Structures, and Type Abstraction

CSE 341 Sample Midterm #2

CS153: Compilers Lecture 15: Local Optimization

SML A F unctional Functional Language Language Lecture 19

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Exercises on ML. Programming Languages. Chanseok Oh

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Introduction to OCaml

CSCC24 Functional Programming Typing, Scope, Exceptions ML

CSc 372, Fall 1996 Mid-Term Examination Monday, October 21, 1996 READ THIS FIRST

References and Exceptions. CS 565 Lecture 14 4/1/08

Standard ML. Data types. ML Datatypes.1

Midterm II CS164, Spring 2006

Part I: Written Problems

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Programming Languages

15 150: Principles of Functional Programming. Exceptions

Handout 2 August 25, 2008

Variables and Bindings

CSc 372, Spring 1996 Mid-term Examination Solution Key

CSE341, Spring 2013, Final Examination June 13, 2013

Specification and Verification in Higher Order Logic

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Some Advanced ML Features

CSC324 Functional Programming Typing, Exceptions in ML

Patterns The Essence of Functional Programming

Recap: ML s Holy Trinity

CSE341 Spring 2017, Midterm Examination April 28, 2017

G Programming Languages - Fall 2012

Programming Languages

COMPUTER SCIENCE TRIPOS

Begin at the beginning

UNIVERSITETET I OSLO

Functional Paradigm II

The type checker will complain that the two branches have different types, one is string and the other is int

Programming Languages and Techniques (CIS120)

Standard ML. Curried Functions. ML Curried Functions.1

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

CSE341 Spring 2017, Final Examination June 8, 2017

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Recap: Functions as first-class values

Lectures 24 and 25: Scheduling; Introduction to Effects

CSCI-GA Final Exam

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Examination Questions Midterm 1

CIS 341 Final Examination 3 May 2011

Functional Programming

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

DO NOT OPEN UNTIL INSTRUCTED

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

CSE341 Spring 2016, Final Examination June 6, 2016

Name: Entry: Gp: 1. CSL 102: Introduction to Computer Science. Minor 2 Mon 21 Mar 2005 VI 301(Gps 1-6)& VI 401(Gps 7-8) 9:30-10:30 Max Marks 40

CSE341 Autumn 2017, Final Examination December 12, 2017

Datatype declarations

CSE 341 : Programming Languages

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 27, 2017

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Fall Recursion and induction. Stephen Brookes. Lecture 4

Lists. Michael P. Fourman. February 2, 2010

CSE341 Spring 2017, Final Examination June 8, 2017

Introduction to SML Getting Started

A quick introduction to SML

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Programming Languages

CS153: Compilers Lecture 16: Local Optimization II

Principles of Functional Programming

Collections and Combinatorial Search

Programming in Standard ML: Continued

Transcription:

Standard ML Exceptions ML Exceptions.1 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 Failure => (case methodb(problem) of Failure => "Both failed" Impossible => "No Good") Impossible => "No Good" ML Exceptions.2 ML Exceptions 1

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.3 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 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.4 ML Exceptions 2

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.5 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.6 ML Exceptions 3

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 -> 'a val tl = fn : 'a list -> 'a list Calculating length using exceptions fun len l = 1 + len(tl l) handle Tl => 0; ML Exceptions.7 Another Example Sum of a list s elements in positions i,f(i),f(f(i)),... - exception Nth; - fun nth(x::_,0) = x nth(x::xs,n) = if n>0 then nth(xs,n-1) else raise Nth nth _ = raise Nth; val nth = fn : 'a list * int -> 'a - fun sumchain (l,f,i) = nth(l,i)+sumchain(l,f,f(i)) handle Nth => 0; > val sumchain = fn : int list * int -> int ML Exceptions.8 ML Exceptions 4

Using Exception Handling - More Example: If methoda fails then methodb is tried case methoda(problem) of Failure => (case methodb(problem) of 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.9 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.10 ML Exceptions 5

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 raise E1 f(3) = (let val x = ( f(1) )...) handle E1 => 1 = 1 raise E2 f(4) = (let val x = ( f(2) ) 1 2 handle E2 => 2 in f(3) + x end = 3 f(5) = (let val x = ( f(3) )... 1 3 1 in f(4) + x end = 4 ML Exceptions.11 Built-in exceptions Standard Exceptions Chr is raised by chr(k) if k<0 or 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 ML Exceptions.12 ML Exceptions 6