ocaml

46
OCaml The PL for the discerning hacker.

Upload: payton

Post on 25-Feb-2016

52 views

Category:

Documents


0 download

DESCRIPTION

OCaml. The PL for the discerning hacker. Hello. I’m Zach, one of Sorin’s students. [email protected]. ML Anatomy 101. ML Program = ? ? ?. ML Program = One Giant, Complex Expression. Controlling complexity is the essence of computer programming. B. Kerninghan. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: OCaml

OCamlThe PL for the discerning

hacker.

Page 2: OCaml

Hello.

I’m Zach, one of Sorin’s students.

[email protected]

Page 3: OCaml

ML Anatomy 101

ML Program = One Giant, Complex Expression

Controlling complexity is the essence of computer programming.

B. Kerninghan

A complex system that works is invariably found to have evolved from a simple system that worked.

J. Gall

ML Program = ? ? ?

Page 4: OCaml

Building ML Programs

ML provides tools to control complexity

Build complex exprs from simple exprs

Build complex types from simple typesPREV

NOW

Page 5: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 6: OCaml

basic

Who cares about types anyway?

Every good programmer!(not just old timers)

Types provide:1. Documentation2. Early bug warning system3. Performance!

Page 7: OCaml

basic

Who cares about types anyway?

Even programmers without a type system!

I think it may just be a property of large systems in dynamic languages, that eventually you end up rewriting your own type system, and you sort of do it badly.

-- Twitter

Page 8: OCaml

basic

Expression Type Kind of Type

5 int basic

“hello” string basic

(5, “hello”) int * string tuple

[1; 2; 3; 4] int list list

[ (1, 1) ; (2, 2) ; (3, 3) ] (int * int) list tuple+list

Page 9: OCaml

more on tuples

Expression Type

(5, (10, 15)) int * (int * int)

((5, 10), 15) (int * int) * int

Page 10: OCaml

more on tuples

Expression Type

(5, (10, 15)) int * (int * int)

((5, 10), 15) (int * int) * int

( [ (1, 2); (3, 4); (5, 6) ], (“hello”, “india”), (5, “int”, (1, 2, 3)))

Page 11: OCaml

more on tuples

Expression Type

(5, (10, 15)) int * (int * int)

((5, 10), 15) (int * int) * int

( [ (1, 2); (3, 4); (5, 6) ], (“hello”, “india”), (5, “int”, (1, 2, 3)))

(int * int) list *(string * string) *(int * string * (int * int * int))

Page 12: OCaml

basic

Don’t know how it works ?Try it in the toplevel !

Page 13: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 14: OCaml

function

A -> B

Type of a function which:expects parameter of type Aproduces a value of type B

Contract:caller satisfies Acallee satisfies B

preconditionpostcondition

Page 15: OCaml

function

let f x = x + 5f: int -> int

let f x = “hello “ ^ xf: string -> string

let f x = “number “ ^ (string_of_int x)f: int -> string

let f x y = x * x + y * yf: int -> int -> int

Page 16: OCaml

function

let f x y = 1 :: x :: yf: int -> int list -> int list

let f x y z = [1] @ x @ [y + z]f: int list -> int -> int -> int list

let rec f = f fERROR

Page 17: OCaml

polymorphic functions

Some functions work on many types:

let id x = xid: ‘a -> ‘a

Takes a value of any type, call it ‘aReturns a value of that type ‘a

Page 18: OCaml

polymorphic functions

let f a b = af: ‘a -> ‘b -> ‘a

let f a b = bf: ‘a -> ‘b -> ‘b

let pipe x f = f xf: ‘a -> (‘a -> ‘b) -> ‘b

let (|>) = pipe“hello” |> id |> print_string(print_string (id (“hello”)))

binaryinfixoperator

Page 19: OCaml

polymorphic functions

let f g x = g xf: (‘a -> ‘b) -> ‘a -> ‘b

let f g h x = g (h x)f: (‘a -> ‘b) -> (‘c -> ‘a) -> ‘c -> ‘b

Page 20: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 21: OCaml

record

type RT ={ NM1 : T1; ...; NMN : TN}

Like a tuple, but refer to members by name.

Page 22: OCaml

record

type person ={ name : string; age : int; hair : string; job : string}

Page 23: OCaml

making a record value

let pres ={ name = “Obama”; age = 49; hair = “black”; job = “president”}

Page 24: OCaml

updating a record value

let pres ={ name = “Obama”; age = 49; hair = “black”; job = “president”}

let pres’ = {pres with hair = “gray” }

Page 25: OCaml

updating a record value

let year_older p =if p.age > 45 then

{ p with age = p.age + 1,hair = “gray” }

else{ p with age = p.age + 1 }

year_older: person -> person

Page 26: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 27: OCaml

variant

type VT =| C1 of T1| ...| CN of TN

C1 to CN are “constructors”

Ci like function from Ti to VT

value of type VT can only be constructed with one of these

Page 28: OCaml

variant

type pet =| Dog of string| Cat of string| Fish of string

Value of type pet constructed with one of: Dog, Cat, Fish

Each takes a string and returns a pet

Page 29: OCaml

variant values

type pet =| Dog of string| Cat of string| Fish of string

let d = Dog “spot”let c = Cat “whiskers”let f = Fish “nemo”

Page 30: OCaml

matching variant values

let name pet =match pet with| Dog nm -> nm| Cat nm -> nm| Fish nm -> nm

let d = Dog “sparky” inname d

Page 31: OCaml

matching variant values

let says pet =match pet with| Dog _ -> “woof”| Cat _ -> “meow”| Fish _ -> “bubble bubble”

let c = Cat “walter” insays c

Page 32: OCaml

variant

type fuel_level =| Empty| Middle| Full

type vehicle =| Car of int * fuel_level| Tank of int * fuel_level| Boat of int * fuel_level

Page 33: OCaml

matching variant values

let miles v =match v with| Car (m, _) -> m| Tank (m, _) -> m| Boat (m, -) -> m

Page 34: OCaml

updating variant values

let reduce f =match f with| Empty -> Empty| Middle -> Empty| Full -> Middle

let drive v =match v with| Car (m, f) -> Car (m, reduce f)| Tank (m, f) -> Tank (m, reduce f)| Boat (m, f) -> Boat (m, reduce f)

Page 35: OCaml

updating variant values

let refill v =match v with| Car (m, f) -> Car (m, Full)| Tank (m, f) -> Tank (m, Full)| Boat (m, f) -> Boat (m, Full)

Page 36: OCaml

recursive variant

type expr =| Val of int| Add of expr * expr| Sub of expr * expr| Mul of expr * expr

let e1 = Val 5let e2 = Add (e1, e1)let e3 = Mul (e2, e1)

Page 37: OCaml

recursive variant

let rec eval e =

Page 38: OCaml

recursive variant

let rec eval e =match e with| Val i -> i| Add (l, r) -> (eval l) + (eval r)| Sub (l, r) -> (eval l) - (eval r)| Mul (l, r) -> (eval l) * (eval r)

Page 39: OCaml

mutual recursion

let rec expr_dot e = match e with | Val i -> string_of_int i | Add (l, r) -> (aux "add" l) ^ (aux "add" r) | Sub (l, r) -> (aux "sub" l) ^ (aux "sub" r) | Mul (l, r) -> (aux "mul" l) ^ (aux "mul" r)

and aux p e = match e with | Val i -> p ^ " -> " ^ string_of_int i ^ ";\n" | Add _ -> p ^ " -> add;\n" ^ (expr_dot e) | Sub _ -> p ^ " -> sub;\n" ^ (expr_dot e) | Mul _ -> p ^ " -> mul;\n" ^ (expr_dot e)

Page 40: OCaml

Pattern Matching: a PL Masterpiece

match is one of ML’s very best features

simultaneous test / extract / bind

auto checks any missed cases

leads to compact, readable code

Page 41: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 42: OCaml

demo

Conway’s Game of Life

Page 43: OCaml

demo

Conway’s Game of Life

code at:

http://github.com/ztatlock/simple-life

Page 44: OCaml

demo

OCaml in the “real world” at JaneStreethttp://ocaml.janestreet.com/

Check out their leader, Yaron Minskyhttp://ocaml.janestreet.com/?q=node/82

Page 45: OCaml

Building Types

1. basic (recap)

2. function

3. record

4. variant

5. demoM.C. Escher’s Relativity

in LEGO

Page 46: OCaml