Sep 15, 2003 ------------- - More on types - Type specification - static - determined by declarations or implicitly - i.e., FORTRAN) - usually goes with "more checks" and safety - dynamic - usually goes with flexibility - Type compatiblity - structural - not so easy to implement - can sometimes lead to counter-intuitive results (e.g., Celcius and Fahrenheit) - name - very easy to implement - declaration - Type inference - e.g., ML style fun cube(x) = x*x*x; fun cube(x) = (x:real)*x*x; - Types and storage - static variables - bound before execution begins - do not change - a language that only has this, cannot support recursion (e.g., old FORTRAN) - dynamic variables - e.g., LISP - What gets bound to a location? - not "const int max=10;" - why? - What gets assigned statically - global variables - Growth of environment - opposite directions (stack and heap) - note: heap is not the data structure - just a pile - Which variable is intended in a reference? - leads to scoping rules - Examples of static and dynamic scoping - Handout: Pascal example: ---------------- program ex; var x : integer; procedure p; var y : boolean; begin ... end; (* p *) procedure q; var z : real; begin ... end; (* q *) begin (* main of ex *) .... end. (* ex *) Algol60 example: ---------------- A: begin integer x; integer y; x := 2; y := false; B: begin integer a,b; if y then a := x else b := y; end; ... end; Modula-2 example 1: -------------------- MODULE Ex; PROCEDURE p; BEGIN x := 2; END p; VAR x: INTEGER; BEGIN (* Ex *) ... END Ex. Modula-2 example 2: -------------------- MODULE A; VAR x: INTEGER; PROCEDURE p; BEGIN ... END p; MODULE B; VAR y: REAL; PROCEDURE q; BEGIN ... END q; .... END B; .... END A. A Dynamic scoping example (hypothetical language): -------------------------------------------------- procedure Big is X: Integer; procedure Sub1 is begin .... X ..... end; procedure Sub2 is X : Integer begin end; begin .... end;