Simula


History:
First OOPL, 1967

Designed by Dahl, Myhrhaug, Nygaard
at Norwegian Computing Center, Oslo

Simula: simulation language for "event based simulation"
(similar structure to gui's)

Stroustrup started with Simula as motivation for C++

Extention/Modification of Algol 60: (Simula might be thought of as Algol60++)

Added:
class concepts and reference-variables (ptrs to objects)
pass-by-reference
char, text, and I/O
co-routines

Removed:
dropped call-by-name (lazy eval) in favor of eager eval and pass-by-value, pass-by-result.
some var initialization requirements
OWN (= C static) variables
string type (in favor of "text" type)

Objects in Simula

(Read excerpt Chap. 4 of Simula Begin)

Class:
Procedure returning a pointer (Simula term: REF) to its activation record.

Object:
Activation record produced by call to a class, called an instance of the class.

Note 1: In Simula, an object is a closure! Classes are procedures that return objects.

Note 2: Simula came before the "object religion". The philosophy of OOPL's and the design methodology, etc. is not here.

Object-oriented features in Simula:

1. Classes

2. Objects (with instance variables and methods/member functions)

3. Inheritance / Derivation (Smalltalk/C++ terminology)

4. Subtyping

5. "Virtual" methods (methods that can be redefined in derived classes)

6. "Inner": supports method combination. (Something like "super" from Smalltalk: When redefining a method, we may use old version of the method.)

7. Inspect/Qua: (run-time class (type) tests, like RTTI abilities in new C++)
Inspect: class (type) test
Qua: class (type) cast, checked for correctness at run-time

OO feature(s) NOT in Simula:

1. Encapsulation.
all data and functions in an object accessible via dot notation.
compare:
Smalltalk: private data (no private methods)
C++: private data, functions

Simula Example:

(Simula Begin; Chap. 4)

Find center and radius of the circle passing through three distinct points, p, q, and r:

1. Draw intersecting circles Cp and Cq, centered around p and q.
2. Draw intersecting circles Cq' and Cr, centered around q and r.
3. We will assume the two circles around q (Cq and Cq') have the same radius (ie, they are the same circle).
4. Draw line L1 through the points of intersection of Cp and Cq.
5. Draw line L2 through the points of intersection of Cq and Cr.
6. The intersection of L1 and L2 is the center of the desired circle.
- Error case: three points are colinear.

Methodology

The objects we will need to have the following properties

Point
rep:
x, y coordinates
op's:
equality(anotherPoint) : boolean
distance(anotherPoint) : real (needed to construct intersection circles)

Line
rep:
All lines have the form: ax + by + c = 0
We will store a,b,c, normalized
When we call the Line class to build a Line object,
we will normalize the values of a, b, and c.
op's:
parallelto(anotherLine) : boolean (needed to see if lines intersect)
meets(anotherLine) : REF(Point)

Circle
rep:
center : REF(Point)
op's:
intersects(anotherCircle) : REF(Line)

Simula Point Class:

	Class Point(x,y); real x,y;
	begin
	  boolean procedure equals(p); ref(Point) p;
            if p =/= none then
		equals := abs(x - p.x) + abs(y - p.y) < 0.00001;
	
	  real procedure distance(p); ref(Point) p;
	    if p == none then error else
	        distance := sqrt(( x - p.x )**2 + (y - p.y) ** 2);

	end ***Point***

	p :- new Point(1.0, 2.5);

Notice:

1. We may access our instance variables by simply naming them.

2. We may access the instance variables of other objects via a "dot notation" (eg: p.x, p.y)

3. All objects are manipulated via "refs". The type of an object is of the form ref(Class) where Class is the name of the Class from which the object was instantiated.

When this code is executed it produces a run-time structure that looks like:





Picture here.

Each point object contains (pointers to) the point class methods. This contrasts with Smalltalk/C++. In both of these languages, each object stores only a pointer to a table storing the pointers to the methods.

Simula Line Class:

	Class Line(a,b,c); real a,b,c;
	begin
	  boolean procedure parallelto(l); ref(Line) l;
	    if l =/= none then
		parallelto := abs(a*l.b - b* l.a) < 0.00001;

	  ref(Point) procedure meets(l); ref(Line) l;
	    begin real t;
		if l =/= none and ~parallelto(l) then
		   begin
		     t := 1/(l.a * b - l.b * a);
		     meets :- new Point(..., ...);
		   end;
	    end; ***meets***

	  real d;

	  d := sqrt(a**2 + b**2);

	  if d = 0.0 then error else
	     begin
		d := 1/d;
		a := a * d; b := b * d; c := c * d;
	      end;

	end *** Line***
Notice:
1. In the procedure meets, we invoke another method of the host object, parallelto. In Smalltalk, this ability is provided by sending messages to a pseudo-variable self. (Similarly, this in C++). This ability distinguishes Simula objects from simple records, since record fields typically cannot access other fields of their host record.

2. Again we see the dot notation to access components of other objects. This ability reflects Simula's lack of encapsulation mechanism.

3. The code following the procedure meets is initialization code; it is executed whenever a Line object is instantiated.

The run-time structure for line objects looks like the following:





Picture here.











Simula Circle Class
See the Simula paper in the course reader.

ML Version

Subclasses and Subtypes in Simula

Simula Summary

1. Objects: created via classes, essentially closures.

2. Classes: functions that create objects; each time a "class" is called, the corresponding activation record is the object created. The body of the "class" function initializes the created object. Objects are "typed" by the classes that instantiate them.

3. Encapsulation: none.

4. Subtyping: determined by class hierarchy.

5. Inheritance: one class's definition may be given as an extention of a previous one (we did not cover this in detail).

Kathleen Fisher / kfisher@cs.stanford.edu