Principles of Programming Languages Lecture 10 Variables and

Principles of Programming Languages Lecture 10 Variables and Assignment C SC 520 Principles of Programming Languages 1

Variable ≠ Variable (Math) (CS) • Math • CS pi R 3. 1415828… symbol definite singular description 3. 14 real x x R referent symbol indefinite ref real singular referent description real … • e. g. Prolog & FP (IFP) C SC 520 Principles of Programming Languages • e. g. Pascal & Algol 68 2

Constants • • Algol 68: real pi = 3. 1416; C: (not quite) #define PI 3. 1416 Pascal: const pi = 3. 1416; Ada: pi: constant FLOAT : = 3. 1416; pi 3. 1416 real 3. 1416 • No same logical status as a literal 3. 1416 reference available • No ref real object exists • pi replaced by literal • No stack (or any other) space allocated (stored in instruction) • Pure definition C SC 520 Principles of Programming Languages 3

• • Algol 68: begin real x, y; C: { float x, y; Pascal: var x, y: real; Ada: x, y: FLOAT; x ref real y ref real alloc on stack Variables x : = 2. 0; y : = 3. 0; 3. 0 C SC 520 Principles of Programming Languages 2. 0 4

Assignment (after y gets 3) • Algol 68: • C: • Bliss: x : = y; x . y x y ref real 3. 0 C SC 520 Principles of Programming Languages 5

Variables: Bliss v. C Bliss • Explicit dereferencing x C • Context dereferencing x x (lvalue) . x x (rvalue) x = . y + . z x = y + z a +. j . (a +. i) a[j] = a[i] lvalue Synonym: *(a+j) = *(a+i) { const rvalue Only lvalue is actually computed C SC 520 Principles of Programming Languages 6

Algol 68 aside: identity declaration • real x : = c NOT same as real x = c • Identity declaration real x = 2. 718; means x 2. 718 • Initialized variable real x : = 2. 718; means x 2. 718 • Why? Implicit local (stack) allocation C SC 520 Principles of Programming Languages 7

identity declaration (cont’d) • real x; abbreviates loc real x; which abbreviates the following identity declaration: • ref real x = loc real ; x ref real Local generator Yields a ref real (lvalue) Ascribed to identifier x C SC 520 Principles of Programming Languages 8

Initialized Variables • • Algol 68: C: Pascal: Ada: begin real p : = 3. 1416; { float p = 3. 1416; var p: real; begin p : = 3. 1416; p: FLOAT : = 3. 1416; p ref real 3. 1416 • What is ``variable’’ about a variable? n Not reference (location, lvalue) u n n n stack real Ref cannot be changed; ``ascribed to’’ or ``possessed by’’ ident Not identifier Not type (in most languages) The value (rvalue) C SC 520 Principles of Programming Languages 9

Algol 68 identity declaration: unifies variable & m constant declaration 1. loc real m : = 3; m 2. real x = m; x 3 3. ref real x x = m; m 3 C SC 520 Principles of Programming Languages 10

Algol 68 identity declaration (cont’d) 4. real x : = m; loc real x: =m; ref real x = loc real : = m; ref real 3 5. real t = x*m; 9 real 3 • t const. In new environment • x*m evaluated when declaration encountered C SC 520 Principles of Programming Languages 11

Pointers Refs to variable refs Algol 68: ref real px ; C: float *px ; Pascal: var px: ^real; Ada: type PTR is access FLOAT; px : PTR; (PTR=``access type’’ to ``base type’’) px • • • ref real C SC 520 Principles of Programming Languages 12

Algol 68 pointer declaration ref real px = loc ref real; ref real stack ref real C SC 520 Principles of Programming Languages 13

Pointers (cont’d) • Algol 68: px : = x ; • C: px = &x ; • Bliss: px x ref real 2. 0 stack ref real • NOT Pascal, Ada picture. Closest is: n Pascal: new(px); px^ : = x; ``anonymous variable’’ C SC 520 Principles of Programming Languages Ada: px : = new FLOAT(x); 14

Pointers in Pascal/Ada: point into heap • Defeats ``dangling pointers’’ at block exit • Pointers cannot point into the stack • var x: real; var px, py: ^real; py x px stack heap 2. 0 C SC 520 Principles of Programming Languages 15

Pascal/Ada pointers (cont’d) px py x px^ : = x; • new(px); stack heap 2. 0 ``anonymous variable px^’’ Pascal/Ada pointers point only to ``unnamed’’ data objects C SC 520 Principles of Programming Languages 16

Pascal/Ada pointers (cont’d) px py x • x : = 4. 0; • new(py); py : = px; stack 1. heap 2. 0 4. 0 C SC 520 Principles of Programming Languages 17

Pascal/Ada pointers (cont’d) • x : = 4. 0; • py^ : = px^; px py x 2. stack heap 2. 0 4. 0 ``pointer value out of legal range’’ C SC 520 Principles of Programming Languages 18

Pascal/Ada pointers (cont’d) px py x • x : = 4. 0; • new(py); py^ : = px^; stack 3. heap 2. 0 4. 0 C SC 520 Principles of Programming Languages 19

Algol 68 has a heap allocator • ref real px = heap real; • ref real hx = heap real; n Abbreviated heap real hx; stack hx heap px • Pascal: lvalues of all var identifiers are in stack n • lvalues of all anonymous variables are in the heap Algol 68: no restriction n allocation orthogonal to declaration C SC 520 Principles of Programming Languages 20

Refs and Aliasing stack px py px • Algol 68: ref real py; py : = px; 1102 ``pointer aliasing’’ 1101 1100 2. 0 1100: C SC 520 Principles of Programming Languages 2. 0 21

Refs and Aliasing (cont’d) • Algol 68: x y ref real no alloc! y x real 1101 1100: (identity declaration) ref real y = x; 2. 0 C SC 520 Principles of Programming Languages 2. 0 ``name aliasing’’ ``equivalencing (Fortran)’’ 22

Bliss: untyped bit strings • local x; x 2; local px; = px x x px 0010 • px x ``ptr to x stored in px’’ 0010 px x picture after assignment. 0010 C SC 520 Principles of Programming Languages 23

Semantics of Variable Uses: C v. Bliss • On LHS x = / x C: = • On RHS x &x x Meaning of x context-sensitive Not ``referentially transparent’’ x x Bliss: x . x Meaning of x is ``referentially transparent’’ C SC 520 Principles of Programming Languages 24

Variable Uses: C v. Bliss (cont’d) • C: & not a unary operator n &(x+y) meaningless n &&x meaningless (can’t reverse a pointer) n &2 meaningless • Bliss: . is a unary operator . (x+y) sensible n. . x sensible n. 2 sensible n C SC 520 Principles of Programming Languages 2 = 0010. 2 0010: 1101 . . 2 1101: 1000 25

C * Bliss . x Bliss: C: • On RHS x lvalue of x . x rvalue of x x x *x Bliss variable occurrences are ``referentially transparent’’ Semantics of x is independent of context C SC 520 Principles of Programming Languages 26
- Slides: 26