Programming Languages Type Conversion Classifying Type Conversions According to the notation –Implicit Conversions (coercion) –Explicit Conversions (casting) According to the sizes of the types involved –Widening Conversions –Narrowing Conversions: may involve a loss of data C Example int x = 5; x = x / 2; int double promotion (widening) int double assignment conversion (narrowing)
Programming Languages Notes on Type Conversions Advantages of Explicit Conversions Type conversions are documented precisely. Make it easier for the translator to resolve overloading. double max(int, double); // max #1 double max(double, int); // max #2... max(2, 3); // calls what? max(2, (double)3); // calls max #1 Structure Casting The sizes of the structure types should be identical. The translation merely reinterprets the memory
Programming Languages 1.Pascal 구문으로 된 자료형과 변수 선언이 다음과 같이 주어졌다. type range = ; table1 = array[range] of char ; table2 = table1 ; var x, y : array[-5..5] of char ; z : table1 ; w: table2 ; i: range ; j: ; (a) 구조적 동치, (b) 이름 동치, (c) 선언동치 하에서 어느 변수들이 형 동등한지와 그 이유를 기술하시오. 주어진 정보만으로는 모호한 경우를 확실히 구별하도록 하시오.
Programming Languages (a) i, j 는 모두 구조가 이므로 구조적 동치이다. x, y, z, w 는 그 구조가 모두 array[range] of char 로 동일 하므로 구조적 동치이다. (b) x 와 y 는 함께 선언되었으므로 이름동치이다. (c) table2 는 table1 로 선언 되었으므로 이 둘의 형을 가진 변수 z, w 는 선언 동치이다. 이름동치를 포함하므로 x 와 y 도 선언동치
Programming Languages 15) 타입의 동등성을 올바르게 설명한 것은 ? A:arrary(1..5) of INTEGER B:arrary(1..5) of INTEGER C, D:arrary(1..10) of INTEGER
Programming Languages A, B: 구조적 동치 C, D : 구조적 동치, 이름 동치, 선언동치 306p 15 문제는 이름 동치에 올바른 지문을 찾는 문제입니다. 배열형으로 선언하였으므로 모두 동치라고 볼수 없습니다. 배열선언시 동일한 명칭으로 동일한 위치에 선언하였을 경우를 동치라고 볼 수 있습니다. 배열 선언 시 A, B 는 함께 선언하지는 않았지만, 구성요소가 모든 측면에서 같기 때문에 구조적 동치라고 볼 수 있고, C,D 는 함께 선언되었으므로 구조적동치이면서 이름동치에 해당합니다. 선언 동치 (declaration equivalence) 는 재 선언을 통하여 원래의 구조를 그대로 사용하는 경우 원 자료형과 동일형으로 인식합니다.
Programming Languages type xy = record x: integer; y: real; end; xy_alias = xy; ab = record a: integer; b: real; end; yx = record y: real; x: integer; end; abc = record a: integer; b: real; c: real; end; intrec = record x: integer; end; iarr = array[1..10] of integer; iare = array[2..11] of integer; var rec1: xy; rec2: xy_alias; rec3: ab; rec4: abc; rec5,rec6: record a: integer; b: real; end; rec7: record a: integer; b: real; end; rec8: yx; rec9: intrec; rec10: xy; arr1: iarr; arr2: iare; arr3,arr4: array[1..10] of integer; arr5: array[1..10] of integer;
Programming Languages In structural equivalence, two types are equivalent if they have the same structure. rec1 and rec3 are structurally equivalent as they are both pairs of (integer,real). rec1 and rec8 are not structurally equivalent, as they order their elements differently. In name equivalence, two types are equivalent if they are named and have the same name. rec1 and rec2 are not name equivalent. rec1 and rec10 are name equivalent. In declaration equivalence, two types are equivalent if they lead back to the same type name. rec1 and rec2 are name equivalent. rec1 and rec10 are name equivalent.
Programming Languages Type Equivalence TYPE T1 = ARRAY [0..10] OF CHAR; TYPE T2 = T1; A : T2; B : T1; C, D: ARRAY [0..10] OF CHAR; E : T2;
Programming Languages Type Equivalence (a) Structural equivalence? 구조동치 A=B=C=D=E A,B,C,D and E all have equivalent type. (b) Strict name equivalence? 이름동치 A=E, C=D Type T2 is distinct from type T1, so C and D have equivalent type, A and E have equivalent type (but C,D have incompatible type with A,B,E) (c) Loose name equivalence? 선언동치 A=B=E, C=D Type T2 is the same as type T1, so A, B, E have equivalent type, and C,D have equivalent type, (but C,D have incompatible type with A,B,E) TYPE T1 = ARRAY [0..10] OF CHAR; TYPE T2 = T1; //aliased type here A : T2; B : T1; C, D: ARRAY [0..10] OF CHAR; E : T2;
Programming Languages Structural Equivalence Check representing types as trees Check equivalence recursively on subtrees Consider … 구조적 동치가 아님 Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer;
Programming Languages Name equivalence … v1: ar1; v2: ar1; v3: ar2; v1 v2 v3 이름 동치 v4: array (INTEGER range ) of INTEGER; v5: array (INTEGER range ) of INTEGER; v4 and v5 이름 동치가 아님 v6,v7: array (INTEGER range ) of INTEGER; v6 v7 이름 동치
Programming Languages Declaration Equivalent Lead back to the same original structure declaration via a series of redeclarations type t1 = array [1..10] of integer; t2 = t1; t3 = t2; T1 t2 t3 선언동치 type t4 = array [1..10] of integer; t5 = array [1..10] of integer; T4 t5 선언동치 아님
Programming Languages Type Equivalence Structural Equivalence Two types are equivalent when they have the same structure. The same structure means the same type constructor and the same component types. Name Equivalence Two types are equivalent only if they have the same name. Declaration Equivalence Two types are equivalent if they are derived from the same name.
Programming Languages struct A {int a; float b;} a; struct B {int c; float d;} b; typedef struct A C; C c,c1;
Programming Languages struct A {int a; float b;} a; struct B {int c; float d;} b; typedef struct A C; C c,c1; struct.equiv. decl.equiv. name equiv. a = b; ok error error a = c; ok ok error c = c1; ok ok ok