Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 4.1 Chapter 4.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Functions Prototypes, parameter passing, return values, activation frams.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
The Reflective Analysis. Swap Screenplays 1.Read your partner’s screenplay 2.Do you like it? Why/not? 3.Did you understand it? Why/not? 4.What did you.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
The for Statement A most versatile loop
EGR 2261 Unit 10 Two-dimensional Arrays
Expressions and Assignment Statements
Copyright © 2013 Elsevier Inc. All rights reserved.
Lecture 3 of Computer Science II
Chap. 6 :: Control Flow Michael L. Scott.
Application of Pharmacology in Nursing Practice
Conversions of the type of the value of an expression
Functions Chapter 4 Instructor: Bindra Shrestha
Chap. 6 :: Control Flow Michael L. Scott.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Copyright © 2016 Elsevier Inc. All rights reserved.
Copyright © 2012, Elsevier Inc. All rights Reserved.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Copyright © 2013 Elsevier Inc. All rights reserved.
Copyright © 2012, Elsevier Inc. All rights Reserved.
The University of Adelaide, School of Computer Science
Predefined Functions Revisited
Copyright © Cengage Learning. All rights reserved.
Copyright © 2012, Elsevier Inc. All rights Reserved.
Copyright © 2013 Elsevier Inc. All rights reserved.
The University of Adelaide, School of Computer Science
Factoring Questions that Appear Difficult, but are Actually Slightly Easier If one factor of x2 + 3x – 18 is x + 6, what is the other factor?
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Copyright © 2013 Elsevier Inc. All rights reserved.
Modeling Functionality with Use Cases
Copyright © 2012, Elsevier Inc. All rights Reserved.
Chapter 15 Functional Programming 6/1/2019.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Copyright © 2012, Elsevier Inc. All rights Reserved.
Chapter 103 Long-Term Care: The Global Impact
Copyright © 2013 Elsevier Inc. All rights reserved.
Copyright © 2012, Elsevier Inc. All rights Reserved.
Chapter 15 Contraception
Copyright © 2013 Elsevier Inc. All rights reserved.
Presentation transcript:

Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott

Dictionary Moment In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequences of the use of a drug

Expressions precedence and associativity Side Effect: influences subsequent computation in any way other than by returning a value operator=(target, value) Since target gets changed (and is a parameter), changing the target is considered a side effect of assignment. (I know that seems weird) Why do we care about side effects (given that they are not a surprise)?

Copyright © 2009 Elsevier Expression Evaluation Several languages outlaw side effects for functions –easier to prove things about programs –closer to Mathematical intuition –easier to optimize –(often) easier to understand But side effects can be nice –consider rand() - changes the seed

Copyright © 2009 Elsevier Expression Evaluation Side effects are a particular problem if they affect state used in other parts of the expression in which a function call appears –It's nice not to specify an order, because it makes it easier to optimize int a = 10; int b = 7; cout << doit (a+b,a++) << " " << a << " " << b; –Fortran says it's OK to have side effects they aren't allowed to change other parts of the expression containing the function call Compilers can't check this completely, and most don't at all

6 Expressions In their purest form, expressions do not involve control issues: subexpressions can be evaluated in arbitrary order, and the order does not affect the result. Functional programming tries to achieve this goal for whole programs. If expressions (with side effects) could have arbitrary evaluation order, programs would become non- deterministic Common subexpression elimination becomes challenging. x+y could have changed even though I don’t see changes to x or y

7 Strictness An evaluation order for expressions is strict (eager) if all subexpressions of an expression are evaluated, whether or not they are needed to determine the value of the result, non-strict (lazy) otherwise. Arithmetic is almost always strict. Every language has at least a few non-strict expressions (?:, &&, ||). A form of strict evaluation called applicative-order is common: "bottom-up" or "inside-out". Expressions are evaluated in the order they are encountered in evaluating the expression tree.

Applicative Order – as needed Consider: (a+b)/(c+d)+((e-f)-g)/(h+j) – which operation is evaluated first? Still leaves open the question: whether left-to-right or not. 8

Chapter 7Louden, Programming Languages 9 Short Circuit Evaluation C, C++, and Java: use short-circuit evaluation for the usual Boolean operators ( && and || ), but also provide bitwise Boolean operators that are not short circuit ( & and | ) Ada: programmer can specify either (short-circuit is specified with and then and or else) Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

Copyright © 2009 Elsevier Control Flow Basic paradigms for specifying ordering –Sequencing –Selection –Iteration –Procedural Calls –Recursion –Concurrency –Exception Handling –Nondeterminacy

Copyright © 2009 Elsevier Expression Evaluation Infix, prefix operators Precedence, associativity –C has 15 levels - too many to remember –Pascal has 3 levels - too few for good semantics –Fortran has 8 –Ada has 6 Ada puts and & or at same level –Lesson: when unsure, use parentheses!

Copyright © 2009 Elsevier Expression Evaluation Figure 6.1 Operator precedence levels in Fortran, Pascal, C, and Ada. The operator s at the top of the figure group most tightly.

In C, what is x= -3 -4

Referential transparency: a property of pure functional languages -whereby an expression can be replaced by its value without affecting the program.Referential transparency Example. If rand() yields 5436, can you replace the call to rand with 5436? In other words: “A referentially transparent function is one that, given the same parameter(s), always returns the same result.“ It does not depend on its referencing environment.

Copyright © 2009 Elsevier Expression Evaluation Ordering of operand evaluation Inability to represent numbers exactly causes problems in application of identities –commutativity (assumed to be safe) –associativity (known to be dangerous) int a = INT_MAX; int b = INT_MIN; int c = 10; cout << a << " " << c << " " << a+c << endl;

Associativity difference for (int i=0; i < ; i++) w-= epsilon; cout << "w " << w << endl; e+009 cout << "w " << w *epsilon<< endl; e+009

Copyright © 2009 Elsevier Expression Evaluation Short-circuiting –Consider (a < b) && (b < c) : If a >= b there is no point evaluating whether b < c because (a < b) && (b < c) is automatically false –Sometimes the difference can be important… if (b != 0 && a/b == c)... if (*p && p->foo)... if (f || expensive())...

Copyright © 2009 Elsevier *Expression Evaluation Variables as values vs. variables as references –value-oriented languages C, Pascal, Ada –reference-oriented languages most functional languages (Lisp, Scheme, ML) Clu, Smalltalk –Java in-between built-in types are values user-defined types are objects - references

Copyright © 2009 Elsevier *Expression Evaluation Orthogonality –Features that can be used in any combination Meaning is consistent if (if b != 0 then a/b == c else false) then... if (if f then true else messy()) then …

Copyright © 2009 Elsevier Selection –sequential if statements if... then... else if... then... elsif... else (Easier to read as limited form of nesting) (cond (C1) (E1) (C2) (E2)... (Cn) (En) (T) (Et) (True – default case) ) Selection

Copyright © 2009 Elsevier Selection –Fortran computed gotos – GO TO (label-list)[,] expr –jump code for selection and logically-controlled loops Selection

Copyright © 2009 Elsevier At the assembly language level, jump is especially useful in the presence of short- circuiting Example (section of book): if then then_clause else else_clause Selection

Copyright © 2009 Elsevier Code generated without short-circuiting (Pascal) r1 := A-- load r2 := B ((A > B) and (C > D)) or (E <> F) r1 := r1 > r2 r2 := C r3 := D r2 := r2 > r3 // Reuse of registers -confusing to read r1 := r1 & r2 r2 := E r3 := F r2 := r2 $<>$ r3 r1 := r1 $|$ r2 if r1 = 0 goto L2 L1: code for then_clause-- label not actually used goto L3 L2: code for else_clause L3: Selection

Copyright © 2009 Elsevier Code generated with short-circuiting (C) r1 := A (A > B) and (C > D)) or (E <> F) r2 := B if r1 r2 goto L1 L4: r1 := E r2 := F if r1 = r2 goto L2 L1: code for then_clause goto L3 L2: code for else_clause L3: Selection

Order of Evaluation int doit(int b) { return b;} q = 2; m = 3*(q++)+ 2*(q++) + (q++); cout << " m2 = " << m<< endl; q=2; m = 3*(q++)+ 2*doit(q++) + doit(q++); cout << " m3 = " << m<< endl; What do you think is produced?

Order of Evaluation int doit(int b) { return b;} q = 2; m = 3*(q++)+ 2*(q++) + (q++); cout << " m2 = " << m<< endl; q=2; m = 3*(q++)+ 2*doit(q++) + doit(q++); cout << " m3 = " << m<< endl; “Equivalent” code is not equivalent Resultant answers depend on compiler (visual studio) (gcc)

sequence point A sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete and no side effects from subsequent evaluations have yet been performed. The sequence points listed in the C standard are: –at the end of the evaluation of a full expression –at the ||, &&, ?:, and comma operators; and –at a function call (after the evaluation of all the arguments, and just before the actual call). In the expression f()+g(), it is possible that either f() or g() will be executed first. In the code f(),g() the order of evaluation is defined

Example 1 int x = 4; x = 2 + x++ * 3; What should x be? Example 2 xxx = 5; cout << xxx++ << xxx++ << xxx++ << endl; What should be output?

Example 1 int x = 4; x = 2 + x++ * 3; x = 15 in Visual studio x = 14 on HPUX (unknown compiler and version) Example 2 xxx = 5; cout << xxx++ << xxx++ << xxx++ << endl; 765 in Visual Studio

Example 3 int xxx = 5; bool res = xxx++ >= 5 && (xxx++ >= 6); if (res) cout << "TRUE"; What should be output?

Example 3 int xxx = 5; bool res = xxx++ >= 5 && (xxx++ >= 6); if (res) cout << "TRUE"; TRUE with visual studio

Take away Within statements, there is an order of evaluation which (a) may not be clear and (b) may differ between compilers

Copyright © 2009 Elsevier Enumeration-controlled –C++ for loops scope of control variable allow changes to bounds within loop? allow other changes to loop variable within loop? value of control variable after the loop? int final = 3; for ( i =0; i <= final*2; i++) { cout << i++ << " " ; final = 4; } cout “)” ; Outputs (10) in C++ Iteration

Copyright © 2009 Elsevier Recursion –equally powerful to iteration –mechanical transformations back and forth –often more intuitive (sometimes less) –naïve implementation less efficient updating call stack is time/memory consuming no special syntax required fundamental to functional languages like Haskell

Copyright © 2009 Elsevier Recursion Tail recursion No computation follows recursive call int gcd (int a, int b) { if (a == b) return a; else if (a > b) return gcd (a - b,b); else return gcd (a, b – a); } Tail calls are significant because they can be implemented without adding a new activation record to the call stack. Most of the frame of the current AR is not needed any more, and can be replaced by the frame of the tail call, modified as appropriate.

36 Switch Statements Rules cases can be constants, constant expressions, constant ranges no overlapping cases error if unspecified case occurs (or ignore) Usually implemented via jump table: vector of unconditional jumps is stored sequentially in memory Need one location in jump table for every value between range of possible values

37 Switch Statements Implementation Options –Series of conditionals Good if few cases Slow if many –Jump Table Lookup branch target Avoids conditionals Possible when cases are small integer constants –Flexible Picks one based on case structure typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; }

38 Jump Table Structure Code Block 0 Go to N Targ0: Code Block 1 Go to N Targ1: Code Block 2 Go to N Targ2: Code Block n–1 Go to N Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: if op > upperbound|op < lowerbound go to N target = JTab[op]; goto target; N: switch(op) { case 0: Block 0 case 1: Block 1 case n-1: Block n–1 } Switch Form Code Generated Jump Table Jump Targets

39 Jump Table Structure Could be a space hog Code Block 0 Go to N Targ0: // no content Go to N Targ1: // no content Go to N Targ2: Code Block 1000 Go to N Targ1000: Targ0 Targ1 Targ2 Targn-1 jtab: if op > upperbound|op < lowerbound go to N target = jtab[op]; goto target; N: switch(op) { case 0: Block 0 case 1000: Block 1 case n-1: Block n–1 } Switch Form Code Generated Jump Table Jump Targets

40 Jump Table Space Costs jump tables best for large number of case labels (  8) may take a large amount of space if the labels are not well-clustered. A jump table with max. and min. case labels c max and c min needs  c max – c min entries. This can be wasteful if the entries aren’t “dense enough”, e.g.: switch (x) { case 1: … case 1000: … case : … } Define the density of a set of case labels as density = number of case labels/(c max – c min ) Compilers will not generate a jump table if density below some threshold (typically, 0.5).

41 Use of Switch Statements if number of case labels is small (  ~ 8), use linear or binary search on table of case values/jump pairs. –use number of case labels to decide between the two. if density  threshold (~ 0.5) : generate a jump table; else : divide the set of case labels into sub-ranges such that each sub- range has density  threshold; generate code to use binary search to choose amongst the sub- ranges;

42 Guarded Commands Selection: if -> [] ->... [] -> fi Semantics: when this construct is reached, –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically. (In Haskell, first true expression is used.) –If none are true, it is a runtime error

43 Guarded Commands Idea: if the order of evaluation is not important, the program should not specify one In Haskell, first one that matches is used.

44 Summary Every language has three major program components: expressions, statements, and declarations. Expressions are executed for their values (but may have side effects), and may or may not be sequenced. Statements are executed solely for their side effects, and they must be sequenced. Declarations define names; they can also give values to those names. They may or may not be viewed by a language as executable.