Exceptions Exceptions Amtoft from Hatcliff Raising Exceptions - - PowerPoint PPT Presentation

exceptions
SMART_READER_LITE
LIVE PREVIEW

Exceptions Exceptions Amtoft from Hatcliff Raising Exceptions - - PowerPoint PPT Presentation

Exceptions Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application ML provides an elegant exception handling mechanism 1. built-in exceptions 2. partial functions 3. user-defined exceptions 4. exception


slide-1
SLIDE 1

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exceptions

ML provides an elegant exception handling mechanism

  • 1. built-in exceptions
  • 2. partial functions
  • 3. user-defined exceptions
  • 4. exception handling
  • 5. further applications
slide-2
SLIDE 2

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Built-in Exceptions

− 5 div 0; uncaught exception Div [ d i v i d e by zero ] r a i s e d at : . . . − chr (5 00); uncaught exception Chr r a i s e d at : . . . − hd ( n i l : i n t l i s t ) ; uncaught exception Empty r a i s e d at : . . .

slide-3
SLIDE 3

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exceptions for Non-Total Functions

Some functions are naturally undefined for some input. Dealing with that can be awkward:

fun l o o k u p t a b l e x n i l = NONE | l o o k u p t a b l e x (( x ’ , v ’ ) : : t ) = i f x = x ’ then SOME v ’ else l o o k u p t a b l e x t ; (∗ ’ ’ a −> ( ’ ’ a ∗ ’b) l i s t −> ’b option ∗)

Instead, one may explicitly raise an exception:

exception Empty table ; fun l o o k u p t a b l e x n i l = r a i s e Empty table | l o o k u p t a b l e x (( x ’ , v ’ ) : : t ) = i f x = x ’ then v ’ else l o o k u p t a b l e x t ; (∗ ’ ’ a −> ( ’ ’ a ∗ ’b) l i s t −> ’b ∗)

slide-4
SLIDE 4

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exceptions with Parameters

exception Empty table

  • f

s t r i n g ; fun l o o k u p t a b l e x n i l = r a i s e Empty table ( x ) | l o o k u p t a b l e x (( x ’ , v ’ ) : : t ) = i f x = x ’ then v ’ else l o o k u p t a b l e x t ; (∗ s t r i n g −> ( s t r i n g ∗ ’ a ) l i s t −> ’ a ∗)

Note: polymorphism of function has been lost

− l o o k u p t a b l e ”mary” [ ( ” joe ” ,12) ,( ”ed” , 7 ) ] ; uncaught exception Empty table − Empty table ; val i t = fn : s t r i n g −> exn − r a i s e Empty table ; Error : argument of r a i s e i s not an exception

slide-5
SLIDE 5

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exception Handling

Wrap a handler around an exception returning expression:

fun lookup table ’ x v = l o o k u p t a b l e x v handle Empty table ( s ) = > ( p r i n t ( ” Entry not found : ” ) ; p r i n t ( x ) ; p r i n t ( ”\n” ) ; 0 ) ; (∗ s t r i n g −> ( s t r i n g ∗ i n t ) l i s t −> i n t ∗) − l o o k u p t a b l e ”ed” [ ( ” joe ” ,12) ,( ”ed” , 7 ) ] ; val i t = 7 : i n t − l o o k u p t a b l e ”mary” [ ( ” joe ” ,12) ,( ”ed” , 7 ) ] ; Entry not found : mary val i t = 0 : i n t

slide-6
SLIDE 6

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Giving Change

Problem: given a set of coins (infinite supply of each denomination), produce

◮ exact change for a given amount ◮ involving minimal number of coins.

This may not always be possible return 7c using 5c coins and 3c coins but is always possible if we have 1c coins.

◮ We would like not to test all combinations

Greedy Strategy: return as many as possible from highest denomination, then as many as possible from second-highest denomination, etc.

◮ this is not always optimal:

return 8c using 5c,4c,1c

◮ but for US coin set {25,10,5,1} it is optimal

(though not trivial to prove)

slide-7
SLIDE 7

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Greedy Implementation

fun change ( coins , 0 ) = [ ] | change ( c : : coins , amount ) = i f amount < c then change ( coins , amount ) (∗ take l a r g e s t coin p o s s i b l e ∗) else c : : change ( c : : coins , amount−c ) change ( [ 2 5 , 1 0 , 5 , 1 ] , 4 8 ) ; val i t = [25 ,10 ,10 ,1 ,1 ,1] : i n t l i s t change ( [ 5 , 2 ] , 1 6 ) ; uncaught exception Match [ nonexhaustive match f a i l u r e ] change ( [ 5 , 4 , 1 ] , 8 ) ; val i t = [ 5 , 1 , 1 , 1 ] : i n t l i s t

slide-8
SLIDE 8

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exhaustive Search

(∗ expects : c u r r e n t s o l u t i o n , coins , amount r e t u r n s : l i s t

  • f

s o l u t i o n s ∗) fun F i n d A l l ( sol , , 0 ) = [ s o l ] | F i n d A l l ( , [ ] , ) = [ ] | F i n d A l l ( sol , c : : coins , amount ) = i f amount < 0 then [ ] else F i n d A l l ( c : : sol , c : : coins , amount−c ) @ F i n d A l l ( sol , coins , amount ) fun change exh ( coins , amount ) = F i n d A l l ( [ ] , coins , amount ) change exh ( [ 5 , 2 ] , 1 6 ) ; val i t = [ [ 2 , 2 , 2 , 5 , 5 ] , [ 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 ] ] : i n t l i s t l i s t change exh ( [ 5 , 4 , 1 ] , 8 ) ; val i t = [ [ 1 , 1 , 1 , 5 ] , [ 4 , 4 ] , [ 1 , 1 , 1 , 1 , 4 ] , [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ] : i n t l i s t l i s t change exh ( [ 2 5 , 1 0 , 5 , 1 ] , 4 8 ) ;

slide-9
SLIDE 9

Exceptions Amtoft from Hatcliff Raising Exceptions Handling Exceptions Application

Exceptions for Backtracking

exception F a i l u r e fun change1 ( coins , 0 ) = [ ] | change1 ( [ ] , amount ) = r a i s e F a i l u r e | change1 ( c : : coins , amount ) = i f amount < c then change1 ( coins , amount ) else ( c : : change1 ( c : : coins , amount−c ) handle F a i l u r e = > change1 ( coins , amount ) change1 ( [ 5 , 2 ] , 1 6 ) ; val i t = [ 5 , 5 , 2 , 2 , 2 ] : i n t l i s t change1 ( [ 2 5 , 1 0 , 5 , 1 ] , 4 8 ) ; val i t = [25 ,10 ,10 ,1 ,1 ,1] : i n t l i s t change1 ( [ 5 , 4 , 1 ] , 8 ) ; val i t = [ 5 , 1 , 1 , 1 ] : i n t l i s t