开发者

Name equivalence question

Suppose I have:

int a;
int b;

Are the variables a and b name开发者_StackOverflow社区 equivalent (more specifically, since primitive types don't have type names, can they be considered name equivalent)?

Thanks.


Name (more properly, nominal) equivalence means that the values have the same type as determined by the (fully qualified) name of their type -- for instance, a and b are nominally type equivalent because they both have "int" type. Structural equivalence means that the values are considered to have the same type because their types are structurally equivalent, regardless of the name. Nominal type equivalence implies structural type equivalence since a named type is structurally equivalent to itself. Your a and b, are nominally type equivalent because they have the same type by name ("int"). The claim that "primitive types don't have type names" is simply false -- int is a type name. And there is no difference between int a; int b; and int a, b; -- both define a and b with the same (structurally and by name) type.

C's type system is generally by name ... e.g., int * and short* are different types even if int and short have the same representation, and struct foo { int x; } and struct bar { int x; } are different types even though they always have the same representation.


Some class notes I found seem to indicate that with:

int a;
int b;

Variables a and b are not name equivalent.

However, with:

int a, b;

Variables a and b are name equivalent.


C adopts structural equivalence except for structures and unions; for these types, name equivalence is used. It is true that the values have the same type as determined by the name of their type in name equivalence and that the values are considered to have the same type as determined by structurally equivalent in structural equivalence. But even in structural equivalence, it is not determined regardless of the name.

Basically, atomic types in a programming language have names and they are used both in name equivalence and structural equivalence. The difference comes from derived types such as int * or char[10]. It is true that "primitive types have type names." And C's type system is comparing the structure of derived types up to user-defined names excluding type synonyms even they are defined by the user using typedef.

C's type system is generally adopting structural equivalence. For instance, int * and short * are different types but int * and int * are the same types even though no names are attributed to the pointers. The reason for that struct foo { int x; } and struct bar { int x; } are different types is because they have different structure up to user-defined type names. Types struct foo * and struct foo * are the same types since they have the same structure. Note that struct defines not only the structure but also name, more precisely structure tag name, of the type.

Simply, in structural equivalence, the types are unfolded up to atomic type names and they are compared. In C, the types are unfolded up to names including user-defined names by struct or union. (For unfolding, please refer http://www.csd.uwo.ca/~moreno//CS447/Lectures/TypeChecking.html/node3.html) This is the difference. Naturally, name equivalent types are structurally equivalent, but not the converse.

For the following case:

int x;
int y;

x and y and both name equivalent and structural equivalent. But, in the following case:

int *x;
int *y;

x and y are structural equivalent but not name equivalent. Even for the following case:

int *x, *y;

x and y are not considered equivalent in a strict name equivalence scheme, for this declaration is considered as just a shorthand of the previous declaration. Ada is a well-known language adopting the strict name equivalence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜