Programming Languages and Compilers (CS 421)

Similar documents
n Takes abstract syntax trees as input n In simple cases could be just strings n One procedure for each syntactic category

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

Programming Languages and Compilers (CS 421)

Example. Programming Languages and Compilers (CS 421) Disambiguating a Grammar. Disambiguating a Grammar. Steps to Grammar Disambiguation

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Programming Languages and Compilers (CS 421)

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

From the λ-calculus to Functional Programming Drew McDermott Posted

Programming Languages and Compilers (CS 421)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Last class. CS Principles of Programming Languages. Introduction. Outline

Lecture 5: The Untyped λ-calculus

Programming Languages and Compilers (CS 421)

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

Programming Languages and Compilers (CS 421)

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Software Paradigms (Lesson 4) Functional Programming Paradigm

Operational Semantics. One-Slide Summary. Lecture Outline

Programming Languages Third Edition

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Lambda Calculus.

CMSC 330: Organization of Programming Languages

CS 242. Fundamentals. Reading: See last slide

Functional Programming

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Programming Languages Fall 2013

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

CMSC 330: Organization of Programming Languages. Operational Semantics

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Denotational semantics

Programming Languages and Compilers (CS 421)

Chapter 5: The Untyped Lambda Calculus

Fundamental Concepts. Chapter 1

Lambda Calculus. Variables and Functions. cs3723 1

CSc 520 Principles of Programming Languages

Lexicografie computationala Feb., 2012

Organization of Programming Languages CS3200/5200N. Lecture 11

Functions Over Lists. Programming Languages and Compilers (CS 421) Structural Recursion. Functions Over Lists. Structural Recursion : List Example

Pure Lambda Calculus. Lecture 17

Denotational Semantics. Domain Theory

Abstract register machines Lecture 23 Tuesday, April 19, 2016

Operational Semantics of Cool

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

UNIT 3

VU Semantik von Programmiersprachen

The Substitution Model

Programming Language Concepts, cs2104 Lecture 04 ( )

Handout 10: Imperative programs and the Lambda Calculus

Shared Variables and Interference

Formal Systems and their Applications

Chapter 11 :: Functional Languages

1 Lexical Considerations

Semantics with Applications 3. More on Operational Semantics

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

To figure this out we need a more precise understanding of how ML works

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 15 CIS 341: COMPILERS

CS558 Programming Languages

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Program Syntax; Operational Semantics

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

CS558 Programming Languages

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

To figure this out we need a more precise understanding of how ML works

Shared Variables and Interference

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Tool demonstration: Spin

1 Introduction. 3 Syntax

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Operational Semantics

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CS 11 Haskell track: lecture 1

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

1 Scope, Bound and Free Occurrences, Closed Terms

What does my program mean?

Transcription:

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 11/14/2017 1

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 11/14/2017 2

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) 11/14/2017 3

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 11/14/2017 4

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 11/14/2017 5

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 11/14/2017 6

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 11/14/2017 7

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 11/14/2017 8

Transitions for Expressions Numerals are values Boolean values = {true, false} Identifiers: (I,m) --> (m(i), m) 11/14/2017 9

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) 11/14/2017 10

Relations (E, m) --> (E,m) (E ~ E, m) --> (E ~E,m) (E, m) --> (E,m) (V ~ E, m) --> (V~E,m) (U ~ V, m) --> (true,m) or (false,m) depending on whether U ~ V holds or not 11/14/2017 11

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 11/14/2017 12

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 11/14/2017 13

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 ) 11/14/2017 14

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. 11/14/2017 15

If Then Else Command (if true then C else C fi, m) --> (if false then C else C fi, m) --> (C, m) (C, m) (B,m) --> (B,m) (if B then C else C fi, m) --> (if B then C else C fi, m) 11/14/2017 16

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. 11/14/2017 17

Example Evaluation First step: (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) -->? 11/14/2017 18

Example Evaluation First step: (x > 5, {x -> 7}) -->? (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) -->? 11/14/2017 19

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) -->? (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) -->? 11/14/2017 20

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) --> (7 > 5, {x -> 7}) (if x > 5 then y:= 2 + 3 else y:=3 + 4 fi, {x -> 7}) -->? 11/14/2017 21

Example Evaluation First step: (x,{x -> 7}) --> (7, {x -> 7}) (x > 5, {x -> 7}) --> (7 > 5, {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}) 11/14/2017 22

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, {x -> 7}) Third Step: (if true then y:=2 + 3 else y:=3 + 4 fi, {x -> 7}) -->(y:=2+3, {x->7}) 11/14/2017 23

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} 11/14/2017 24

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} 11/14/2017 25

Transition Semantics Evaluation A sequence of steps with trees of justification for each step (C 1,m 1 ) --> (C 2,m 2 ) --> (C 3,m 3 ) --> --> m Let -->* be the transitive closure of --> Ie, the smallest transitive relation containing --> 11/14/2017 26

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 11/14/2017 27

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) 11/14/2017 28

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 11/14/2017 29

Church-Rosser Property Church-Rosser Property: If E-->* E 1 and E-->* E 2, if there exists a value V such that E 1 -->* V, then E 2 -->* V Also called confluence or diamond property Example: E= 2 + 3 + 4 E 1 = 5 + 4 E 2 = 2 + 7 V =9 11/14/2017 30

Does It always Hold? No. Languages with side-effects tend not be Church- Rosser with the combination of call-by-name and callby-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) 11/14/2017 31