Principles of Programming Languages Lecture 10 Variables and Assignment 1
Variable Variable (Math) (CS) Math π CS pi symbol 3.1415828 R definite singular description 3.14 referent symbol R indefinite singular description referent e.g. Prolog & FP (IFP) e.g. Pascal & Algol 68 2
Constants Algol 68: pi = 3.1416; C: (not quite) #define PI 3.1416 Pascal: const pi = 3.1416; Ada: pi: constant FLOAT := 3.1416; pi 3.1416 3.1416 3.1416 same logical status as a literal No reference available No object eists pi replaced by literal No stack (or any other) space allocated (stored in instruction) Pure definition 3
Variables Algol 68: begin,y; C: { float,y; Pascal: var,y: ; Ada:,y: FLOAT; y alloc on stack := 2.0; y := 3.0; 3.0 2.0 4
Assignment (after y gets 3) Algol 68: := y; C: = y; Bliss:.y y 3.0 3.0 5
Variables: Bliss v. C Bliss Eplicit dereferencing Contet dereferencing C (lvalue). =.y +.z a +.j.(a +.i) const (rvalue) = y + z a[j] = a[i] lvalue rvalue Synonym: *(a+j) = *(a+i) { Only lvalue is actually computed 6
Algol 68 aside: identity declaration := c NOT same as = c Identity declaration = 2.718; means 2.718 Initialized variable := 2.718; means 2.718 Why? Implicit local (stack) allocation 7
identity declaration (cont d) ; abbreviates loc ; which abbreviates the following identity declaration: = loc ; Local generator Yields a (lvalue) Ascribed to identifier 8
Initialized Variables Algol 68: begin p := 3.1416; C: { float p = 3.1416; Pascal: var p: ; begin p := 3.1416; Ada: p: FLOAT := 3.1416; p What is ``variable about a variable?! Not reference (location, lvalue) Ref cannot be changed; ``ascribed to or ``possessed by ident! Not identifier! Not type (in most languages)! The value (rvalue) 3.1416 stack 9
Algol 68 identity declaration: unifies variable m & constant declaration 1. loc m := 3; 2. = m; 3 m 3 3 3. = m; m 3 10
Algol 68 identity declaration (cont d) 4. := m; loc :=m; = loc := m; 3 3 5. t = *m; 9 t const. In new environment *m evaluated when declaration encountered 11
Pointers Refs to variable refs Algol 68: p ; C: float *p ; Pascal: var p: ^; Ada: type PTR is access FLOAT; p : PTR; (PTR=``access type to ``base type ) p ref 12
Algol 68 pointer declaration p ref p = loc ; ref stack 13
Pointers (cont d) Algol 68: p := ; C: p = & ; Bliss: p p ref stack 2.0 NOT Pascal, Ada picture. Closest is:! Pascal: new(p); p^ := ; Ada: p := new FLOAT(); ``anonymous variable 14
Pointers in Pascal/Ada: point into heap Defeats ``dangling pointers at block eit Pointers cannot point into the stack var : ; var p, py: ^; p py stack heap 2.0 15
Pascal/Ada pointers (cont d) new(p); p^ := ; p py stack heap 2.0 2.0 ``anonymous variable p^ Pascal/Ada pointers point only to ``unnamed data objects 16
Pascal/Ada pointers (cont d) := 4.0; new(py); py := p; 1. p py stack heap 2.0 4.0 17
Pascal/Ada pointers (cont d) := 4.0; py^ := p^; 2. p py stack heap 2.0 4.0 ``pointer value out of legal range 18
Pascal/Ada pointers (cont d) := 4.0; new(py); py^ := p^; 3. p py stack heap 2.0 2.0 4.0 19
Algol68 has a heap allocator ref p = heap ; h = heap ;! Abbreviated heap h; p stack h heap Pascal: lvalues of all var identifiers are in stack! lvalues of all anonymous variables are in the heap Algol68: no restriction! allocation orthogonal to declaration 20
Refs and Aliasing Algol68: py; py := p; p py stack p py 1101 1102 ``pointer aliasing 2.0 1100 1100 1100: 2.0 21
Refs and Aliasing (cont d) Algol 68: y = ; (identity declaration) y y no alloc! 1101 1101 2.0 1100: ``name aliasing 2.0 ``equivalencing (Fortran) 22
Bliss: untyped bit strings = local ; 2; local p; p p p p 0010 0010 ``ptr to stored in p picture after assignment. 0010 23
Semantics of Variable Uses: C v. Bliss On LHS = / On RHS C: = & Meaning of contet-sensitive Not ``referentially transparent Bliss:. Meaning of is ``referentially transparent. 24
Variable Uses: C v. Bliss (cont d) C: & not a unary operator! &(+y) meaningless! && meaningless (can t reverse a pointer)! &2 meaningless Bliss:. is a unary operator!.(+y) sensible!.. sensible!.2 sensible 2 = 0010.2 0010: 1101..2 1101: 1000 25
C * Bliss. Bliss: C: On RHS. lvalue of rvalue of * Bliss variable occurrences are ``referentially transparent Semantics of is independent of contet 26