SlideShare a Scribd company logo
09/19/24
1
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
09/19/24 2
Contact Information - Madhu
 Office: 3112 SC
 Office hours: TBD
 Email: madhu@illinois.edu
3
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.
4
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. 5
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 formal
semantics 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 literal
Eg: 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
09/19/24 10
I
New
Programming
Paradigm
III
Language
Semantics
Three Main Topics of the
Course
Programming Languages &
Compilers
09/19/24 11
I
New
Programming
Paradigm
II
Language
Translation
III
Language
Semantics
Programming Languages &
Compilers
09/19/24 12
Functional
Programming
Environments
and
Closures
Continuation
Passing
Style
Patterns of
Recursion
I : New Programming Paradigm
Programming Languages &
Compilers
09/19/24 13
Functional
Programming
Environments
and
Closures
Continuation
Passing
Style
Patterns of
Recursion
Programming Languages &
Compilers
09/19/24 14
Lexing and
Parsing
Type
Systems
Interpretation
II : Language Translation
Programming Languages &
Compilers
09/19/24 15
Lexing and
Parsing
Type
Systems
Interpretation
Programming Languages &
Compilers
09/19/24 16
Operational
Semantics
Lambda
Calculus
Axiomatic
Semantics
III : Language Semantics
Programming Languages &
Compilers
09/19/24 17
Operational
Semantics
Lambda
Calculus
Axiomatic
Semantics
CS422
CS426
CS477
Some Course References
 No required textbook
 Some suggested references
09/19/24 18
COURSE LOGISTICS
09/19/24
19
09/19/24 20
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.
09/19/24 23
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
24
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
0
9
/
1
9
/
2
4
2
5
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
09/19/24 27
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)
29
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
30
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
09/19/24 33
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
09/19/24 34
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
09/19/24 35
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
09/19/24 36
09/19/24 37
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
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
09/19/24 38
09/19/24 39
Environments
 Environments record 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
09/19/24 40
X  3
y  17
name  “Steve”
b  true
region  (5.4, 3.7)
id  {Name = “Paul”,
Age = 23,
SSN = 999888777}
. . .
09/19/24 41
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
09/19/24 42
b  5
test  true
a  1
New Bindings Hide Old
// 2 = {b  5, a  1, test  false}
let test = 3.7;;
 What is the environment after this declaration?
09/19/24 43
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}
09/19/24 44
Environments
09/19/24 45
b  5
test  3.7
a  1
Now it’s your turn
You should be able to do WA1
Problem 1 , parts (* 1 *) and (* 2 *)
09/19/24 46
09/19/24 47
Local Variable Creation
// 3 = {test  3.7, a  1, b  5}
# let b = 5 * 4
// 4 = {b  20, test  3.7, a  1}
in 2 * b;;
- : int = 40
// 5 = 3= {test  3.7, a  1, b  5}
# b;;
- : int = 5
b  5
test  3.7
a  1
b  5
test  3.7
a  1b  20
b  5
test  3.7
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
09/19/24 48
Local let binding
b  5
test  3.7
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
b  5
test  3.7
a  1
09/19/24 49
Local let binding
b  5
test  3.7
a  1
b  2
// 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
b  5
test  3.7
a  1
09/19/24 50
Local let binding
b  5
test  3.7
a  1
b  2
b  5
test  3.7
a  1
c  4
09/19/24 59
Functions
# let plus_two n = n + 2;;
val plus_two : int -> int = <fun>
# plus_two 17;;
- : int = 19
09/19/24 60
Functions
let plus_two n = n + 2;;
plus_two 17;;
- : int = 19
09/19/24 61
Nameless Functions (aka Lambda Terms)
fun n -> n + 2;;
(fun n -> n + 2) 17;;
- : int = 19
09/19/24 62
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
09/19/24 63
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)
09/19/24 64
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;;
What is the result?
X  12
…
09/19/24 65
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
09/19/24 66
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?
09/19/24 67
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?
X  12
…
X  7
…
09/19/24 68
Values fixed at declaration time
# let x = 7;; (* New declaration, not an update *)
val x : int = 7
# plus_x 3;;
- : int = 15
09/19/24 69
Question
 Observation: Functions are first-class values in
this language
 Question: What value does the environment
record for a function variable?
 Answer: a closure
09/19/24 70
Save the Environment!
 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  < (v1,…,vn)  exp, f >
 Where f is the environment in effect when f is
defined (if f is a simple function)
09/19/24 71
Closure for plus_x
 When plus_x was defined, had environment:
plus_x = {…, x  12, …}
 Recall: let plus_x y = y + x
is really let plus_x = fun y -> y + x
 Closure for fun y -> y + x:
<y  y + x, plus_x >
 Environment just after plus_x defined:
{plus_x  <y  y + x, plus_x >} + plus_x
Now it’s your turn
You should be able to do WA1
Problem 1 , parts (* 7 *) and (* 8 *)
09/19/24 72

More Related Content

PPT
Compiler Design - Introduction to Compiler
PPTX
Expection Setting - 1st ppt. pptx
PDF
data Integrative Programming untuk pemula.pdf
PDF
event driven programing course for all.pdf
PDF
Object oriented-programming-in-c-sharp
PPT
Intro. to prog. c++
PPT
Administrative
PDF
Expection Setting-1st ppt-Reshma.pdfjjkk
Compiler Design - Introduction to Compiler
Expection Setting - 1st ppt. pptx
data Integrative Programming untuk pemula.pdf
event driven programing course for all.pdf
Object oriented-programming-in-c-sharp
Intro. to prog. c++
Administrative
Expection Setting-1st ppt-Reshma.pdfjjkk

Similar to PRORAMMING LANGUAGES AND COMPILERS (CS 421) (20)

PPT
week1.ppt
PDF
Module 201 2 20 just 20 basic
PDF
Module 1 2 just basic-
PPTX
Introduction to Programming
DOCX
(D 15 180770107240)
PDF
concepts-in-programming-languages-2kuots4121.pdf
PPT
Lecture01 0089
PDF
The I in PRIMM - Code Comprehension and Questioning
PDF
Chapter- 1 Introduction to Object-Oriented Programming.pdf
PPTX
01 computer programming
PPT
Lecture1.ppt
PPT
Programming of c++
PPT
Lecture#1-Fundamental bt nch xhhs (1).ppt
PDF
iFL: An Interactive Environment for Understanding Feature Implementations
PPTX
CSE-1203-Lecture-01-Introduction for c programming.pptx
PDF
Feedback on Part 1 of the CSLP
PDF
A Programming Course Including C And Matlab For Mechanical Engineering Students
DOCX
Lab Report sample of c programming.docx
week1.ppt
Module 201 2 20 just 20 basic
Module 1 2 just basic-
Introduction to Programming
(D 15 180770107240)
concepts-in-programming-languages-2kuots4121.pdf
Lecture01 0089
The I in PRIMM - Code Comprehension and Questioning
Chapter- 1 Introduction to Object-Oriented Programming.pdf
01 computer programming
Lecture1.ppt
Programming of c++
Lecture#1-Fundamental bt nch xhhs (1).ppt
iFL: An Interactive Environment for Understanding Feature Implementations
CSE-1203-Lecture-01-Introduction for c programming.pptx
Feedback on Part 1 of the CSLP
A Programming Course Including C And Matlab For Mechanical Engineering Students
Lab Report sample of c programming.docx
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Spectroscopy.pptx food analysis technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
August Patch Tuesday
PDF
Approach and Philosophy of On baking technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Weekly Chronicles - August'25-Week II
Spectroscopy.pptx food analysis technology
Assigned Numbers - 2025 - Bluetooth® Document
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
August Patch Tuesday
Approach and Philosophy of On baking technology
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Heart disease approach using modified random forest and particle swarm optimi...
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Machine Learning_overview_presentation.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Ad

PRORAMMING LANGUAGES AND COMPILERS (CS 421)

  • 1. 09/19/24 1 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
  • 2. 09/19/24 2 Contact Information - Madhu  Office: 3112 SC  Office hours: TBD  Email: madhu@illinois.edu
  • 3. 3 Teaching Assistants  Andre Gou  John Lee  Liyi Li  Qianyang Peng  Nicholas Walker  Office hours in 0207 SC For office hour times, see website
  • 4. 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. 4
  • 5. 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. 5
  • 6. 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.
  • 7. 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)
  • 8. 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 formal semantics 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.
  • 9. 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 literal Eg: 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??!!
  • 10. Programming Languages & Compilers 09/19/24 10 I New Programming Paradigm III Language Semantics Three Main Topics of the Course
  • 11. Programming Languages & Compilers 09/19/24 11 I New Programming Paradigm II Language Translation III Language Semantics
  • 12. Programming Languages & Compilers 09/19/24 12 Functional Programming Environments and Closures Continuation Passing Style Patterns of Recursion I : New Programming Paradigm
  • 13. Programming Languages & Compilers 09/19/24 13 Functional Programming Environments and Closures Continuation Passing Style Patterns of Recursion
  • 14. Programming Languages & Compilers 09/19/24 14 Lexing and Parsing Type Systems Interpretation II : Language Translation
  • 15. Programming Languages & Compilers 09/19/24 15 Lexing and Parsing Type Systems Interpretation
  • 16. Programming Languages & Compilers 09/19/24 16 Operational Semantics Lambda Calculus Axiomatic Semantics III : Language Semantics
  • 17. Programming Languages & Compilers 09/19/24 17 Operational Semantics Lambda Calculus Axiomatic Semantics CS422 CS426 CS477
  • 18. Some Course References  No required textbook  Some suggested references 09/19/24 18
  • 20. 09/19/24 20 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
  • 21. 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.
  • 22. 09/19/24 23 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
  • 23. 24 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
  • 24. 0 9 / 1 9 / 2 4 2 5 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
  • 25. 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
  • 26. 09/19/24 27 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
  • 27. 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)
  • 28. 29 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
  • 29. 30 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
  • 30. 09/19/24 33 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
  • 31. 09/19/24 34 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
  • 32. 09/19/24 35 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
  • 33. 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 09/19/24 36
  • 34. 09/19/24 37 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
  • 35. 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 09/19/24 38
  • 36. 09/19/24 39 Environments  Environments record 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
  • 37. Environments 09/19/24 40 X  3 y  17 name  “Steve” b  true region  (5.4, 3.7) id  {Name = “Paul”, Age = 23, SSN = 999888777} . . .
  • 38. 09/19/24 41 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}
  • 39. Environments 09/19/24 42 b  5 test  true a  1
  • 40. New Bindings Hide Old // 2 = {b  5, a  1, test  false} let test = 3.7;;  What is the environment after this declaration? 09/19/24 43
  • 41. 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} 09/19/24 44
  • 42. Environments 09/19/24 45 b  5 test  3.7 a  1
  • 43. Now it’s your turn You should be able to do WA1 Problem 1 , parts (* 1 *) and (* 2 *) 09/19/24 46
  • 44. 09/19/24 47 Local Variable Creation // 3 = {test  3.7, a  1, b  5} # let b = 5 * 4 // 4 = {b  20, test  3.7, a  1} in 2 * b;; - : int = 40 // 5 = 3= {test  3.7, a  1, b  5} # b;; - : int = 5 b  5 test  3.7 a  1 b  5 test  3.7 a  1b  20 b  5 test  3.7 a  1
  • 45. // 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 09/19/24 48 Local let binding b  5 test  3.7 a  1
  • 46. // 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 b  5 test  3.7 a  1 09/19/24 49 Local let binding b  5 test  3.7 a  1 b  2
  • 47. // 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 b  5 test  3.7 a  1 09/19/24 50 Local let binding b  5 test  3.7 a  1 b  2 b  5 test  3.7 a  1 c  4
  • 48. 09/19/24 59 Functions # let plus_two n = n + 2;; val plus_two : int -> int = <fun> # plus_two 17;; - : int = 19
  • 49. 09/19/24 60 Functions let plus_two n = n + 2;; plus_two 17;; - : int = 19
  • 50. 09/19/24 61 Nameless Functions (aka Lambda Terms) fun n -> n + 2;; (fun n -> n + 2) 17;; - : int = 19
  • 51. 09/19/24 62 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
  • 52. 09/19/24 63 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)
  • 53. 09/19/24 64 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;; What is the result? X  12 …
  • 54. 09/19/24 65 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
  • 55. 09/19/24 66 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?
  • 56. 09/19/24 67 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? X  12 … X  7 …
  • 57. 09/19/24 68 Values fixed at declaration time # let x = 7;; (* New declaration, not an update *) val x : int = 7 # plus_x 3;; - : int = 15
  • 58. 09/19/24 69 Question  Observation: Functions are first-class values in this language  Question: What value does the environment record for a function variable?  Answer: a closure
  • 59. 09/19/24 70 Save the Environment!  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  < (v1,…,vn)  exp, f >  Where f is the environment in effect when f is defined (if f is a simple function)
  • 60. 09/19/24 71 Closure for plus_x  When plus_x was defined, had environment: plus_x = {…, x  12, …}  Recall: let plus_x y = y + x is really let plus_x = fun y -> y + x  Closure for fun y -> y + x: <y  y + x, plus_x >  Environment just after plus_x defined: {plus_x  <y  y + x, plus_x >} + plus_x
  • 61. Now it’s your turn You should be able to do WA1 Problem 1 , parts (* 7 *) and (* 8 *) 09/19/24 72

Editor's Notes

  • #10: Add roman numerals, make each a different color
  • #11: Add roman numerals, make each a different color