C-mantics

R.VANDERBEI vdb at hou2g.UUCP
Mon Apr 15 10:44:58 AEST 1985


C is perhaps the nicest language I've seen (except APL) however I
do have some criticisms:

(1) Operators, functions and procedures should not have "side effects".

   (a) For operators, this means that no operator should ever be 
       allowed to change the value of its operands.  This eliminates
       the possibility of "slangy" type operators like the increment
       operator  ++   in C among others.  Also the assignment operator
       is in fact not an operator but is a "copy statement".
   (b) For functions, this means that a function should only be allowed 
       to alter the values of locally defined variables (one of which
       is returned to the calling statement).  It is a terrible bastard-
       ization of the meaning of a function to allow it to change the
       values of any of its arguments or any variables which are defined
       at a higher level.
       You may respond that C already doesn't allow the programmer to 
       change the value of parameters passed to the function.  But, as we
       all know, C  allows one to pass a pointer to a variable and then
       allows one to change the value at that memory location.  This is
       a perfect example of a side effect where a variable defined at a higher
       level is changed.  Most people consider this to be an asset but in my 
       opinion it is a serious liability which encourages a programmer
       to write code which is harder to debug than the much maligned spaghetti
       code of yore.  I think that all variables should be, by definition,
       pointers to locations in memory - see point (2).
       To make this restriction easy to use, it is neccessary that the 
       language allow the programmer to define elaborate structures which can
       be returned as the result of a function.  For example, if one wishes to
       write a "print on line printer" function "print", then the function
       should know how many lines are on a page and what the current line 
       number is.  One should define a structure which has fields for all 
       the relevant printer information and then initialize a variable say
       prn which contains the starting values for these parameters.  Then a
       function call would look like this:
   
               prn = print(prn, ... ).

       prn  goes in as input containing the current state, print  returns
       a variable of the same structure which contains the updated information
       and the  =  copies the information into  prn.  This is only an example
       I think a language should supply such functions as print and, where 
       appropriate, take advantage of mare streamlined semantics and more effic- 
       ient implementations.
(2) All variable names should represent memory locations and not the value at 
   the location.  Then operators have to be defined appropriately.  For 
   instance, the plus operator  +  would be defined so that x+y would mean:
   "add the value stored at location x to the value stored at location y
   and return a pointer to the location where the result is stored".
   Pointer arithmetic can be accomplished by the notation  x[j]  which
   would represent the memory location x plus j times the length of
   the structure to which x points.
3. The assignment statement  =  should be the only way of actually copying
   one area of memory into another.  If x and y are (perhaps structured)
   variables then  y=x  should copy the structure to which  x  points
   into the location to which y points.  When one defines a structure there
   should be included the facility to indicate which other structured types
   can be assigned into the type being defined and how to perform any 
   neccessary conversions.  This would allow, for example, one to define a
   complex variable structure and if z is a complex variable and x is a 
   real variable (double say) then the assignment
                            z=x
   should work and be defined in the definition of the complex variable
   structure.  This is one of the nice features of C++.
4. In the definition of a stucture, one should be able to specify how 
   operators act on newly defined structures.  For example, this would allow
   one to define a matrix  A  and a vector  x  and hove  A*x  make sense
   and agree with the usual mathematical definition.
Final Remarks. C++ addresses criticisms (3) and (4) but fails (I think) in
   regard to (1) and (2).
   Since I am a mathematician and not a computer scientist, my terminology
   may not be standard, but I hope that my above criticisms are clear
   enough that readers will understand my points.  If anyone knows of
   of a language which does things as I have suggested, please let me 
   know  -  especially if it runs on a Tandy 2000 and offers a nice
   programming environment (I program in Turbo pascal because the compiler
   is fast, the code is fast, and it comes with an editor which can 
   be configured as you like - a truly nice operating environment).  If
   there is anyone who would like to implement a language such as I have 
   described, I would be happy to take an active part in the language design.



More information about the Comp.lang.c mailing list