
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 = <fun> val d : (int * int * int) * (string * int) * float = # plus_two 17;; ((1, 4, 62), ("bye", 15), 73.95) - : int = 19 # (*Patterns can be nested *) # let plus_two = fun n -> n + 2;; let (p,(st,_),_) = d;; (* _ matches all, binds nothing val plus_two : int -> int = <fun> *) # plus_two 14;; val p : int * int * int = (1, 4, 62) - : int = 16 val st : string = "bye" First definition syntactic sugar for second 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 = <fun> - : (float -> float) * (int -> int) = (<fun>, <fun>) # plus_x 3;; 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;;
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages10 Page
-
File Size-