============================================================================== | A P P E N D I X 9 | ============================================================================== //{{{ stamp Date: Wed, 20 Mar 96 12:26:55 GMT From: phw To: teig@autronica.no Subject: Aliasing problems in Java (SEMANTIC SINGULARITIES) //}}} Java can do nothing to prevent threads updating and reading data at the same time since it has no ability to control name aliasing -- something at which occam has to, and does, excel. Enclosed is a small Java example to demonstrate this -- my first Java program! It's just the "harmless" procedure that I used in my "Going to Ceed" and "Role and Future of occam" papers from long ago. It shows that Java has the same semantic complexities ("singularities") as C, Pascal and all the others -- excepting occam ... These semantic singularities are dangerous! The "harmless" method does not change either of its parameters ... unless you pass it the same object twice. This is a complex piece of semantics and easilly overlooked. The problem is that, while Java has no explicit pointers, it has plenty of implicit ones and it's only too possible to create aliases that you aren't intending. They did a great job applying occam's razor to C/C++ to get rid of the high-level superstructure and yield Java. Unfortunately, all the low-level rubbish of C still remains -- assignment as an operator, ++, the total confusion between methods that return values (i.e. functions) and methods that cause state change (i.e. procedures), a for-loop that ain't a for-loop, break/continues out of loops (labelled loops ain't the answer, they need occam's if-loops), etc. They need to finish the job!!! Cheers, Peter. (cut here) ----------------------------------------------------------------------------- Below is a script session. It first lists a Java program, then compiles and runs it. The last line shows the semantic singularity ... bay% more alias_test.java class thing { int x; public thing(int v) { x = v; } public static void harmless (thing t1, thing t2) { // this doesn't look as though it changes anything ... // but it's semantics is not WYSYWIG (as in occam) ... // it depends on the context of its call ... // if t1 and t2 are different objects, it is harmless ... // if t1 and t2 are aliases for the same object, it zaps it !!! // in occam, since t1 and t2 are different names, we know that // they are different objects: what-you-see-is-what-you-get ... // so if this were occam, there is no context dependency and // the `obvious' harmless semantics is universally obeyed. t1.x = t1.x + t2.x; t1.x = t1.x - t2.x; } } class alias { public static void main (String args[]) { thing c1 = new thing(10); thing c2 = new thing(20); System.out.println("BEFORE: c1 = " + c1.x + ", c2 = " + c2.x); thing.harmless (c1, c2); System.out.println(" AFTER: c1 = " + c1.x + ", c2 = " + c2.x); thing.harmless (c1, c1); System.out.println(" ALIAS: c1 = " + c1.x + ", c2 = " + c2.x); } } bay% javac alias_test.java bay% java alias BEFORE: c1 = 10, c2 = 20 AFTER: c1 = 10, c2 = 20 ALIAS: c1 = 0, c2 = 20 --------------------------------------------------------------------------------