610 likes | 616 Views
Programming Languages and Compilers (CS 421). Madhusudan Parthasarathy (madhu) 3112 Siebel Center madhu@illinois.edu https://courses.engr.illinois.edu/cs421/fa2017/CS421D.
E N D
Programming Languages and Compilers (CS 421) Madhusudan Parthasarathy (madhu) 3112 Siebel Center madhu@illinois.edu https://courses.engr.illinois.edu/cs421/fa2017/CS421D Based on slides by Elsa Gunter, who made these slides mostly, in part borrowing from slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha
Contact Information - Madhu • Office: 3112 SC • Office hours: TBD • Email: madhu@illinois.edu
Teaching Assistants • Andre Gou • John Lee • Liyi Li • Qianyang Peng • Nicholas Walker • Office hours in 0207 SC For office hour times, see website
What this course is about • Functional programming (Ocaml) • A different paradigm of programming that you are used to • Closer to mathematics; simple; more elegant; at the heart of PL research • Key idea: Manipulate functions to get the function you want. • Programming Languages: Under the hood • Look under-the-hood of a programming language • How exactly do you define a programming language? It’s syntax. What a program “means” (Semantics) How do you build interpreters for them? Compilers? • How do you prove properties about them? How do you reason with them? (Types and Hoare logic) • Functional programming is really an arena where you can study all this clearly.
What this course is about • There are umpteen programming languages Fortran, LISP, Pascal (~60yrs ago) C (~45yrs ago) Java (~25yrs ago) C# (~15yrs ago) Rust (~10yrs ago): systems programming Go (~5yrs ago) The next 700 programming languages. It’s easy to program in them. But it’s hard to program well in them. And it’s hard to understand the concepts on which they are built.
What this course is about • Linguistic relativity hypothesis: The structure of a language affects its speakers’ world view or cognition. • In this course, you’ll learn to think clearly about programs and learn to express them naturally. This course hence can rewire the way you look at programs and make you better programmers in any language.
What this course is about Functional programming is but one paradigm. But the notions of syntax, semantics, interpreters, lexers, compilers, recursion, lambdas, higher-order functions, programming abstractions, types, type safety, type inference, verification, Hoare-logic style reasoning, etc. are ubiquitous. The fundamental building blocks in prog languages are often the same; but they occur in different disguises. (e.g., look at lambdas in Java)
Why not C? • A formal semantics of C is very very hard. • At research level!! [Rosu et al POPL 12] • Compilers do not agree! • Leroy’s CompCert, a certified compiler for a formalsemantics of a subset of C is hugely influential in PL research. E.g.: int main(void) { int x = 0; return (x = 1) + (x = 2); } GCC4: 4 GCC3, ICC, Clang: 3 There are in fact verification tools that prove it returns 4 according to a formal semantics.
Why not C? C has too much of strange stuff: int main(int argc, char* argv[]) { printf("%p", malloc(42)); return 0; } Pointers return a different result each time. Nondeterminism can be good; but should be controlled! • It has too many things! Hard to formalize even a fragment and understand it well. • Undefined behavior Eg: char *p = "wikipedia"; p[0] = 'W’; //mod a string literalEg: int main(void) { int a = 0; int b = 0; return &a < &b; } • Not type safe • Buffer overflows can rewrite the program itself… • Not math friendly! x = x + 1; WTF??!!
Programming Languages & Compilers Three Main Topics of the Course I II III New Programming Paradigm Language Semantics Language Translation
Programming Languages & Compilers Order of Evaluation I II III New Programming Paradigm Language Translation Language Semantics Specification to Implementation
Programming Languages & Compilers I : New Programming Paradigm Patterns of Recursion Environments and Closures Functional Programming Continuation Passing Style
Programming Languages & Compilers Order of Evaluation Patterns of Recursion Environments and Closures Functional Programming Continuation Passing Style Specification to Implementation
Programming Languages & Compilers II : Language Translation Interpretation Type Systems Lexing and Parsing
Programming Languages & Compilers Order of Evaluation Interpretation Type Systems Lexing and Parsing Specification to Implementation
Programming Languages & Compilers III : Language Semantics Axiomatic Semantics Lambda Calculus Operational Semantics
Programming Languages & Compilers Order of Evaluation Axiomatic Semantics Lambda Calculus Operational Semantics CS426 CS477 CS422 Specification to Implementation
Some Course References • No required textbook • Some suggested references
Course Website https://courses.engr.illinois.edu/cs421/fa2018/CS421D • Main page - summary of news items • Policy - rules governing course • Lectures - syllabus and slides • MPs - information about assignments • Exams • Unit Projects - for 4 credit students • Resources - tools and helpful info • FAQ
News forum • Piazza --- Sign up now! • piazza.com/illinois/fall2018/cs421 • Forum for asking questions, answered by TAs and professors.May also have reminders for homework, etc.
Course Grading • Assignments 20% • About 12 Web Assignments (WA) (~7%) • About 6 MPs (in Ocaml) (~7%) • About 5 Labs (~6%) • All WAs and MPs Submitted in PrairieLearn • Late submission penalty: 20% • Labs in Computer-Based Testing Center (Grainger) Self-scheduled over a three day period No extensions beyond the three day period • Fall back: Labs become MPs
Course Grading • A salient aspect of this course: • All assignments/exams are AUTOGRADED! • PrairieLearn: https://prairielearn.engr.illinois.edu/ • A wonderful system on PrairieLearn mainly developed by Elsa Gunter, does this. Its inner workings are in fact applications of PL theory, some of it covered by what you learn in this course. • TAs will be working on the system and partially supported by the dept. and a SIIP Grant
Course Grading • Assignments 20% • About 12 Web Assignments (WA) (~7%) • About 6 MPs (in Ocaml) (~7%) • About 5 Labs (~6%) • All WAs and MPs Submitted through PrairieLearn • Late submission penalty: 20% • Labs in Computer-Based Testing Center (Grainger) • Self-scheduled over a three day period • No extensions beyond the three day period • Fall back: Labs become MPs
Course Grading • 2 Midterms - 20% each • Labs in Computer-Based Testing Center (Grainger) • Self-scheduled over a three day period • No extensions beyond the three day period • Dates: Oct 2-4 (Midterm 1) Nov 6-8 (Midterm 2) • Fall back: In class backup dates – Oct 9, Nov 13 • DO NOT MISS EXAM DATES! • Final 40% - Dec 19, 8:00am – 11:00am (nominally) • Will likely use CBTF for Final (3 day window) • Percentages are approximate
Course Assingments – WA & MP • You may discuss assignments and their solutions with others • You may work in groups, but you must list members with whom you worked if you share solutions or solution outlines • Each student must write up and turn in their own solution separately • You may look at examples from class and other similar examples from any source – cite appropriately • Note: University policy on plagiarism still holds - cite your sources if you are not the sole author of your solution
Your schedule • MP: Machine problems; attempted anywhere, on the web, on PrairieLearn • ML: Machine labs; problems in the testing center • WA: Web assignments, attempted anywhere, on the web, on PrairieLearn. Difference from MPs: may not be programming assignments Your schedule: • Attend lectures • Do the MPs and WAs (homework) • Seek out teaching staff in their office hours • Interact with teaching staff on Piazza • Schedule times at the testing center to do the MLs • Take the exams (midterms and final)
OCAML • Locally: • Compiler is on the EWS-linux systems at /usr/local/bin/ocaml • Globally: • Main CAML home: http://ocaml.org • To install OCAML on your computer see: http://ocaml.org/docs/install.html
References for OCaml • Supplemental texts (not required): • The Objective Caml system release 4.05, by Xavier Leroy, online manual • 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
Why learn OCAML? • Many features not clearly in languages you have already learned • Assumed basis for much research in programming language research • OCAML is particularly efficient for programming tasks involving languages (eg parsing, compilers, user interfaces) • Industrially Relevant: Jane Street trades billions of dollars per day using OCaml programs • Similar languages: Microsoft F#, SML, Haskell, Scala
Session in OCAML % ocaml Objective Caml version 4.01 # (* Read-eval-print loop; expressions and declarations *) 2 + 3;;(* Expression *) • : int = 5 # 3 < 2;; - : bool = false
No Overloading for Basic Arithmetic Operations # 15*2;; - : int = 30 # 1.35+0.23;; (* Wrong type of addition *) 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 # 1.35+.0.23;; - : float = 1.58
No Implicit Coercion # 1.0 * 2;; (* No Implicit Coercion *) Characters 0-3: 1.0 * 2;; (* No Implicit Coercion *) ^^^ Error: This expression has type float but an expression was expected of type int
Sequencing Expressions # "Hi there";; (* has type string *) - : string = "Hi there" # print_string"Hello world\n";; (* has type unit *) Hello world - : unit = () # (print_string "Bye\n";25);; (* Sequence of exp *) Bye - : int = 25
Declarations; Sequencing of Declarations # letx = 2 + 3;; (* declaration *) val x : int = 5 # let test = 3 < 2;; val test : bool = false # let a = 1 let b = a + 4;; (* Sequence of dec *) val a : int = 1 val b : int = 5
Environments • Environmentsrecord what value is associated with a given identifier • Central to the semantics and implementation of a language • Notation = {name1 value1, name2 value2, …} Using set notation, but describes a partial function • Often stored as list, or stack • To find value start from left and take first match
Environments X 3 name “Steve” . . . region (5.4, 3.7) y 17 id {Name = “Paul”, Age = 23, SSN = 999888777} b true
Global Variable Creation # 2 + 3;; (* Expression *) // doesn’t affect the environment # let test = 3 < 2;; (* Declaration *) val test : bool = false // 1 = {test false} # let a = 1 let b = a + 4;; (* Seq of dec *) // 2 = {b 5, a 1, test false}
Environments test true a 1 b 5
New Bindings Hide Old // 2 = {b 5, a 1, test false} let test = 3.7;; • What is the environment after this declaration?
New Bindings Hide Old // 2 = {b 5, a 1, test false} let test = 3.7;; • What is the environment after this declaration? // 3 = {test 3.7, a 1, b 5}
Environments test 3.7 a 1 b 5
Now it’s your turn You should be able to do WA1 Problem 1 , parts (* 1 *) and (* 2 *)
Local Variable Creation // 3 = {test 3.7, a 1, b 5} # letb = 5 * 4 // 4 = {b 20, test 3.7, a 1} in2 * b;; - : int = 40 // 5 = 3= {test 3.7, a 1, b 5} # b;; - : int = 5 b 20 test 3.7 test 3.7 test 3.7 a 1 a 1 a 1 b 5 b 5 b 5
Local let binding // 5 = {test 3.7, a 1, b 5} # let c = letb = a + a // 6 = {b 2} + 3 // ={b 2, test 3.7, a 1} inb * b;; val c : int = 4 // 7 = {c 4, test 3.7, a 1, b 5} # b;; - : int = 5 test 3.7 a 1 b 5
Local let binding // 5 = {test 3.7, a 1, b 5} # let c = letb = a + a // 6 = {b 2} + 3 // ={b 2, test 3.7, a 1} inb * b;; val c : int = 4 // 7 = {c 4, test 3.7, a 1, b 5} # b;; - : int = 5 test 3.7 test 3.7 a 1 a 1 b 2 b 5 b 5
Local let binding // 5 = {test 3.7, a 1, b 5} # let c = letb = a + a // 6 = {b 2} + 3 // ={b 2, test 3.7, a 1} inb * b;; val c : int = 4 // 7 = {c 4, test 3.7, a 1, b 5} # b;; - : int = 5 test 3.7 test 3.7 test 3.7 a 1 a 1 a 1 b 2 c 4 b 5 b 5 b 5
Functions # let plus_two n= n + 2;; val plus_two : int -> int = <fun> # plus_two 17;; - : int = 19
Functions let plus_two n= n + 2;; plus_two 17;; - : int = 19
Nameless Functions (aka Lambda Terms) fun n-> n + 2;; (fun n-> n + 2) 17;; - : int = 19