Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker!husc6!spdcc!ima!esegue!compilers-sender From: pase@orville.nas.nasa.gov (Douglas M. Pase) Newsgroups: comp.compilers Subject: Re: Defining polymorphism vs. overloading Keywords: design, polymorphism Message-ID: <9009070438.AA00687@orville.nas.nasa.gov> Date: 7 Sep 90 04:38:23 GMT References: <9008310419.AA06194@karakorum.berkeley.edu> <4c98dcd0b.001a92c@caen.engin.umich.edu> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: Douglas M. Pase Organization: NASA Ames Research Center, Moffett Field, CA Lines: 75 Approved: compilers@esegue.segue.boston.ma.us In-Reply-To: <9009051416.AA29612@uvacs.cs.Virginia.EDU> In article <4c98dcd0b.001a92c@caen.engin.umich.edu> plph@caen.engin.umich.edu (Mark Montague) writes: >Are the square braces in C polymorphic or overloaded? Which term should be >used? >[In the C compilers I've seen subscripts are turned into the intermediate code >equivalent of *(a+b) very early in the compiler, typically as the intermediate >form is being built. -John] Yes, well, it is important to note that a[b] => *(a+b). So what is the type signature of this function? You can look at it several ways. In the first case it depends on the type of a. (It also depends on whether a[b] occurs on the rhs or lhs of an assignment, but I will ignore that additional complication.) Given the address that a represents, the amount added to that address will depend on the size of its elements. For example, in a byte oriented 32-bit machine, char a[MAX]; ... a[b] ... gives something like LDW,r6 b ; load the contents of b ADD,r6 r6,a ; add the address a LDB,r7 r6 ; load the byte contents of address in r6 but int a[MAX]; ... a[b] ... gives LDW,r6 b ; load the contents of b MUL,6 r6,4 ; multiply b by the size of an int ADD,r6 r6,a ; add the address a LDW,r7 r6 ; load an int located at the byte address in reg 7 Judging from this, it seems that the type signatures are [] : array of char x int -> char : array of int x int -> int ... This seems to suggest that [] is overloaded, and represents an infinite family of functions, all differing in the size of the elements of a. Because [] seems to fit the definition, one could say [] is overloaded. It's hard to believe, though, that much useful information is gained by looking at it in this way. On the other hand, it might also be said that it is universal polymorphic. Its type signature would be [] : array of *a x int -> *a Looking at it in this way assumes either that it is a primitive polymorphic function, and is therefore polymorphic almost by definition, or that it is constructed from functions which are. That means loads, sizeofs, etc., must be universal polymorphic. The use of the "+" function doesn't give us any problem as long as only one definition of "+" is used, namely + : byte_address x int -> byte_address If the "+" somehow depended on the type of "a", [] wouldn't be universal polymorphic. It would be ad hoc polymorphic (i.e., overloaded). Assuming that the above mentioned restrictions are met, it seems much simpler and more useful to view [] as universal polymorphic. -- Dr. Douglas M. Pase, Computer Sciences Corporation NASA Ames Research Center MS 258-6, Moffett Field, CA 94035 (415) 604-6394, pase@orville.nas.nasa.gov -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.