introduction to ml - part 2

25
Introduction to ML - Part 2 Kenny Zhu

Upload: buffy-floyd

Post on 02-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

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 = 35} Lists: 3::4::5::nil or [3,4]@[5] Datatypes Functions And more! We put things together in a more complex program. An interpreter. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to ML - Part 2

Introduction to ML - Part 2

Kenny Zhu

Page 2: Introduction to ML - Part 2

What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age = 35} Lists: 3::4::5::nil or [3,4]@[5] Datatypes Functions And more!

We put things together in a more complex program

Page 3: Introduction to ML - Part 2

An interpreter Interpreters are usually implemented as a series of transformers:

stream ofcharacters

abstractsyntax

lexing/parsing

evaluate

abstractvalue

print

stream ofcharacters

Page 4: Introduction to ML - Part 2

A little language (LL) An arithmetic expression e is

a boolean value an if statement (if e1 then e2 else e3)

an integer an add operation a test for zero (isZero e)

Page 5: Introduction to ML - Part 2

LL abstract syntax in ML

datatype term = Bool of bool| If of term * term * term| Num of int| Add of term * term| IsZero of term

-- constructors are capitalized

-- constructors can take a single argument of a particular type

type of a tupleanother eg: string * char

vertical barseparates alternatives

Page 6: Introduction to ML - Part 2

LL abstract syntax in ML

Add (Num 2, Num 3)

represents the expression “2 + 3”

Add

Num Num

2 3

Page 7: Introduction to ML - Part 2

LL abstract syntax in ML

If (Bool true, Num 0, Add (Num 2, Num 3))

represents

“if true then 0 else 2 + 3”

Add

Num Num

2 3

true

Bool Num

0

If

Page 8: Introduction to ML - Part 2

Function declarations

fun isValue t = case t of Num n => true | Bool b => true | _ => false

function name function parameter

default pattern matches anything

Page 9: Introduction to ML - Part 2

What is the type of the parameter t? Of the function?

fun isValue t = case t of Num n => true | Bool b => true | _ => false

function name function parameter

default pattern matches anything

Page 10: Introduction to ML - Part 2

What is the type of the parameter t? Of the function?

fun isValue (t:term) : bool = case t of Num n => true | Bool b => true | _ => false

val isValue : term -> bool

ML does type inference => you need notannotate functions yourself (but it can be helpful)

Page 11: Introduction to ML - Part 2

A type error

fun isValue t = case t of Num _ => 1 | _ => false

ex.sml:22.3-24.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: _ => false

Page 12: Introduction to ML - Part 2

A type error

Actually, ML may give you several errors in a row:

ex.sml:22.3-25.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule:

Num t2 => trueex.sml:22.3-25.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: _ => false

Page 13: Introduction to ML - Part 2

A very subtle error

fun isValue t = case t of num => true | _ => false

The code above type checks. But whenwe test it, the function always returns “true.”What has gone wrong?

Page 14: Introduction to ML - Part 2

A very subtle error

fun isValue t = case t of num => true | _ => false

The code above type checks. But whenwe test it, the function always returns “true.”What has gone wrong?-- num is not capitalized (and has no argument)-- ML treats it like a variable pattern (matches anything!)

Page 15: Introduction to ML - Part 2

Exceptions

exception Error of string

fun debug s : unit = raise (Error s)

Page 16: Introduction to ML - Part 2

Exceptions

exception Error of string

fun debug s : unit = raise (Error s)

- debug "hello";

uncaught exception Error raised at: ex.sml:15.28-15.35

in SML interpreter:

Page 17: Introduction to ML - Part 2

Evaluator

fun isValue t = ...

exception NoRule

fun eval t = case t of Bool _ | Num _ => t | ...

Page 18: Introduction to ML - Part 2

Evaluator

...

fun eval t = case t of Bool _ | Num _ => t | If(t1,t2,t3) => let val v = eval t1 in case v of Bool b => if b then (eval t2) else (eval t3) | _ => raise NoRule end

let statementfor rememberingtemporaryresults

Page 19: Introduction to ML - Part 2

Evaluatorexception NoRule

fun eval1 t = case t of Bool _ | Num _ => ... | ... | Add (t1,t2) => case (eval v1, eval v2) of (Num n1, Num n2) => Num (n1 + n2) | (_,_) => raise NoRule

Page 20: Introduction to ML - Part 2

Finishing the Evaluatorfun eval1 t = case t of ... | ... | Add (t1,t2) => ... | IsZero t => ...

be sure yourcase isexhaustive

Page 21: Introduction to ML - Part 2

Finishing the Evaluatorfun eval1 t = case t of ... | ... | Add (t1,t2) => ... What if we

forgot a case?

Page 22: Introduction to ML - Part 2

Finishing the Evaluator

ex.sml:25.2-35.12 Warning: match nonexhaustive (Bool _ | Zero) => ... If (t1,t2,t3) => ... Add (t1,t2) => ...

fun eval1 t = case t of ... | ... | Add (t1,t2) => ... What if we

forgot a case?

Page 23: Introduction to ML - Part 2

Demo Managing the source files for the interpreter

Page 24: Introduction to ML - Part 2

More on lists: Mapfun map f l = case l of nil => [] l x :: l => (f x) :: (map f l)

applies the function f to every element in the list

- fun add1 x = x + 1;- map add1 [1,2,3]; > val it = [2,3,4] : int list

Page 25: Introduction to ML - Part 2

More on lists: Foldfun fold f init l = case l of nil => init | x :: l => f (x, fold f init l)

applies function f (x, y) on the elements of l and the result from previous application recursively

- fun sum (x, y) = x + y;- foldr sum 0 [1,2,3,4];val it = 10 : int