Values and Types Types of values Primitive composite

  • Slides: 23
Download presentation
Values and Types § Types of values. § Primitive, composite, recursive types. § Type

Values and Types § Types of values. § Primitive, composite, recursive types. § Type systems: static vs dynamic typing, type completeness. § Expressions • Functions • Conditional expressions • Arrays 2 -1 § Implementation notes.

What is the difference between expression and statement in programming? § A statement is

What is the difference between expression and statement in programming? § A statement is a complete line of code that performs some action § An expression is any section of the code that evaluates to a value § If you can print it, or assign it to a variable, it’s an expression. If you can’t, it’s a statement! § How about print in Python 2 vs Python 3? 2 -2

Statement vs Expressions in Statements: Python import os assert True pass del x try:

Statement vs Expressions in Statements: Python import os assert True pass del x try: raise Exception() except: pass with open(‘/dev/null’) as fd: pass Expressions Values are expressions print(10+2) print(‘a’) Function calls are expressions print(10)) List comprehensions are expressions Print([x*2 for x in range(2)]) 2 -3

Function calls § An operator may be thought of as denoting a function. §

Function calls § An operator may be thought of as denoting a function. § Applying a unary operator Å to its operand is essentially a function call with one argument: Å E is essentially equivalent to Å(E) § Applying a binary operator Ä to its operands is essentially a function call with two arguments: E 1 Ä E 2 is essentially equivalent to Ä(E 1, E 2) § Thus a conventional arithmetic expression is essentially equivalent to a composition of function calls: a * b + c / d is essentially equivalent to +(*(a, b), /(c, d)) => infix notation => prefix notation 2 -4

Conditional expressions chooses one of its subexpressions to evaluate, depending on a condition. §

Conditional expressions chooses one of its subexpressions to evaluate, depending on a condition. § An if-expression chooses from two subexpressions, using a boolean condition. § A case-expression chooses from several subexpressions 2 -5

Example: Java if-expressions § Java if-expression: x>y ? x : y § Conditional expressions

Example: Java if-expressions § Java if-expression: x>y ? x : y § Conditional expressions tend to be more elegant than conditional commands. Compare: int max 1 (int x, int y) { return (x>y ? x : y); } int max 2 (int x, int y) { if (x>y) return x; else return y; } 2 -6

Example: Haskell if- and case-expressions § Haskell if-expression: if x>y then x else y

Example: Haskell if- and case-expressions § Haskell if-expression: if x>y then x else y § Haskell case-expression: case m of feb -> if is. Leap y then 29 else 28 apr -> 30 jun -> 30 sep -> 30 nov -> 30 _ -> 31 2 -7

Iterative expressions § An iterative expression • performs a computation over a series of

Iterative expressions § An iterative expression • performs a computation over a series of values (typically the components of an array or list), yielding some result. § Iterative expressions are uncommon • but they are supported by Perl by grep 2 -8

Example: Perl iterative expressions via grep #!/usr/bin/perl use strict; use warnings; my %data =

Example: Perl iterative expressions via grep #!/usr/bin/perl use strict; use warnings; my %data = ("A" => 0, "B" => "yes", "C" => 0 ); my @arrkeys = grep { $data{$_} } keys %data; § keys %data returns a list of characters (“A”, ”B”, ”C”) § { $data{$_} } runs once for each character , and returns (0, ”yes”, 0) § grep returns only the values from the list on the right-hand side for which the expression in braces evaluates to a true value. 2 -9

Implementation Details: Representation of Cartesian products § Tuples, records, and structures are represented by

Implementation Details: Representation of Cartesian products § Tuples, records, and structures are represented by juxtaposing the components in a fixed order. § Example (C++): struct Date{ int y; Month m; int d; y 2000 m jan d 1 2004 dec 25 }; § Implementation of component selection: • Let r be a record or structure. • Each component r. f has a fixed offset (determined by the compiler) relative to the base address of r. 2 -10

Representation of arrays (1) § The values of an array type are represented by

Representation of arrays (1) § The values of an array type are represented by juxtaposing the components in ascending order of indices. § Example (C++): typedef float Vector[3]; 1 2 3 3. 0 4. 0 0. 0 1. 0 0. 5 2 -11

Representation of arrays (2) § Implementation of array indexing: • Let a be an

Representation of arrays (2) § Implementation of array indexing: • Let a be an array with index range {l, …, u}. • Assume that each component occupies s bytes (determined by the compiler). • Then a(i) has offset s(i–l) bytes relative to the base address of a. (In C and Java l = 0, so this simplifies to si bytes. ) • The offset computation must be done at run-time (since the value of i is not known until run-time). • A range check must also be done at run-time, to ensure that l i u. 2 -12

Representation of disjoint unions (1) § Each value of a disjoint-union type is represented

Representation of disjoint unions (1) § Each value of a disjoint-union type is represented by juxtaposing a tag with one of the possible variants. The type (and therefore representation) of the variant depends on the current value of the tag. § Example (Haskell): data Number = Exact Int | Inexact Float tag Exact variant 2 tag Inexact variant 3. 1416 2 -13

Representation of disjoint unions (2) § Example (C++): typedef enum { exact, inexact }

Representation of disjoint unions (2) § Example (C++): typedef enum { exact, inexact } Accuracy ; typedef union Number{ int ival; float rval; }Number; typedef struct Data. Node { Accuracy acc; Number rval; }Data. Node; acc exact ival 2 acc inexact rval 3. 1416 2 -14

Representation of objects (simplified) § Example (Java): class Point { private float x, y;

Representation of objects (simplified) § Example (Java): class Point { private float x, y; … // methods } class Circle extends Point { private float r; … // methods } class Rectangle extends Point { private float w, h; … // methods } Point tag Circle tag Rect. tag 1. 5 x 2. 0 y 3. 0 w 4. 0 h 0. 0 x 0. 0 y 5. 0 r 1. 0 x 2. 0 y 2 -15

Representation of disjoint unions (3) § Implementation of tag test and projection: • Let

Representation of disjoint unions (3) § Implementation of tag test and projection: • Let u be a disjoint-union value/object. • The tag of u has an offset of 0 relative to the base of u. • Each variant of u has a fixed offset (determined by the compiler) relative to the base of u. 2 -16

Variables and Storage ‘‘Once a programmer has understood the use of variables, he has

Variables and Storage ‘‘Once a programmer has understood the use of variables, he has understood the essence of programming. ’’ by Edsger Dijkstra 3 -17

Variables and Storage § A simple storage model. § Simple and composite variables. §

Variables and Storage § A simple storage model. § Simple and composite variables. § Copy semantics vs reference semantics. § Lifetime. § Pointers. § Commands. § Expressions with side effects. § Implementation notes. 3 -18

An abstract model of storage (1) § In functional and logic PLs, a “variable”

An abstract model of storage (1) § In functional and logic PLs, a “variable” stands for a fixed but unknown value. § In imperative and OO PLs, a variable is a container for a value, which may be inspected and updated as often as desired. § Such a variable can be used to model a real-world object whose state changes over time. 3 -19

An abstract model of storage (2) § To understand such variables, assume a simple

An abstract model of storage (2) § To understand such variables, assume a simple abstract model of storage: • A store is a collection of storage cells. Each storage cell has a unique address. • Each storage cell is either allocated or unallocated. • Each allocated storage cell contains either a simple value or undefined. unallocated cells 7 true 3. 14 allocated cells ? undefined ‘X’ 3 -20

Variables and Storage § A simple storage model. § Simple and composite variables. §

Variables and Storage § A simple storage model. § Simple and composite variables. § Copy semantics vs reference semantics. § Lifetime. § Pointers. 3 -21

Simple vs composite variables § A simple value is one that can be stored

Simple vs composite variables § A simple value is one that can be stored in a single storage cell (typically a primitive value or a pointer). § A simple variable occupies a single allocated storage cell. § A composite variable occupies a group of allocated storage cells. 3 -22

Simple variables § When a simple variable is declared, a storage cell is allocated

Simple variables § When a simple variable is declared, a storage cell is allocated for it. § Assignment to the simple variable updates that storage cell. § At the end of the code block, that storage cell is deallocated. § Animation (C++): If (1 == 1 ){ int n; n 10? n = 0; n = n+1; } 3 -23