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

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
Reasoning About Code; Hoare Logic, continued
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CS314 – Section 5 Recitation 9
Polymorphic Functions
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CSE341: Programming Languages Lecture 7 First-Class Functions
A Simple Syntax-Directed Translator
Introduction to Computer Science / Procedural – 67130
CSE 341: Programming Languages Section 1
CSE 341: Programming Langs
CSE341: Programming Languages Section 1
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE341: Programming Languages Lecture 7 First-Class Functions
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341: Programming Languages Section 1
CSE 341 Section 5 Winter 2018.
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
CSE341: Programming Languages Lecture 7 First-Class Functions
Nate Brunelle Today: Conditional Decision Statements
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Lecture 11 b closures; scoping rules
Introduction to the Lab
CSE341: Programming Languages Lecture 7 First-Class Functions
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 7 First-Class Functions
Presentation transcript:

OCaml The PL for the discerning hacker.

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

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 = ? ? ?

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 1.basic (recap) 2.let 3.if 4.fun 5.demo M.C. Escher’s Waterfall in LEGO

basic TypeExpressionValue int55 5 * 5 * 5125 string“hello” string“Je” ^ “ll” ^ “o”“Jello” tuple(5 * 5, “ab” ^ “cd”)(25, “abcd”) int list[1; [3; 4][1; 2; 3; 4] int list1 :: 2 :: 3 :: 4 :: [][1; 2; 3; 4]

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

Building Expressions 1.basic (recap) 2.let 3.if 4.fun 5.demo M.C. Escher’s Waterfall in LEGO

Variables are central to programming Associate a name with a computation let expressions are how ML does it 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

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): 1.evaluate E1 to value V 2.replace UNBOUND NM 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 = in let x = x * x in x in x + x in x * x

name clash mania let x = let x = in let x = x * x in x in x + x in x * x

name clash mania 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 = 50 in let x = 50 * 50 in x in x + x in x * x

name clash mania let x = let x = 50 * 50 in x in x + x in x * x

name clash mania let x = let x = 2500 in x in x + x in x * x

name clash mania let x = let x = 2500 in 2500 in x + x in x * x

name clash mania 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 in x * x

name clash mania let x = 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? 1.Behavior fixed at definition 2.Localize debugging 3.Simplifies reasoning

Building Expressions 1.basic (recap) 2.let 3.if 4.fun 5.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 TEST evals to true, eval expr E1. Otherwise, eval expr E2.

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 examples TypeExpressionValue intif true then 5 else 105 intif false then 5 else 1010 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]

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; }

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

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

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

Building Expressions 1.basic (recap) 2.let 3.if 4.fun 5.demo M.C. Escher’s Waterfall in LEGO

abstraction

fun Abstraction: ultimate complexity manager Provide simple interface to complex expr functions are how ML does it

fun fun NM -> E Accept value V as input. Replace unbound name NM in expr E with V.

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”

functions are values bind a function to a name with let : let double = fun x -> x + x in double 5 let quad = fun x -> (double x) + (double x) in quad 5

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

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

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))

function on function action let add = fun x -> fun y -> x + y in add 5 10

function on function action let str_concat = fun x -> fun y -> x ^ y in str_concat “hello “ “india”

functions are values functions can take functions as input! let apply = fun f -> fun x -> f x in apply print_string “hello”

functions are values functions can take functions as input! (fun f -> fun x -> f x) print_string “hello”

functions are values functions can take functions as input! (fun x -> print_string x) “hello”

functions are values functions can take functions as input! print_string “hello”

function shorthand syntactic sugar let NM P1... PN = E means let NM = fun P1 ->... fun PN -> E

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)

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...

recursive fun Need let rec : let rec NM P = E NM is bound to this definition inside E.

recursive fun let rec is_even n =...

recursive fun let rec is_even n = if n = 0 then true else if n = 1 then false else is_even (n – 2)

recursive fun let rec fib n =...

recursive fun let rec fib n = if n = 0 then 1 else if n = 1 then 1 else (fib (n – 1)) + (fib (n – 2))

recursive fun let rec factorial n =...

recursive fun let rec factorial n = if n = 0 then 1 else n * (factorial (n – 1))

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

recursive fun let rec forever () = forever ()

fun and let Simple enough: let x = 5 in let f y = x + y in f 10 Evals to 15.

fun and let But what about: let x = 5 in let f y = x + y in let x = 10 f 10 Still evals to 15. Behavior fixed at binding !

Building Expressions 1.basic (recap) 2.let 3.if 4.fun 5.demo M.C. Escher’s Waterfall in LEGO

demo Conway’s Game of Life

Building Expressions 1.basic (recap) 2.let 3.if 4.fun 5.demo Next Time : Building Types M.C. Escher’s Waterfall in LEGO