Contact Information – Grigore Rosu
Programming Languages and Office: 2110 SC
Compilers (CS 421) Office hours:
Tuesdays 11:00am – 12:00pm
Thursdays 11:00am – 12:00pm
Grigore Rosu Also by appointment 2110 SC, UIUC Email: [email protected] http://courses.engr.illinois.edu/cs421 Slides by Elsa Gunter, based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha
1/23/2014 1 1/23/2014 2
Contact Information - TAs Course Website
Teaching Assistants Office: 0207 SC http://courses.engr.illinois.edu/cs421
Edgar Pek ([email protected]) Main page - summary of news items
Hours: Wed 2:00pm – 4:00pm Policy - rules governing course
Amarin Phaosawasdi ([email protected]) Lectures - syllabus and slides
Hours: Monday 2:00pm – 4:00pm MPs - information about homework
Andrei Stefanescu ([email protected]) Exams
Hours: Friday 2:00pm – 4:00pm Unit Projects - for 4 credit students
Resources - tools and helpful info
FAQ
1/23/2014 3 1/23/2014 4
Some Course References Course Grading
No required textbook. Homework 20% Essentials of Programming Languages (2nd Edition) by Daniel P. Friedman, Mitchell Wand and About 11 MPs (in Ocaml) and 11 written assignments Christopher T. Haynes, MIT Press 2001. Submitted by handin on EWS linux machines MPs – plain text code that compiles; HWs – pdf Compilers: Principles, Techniques, and Tools, (also No extensions and no late submissions granted known as "The Dragon Book"); by Aho, Sethi, and (unless there are very special circumstances) Ullman. Published by Addison-Wesley. ISBN: 0- 201-10088-6. 2 Midterms - 20% each In class – March 6, April 17 Modern Compiler Implementation in ML by Andrew W. Appel, Cambridge University Press 1998 DO NOT MISS EXAM DATES! Final 40% - May 13, 3 hours (details to be given) Additional ones for Ocaml given separately
1/23/2014 5 1/23/2014 6 Course Homework Course Homework and Exams
You may discuss homeworks and their solutions Problems from homework may appear verbatim, or with others with some modification on exams You may work in groups, but you must list members with whom you worked if you share Caution: Failure to be able to do homework solutions or solution outlines problems when they appear on the exam may Each student must turn in their own solution cause you to loose credit for the assignment from separately which they came, if you turned in a solution for the You may look at examples from class and other problem on the assignment similar examples from any source Note: University policy on plagiarism still holds - cite your sources if you are not the sole author of your solution
1/23/2014 7 1/23/2014 8
Course Objectives OCAML
New programming paradigm Compiler is on the EWS-linux systems at Functional programming /usr/local/bin/ocaml Order of Evaluation Tail Recursion A (possibly better, non-PowerPoint) text
Continuation Passing Style version of this lecture can be found at Phases of an interpreter / compiler http://courses.engr.illinois.edu/cs421/lecture Lexing and parsing s/ocaml-intro-shell.txt Type checking For the OCAML code for today’s lecture see Evaluation http://courses.engr.illinois.edu/cs421/lecture Programming Language Semantics s/ocaml-intro.ml Lambda Calculus
Operational Semantics
1/23/2014 9 1/23/2014 10
WWW Addresses for OCAML References for CAML
Main CAML home: Supplemental texts (not required): http://caml.inria.fr/index.en.html The Objective Caml system release 4.0, by To install OCAML on your computer see: Xavier Leroy, online manual http://caml.inria.fr/ocaml/release.en.html Introduction to the Objective Caml Programming Language, by Jason Hickey Developing Applications With Objective Caml, by Emmanuel Chailloux, Pascal Manoury, and Bruno Pagano, on O’Reilly
Available online from course resources
1/23/2014 11 1/23/2014 12 OCAML Features of OCAML
CAML is European descendant of original ML Higher order applicative language
American/British version is SML Call-by-value parameter passing
O is for object-oriented extension Modern syntax
ML stands for Meta-Language Parametric polymorphism ML family designed for implementing Aka structural polymorphism theorem provers Automatic garbage collection
It was the meta-language for programming the User-defined algebraic data types “object” language of the theorem prover
Despite obscure original application area, OCAML It’s fast - winners of the 1999 and 2000 ICFP is a full general-purpose programming language Programming Contests used OCAML
1/23/2014 13 1/23/2014 14
Why learn OCAML? Session in OCAML
Something new: many features may not % ocaml appear in languages that you already know Objective Caml version 3.12.0 # (* Read-eval-print loop; expressions and Assumed basis for much research in declarations *) programming language research 2 + 3;; (* Expression *) Closely related to popular - : int = 5 OCAML is particularly efficient for # 3 < 2;; programming tasks involving languages (e.g. - : bool = false parsing, compilers, user interfaces)
Used at Microsoft Research for writing SLAM, a formal methods tool for C programs 1/23/2014 15 1/23/2014 16
No Overloading for Basic Arithmetic Operations No Implicit Coercion
# 15 * 2;; # 1.0 * 2;; (* No Implicit Coercion *) - : int = 30 Characters 0-3: # 1.35 + 0.23;; (* Wrong type of addition *) 1.0 * 2;; (* No Implicit Coercion *) Characters 0-4: ^^^ 1.35 + 0.23;; (* Wrong type of addition *) Error: This expression has type float but an expression was expected of type ^^^^ int Error: This expression has type float but an expression was expected of type int # 1.35 +. 0.23;; - : float = 1.58
1/23/2014 17 1/23/2014 18 Sequencing Expressions Terminology
# "Hi there";; (* has type string *) Output refers both to the result returned - : string = "Hi there" from a function application … # print_string "Hello world\n";; (* has type unit *) As in + outputs integers, whereas +. outputs floats Hello world … and to text printed as a side-effect of a - : unit = () computation # (print_string "Bye\n"; 25);; (* Sequence of exp *) As in print_string “\n” outputs a carriage return Bye In terms of values, it outputs ( ) (“unit”) - : int = 25 We will standardly use “output” to refer to the value returned
1/23/2014 19 1/23/2014 20
Declarations; Sequencing of Declarations Environments
# let x = 2 + 3;; (* declaration *) Environments record what value is associated with val x : int = 5 a given identifier Central to the semantics and implementation of a # let test = 3 < 2;; language val test : bool = false Notation
# let a = 3 let b = a + 2;; (* Sequence of dec = {name1 value1, name2 value2, …} *) Using set notation, but describes a partial function val a : int = 3 Often stored as list, or stack To find value start from left and take first match val b : int = 5
1/23/2014 21 1/23/2014 22
Global Variable Creation New Bindings Hide Old
# 2 + 3;; (* Expression *) // 2 = {b 5, a 3, test false} // doesn’t affect the environment let test = 3.7;; # let test = 3 < 2;; (* Declaration *) val test : bool = false What is the environment after this declaration? // 1 = {test false} # let a = 3 let b = a + 2;; (* Seq of dec *)
// 2 = {b 5, a 3, test false}
1/23/2014 23 1/23/2014 24 New Bindings Hide Old Local let binding
// = {a 3, b 5, test 3.7} // 2 = {b 5, a 3, test false} 3 let test = 3.7;; # let c = let b = a + a
// 4 = {b 6} + 3 What is the environment after this // ={b 6, a 3, test 3.7} declaration? in b * b;; val c : int = 36 // = {c 36, a 3, b 5, test 3.7} // = {a 3, b 5, test 3.7} 5 3 # b;; - : int = 5
1/23/2014 25 1/23/2014 26
Local Variable Creation Booleans (aka Truth Values)
// 5 = {c 36, b 5, a 3, test 3.7} # true;; # let b = 5 * 4 - : bool = true // 6 = {b 20, c 36, a 3, test 3.7} # false;; in 2 * b;; - : bool = false - : int = 40 // = 7 5 # if b > a then 25 else 0;; # b;; - : int = 25 - : int = 5
1/23/2014 27 1/23/2014 28
Booleans Tuples
# 3 > 1 && 4 > 6;; # let s = (5,"hi",3.2);; - : bool = false val s : int * string * float = (5, "hi", 3.2) # 3 > 1 || 4 > 6;; # let (a,b,c) = s;; (* (a,b,c) is a pattern *) - : bool = true val a : int = 5 # (print_string "Hi\n"; 3 > 1) || 4 > 6;; val b : string = "hi" Hi val c : float = 3.2 - : bool = true # 3 > 1 || (print_string "Bye\n"; 4 > 6);; # let x = 2, 9.3;; (* tuples don't require parens in Ocaml *) - : bool = true # not (4 > 6);; val x : int * float = (2, 9.3) - : bool = true
1/23/2014 29 1/23/2014 30 Tuples Functions
# (*Tuples can be nested *) # let plus_two n = n + 2;; let d = ((1,4,62),("bye",15),73.95);; val plus_two : int -> int =
1/23/2014 31 1/23/2014 32
Using a nameless function Values fixed at declaration time
# (fun x -> x * 3) 5;; (* An application *) # let x = 12;; - : int = 15 val x : int = 12 # ((fun y -> y +. 2.0), (fun z -> z * 3));; # let plus_x y = y + x;; (* As data *) val plus_x : int -> int =
Note: in fun v -> exp(v), scope of variable is What is the result? only the body exp(v)
1/23/2014 33 1/23/2014 34
Values fixed at declaration time Values fixed at declaration time
# let x = 12;; # let x = 7;; (* New declaration, not an val x : int = 12 update *) # let plus_x y = y + x;; val x : int = 7 val plus_x : int -> int =
1/23/2014 35 1/23/2014 36 Values fixed at declaration time Functions with more than one argument
# let x = 7;; (* New declaration, not an # let add_three x y z = x + y + z;; update *) val add_three : int -> int -> int -> int =
Again, first syntactic sugar for second
1/23/2014 37 1/23/2014 38
Partial application of functions Functions as arguments
let add_three x y z = x + y + z;; # let thrice f x = f (f (f x));; val thrice : ('a -> 'a) -> 'a -> 'a =
1/23/2014 39 1/23/2014 40
Question Save the Environment!
Observation: Functions are first-class values A closure is a pair of an environment and an in this language association of a sequence of variables (the input variables) with an expression (the
Question: What value does the environment function body), written: record for a function variable? f < (v1,…,vn) exp, f > Where f is the environment in effect when f Answer: a closure is defined (if f is a simple function)
1/23/2014 41 1/23/2014 42 Closure for plus_x Evaluation of Application of plus_x;;
When plus_x was defined, had environment: Have environment: = {plus_x
1/23/2014 43 1/23/2014 44
Functions on tuples Match Expressions
# let plus_pair (n,m) = n + m;; # let triple_to_pair triple = val plus_pair : int * int -> int =
1/23/2014 45 1/23/2014 46
Closure for plus_pair Evaluation of Application with Closures
Assume was the environment just In environment , evaluate left term to closure, plus_pair c = <(x ,…,x ) b, > before plus_pair was defined 1 n (x1,…,xn) variables in (first) argument Closure for plus_pair: Evaluate the right term to values, (v1,…,vn) <(n,m) n + m, plus_pair> Update the environment to Environment just after plus_pair defined: ’ = {x1 v 1,…, xn vn}+ {plus_pair <(n,m) n + m, >} plus_pair Evaluate body b in environment ’ + plus_pair
1/23/2014 47 1/23/2014 48 Evaluation of Application of plus_pair Closure question
Assume environment If we start in an empty environment, and we = {x 3…, execute:
plus_pair <(n,m) n + m, plus_pair>} + let f n = n + 5;; plus_pair (* 0 *) Eval (plus_pair (4,x), )= let pair_map g (n,m) = (g n, g m);; Eval (app <(n,m) n + m, > (4,x), )) = plus_pair let f = pair_map f;; Eval (app <(n,m) n + m, plus_pair> (4,3), )) =
Eval (n + m, {n -> 4, m -> 3} + ) = plus_pair What is the environment at (* 0 *)? Eval (4 + 3, {n -> 4, m -> 3} + plus_pair) = 7
1/23/2014 49 1/23/2014 50
Answer Closure question
0 = {f
What is the environment at (* 1 *)?
1/23/2014 51 1/23/2014 52
Answer Closure question
0 = {f
What is the environment at (* 2 *)?
1/23/2014 53 1/23/2014 54 Answer Curried vs Uncurried
Recall val add_three : int -> int -> int -> int =
How does it differ from # let add_triple (u,v,w) = u + v + w;; val add_triple : int * int * int -> int =
add_three is curried;
add_triple is uncurried
1/23/2014 55 1/23/2014 56
Curried vs Uncurried Scoping Question
# add_triple (6,3,2);; Consider this code: - : int = 11 let x = 27;; # add_triple 5 4;; let f x = Characters 0-10: let x = 5 in add_triple 5 4;; (fun x -> print_int x) 10;; ^^^^^^^^^^ f 12;; This function is applied to too many arguments, What value is printed? maybe you forgot a `;' 5 # fun x -> add_triple (5,4,x);; 10 : int -> int =
1/23/2014 57 1/23/2014 58