Crash course on SML Wojciech Moczydlowski - PowerPoint PPT Presentation

About This Presentation
Title:

Crash course on SML Wojciech Moczydlowski

Description:

Extremely convenient for writing compilers, interpreters, provers and ... hd, tl, last, map, find, filter... Option : OPTION. option, isSome, val Of, filter... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: wojciechmo
Category:

less

Transcript and Presenter's Notes

Title: Crash course on SML Wojciech Moczydlowski


1
Crash course on SMLWojciech Moczydlowski
  • SML functional programming language
  • Complete formal semantics
  • Extremely convenient for writing compilers,
    interpreters, provers and 611 homeworks
  • OCaml, based on SML, is practical and used in
    industry

2
SML - ideology
  • 3 worlds expressions, values and types.
  • Values have types, i.e. 0 int, asd string
  • Expressions have types as well, i.e. 34 int,
    fact int -gt int
  • Expressions evaluate to values

3
SML types
  • Some types
  • int, string, char
  • intint, intstring
  • int -gt int, intint -gt int
  • int -gt int -gt int
  • int list
  • a list
  • a -gt b -gt a

4
Interpreter
  • - 22
  • val it 4 int
  • 2317
  • val it 40 int
  • 611 rules
  • val it 611 rules string
  • - 17 17
  • val it 0 int
  • (17, a)
  • val it (17, a) int string

5
Definitions
  • val i 10
  • val i 10 int
  • val j i i
  • val j 20 int
  • val s Int.toString(j)
  • val s 20 string
  • val t (i, i 1, i 2)
  • val t (10, 11, 12) int int int
  • val q 2 t
  • val q 11 int

6
Datatypes
  • datatype Bool True False
  • datatype Color Red Black
  • datatype Nat Zero S of Nat
  • True
  • val it True Bool
  • S(S(Zero))
  • val it S(S(Zero)) Nat

7
Functions
  • fun add(n, m Nat) Nat
  • case n of
  • Zero gt m
  • S(x) gt S(add(x, m))
  • fun mult(n, m)
  • case n of
  • Zero gt Zero
  • S(x) gt add(n, mult(x, m))
  • Evaluation call by value.
  • All functions have one argument add Nat Nat
    -gt Nat
  • mult Nat Nat -gt Nat

8
Anonymous functions
  • As in lambda calculus, a function can be
    specified on fly
  • (fn x gt x 17) 26
  • val it 43 int
  • Large parts of lambda calculus can be expressed.

9
Polymorphism
  • val K fn x gt fn y gt x
  • val K fn a -gt b -gt a
  • a b are type variables.
  • K 6 4
  • val it 6 int
  • K 611 170
  • val it 611 string
  • K K K

10
Polymorphism
  • val I fn x gt x
  • val S fn x gt fn y gt fn z gt
  • (x z) (y z)
  • val Ap fn f gt fn x gt f x
  • However, the following wont type-check
  • val Y fn f gt (fn x gt f (x x)) (fn x gt f (x
    x))
  • (fn x gt x x) (fn x gt x x)

11
Datatypes reloaded
  • datatype a option NONE SOME of a
  • NONE
  • val it NONE a option
  • SOME
  • val it fn a -gt a option
  • SOME 5
  • val it SOME 5 int option
  • SOME Cornell
  • val it SOME Cornell string option

12
Datatypes
  • fun div(m, n) if n 0 then NONE else SOME (m
    div n)
  • datatype (a, b) Either
  • Left of a Right of b

13
Recursive polymorphic datatypes
  • datatype a Tree Null
  • Node of (a Tree) a (a Tree)
  • Node (Node(Null, 5, Null), 3, Null)
  • fun sum(t) case t of
  • Null gt 0
  • Node (a, b, c) gt
  • sum(a) b sum(c)

14
RPDs
  • fun size(t) case t of
  • Null gt 0
  • Node(t1, _, t2) gt
  • size t1 (size t2) 1
  • fun add(t, n) case t of
  • Null gt Null
  • Node(t1, m, t2) gt
  • Node(add(t1, n), m n,
  • add(t2, n))

15
RPDs
  • fun mul(t, n) case t of
  • Null gt Null
  • Node(t1, m, t2) gt
  • Node(mul(t1, n), m n,
  • mul(t2, n))
  • In general?

16
RPDs
  • fun mapTree(t, f) case t of
  • Null gt Null
  • Node(t1, m, t2) gt
  • Node(mapTree(t1, f), f m,
  • mapTree(t2, f))
  • fun add(t, n)
  • mapTree(t, fn m gt m n)
  • val mul fn (t, n) gt
  • mapTree (t, fn m gt n m)

17
RPDs
  • datatype a list nil cons of
  • (a a list)
  • Notation
  • denotes nil
  • (xxs) denotes cons (x, xs)

18
Lists
  • Some lists
  • (1(2(3nil)))
  • (abcnil)
  • Another notation
  • a, b, c abcnil
  • 1,2,3, a, b, c

19
Lists
  • fun length(l) case l of
  • gt 0
  • (xxs) gt 1 length xs
  • fun hd raise Fail Empty
  • hd(xxs) x
  • fun tl raise Fail Empty
  • tl (xxs) xs

20
Lists
  • fun map(f, l) case l of
  • gt
  • (xxs) gt f x (map f
    xs)

21
Modules
  • signature SET sig
  • type a set
  • val empty a set
  • val insert a -gt a set -gt a set
  • val elem a -gt a set -gt bool
  • end

22
Modules
  • structure Set gt SET struct
  • type a set a list
  • val empty a set
  • val insert a -gt a set -gt a set fn
    x gt fn y gt xy
  • val elem a -gt a set -gt bool ...
  • end

23
Accessing structure elements
  • Set.insert, Set.add ....
  • open Set
  • insert, add, ...

24
Standard library
  • Many useful structures
  • List gt LIST
  • hd, tl, last, map, find, filter...
  • Option gt OPTION
  • option, isSome, val Of, filter...
  • String gt STRING
  • size, isSubstring, , lt ...
Write a Comment
User Comments (0)
About PowerShow.com