simula and smalltalk
play

Simula and Smalltalk First object-oriented language Designed for - PDF document

CS 242 Simula 67 Simula and Smalltalk First object-oriented language Designed for simulation Later recognized as general-purpose prog language Extension of Algol 60 John Mitchell Standardized as Simula (no 67) in 1977


  1. CS 242 Simula 67 Simula and Smalltalk � First object-oriented language � Designed for simulation • Later recognized as general-purpose prog language � Extension of Algol 60 John Mitchell � Standardized as Simula (no “67”) in 1977 � Inspiration to many later designers • Smalltalk • C+ + • ... Brief history Comparison to Algol 60 � Norwegian Computing Center � Added features • Designers: Dahl, Myhrhaug, Nygaard • class concept • Simula-1 in 1966 ( strictly a simulation language ) • reference variables (pointers to objects) • General language ideas • pass-by -reference – Influenced by Hoare’s ideas on data types • char, text, I/O – Added classes and prefixing ( subtyping) to Algol 60 • coroutines • Nygaard � Removed – Operations Research specialist and political activist • Changed default par passing from pass-by -name – Wanted language to describe social and industrial systems • some var initialization requirements – Allow “ordinary people” to understand political (?) changes • Dahl and Myhrhaug • own (= C static) variables – Maintained concern for general programming • string type (in favor of text type) Objects in Simula Example: Circles and lines � Problem � Class • Find the center and radius of the circle q • A procedure that returns a pointer to its activation record passing through three distinct points, p, � Object p q, and r r � Solution • Activation record produced by call to a class � Object access • Draw intersecting circles Cp, Cq around p,q and circles Cq’, Cr around q, r • Access any local variable or procedures using dot (Picture assumes Cq = Cq’) notation: object. • Draw lines through circle intersections � Memory management • The intersection of the lines is the center of the desired circle. • Objects are garbage collected Error if the points are colinear . • – user destructors considered undesirable 1

  2. Approach in Simula Simula Point Class � Methodology class Point(x,y); real x,y; formal p is pointer to Point begin • Represent points, lines, and circles as objects. boolean procedure equals(p); ref(Point) p; • Equip objects with necessary operations. if p = /= none then equals := abs(x - p.x) + abs(y - p.y) < 0.00001 � Operations real procedure distance(p); ref(Point) p; • Point if p = = none then error else equality(anotherPoint ) : boolean distance := sqrt (( x - p.x )* * 2 + (y - p.y) * * 2); distance(anotherPoint ) : real (needed to construct circles) end * * * Point* * * • Line p :- new Point(1.0, 2.5); parallelto(anotherLine) : boolean (to see if lines intersect) uninitialized ptr has q :- new Point(2.0,3.5); value none meets(anotherLine) : REF(Point) if p.distance(q) > 2 then ... • Circle intersects(anotherCircle) : REF(Line) pointer assignment Representation of objects Simula line class class Line(a,b,c); real a,b,c; Local variables begin p access link line determined by boolean procedure parallelto(l); ref(Line) l; real x 1.0 code for ax+ by+ c= 0 if l = /= none then parallelto := ... equals real y 2.5 ref(Point) procedure meets(l); ref(Line) l; Procedures proc equals begin real t; proc distance code for if l = /= none and ~ parallelto(l) then ... distance end; real d; d := sqrt(a* * 2 + b* * 2); if d = 0.0 then error else begin Initialization: d := 1/d; Object is represented by activation record with access “ normalize” a,b,c a := a*d; b := b*d; c := c*d; link to find global variables according to static scoping end; end * * * Line* * * Derived classes in Simula Subtyping � A class decl may be prefixed by a class name � The type of an object is its class � The type associated with a subclass is treated class A as a subtype of the type assoc with superclass A class B � Example: A class C class A(…); ... B class D A class B(…); ... � An object of a “prefixed class” is the ref (A) a :- new A(…) concatenation of objects of each class in prefix ref (B) b : - new B(…) • d :- new D(…) A part a := b / * legal since B is subclass of A * / B part ... d D part b := a /* also legal, but run-time test * / 2

  3. Main object-oriented features Features absent from Simula 67 � Classes � Encapsulation � Objects • All data and functions accessible; no private, protected � Self/Super mechanism of Smalltalk � Inheritance (“class prefixing”) • But has an expression this 〈 class 〉 to refer to object � Subtyping itself, regarded as object of type 〈 class 〉 . Not clear how � Virtual methods powerful this is… • A function can be redefined in subclass � Class variables � Inner • But can have global variables • Combines code of superclass with code of subclass � Exceptions � Inspect/Qua • Not an OO feature anyway ... • run-time class/type tests Simula Summary Smalltalk � Class � Major language that popularized objects � Developed at Xerox PARC • ”procedure" that returns ptr to activation record • initialization code always run as procedure body • Smalltalk -76, Smalltalk -80 were important versions � Objects: closure created by a class � Object metaphor extended and refined � Encapsulation • Used some ideas from Simula, but very different lang • protected and private not recognized in 1967 • Everything is an object, even a class • added later and used as basis for C+ + • All operations are “messages to objects” � Subtyping: determined by class hierarchy • Very flexible and powerful language – Similar to “everything is a list” in Lisp, but more so � Inheritance: provided by class prefixing – Example: object can detect that it has received a message it does not understand, can try to figure out how to respond. Motivating application: Dynabook Smalltalk language terminology � Concept developed by Alan Kay (now Disney?) � Object Instance of some class � Small portable computer � Class Defines behavior of its objects � Selector Name of a message • Revolutionary idea in early 1970’s – At the time, a minicomputer was shared by 10 people, � Message Selector together with parameter values stored in a machine room. � Method Code used by a class to respond to message • What would you compute on an airplane? � Influence on Smalltalk � Instance variable Data stored in object � Subclass Class defined by giving incremental • Language intended to be programming language and operating system interface modifications to some superclass • Intended for “non-programmer” • Syntax presented by language-specific editor 3

  4. Example: Point class Class messages and methods Three class methods Explanation � Class definition written in tabular form newX:xvalue Y:yvalue | | - selector is mix-fix newX:Y: class name Point ^ self new x: xvalue e.g, Point newX:3 Y:2 super class Object y: yvalue - symbol ^ marks return value class var pi - new is method in all classes, instance var x y inherited from Object newOrigin | | - | | marks scope for local decl class messages and methods ^ self new x: 0 〈 …names and code for methods... 〉 y: 0 - initialize method sets pi, called instance messages and methods automatically 〈 …names and code for methods... 〉 initialize | | - < - is syntax for assignment pi < - 3.14159 Instance messages and methods Run-time representation of point Five instance methods Explanation to superclass Object x: xcoord y: ycoord | | set x,y coordinates, Point class x < - xcoord e.g, pt x:5 y:3 Template Point object y < - ycoord x moveDx: dx Dy : dy | | move point by given amount class y x < - dx + x x 3 y 2 y < - dy + y Method dictionary code x | | ^ x return hidden inst var x newX:Y: y | | ^ y return hidden inst var y ... ... Detail: class method shown in draw | | draw point on screen dictionary, but lookup procedure move 〈 ...code to draw point... 〉 distinguishes class and instance code methods Inheritance Run-time representation Point class Template � Define colored points from points Point object Method dictionary x newX:Y: class name ColorPoint y 2 ... draw super class Point 3 move class var new instance instance var color variable ColorPoint class Template ColorPoint object class messages and methods Method dictionary x newX:xv Y:yv C:cv 〈 … code … 〉 new method y newX:Y:C: 4 instance messages and methods color color 5 draw color | | ^ color red override Point 〈 … code … 〉 draw method This is a schematic diagram meant to illustrate the main idea. Actual implementations may differ. 4

Recommend


More recommend