2006년 6월 6일 화요일

[PL]Ch.11 - Simula and Smalltalk

. Simula 67
  . First object-oriented language
  . Designed for simulation
   . Later recognized as general-purpose prog language
  . Extension of Algol 60
  . Standardized as Simula(no "67") in 1977
  . Inspiration to many later designers
   . Smalltalk
   . C++

. Brief histroy
  . Norwegian Computing Center
   . Designers : Dahl, Myhrhaug, Nygaard
   . Simula-1 in 1966 (strictly a simulation language)
   . General language ideas
     . Influenced by Hoare's ideas on data types
     . Added classes and prefixing (subtyping) to Algol 60
   . Nygaard
     . Operations Research specialist and political activist
     . Wanted language to describe social and industrial systems
     . Allow "ordinary people" to understand political(?) changes
   . Dahl and Myhrhaug
     . Maintained concern for general programming

. Comparison to Algol 60
  . Added features
   . class concept
   . reference variables(pointers to objects)
   . pass-by-reference
   . char, text, I/O
   . coroutines
  . Removed
   . Changed default par passing from pass-by-name
   . some var initialization requirements
   . own(= C static) variables
   . string type (in favor of text type)

. Objects in simula
  . class
   . A procedure that returns a pointer to its activation record
  . object
   . Activation record produced by call to a class
  . object access
   . access any local variable or procedures using dot notation : object.
  . Memory management
   . Objects are garbage collected
     . user destructors considered undesirable

. Ex) Circles and lines
  . Problem
   . Find the center and radius of the circle passing through three distinct points, p, q, and r
  . Solution
   . Draw intersecting circles Cp, Cq around p, q and circles Cq', Cr around q, r
     (Picture assumes Cq = Cq')
   . Draw lines through circle intersections
   . The intersection of the lines is the center of the desired circle.
   . Error if the points are colinear

. Approach in Simula
  . Methodology
   . Represent points, lines, and circles as objects
   . Equip objects with necessary operations
  . Operations
   . Point
     . equality(anotherPoint) : boolean
     . distance(anotherPoint) : real (needed to construct circles)
   . Line
     . parallelto(anotherLine) : boolean (to see if lines intersect)
     . meets(anotherLine) : REF(Point)
   . Circle
     . intersects(anotherCircle) : REF(Line)

. Simula Point Class
  . uninitialized ptr has value none
  . =/= : 다름
  . :- : pointer assignment
  . := : value assignment

. Representation of objects
  . Object is represented by activation record with access link to find global variables according to static scoping

. simular line class
. Derived classes in Simula
  . A class decl may be prefixed by a class name
   class A
   A class B
   A class C
   B class D
  . An object of a "prefixed class" is the concatenation of objects of each class in prefix

. Subtyping
  . The type of an object is its class
  . The type associated with a subclass is treated as a subtype of the type assoc with superclass

. Main object-oriented features
  . Classes
  . Objects
  . Inheritance ("class prefixing")
  . Subtyping
  . virtual methods
   . A function can be redefined in subclass
  . Inner : combines code of superclass with code of subclass
  . Inspect/Qua : run-time class/type tests

. Error in simula type system
  . B<:A does not mean ref(B) <: ref(A)

. Coroutine in Simula 67
  . detach
  . resume

. features absent from simula 67
  . Encapsulation
   . All data and functions accessible; no private, protected
  . Self/super mechanism of Samlltalk
   . No self, super, ..
  . Class variables
   . But can have global variables
  . Exceptions
   . Not an OO feature anyway

. simula summary
  . Class
   . "procedure" that returns ptr to activation record
   . initialization code always run as procedure body
  . Objects : closure created by a class
  . Encapsulation
   . protected and private not recognized in 1967
   . added later and used as basis for C++
  . Subtyping : determined by class hierarchy
  . Inheritance : provided by class prefixing

. Smalltalk
  . major language that popularized objects
  . Developed at Xerox PARC
   . Smalltalk-76, Smalltalk-80 were important versions
  . Object metaphor exttended and refined
   . Used some ideas from Simula, but very different lang
   . Everything is an object, even a class
   . All operations are "messages to objects"
   . Very flexible and powerful language
     . Similar to "everything is a list" in Lisp, but more so

. Smalltalk language terminology
  . Object : Instance of some class
  . Class : Defines behavior of its objects
  . Selector : Name of a message
  . Message : Selector together with parameter values
  . Method : Code used by a class to respond to message
  . Instance variable : Data stored in object
  . Subclass : Class defined by giving incremental modifications to some superclass

. Ex) Point class
  . Class definition written in tabular form
  . ^ : return value
  . || : local decl
  . <- : syntax for assginment

. Instance messages and methods

. Encapsulation in Smalltalk
  . Methods are public
  . Instance variables are hidden
   . Not visible to other objects
     . pt x is not allowed unless x is a method
   . But may be manipulated by subclass methods
     . This limits ability to establish invariants
     . Ex)
       . Superclass maintains sorted list of messages with some selector, say insert
       . Sublcass may access this list directly, rearrange order

. Object types
  . Each object has interface
   . Set of instance methods declared in class
   . This is a form of type
     . Names of methods, does not include type/protocol of arguments
  . Object expression and type
   . Send message to object
   . Expression OK if message is in interface

. Subtyping
  . Relation between interfaces
   . Suppose expression makes sense
   . Replace p by q if interface of q contains interface of p

  . Subtyping
   . If interface is superset, then a subtype
   . Ex) ColorPoint subtype of Point
   . Sometimes called "conformance"(일치, 적합, 순응)
  . Can extend to mroe detailed interfaces that include types of parameters

. Subtyping and Inheritance
  . Subtyping is implicit
   . Not a part of the programming language
   . Important aspect of how systems are built
  . Inheritance is explicit
   . Used to implement systems
   . No forced relationship to subtyping

. Smalltalk Flexiblity
  . Measure of PL expressiveness
   . Can constructs of the language be defined in the language itself?
   . Ex)
     . Lisp cond : Lisp allows user-defined special forms
     . ML datatype : sufficient to define polymorphic lists, equivalent to built-in list type
     . ML overloading : limitation, since not available to programmer
     . C, C++ : ???
  . Smalltalk is expressive in this sense
   . many constructs that would be "primitives" other are definable in Smalltalk
   . ex) Booleans and Blocks

. Smalltalk booleans and blocks
  . Boolean value is object with ifTrue:ifFalse:
   . Class boolean with subclasses True and False
   . True ifTrue:B1 ifFalse:B2 executes B1
   . False ifTrue:B1 ifFalse:B2 executes B2
  . Example expression
   . i < j ifTrue: [i add 1] ifFalse: [j subtract 1]
   . i < j is boolean expression, produces boolean object
   . arg's are blocks, objects with execute methods
  . Since booleans and blocks are very common
   . Optimization of boolean
   . Special syntax for blocks

. Self and Super
  . This method can be implemented in Integer, and works even if SmallInt and LargeInt are represented differently,.
  . C++ and Java type systems can't really cope with this.

. Ingalls' test
  . Dan Ingalls : principal designer Samlltalk system
   . Grace Murray Hopper award for Smalltalk and Bitmap graphics work at Xerox PARC
   . 1987 ACM Software Systems Award with Kay, Goldberg
  . Proposed test for "object oriented"
   . Can you define a new kind of integer, put your new integers into rectangles
     (which are already part of the window system), ask the system to blacken a rectangle,
     and have everything work?
   . Smalltalk passed, C++ fails this test

. Smalltalk integer operations
  . Integer expression
   . x plus : 1 times: 3 plus: (y plus: 1) print
  . Properties
   . All operations are executed by sending messages
   . If x is from some "new" kind of integer, expression makes sense as long as x has plus, times, print methods.
  . Actually, compiler does some optimization.
  . But will revert to this if x is not built-in integer
   (revert : 되돌아가다, 복귀v, 회상v)

. Costs and benefits of "true ))"
  . Why is property of Ingalls test useful?
   . Everything is an object
   . All objects are accessed only through interface
   . Makes programs extensible
  . What is implementation cost?
   . Every integer operation involves method call
     . Unless optimizing compiler can recognize many cases
   . Is this worth it?
     . One application where it seems useful?
     . One application where it seems too costly?
     . Are there other issues? Security?

. Smalltalk Summary
  . Class
   . creates objects that share methods
   . pointers to template, dictionary, parent class
  . Objects : created by a class, contains instance variables
  . Encapsulation
   . Mathods public, instance variables hidden
  . Subtyping : implicit, no static type system
  . Inheritance : subclasses, self, super
   . Single inheritance in Samlltalk-76, Smalltalk-80

. Ruby - http://www.ruby-lang.org/
  . Ruby is a "complete, full, pure object oriented language"
   . All data in Ruby are objects, in the sense of Smalltalk
   . ex) the number 1 is an instance of class Fixnum
  . Very flexible
   . Can add methods to a clsss, or even to instance during runtime
   . closures
   . Automatic small integer (Fixnum) large integer (Bignum) conversion
  . Single inheritance only
   . Ruby features single inheritance only, *on purpose*
   . Modules are colections of methods
     . A class can import a module and gets all its methods
  . http://www.rubycentral.com/book/

댓글 없음:

댓글 쓰기