Programming Languages and Compilers (CS 421)

Slides:



Advertisements
Similar presentations
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Advertisements

The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Imperative Programming
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
10/7/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
10/25/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CMSC 330: Organization of Programming Languages Operational Semantics.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Programming Languages and Compilers (CS 421)
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Programming Language Concepts (CIS 635)
Programming Languages and Compilers (CS 421)
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Programming Languages and Compilers (CS 421)
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Functional Programming
Programming Languages and Compilers (CS 421)
T. Jumana Abu Shmais – AOU - Riyadh
Programming Languages and Compilers (CS 421)
Madhusudan Parthasarathy
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/CS421A Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa L Gunter 12/6/2018

Interpreter Takes abstract syntax trees as input In simple cases could be just strings One procedure for each syntactic category (nonterminal) eg one for expressions, another for commands If Natural semantics used, tells how to compute final value from code If Transition semantics used, tells how to compute next “state” To get final value, put in a loop 12/6/2018

Natural Semantics Example compute_exp (Var(v), m) = look_up v m compute_exp (Int(n), _) = Num (n) … compute_com(IfExp(b,c1,c2),m) = if compute_exp (b,m) = Bool(true) then compute_com (c1,m) else compute_com (c2,m) 12/6/2018

Natural Semantics Example compute_com(While(b,c), m) = if compute_exp (b,m) = Bool(false) then m else compute_com (While(b,c), compute_com(c,m)) May fail to terminate - exceed stack limits Returns no useful information then 12/6/2018

(C, m) --> (C’, m’) or (C,m) --> m’ Transition Semantics Form of operational semantics Describes how each program construct transforms machine state by transitions Rules look like (C, m) --> (C’, m’) or (C,m) --> m’ C, C’ is code remaining to be executed m, m’ represent the state/store/memory/environment Partial mapping from identifiers to values Sometimes m (or C) not needed Indicates exactly one step of computation 12/6/2018

Expressions and Values C, C’ used for commands; E, E’ for expressions; U,V for values Special class of expressions designated as values Eg 2, 3 are values, but 2+3 is only an expression Memory only holds values Other possibilities exist 12/6/2018

Evaluation Semantics Transitions successfully stops when E/C is a value/memory Evaluation fails if no transition possible, but not at value/memory Value/memory is the final meaning of original expression/command (in the given state) Coarse semantics: final value / memory More fine grained: whole transition sequence 12/6/2018

Simple Imperative Programming Language I  Identifiers N  Numerals B ::= true | false | B & B | B or B | not B | E < E | E = E E::= N | I | E + E | E * E | E - E | - E C::= skip | C;C | I ::= E | if B then C else C fi | while B do C od 12/6/2018

Transitions for Expressions Numerals are values Boolean values = {true, false} Identifiers: (I,m) --> (m(I), m) 12/6/2018

Boolean Operations: Operators: (short-circuit) (false & B, m) --> (false,m) (B, m) --> (B”, m) (true & B, m) --> (B,m) (B & B’, m) --> (B” & B’, m) (true or B, m) --> (true,m) (B, m) --> (B”, m) (false or B, m) --> (B,m) (B or B’, m) --> (B” or B’,m) (not true, m) --> (false,m) (B, m) --> (B’, m) (not false, m) --> (true,m) (not B, m) --> (not B’, m) 12/6/2018

Relations (E, m) --> (E’’,m) (E ~ E’, m) --> (E’’~E’,m) (V ~ E, m) --> (V~E’,m) (U ~ V, m) --> (true,m) or (false,m) depending on whether U ~ V holds or not 12/6/2018

Arithmetic Expressions (E, m) --> (E’’,m) (E op E’, m) --> (E’’ op E’,m) (E, m) --> (E’,m) (V op E, m) --> (V op E’,m) (U op V, m) --> (N,m) where N is the specified value for U op V 12/6/2018

Commands - in English skip means done evaluating When evaluating an assignment, evaluate the expression first If the expression being assigned is already a value, update the memory with the new value for the identifier When evaluating a sequence, work on the first command in the sequence first If the first command evaluates to a new memory (ie completes), evaluate remainder with new memory 12/6/2018

Commands (skip, m) --> m (E,m) --> (E’,m) (I::=E,m) --> (I::=E’,m) (I::=V,m) --> m[I <-- V ] (C,m) --> (C”,m’ ) (C,m) --> m’ (C;C’, m) --> (C”;C’,m’) (C;C’, m) --> (C’,m’ ) 12/6/2018

If Then Else Command - in English If the boolean guard in an if_then_else is true, then evaluate the first branch If it is false, evaluate the second branch If the boolean guard is not a value, then start by evaluating it first. 12/6/2018

If Then Else Command (C, m) (if true then C else C’ fi, m) --> (if false then C else C’ fi, m) --> (B,m) --> (B’,m) (if B then C else C’ fi, m) --> (if B’ then C else C’ fi, m) (C’, m) 12/6/2018

While Command (while B do C od, m) --> (if B then C; while B do C od else skip fi, m) In English: Expand a While into a test of the boolean guard, with the true case being to do the body and then try the while loop again, and the false case being to stop. 12/6/2018

(if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, Example Evaluation First step: (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> ? 12/6/2018

Example Evaluation First step: (x > 5, {x -> 7}) --> ? (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> ? 12/6/2018

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> ? 12/6/2018

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> ? 12/6/2018

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> (if 7 > 5 then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) 12/6/2018

Example Evaluation Second Step: (7 > 5, {x -> 7}) --> (true, {x -> 7}) (if 7 > 5 then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) --> (if true then y:=2 + 3 else y:=3 + 4 fi, Third Step: (if true then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) -->(y:=2+3, {x->7}) 12/6/2018

Example Evaluation Fourth Step: (2+3, {x-> 7}) --> (5, {x -> 7}) (y:=2+3, {x->7}) --> (y:=5, {x->7}) Fifth Step: (y:=5, {x->7}) --> {y -> 5, x -> 7} 12/6/2018

Example Evaluation Bottom Line: (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) --> (if 7 > 5 then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) -->(if true then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) -->(y:=2+3, {x->7}) --> (y:=5, {x->7}) --> {y -> 5, x -> 7} 12/6/2018

Transition Semantics Evaluation A sequence of steps with trees of justification for each step (C1,m1) --> (C2,m2) --> (C3,m3) --> … --> m Let -->* be the transitive closure of --> Ie, the smallest transitive relation containing --> 12/6/2018

Adding Local Declarations Add to expressions: E ::= … | let I = E in E’ | fun I -> E | E E’ fun I -> E is a value Could handle local binding using state, but have assumption that evaluating expressions doesn’t alter the environment We will use substitution here instead Notation: E [E’ / I ] means replace all free occurrence of I by E’ in E 12/6/2018

Call-by-value (Eager Evaluation) (let I = V in E, m) --> (E[V/ I ],m) (E, m) --> (E’’,m) (let I = E in E’, m) --> (let I = E’’ in E’) ((fun I -> E) V, m) --> (E[V / I ],m) (E’, m) --> (E’’,m) ((fun I -> E) E’, m) --> ((fun I -> E) E’’, m) 12/6/2018

Call-by-name (Lazy Evaluation) (let I = E in E’, m) --> (E’ [E / I ],m) ((fun I -> E’ ) E, m) --> (E’ [E / I ],m) Question: Does it make a difference? It can depending on the language 12/6/2018

Church-Rosser Property Church-Rosser Property: If E-->* E1 and E-->* E2, if there exists a value V such that E1 -->* V, then E2 -->* V Also called confluence or diamond property Example: E= 2 + 3 + 4 E1 = 5 + 4 E2= 2 + 7 V =9 12/6/2018

Does It always Hold? No. Languages with side-effects tend not be Church-Rosser with the combination of call-by-name and call-by-value Alonzo Church and Barkley Rosser proved in 1936 the -calculus does have it Benefit of Church-Rosser: can check equality of terms by evaluating them (Given evaluation strategy might not terminate, though) 12/6/2018