Introduction to ML Part 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to ML Part 2

Description:

We put things together in a more complex program. An interpreter ... uncaught exception Error. raised at: ex.sml:15.28-15.35. in SML interpreter: Evaluator ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 26
Provided by: studen188
Category:

less

Transcript and Presenter's Notes

Title: Introduction to ML Part 2


1
Introduction to ML - Part 2
  • Kenny Zhu

2
What is next?
  • ML has a rich set of structured values
  • Tuples (17, true, stuff)
  • Records name george, age 35
  • Lists 345nil or 3,4_at_5
  • Datatypes
  • Functions
  • And more!
  • We put things together in a more complex program

3
An interpreter
  • Interpreters are usually implemented as a series
    of transformers

lexing/ parsing
evaluate
print
stream of characters
abstract syntax
abstract value
stream of characters
4
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)

5
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 tuple another eg string char
vertical bar separates alternatives
6
LL abstract syntax in ML
Add
Add (Num 2, Num 3) represents the expression 2
3
Num
Num
2
3
7
LL abstract syntax in ML
If
If (Bool true, Num 0, Add (Num 2, Num
3)) represents if true then 0 else 2 3
Add
Bool
Num
true
Num
Num
0
3
2
8
Function declarations
function name
function parameter
fun isValue t case t of Num n gt true
Bool b gt true _ gt false
default pattern matches anything
9
What is the type of the parameter t? Of the
function?
function name
function parameter
fun isValue t case t of Num n gt true
Bool b gt true _ gt false
default pattern matches anything
10
What is the type of the parameter t? Of the
function?
fun isValue (tterm) bool case t of Num
n gt true Bool b gt true _ gt false
val isValue term -gt bool
ML does type inference gt you need not annotate
functions yourself (but it can be helpful)
11
A type error
fun isValue t case t of Num _ gt 1 _
gt false
ex.sml22.3-24.15 Error types of rules don't
agree literal earlier rule(s) term -gt int
this rule term -gt bool in rule _ gt false
12
A type error
Actually, ML may give you several errors in a
row ex.sml22.3-25.15 Error types of rules
don't agree literal earlier rule(s) term -gt
int this rule term -gt bool in rule Num t2
gt true ex.sml22.3-25.15 Error types of rules
don't agree literal earlier rule(s) term -gt
int this rule term -gt bool in rule _ gt
false
13
A very subtle error
fun isValue t case t of num gt true _
gt false
The code above type checks. But when we test it,
the function always returns true. What has gone
wrong?
14
A very subtle error
fun isValue t case t of num gt true _
gt false
The code above type checks. But when we 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!)
15
Exceptions
exception Error of string fun debug s unit
raise (Error s)
16
Exceptions
exception Error of string fun debug s unit
raise (Error s)
in SML interpreter
- debug "hello" uncaught exception Error
raised at ex.sml15.28-15.35
17
Evaluator
fun isValue t ... exception NoRule fun eval t
case t of Bool _ Num _ gt t ...
18
Evaluator
... fun eval t case t of Bool _ Num _
gt t If(t1,t2,t3) gt let val v eval
t1 in case v of Bool b gt if
b then (eval t2) else (eval t3) _ gt
raise NoRule end
let statement for remembering temporary results
19
Evaluator
exception NoRule fun eval1 t case t of
Bool _ Num _ gt ... ... Add (t1,t2) gt
case (eval v1, eval v2) of (Num
n1, Num n2) gt Num (n1 n2) (_,_) gt
raise NoRule
20
Finishing the Evaluator
fun eval1 t case t of ... ... Add
(t1,t2) gt ... IsZero t gt ...
be sure your case is exhaustive
21
Finishing the Evaluator
fun eval1 t case t of ... ... Add
(t1,t2) gt ...
What if we forgot a case?
22
Finishing the Evaluator
fun eval1 t case t of ... ... Add
(t1,t2) gt ...
What if we forgot a case?
ex.sml25.2-35.12 Warning match nonexhaustive
(Bool _ Zero) gt ... If
(t1,t2,t3) gt ... Add (t1,t2) gt ...
23
Demo
  • Managing the source files for the interpreter

24
More on lists Map
  • fun map f l
  • case l of
  • nil gt
  • l x l gt (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
  • gt val it 2,3,4 int list

25
More on lists Fold
  • fun fold f init l
  • case l of
  • nil gt init
  • x l gt 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
Write a Comment
User Comments (0)
About PowerShow.com