800 likes | 1.02k Views
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 = ? ? ?. ML Program = One Giant, Complex Expression. Controlling complexity is the essence of computer programming. B. Kerninghan.
E N D
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 = ? ? ? 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
Building ML Programs ML provides tools to control complexity Build complex exprs from simple exprs Build complex types from simple types NOW THU
Building Expressions basic (recap) let if fun demo M.C. Escher’s Waterfall in LEGO
basic Don’t know how it works ? Try it in the toplevel !
Building Expressions basic (recap) let if fun demo M.C. Escher’s Waterfall in LEGO
let Variables are central to programming Associate a name with a computation let expressions are how ML does it let
let Bind name NM to expression E1 within E2: let NM = E1 in E2 Semantics (what it means): evaluate E1 to value V replace NM with V in E2
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 in print “hello”
let chaining (outside) Let syntax : let NM = E1 in E2 E2 can be another let let x = 2 in let y = 3 in let x2 = x * x in let y2 = y * y in x2 + y2
let nesting (inside) Let syntax : let NM = E1 in E2 E1 can be another let let x2 = let x = 5 in x * x in x2 + x2
let name clashes (outside) Let syntax : let NM = E1 in E2 What if NM appears in E2 ? let x = 1 in let x = 2 in x Our naïve semantics were wrong!
let name clashes Let syntax : let NM = E1 in E2 Semantics (what it means): evaluate E1 to value V replace UNBOUNDNM with V in E2 Essentially, use nearest binding.
let name clashes (inside) Let syntax : let NM = E1 in E2 What if NM appears in E1 ? let x = let x = 5 in x * x in x * x
name clash mania let x = let x = 5 in let x = x * x in let x = let x = x + x in let x = x * x in x in x + x in x * x
name clash mania let x = let x = 5 in let x = 5 * 5 in let x = let x = x + x in let x = x * x in x in x + x in x * x
name clash mania let x = let x = 5 * 5 in let x = let x = x + x in let x = x * x in x in x + x in x * x
name clash mania let x = let x = 25 in let x = let x = x + x in let x = x * x in x in x + x in x * x
name clash mania let x = let x = 25 in let x = let x = 25 + 25 in let x = x * x in x in x + x in x * x
name clash mania let x = let x = let x = 25 + 25 in let x = x * x in x in x + x in x * x
name clash mania let x = let x = let x = 50 in let x = x * x in x in x + x in x * x
name clash mania let x = let x = let x = 50 in let x = 50 * 50 in x in x + x in x * x
name clash mania let x = let x = let x = 50 * 50 in x in x + x in x * x
name clash mania let x = let x = let x = 2500 in x in x + x in x * x
name clash mania let x = let x = let x = 2500 in 2500 in x + x in x * x
name clash mania let x = let x = 2500 in x + x in x * x
name clash mania let x = let x = 2500 in x + x in x * x
name clash mania let x = let x = 2500 in 2500 + 2500 in x * x
name clash mania let x = 2500 + 2500 in x * x
name clash mania let x = 5000 in x * x
name clash mania let x = 5000 in 5000 * 5000
name clash mania 5000 * 5000
name clash mania 25,000,000
name clash, but later let x = 5 in let y = x * x in let x = 10 in y What is the value of this expr? 25 : because x was 5 when y was defined Binding to value is fixed at definition.
let vs. assign What’s the difference? No Time Travel let cannot affect anything before itself Lexical Scoping know where in prog each name defined
let vs. assign No Time Travel + Lexical Scoping Why are these good? Behavior fixed at definition Localize debugging Simplifies reasoning
Building Expressions basic (recap) let if fun demo M.C. Escher’s Waterfall in LEGO
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
if if TEST then E1 else E2 If TESTevals to true, evalexprE1. Otherwise, evalexprE2.
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
if style exercise : Java to OCaml intfoo(inti, boolean b, c, d) { if (b) { i++; } if (c) { return i + 2; } if (d) { i = i + 3; } else { return i + 4; } return i; }
if style exercise : Java to OCaml let fooi 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
if So far, then and elseexprs had same type What about: if ... then 5 else “hello” Rejected! then and elseexprsmust have same type
if rules e1 : bool e2: T e3: T if e1 then e2 else e3 : T Typing: if has same type as then and elseexprs Eval (semantics): e1 )) true e2 )) v2 . if e1 then e2 else e3 )) v2 e1 )) false e3 )) v3 . if e1 then e2 else e3 )) v3
Building Expressions basic (recap) let if fun demo M.C. Escher’s Waterfall in LEGO
fun Abstraction: ultimate complexity manager Provide simple interface to complex expr functions are how ML does it