Programming Languages and Compilers CS 421 Elsa L

  • Slides: 54
Download presentation
Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC https: //courses.

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC https: //courses. engr. illinois. edu/cs 421/fa 2019 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/2/2020 1

Programming Languages & Compilers Three Main Topics of the Course I III New Programming

Programming Languages & Compilers Three Main Topics of the Course I III New Programming Paradigm Language Semantics 10/2/2020 2

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics 10/2/2020 3

Programming Languages & Compilers I : New Programming Paradigm Functional Environments Patterns of Programming

Programming Languages & Compilers I : New Programming Paradigm Functional Environments Patterns of Programming and Recursion Closures 10/2/2020 Continuation Passing Style 4

Programming Languages & Compilers Functional Environments Patterns of Programming and Recursion Closures 10/2/2020 Continuation

Programming Languages & Compilers Functional Environments Patterns of Programming and Recursion Closures 10/2/2020 Continuation Passing Style 5

Programming Languages & Compilers II : Language Translation Lexing and Parsing 10/2/2020 Type Systems

Programming Languages & Compilers II : Language Translation Lexing and Parsing 10/2/2020 Type Systems Interpretation 6

Programming Languages & Compilers Lexing and Parsing 10/2/2020 Type Systems Interpretation 7

Programming Languages & Compilers Lexing and Parsing 10/2/2020 Type Systems Interpretation 7

Programming Languages & Compilers III : Language Semantics Operational Semantics 10/2/2020 Lambda Calculus Axiomatic

Programming Languages & Compilers III : Language Semantics Operational Semantics 10/2/2020 Lambda Calculus Axiomatic Semantics 8

Programming Languages & Compilers Operational Semantics Lambda Calculus CS 422 10/2/2020 Axiomatic Semantics CS

Programming Languages & Compilers Operational Semantics Lambda Calculus CS 422 10/2/2020 Axiomatic Semantics CS 426 CS 477 9

Contact Information - Elsa L Gunter n n Office: 2112 SC Office hours: n

Contact Information - Elsa L Gunter n n Office: 2112 SC Office hours: n n Monday 10: 30 am – 11: 20 pm Wednesday 1: 30 pm 2: 20 pm – Also by appointment Email: egunter@illinois. edu 10/2/2020 10

Course TAs Paul Krogmeier Jacob Laurel 10/2/2020 John Lee Leon Medvinsky Liyi Li Adithya

Course TAs Paul Krogmeier Jacob Laurel 10/2/2020 John Lee Leon Medvinsky Liyi Li Adithya Murali 11

Contact Information - TAs n n Teaching Assistants Office: 0207 SC Paul M Krogmeier

Contact Information - TAs n n Teaching Assistants Office: 0207 SC Paul M Krogmeier n n n Email: paulmk 2@illinois. edu Hours: Wed 2: 30 pm – 3: 20 pm Fri 2: 30 pm – 3: 20 pm Jacob Scott Laurel n n Email: jlaurel 2@illinois. edu Hours: Fri 10: 00 am – 11: 40 pm 10/2/2020 12

Contact Information - TAs n n Teaching Assistants Office: 0207 SC John J Lee

Contact Information - TAs n n Teaching Assistants Office: 0207 SC John J Lee n n n Email: jlee 170@illinois. edu Hours: Tues 2: 00 pm – 2: 50 pm Thurs 2: 00 pm – 2: 50 pm Liyi Li n n Email: jlaurel 2@illinois. edu Hours: Mon & Fri 1: 00 pm – 1: 50 pm 10/2/2020 13

Contact Information – TAs cont n Leon Ken Medvinsky n n n Email: leonkm

Contact Information – TAs cont n Leon Ken Medvinsky n n n Email: leonkm 2@illinois. edu Hours: Mon 2: 30 pm , 3: 20 pm – Tues 11: 00 am-11: 50 am Adithya Murali n n Email: adithya 5@illinois. edu Hours: Tues & Thurs 10: 00 am – 10: 50 am 10/2/2020 14

Course Website n n n n n https: //courses. engr. illinois. edu/cs 421/fa 2019

Course Website n n n n n https: //courses. engr. illinois. edu/cs 421/fa 2019 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 10/2/2020 15

Some Course References n n No required textbook Some suggested references 10/2/2020 16

Some Course References n n No required textbook Some suggested references 10/2/2020 16

Some Course References n n n No required textbook. Pictures of the books on

Some Course References n n n No required textbook. Pictures of the books on previous slide Essentials of Programming Languages (2 nd Edition) by Daniel P. Friedman, Mitchell Wand Christopher T. Haynes, MIT Press 2001. Compilers: Principles, Techniques, and Tools, (also known as "The Dragon Book"); by Aho, Sethi, and Ullman. Published by Addison-Wesley. ISBN: 0 -20110088 -6. Modern Compiler Implementation in ML by Andrew W. Appel, Cambridge University Press 1998 Additional ones for Ocaml given separately 10/2/2020 17

Course Grading n Assignments 20% n n n n n About 12 Web Assignments

Course Grading n Assignments 20% n n n n n About 12 Web Assignments (WA) (~7%) About 5 MPs (in Ocaml) (~6%) About 6 Labs (~7%) All WAs and MPs Submitted by Prairie. Learn Late submission penalty: 20% Labs in Computer-Based Testing Center (Grainger) Self-scheduled over a four day period Rules of CBTF apply Fall back: Labs become MPs 10/2/2020 18

Course Grading n 2 Midterms - 20% each n n n Labs in Computer-Based

Course Grading n 2 Midterms - 20% each n n n Labs in Computer-Based Testing Center (Grainger) Self-scheduled over a four day period Fall back: In class backup dates – Oct 7, Nov 18 BE AVAILABLE FOR FALL BACK DATES! Final 40% - CBTF Fall back: In class backup date: Dec 20, 7: 00 pm 10: 00 pm Percentages are approximate n n 10/2/2020 19

Course Assingments – WA & MP n n You may discuss assignments and their

Course Assingments – WA & MP n n 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 n n Note: University policy on plagiarism still holds - cite your sources if you are not the sole author of your solution Do not have to cite course notes or me 10/2/2020 20

OCAML n Locally: n n Compiler is on the EWS-linux systems at /usr/local/bin/ocaml Globally:

OCAML n Locally: n n Compiler is on the EWS-linux systems at /usr/local/bin/ocaml Globally: n n n Main CAML home: http: //ocaml. org To install OCAML on your computer see: http: //ocaml. org/docs/install. html To try on the web: https: //try. ocamlpro. com 10/2/2020 22

References for OCaml n n Supplemental texts (not required): The Objective Caml system release

References for OCaml n n 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 n Available online from course resources 10/2/2020 23

Why learn OCAML? n n Many features not clearly in languages you have already

Why learn OCAML? n n 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: n n n Jane Street trades billions of dollars per day using OCaml programs Major language supported at Bloomberg Similar languages: Microsoft F#, SML, Haskell, Scala 10/2/2020 26

Session in OCAML % ocaml Objective Caml version 4. 01 # (* Read-eval-print loop;

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 10/2/2020 27

No Overloading for Basic Arithmetic Operations # 15 * 2; ; - : int

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 10/2/2020 28

No Implicit Coercion # 1. 0 * 2; ; (* No Implicit Coercion *)

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 10/2/2020 29

Sequencing Expressions # "Hi there"; ; (* has type string *) - : string

Sequencing Expressions # "Hi there"; ; (* has type string *) - : string = "Hi there" # print_string "Hello worldn"; ; (* has type unit *) Hello world - : unit = () # (print_string "Byen"; 25); ; (* Sequence of exp *) Bye - : int = 25 10/2/2020 30

Declarations; Sequencing of Declarations # let x = 2 + 3; ; (* declaration

Declarations; Sequencing of Declarations # let x = 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 10/2/2020 31

Environments n n n Environments record what value is associated with a given identifier

Environments n n n Environments record what value is associated with a given identifier Central to the semantics and implementation of a language Notation = {name 1 value 1, name 2 value 2, …} Using set notation, but describes a partial function n Often stored as list, or stack n To find value start from left and take first match 10/2/2020 32

Environments X 3 name “Steve” y 17 . . . region (5. 4, 3.

Environments X 3 name “Steve” y 17 . . . region (5. 4, 3. 7) id {Name = “Paul”, Age = 23, b true SSN = 999888777} 10/2/2020 33

Global Variable Creation # 2 + 3; ; (* Expression *) // doesn’t affect

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} 10/2/2020 34

Environments test true a 1 10/2/2020 b 5 35

Environments test true a 1 10/2/2020 b 5 35

New Bindings Hide Old // 2 = {b 5, a 1, test false} let

New Bindings Hide Old // 2 = {b 5, a 1, test false} let test = 3. 7; ; n What is the environment after this declaration? 10/2/2020 36

New Bindings Hide Old // 2 = {b 5, a 1, test false} let

New Bindings Hide Old // 2 = {b 5, a 1, test false} let test = 3. 7; ; n What is the environment after this declaration? // 3 = {test 3. 7, a 1, b 5} 10/2/2020 37

Environments test 3. 7 a 1 10/2/2020 b 5 38

Environments test 3. 7 a 1 10/2/2020 b 5 38

Now it’s your turn You should be able to do WA 1 Problem 1

Now it’s your turn You should be able to do WA 1 Problem 1 , parts (* 1 *) and (* 2 *) 10/2/2020 39

Local Variable Creation test 3. 7 b 5 // 3 = {test 3. 7,

Local Variable Creation test 3. 7 b 5 // 3 = {test 3. 7, a 1, b 5} # let b = 5 * 4 // 4 = {b 20, test 3. 7, a 1} test 3. 7 a 1 b 20 in 2 * b; ; b 5 - : int = 40 // 5 = 3= {test 3. 7, a 1, b 5} test 3. 7 # b; ; a 1 b 5 - : int = 5 a 1 10/2/2020 40

Local let binding a 1 // 5 = {test 3. 7, a 1, b

Local let binding a 1 // 5 = {test 3. 7, a 1, b 5} # let c = let b = a + a // 6 = {b 2} + 3 // ={b 2, test 3. 7, a 1} in b * b; ; val c : int = 4 // 7 = {c 4, test 3. 7, a 1, b 5} # b; ; - : int = 5 10/2/2020 test 3. 7 b 5 41

Local let binding a 1 test 3. 7 // 5 = {test 3. 7,

Local let binding a 1 test 3. 7 // 5 = {test 3. 7, a 1, b 5} b 5 # let c = let b = a + a test 3. 7 a 1 b 25 // 6 = {b 2} + 3 b // ={b 2, test 3. 7, a 1} in b * b; ; val c : int = 4 // 7 = {c 4, test 3. 7, a 1, b 5} # b; ; - : int = 5 10/2/2020 42

Local let binding a 1 test 3. 7 // 5 = {test 3. 7,

Local let binding a 1 test 3. 7 // 5 = {test 3. 7, a 1, b 5} b 5 # let c = let b = a + a test 3. 7 a 1 b 25 // 6 = {b 2} + 3 b // ={b 2, test 3. 7, a 1} in b * b; ; test 3. 7 a 1 c 4 b 5 val c : int = 4 // 7 = {c 4, test 3. 7, a 1, b 5} # b; ; - : int = 5 10/2/2020 43

Functions # let plus_two n = n + 2; ; val plus_two : int

Functions # let plus_two n = n + 2; ; val plus_two : int -> int = <fun> # plus_two 17; ; - : int = 19 10/2/2020 52

Functions let plus_two n = n + 2; ; plus_two 17; ; - :

Functions let plus_two n = n + 2; ; plus_two 17; ; - : int = 19 10/2/2020 53

Nameless Functions (aka Lambda Terms) fun n -> n + 2; ; (fun n

Nameless Functions (aka Lambda Terms) fun n -> n + 2; ; (fun n -> n + 2) 17; ; - : int = 19 10/2/2020 54

Functions # let plus_two n = n + 2; ; val plus_two : int

Functions # let plus_two n = n + 2; ; val plus_two : int -> int = <fun> # plus_two 17; ; - : int = 19 # let plus_two = fun n -> n + 2; ; val plus_two : int -> int = <fun> # plus_two 14; ; - : int = 16 First definition syntactic sugar for second 10/2/2020 55

Using a nameless function # (fun x -> x * 3) 5; ; (*

Using a nameless function # (fun x -> x * 3) 5; ; (* An application *) - : int = 15 # ((fun y -> y +. 2. 0), (fun z -> z * 3)); ; (* As data *) - : (float -> float) * (int -> int) = (<fun>, <fun>) Note: in fun v -> exp(v), scope of variable is only the body exp(v) 10/2/2020 56

Values fixed at declaration time X 12 … # let x = 12; ;

Values fixed at declaration time X 12 … # let x = 12; ; val x : int = 12 # let plus_x y = y + x; ; val plus_x : int -> int = <fun> # plus_x 3; ; What is the result? 10/2/2020 57

Values fixed at declaration time # let x = 12; ; val x :

Values fixed at declaration time # let x = 12; ; val x : int = 12 # let plus_x y = y + x; ; val plus_x : int -> int = <fun> # plus_x 3; ; - : int = 15 10/2/2020 58

Values fixed at declaration time # let x = 7; ; (* New declaration,

Values fixed at declaration time # let x = 7; ; (* New declaration, not an update *) val x : int = 7 # plus_x 3; ; What is the result this time? 10/2/2020 59

Values fixed at declaration time # let x = 7; ; (* New declaration,

Values fixed at declaration time # let x = 7; ; (* New declaration, not an X 7 update *) … val x : int = 7 X 12 … # plus_x 3; ; What is the result this time? 10/2/2020 60

Values fixed at declaration time # let x = 7; ; (* New declaration,

Values fixed at declaration time # let x = 7; ; (* New declaration, not an update *) val x : int = 7 # plus_x 3; ; - : int = 15 10/2/2020 61

Question n Observation: Functions are first-class values in this language Question: What value does

Question n Observation: Functions are first-class values in this language Question: What value does the environment record for a function variable? Answer: a closure 10/2/2020 62

Save the Environment! n n A closure is a pair of an environment and

Save the Environment! n n A closure is a pair of an environment and an association of a sequence of variables (the input variables) with an expression (the function body), written: f < (v 1, …, vn) exp, f > Where f is the environment in effect when f is defined (if f is a simple function) 10/2/2020 63

Closure for plus_x n When plus_x was defined, had environment: plus_x = {…, x

Closure for plus_x n When plus_x was defined, had environment: plus_x = {…, x 12, …} n Recall: let plus_x y = y + x is really let plus_x = fun y -> y + x n Closure for fun y -> y + x: n <y y + x, plus_x > Environment just after plus_x defined: {plus_x <y y + x, plus_x >} + plus_x 10/2/2020 64

Now it’s your turn You should be able to do WA 1 Problem 1

Now it’s your turn You should be able to do WA 1 Problem 1 , parts (* 7 *) and (* 8 *) 10/2/2020 65