OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students....

80
OCaml The PL for the discerning hacker.

Transcript of OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students....

Page 1: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

OCamlThe PL for the discerning

hacker.

Page 2: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Hello.

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

[email protected]

Page 3: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

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 The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building ML Programs

ML provides tools to control complexity

Build complex exprs from simple exprs

Build complex types from simple types

NOW

THU

Page 5: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demoM.C. Escher’s Waterfall

in LEGO

Page 6: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

basic

Type Expression Value

int 5 5

int 5 * 5 * 5 125

string “hello” “hello”

string “Je” ^ “ll” ^ “o” “Jello”

tuple (5 * 5, “ab” ^ “cd”) (25, “abcd”)

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

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

Page 7: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

basic

Don’t know how it works ?

Try it in the toplevel !

Page 8: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demoM.C. Escher’s Waterfall

in LEGO

Page 9: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Variables are central to programming

Associate a name with a computation

let expressions are how ML does it

let

let

Page 10: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let

Bind name NM to expression E1 within E2:

let NM = E1 in E2

Semantics (what it means):

1. evaluate E1 to value V

2. replace NM with V in E2

Page 11: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let examples

let x = 5 in x

let x = 5 in x * x

let x = 5 * 5 in x * x

let x = “hello” in print_string x

let print = print_string inprint “hello”

Page 12: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let chaining (outside)

Let syntax : let NM = E1 in E2

E2 can be another let

let x = 2 inlet y = 3 inlet x2 = x * x inlet y2 = y * y inx2 + y2

Page 13: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let nesting (inside)

Let syntax : let NM = E1 in E2

E1 can be another let

let x2 =let x = 5 inx * x

inx2 + x2

Page 14: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let name clashes (outside)

Let syntax : let NM = E1 in E2

What if NM appears in E2 ?

let x = 1 inlet x = 2 inx

Our naïve semantics were wrong!

Page 15: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let name clashes

Let syntax : let NM = E1 in E2

Semantics (what it means):

1. evaluate E1 to value V

2. replace UNBOUND NM with V in E2

Essentially, use nearest binding.

Page 16: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let name clashes (inside)

Let syntax : let NM = E1 in E2

What if NM appears in E1 ?

let x =let x = 5 inx * x

inx * x

Page 17: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 5 inlet x = x * x inlet x =

let x = x + x inlet x = x * x inx

inx + x

inx * x

Page 18: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 5 inlet x = 5 * 5 inlet x =

let x = x + x inlet x = x * x inx

inx + x

inx * x

Page 19: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 5 * 5 inlet x =

let x = x + x inlet x = x * x inx

inx + x

inx * x

Page 20: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 25 inlet x =

let x = x + x inlet x = x * x inx

inx + x

inx * x

Page 21: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 25 inlet x =

let x = 25 + 25 inlet x = x * x inx

inx + x

inx * x

Page 22: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 25 + 25 inlet x = x * x inx

inx + x

inx * x

Page 23: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 50 inlet x = x * x inx

inx + x

inx * x

Page 24: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 50 inlet x = 50 * 50 inx

inx + x

inx * x

Page 25: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 50 * 50 inx

inx + x

inx * x

Page 26: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 2500 inx

inx + x

inx * x

Page 27: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

let x = 2500 in2500

inx + x

inx * x

Page 28: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x =

2500inx + x

inx * x

Page 29: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 2500 inx + x

inx * x

Page 30: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =let x = 2500 in2500 + 2500

inx * x

Page 31: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x =2500 + 2500

inx * x

Page 32: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x = 5000 inx * x

Page 33: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

let x = 5000 in5000 * 5000

Page 34: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

5000 * 5000

Page 35: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash mania

25,000,000

Page 36: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

name clash, but later

let x = 5 inlet y = x * x inlet x = 10 iny

What is the value of this expr?

25 : because x was 5 when y was defined

Binding to value is fixed at definition.

Page 37: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let vs. assign

What’s the difference?

No Time Travellet cannot affect anything before itself

Lexical Scopingknow where in prog each name defined

Page 38: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

let vs. assign

No Time Travel + Lexical Scoping

Why are these good?

1. Behavior fixed at definition

2. Localize debugging

3. Simplifies reasoning

Page 39: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demoM.C. Escher’s Waterfall

in LEGO

Page 40: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if

Programs make decisions

Ask our patient and careful friend (computer):

“If X is true, please go do A. Otherwise, please go do B.”

if expressions are how ML does it

Page 41: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if

if TEST thenE1

elseE2

If TEST evals to true, eval expr E1.

Otherwise, eval expr E2.

Page 42: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if : just an expression

if TEST then E1 else E2

if is an expression

evaluates to a value

has a type

use anywhere expr accepted

Page 43: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if examples

Type Expression Value

int if true then 5 else 10 5

int if false then 5 else 10 10

string if 1 < 5 then “hello”else “goodbye” “hello”

int list 1 :: (if 10 mod 5 = 0 then [2; 3] else [4; 5]) [1; 2; 3]

Page 44: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if style exercise : Java to OCaml

int foo(int i, boolean b, c, d) { if (b) { i++; } if (c) { return i + 2; } if (d) { i = i + 3; } else { return i + 4; } return i;}

Page 45: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if style exercise : Java to OCaml

let foo i b c d = let j = if b then i + 1 else i in if c then j + 2 else if d then j + 3 else j + 4

Page 46: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if

So far, then and else exprs had same type

What about: if ... then 5 else “hello”

Rejected!

then and else exprs must have same type

Page 47: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

if rules

Typing:

if has same type as then and else exprs

Eval (semantics):

e1 : bool e2: T e3: T

if e1 then e2 else e3 : T

e1 )) true e2 )) v2 .

if e1 then e2 else e3 )) v2 e1 )) false e3 )) v3

.if e1 then e2 else e3 )) v3

Page 48: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demoM.C. Escher’s Waterfall

in LEGO

Page 49: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

abstraction

Page 50: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

fun

Abstraction: ultimate complexity manager

Provide simple interface to complex expr

functions are how ML does it

Page 51: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

fun

fun NM -> E

Accept value V as input.

Replace unbound name NM in expr E with V.

Page 52: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

fun examples

(fun x -> x) 5

(fun x -> x * x) 5

(fun x -> 5) 10

(fun x -> “hello “ ^ x) “india”

let h = “hello ” in(fun x -> h ^ x) “india”

Page 53: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

bind a function to a name with let:

let double = fun x -> x + x indouble 5

let quad = fun x ->(double x) + (double x)

inquad 5

Page 54: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

store functions in a tuple:

( fun x -> x, fun x -> 5, fun x -> “hello”)

tuple : can have different types

: ‘a -> ‘a: ‘a -> int: ‘a -> string

Page 55: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

store functions in a list:

[ fun x -> x * x; fun x -> 5; fun x -> x / 2]

list : must have same type

: int -> int: int -> int: int -> int

Page 56: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

more fun parameters

Can functions have only one parameter?

Nope.

functions can return functions!

fun x ->fun y ->

x + y

takes x and returns (takes y and returns (sum of x and y))

Page 57: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

function on function action

let add =fun x ->

fun y ->x + y

inadd 5 10

Page 58: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

function on function action

let str_concat =fun x ->

fun y ->x ^ y

instr_concat “hello “ “india”

Page 59: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

functions can take functions as input!

let apply =fun f ->

fun x ->f x

inapply print_string “hello”

Page 60: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

functions can take functions as input!

(fun f ->fun x ->

f x)print_string “hello”

Page 61: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

functions can take functions as input!

(fun x ->print_string x)

“hello”

Page 62: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

functions are values

functions can take functions as input!

print_string “hello”

Page 63: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

function shorthand

syntactic sugar

let NM P1 ... PN = E

means

let NM =fun P1 ->

...fun PN ->

E

Page 64: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

function shorthand

let add5 x = x + 5

let add x y = x + y

let str_concat x y = x ^ y

let apply f x = f x

let apply_twice f x = f (f x)

Page 65: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

How do we write recursive functions?

We can’t yet!

Problem: NM is not bound until after let.

Need more than just sugar . . .

Page 66: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

Need let rec:

let rec NM P = E

NM is bound to this definition inside E.

Page 67: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec is_even n =...

Page 68: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec is_even n =if n = 0 then

trueelse if n = 1 then

falseelse

is_even (n – 2)

Page 69: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec fib n =...

Page 70: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec fib n =if n = 0 then

1else if n = 1 then

1else

(fib (n – 1)) + (fib (n – 2))

Page 71: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec factorial n =...

Page 72: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec factorial n =if n = 0 then

1else

n * (factorial (n – 1))

Page 73: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec forever () =(* infinite loop *)

Page 74: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

recursive fun

let rec forever () =forever ()

Page 75: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

fun and let

Simple enough:

let x = 5 inlet f y = x + y inf 10

Evals to 15.

Page 76: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

fun and let

But what about:

let x = 5 inlet f y = x + y inlet x = 10f 10

Still evals to 15.

Behavior fixed at binding !

Page 77: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demoM.C. Escher’s Waterfall

in LEGO

Page 78: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

demo

Conway’s Game of Life

Page 79: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.

Building Expressions

1. basic (recap)

2. let

3. if

4. fun

5. demo

Next Time : Building Types

M.C. Escher’s Waterfallin LEGO

Page 80: OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu.