showTitle("https://tistory1.daumcdn.net/tistory/520789/skin/images/", "viewTitle.swf", 600, 30, "Name equivalence & Structural equivalence", "/entry/Name-equivalence-Structural-equivalence", "left", "#000000");
2010. 3. 29. 19:56
inSDSU 2010 Spring/Programming Language
Data type의 equility를 비교할 때, Name equivalence와 Structural equivalence의 두가지 컨셉이 있습니다.
Name equivalence
Name equivalence는 두 data type의 type name이 같을 때만 data type이 같다고 봅니다. 즉 type name Vect1을 가진 X와 Z는 대입이 되지만(X := Z), X := Y(Vect2)는 대입이 불가능합니다.
/* Figure 1 Type equility*/ program main(input, output); type Vect1: array[1 .. 10] of real; Vect2: array[1 .. 10] of real; var X, Z: Vect1; Y: Vect2; procedure Sub(A: Vect1); ... end; begin - main program X := Y; SUub(Y) end.
- 대입 연산에 사용되는 모든 data object가 type name을 가져야 합니다. 즉, anonymous type이 존재하지 않습니다.
- Data object가 subprogram의 argument로 전달될 때, data type이 재정의될 수 없습니다. 즉, Single type definition이 프로그램 전체 범위를 지원해야 하므로 global type definition이 사용되야 합니다.
Structural equivalence
Structural equivalence는 두 data type의 internal components가 같을 때 data type이 같다고 봅니다. Internal components가 같다는 뜻은 Storage representation이 같다는 것을 의미하기 때문입니다.
Figure 1의 Vect1과 Vect2는 Name equivalence의 관점에서는 다른 data type이지만 Structural equivalence의 관점에서는 component의 number, type, order가 같기 때문에 같습니다.
/* Figure 2 Structural equivalence */ type Meters = integer; Liters = integer; var Len: Meters; Vol: Liters;
- Component의 이름, type, 순서가 같은가? 만약 component가 배열일 경우 subscript range가 같은가? 하는 미묘한 차이점으로 인한 문제가 발생할 수 있습니다.
- Figure 2와 같이 프로그래머가 다른 type으로 선언했지만 우연하게 Structural equivalence에 따라 같은 data type으로 인식되어 static type checking에 걸리지 않을 수 있습니다.
- Type checking을 위해서는 data type의 structure를 모두 검사해야 하기때문에 매우 costly합니다.