The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Similar documents
G Programming Languages - Fall 2012

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Variables. Substitution

CS558 Programming Languages

CS240: Programming in C

CSE 307: Principles of Programming Languages

CS558 Programming Languages

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.

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CHAPTER : 9 FLOW OF CONTROL

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CS558 Programming Languages

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Control Structures in Java if-else and switch

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

Programming Languages and Techniques (CIS120)

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

Overview. Elements of Programming Languages. Objects. Self-Reference

G Programming Languages - Fall 2012

Overview. Elements of Programming Languages. Objects. Self-Reference

Flow Control. CSC215 Lecture

CS558 Programming Languages

Scope and Parameter Passing 1 / 19

Chapter 7 Control I Expressions and Statements

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

cs173: Programming Languages Midterm Exam

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Introduction. C provides two styles of flow control:

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Recap: Functions as first-class values

Using Scala in CS241

Intro. Scheme Basics. scm> 5 5. scm>

Concepts of Programming Languages

CSE 341: Programming Languages

LECTURE 18. Control Flow

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

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

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Lecture 15 CIS 341: COMPILERS

Induction and Semantics in Dafny

Lecture 9: Control Flow

Lecture #11: Immutable and Mutable Data. Last modified: Sun Feb 19 17:03: CS61A: Lecture #11 1

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Variables and Bindings

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

CS 11 Ocaml track: lecture 3

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Lectures Basics in Procedural Programming: Machinery

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Programming with Math and Logic

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Repetition Structures

Introduction to Python (All the Basic Stuff)

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 8. Statement-Level Control Structures

CS4215 Programming Language Implementation. Martin Henz

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Overview. Elements of Programming Languages. Records. Named variants. Last time: Simple data structures: pairing (product types), choice (sum types)

CSE 341, Autumn 2015, Ruby Introduction Summary

Programming Languages

CSE341, Spring 2013, Final Examination June 13, 2013

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CSCE 314 Programming Languages. Type System

Begin at the beginning

Introduction to Object-Oriented Programming

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

The SPL Programming Language Reference Manual

References and Mutable Data Structures

FUNCTIONS. The Anatomy of a Function Definition. In its most basic form, a function definition looks like this: function square(x) { return x * x; }

Program Analysis: Lecture 02 Page 1 of 32

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

Functional Programming in Scala. Raj Sunderraman

Software Engineering using Formal Methods

Loop structures and booleans

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Programming Languages

Stop coding Pascal. Saturday, April 6, 13

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

CS558 Programming Languages

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

LESSON 6 FLOW OF CONTROL

Ruby: Introduction, Basics

Understanding Recursion

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Transcription:

The story so far Elements of Programming Languages Lecture 12: Imperative programming James Cheney University of Edinburgh November 4, 2016 So far we ve mostly considered pure computations. Once a variable is bound to a value, the value never changes. that is, variables are immutable. This is not how most programming languages treat variables! In most languages, we can assign new values to variables: that is, variables are mutable by default Just a few languages are completely pure (Haskell). Others strike a balance: e.g. Scala distinguishes immutable (val) variables and mutable (var) variables similarly const in Java, C Mutable vs. immutable While-programs Advantages of immutability: Referential transparency (substitution of equals for equals); programs easier to reason about and optimize Types tell us more about what a program can/cannot do Advantages of mutability: Some common data structures easier to implement Easier to translate to machine code (in a performance-preserving way) Seems closely tied to popular OOP model of objects with hidden state and public methods Today we ll consider programming with assignable variables and loops (L While ) and then discuss procedures and other forms of control flow Let s start with a simple example: L While, with statements Stmt s ::= skip s 1 ; s 2 x := e skip does nothing if e then s 1 else s 2 while e do s s 1 ; s 2 does s 1, then s 2 x := e evaluates e and assigns the value to x if e then s 1 else s 2 evaluates e, and evaluates s 1 or s 2 based on the result. while e do s tests e. If true, evaluate s and loop; otherwise stop. We typically use { to parenthesize statements.

A simple example: factorial again An interpreter for L While In Scala, mutable variables can be defined with var var n = var x = 1 while(n > 0) { x = n * x n = n-1 In L While, all variables are mutable x := 1; while (n > 0) do {x := n x; n := n 1 We will define a pure interpreter: def exec(env: Env[Value], s: Stmt): Env[Value] = s match { case Skip => env case Seq(s1,s2) => val env1 = exec(env, s1) exec(env1,s2) case IfThenElseS(e,s1,s2) => eval(env,e) match { case BoolV(true) => exec(env,s1) case BoolV(false) => exec(env,s2) An interpreter for L While While-programs: evaluation def exec(env: Env[Value], s: Stmt): Env[Value] = s match { case WhileDo(e,s) => eval(env, e) match { case BoolV(true) => val env1 = exec(env,s) exec(env1, WhileDo(e,s)) case BoolV(false) => env case Assign(x,e) => val v = eval(env,e) env + (x -> v) σ, s σ σ, skip σ σ, s 1 σ σ, s 2 σ σ, s 1 ; s 2 σ σ, e true σ, s 1 σ σ, e) false σ, s 2 σ σ, if e then s 1 else s 2 σ σ, if e then s 1 else s 2 σ σ, e true σ, s σ σ, while e do s σ σ, while e do s σ σ, e false σ, while e do s σ σ, e v σ, x := e σ[x := v] Here, we use evaluation in context σ, e v.

Examples Other control flow constructs σ 2, 2 x 4 x := y + 1; z := 2 x σ 1, y + 1 2 σ 1, x := y + 1 σ 2 σ 2, z := 2 x σ 3 σ 1, x := y + 1; z := 2 x σ 3 where σ 1 = [y := 1] σ 2 = [x := 2, y := 1] σ 3 = [x := 2, y := 1, z := 4] We ve taken if (with both then and else branches) and while to be primitive We can define some other operations in terms of these: if e then s if e then s else skip do s while e s; while e do s for (i n... m) do s i := n; as seen in C, Java, etc. while i m do { s; i = i + 1 Procedures L While is not a realistic language. Among other things, it lacks procedures Example (C/Java): int fact(int n) { int x = 1; while(n > 0) { x = x*n; n = n-1; return x; Procedures can be added to L While (much like functions in L Rec ) Rather than do this, we ll show how to combine L While with L Rec later. Structured vs. unstructured programming [Non-examinable] All of the languages we ve seen so far are structured meaning, control flow is managed using if, while, procedures, functions, etc. However, low-level machine code doesn t have any of these. A machine-code program is just a sequence of instructions in memory The only control flow is branching: unconditionally go to instruction at address n if some condition holds, go to instruction at address n Similarly, goto statements were the main form of control flow in many early languages

GO TO Considered Harmful [Non-examinable] goto in C [Non-examinable] In a famous letter (CACM 1968), Dijkstra listed many disadvantages of goto and related constructs It allows you to write spaghetti code, where control flow is very difficult to decipher For efficiency/historical reasons, many languages include such unstructured features: goto jump to a specific program location switch statements break and continue in loops It s important to know about these features, their pitfalls and their safe uses. The C (and C++) language includes goto In C, goto L jumps to the statement labeled L A typical (relatively sane) use of goto do some stuff if (error) goto error; do some more stuff if (error2) goto error; do some more stuff error:.. handle the error We ll see other, better-structured ways to do this using exceptions. goto in C: pitfalls [Non-examinable] goto: caveats [Non-examinable] The scope of the goto L statement and the target L might be different for that matter, they might not even be in the same procedure! For example, what does this do: goto L; if(1) { int k = fact(3); L: printf("%d",k); Answer: k will be some random value! goto can be used safely in C, but is best avoided unless you have a really good reason e.g. very high performance/systems code Safe use: within same procedure/scope Or: to jump out of a nested loop

goto fail [Non-examinable] What s wrong with this picture? if (error test 1) if (error test 2) if (error test 3) fail: handle error (In C, braces on if are optional; if they re left out, only the first goto fail statement is conditional!) This led to an Apple SSL security vulnerability in 2014 (see https://gotofail.com/) switch statements: gotchas [Non-examinable] switch statements [Non-examinable] We ve seen case or match constructs in Scala The switch statement in C, Java, etc. is similar: switch (month) { case 1: print("january"); break; case 2: print("february"); break; default: print("unknown month"); break; However, typically the argument must be a base type like int Break and continue [Non-examinable] See the break; statement? It s an important part of the control flow! it says now jump out the end of the switch statement month = 1; switch (month) { case 1: print("january"); case 2: print("february"); default: print("unknown month"); // prints all months! The break and continue statements are also allowed in loops in C/Java family languages. for(i = 0; i < 10; i++) { if (i % 2 == 0) continue; if (i == 7) break; print(i); Continue says Skip the rest of this iteration of the loop. Break says Jump to the next statement after this loop Can you think of a good reason why you would want to leave out the break?

Break and continue [Non-examinable] Labeled break and continue [Non-examinable] The break and continue statements are also allowed in loops in C/Java family languages. for(i = 0; i < 10; i++) { if (i % 2 == 0) continue; if (i == 7) break; print(i); Continue says Skip the rest of this iteration of the loop. Break says Jump to the next statement after this loop This will print 135 and then exit the loop. In Java, break and continue can use labels. OUTER: for(i = 0; i < 10; i++) { INNER: for(j = 0; j < 10; j++) { if (j > i) continue INNER; if (i == 4) break OUTER; print(j); This will print 001012 and then exit the loop. Labeled break and continue [Non-examinable] Summary In Java, break and continue can use labels. OUTER: for(i = 0; i < 10; i++) { INNER: for(j = 0; j < 10; j++) { if (j > i) continue INNER; if (i == 4) break OUTER; print(j); This will print 001012 and then exit the loop. (Labeled) break and continue accommodate some of the safe uses of goto without as many sharp edges Many real-world programming languages have: 1 mutable state 2 structured control flow (if/then, while, exceptions) 3 procedures We ve showed how to model and interpret L While, a simple imperative language and discussed a variety of (unstructured) control flow structures, such as goto, switch and break/continue. Next time: Small-step semantics and type soundness