Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Similar documents
Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CPS 506 Comparative Programming Languages. Programming Language

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 5 Object-Oriented Programming

C++ Important Questions with Answers

What about Object-Oriented Languages?

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Lecture Notes on Programming Languages

What are the characteristics of Object Oriented programming language?

Java Object Oriented Design. CSC207 Fall 2014

[ L5P1] Object-Oriented Programming: Advanced Concepts

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CS-202 Introduction to Object Oriented Programming

Chapter 4 Java Language Fundamentals

COP 3330 Final Exam Review

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Data Structures (list, dictionary, tuples, sets, strings)

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

A Short Summary of Javali

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Computer Components. Software{ User Programs. Operating System. Hardware

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

2.4 Structuring programs

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Data Abstraction. Hwansoo Han

CS 251 Intermediate Programming Inheritance

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

StackVsHeap SPL/2010 SPL/20

CS105 C++ Lecture 7. More on Classes, Inheritance

1: Introduction to Object (1)

Subtyping (Dynamic Polymorphism)

Naming in OOLs and Storage Layout Comp 412

VIRTUAL FUNCTIONS Chapter 10

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CSC 533: Organization of Programming Languages. Spring 2005

Late Binding; OOP as a Racket Pattern

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CS153: Compilers Lecture 11: Compiling Objects

APPENDIX C. TOWARD A DISTRIBUTED, OBJECT-BASED FORTH

Programming II (CS300)

Object-Oriented Programming (OOP) Fundamental Principles of OOP

CSE 504: Compiler Design. Runtime Environments

PROGRAMMING LANGUAGE 2

Inheritance -- Introduction

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

CMSC 132: Object-Oriented Programming II

The Procedure Abstraction

Programming II (CS300)

Rules and syntax for inheritance. The boring stuff

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object Oriented Paradigm

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Chapter 7. Inheritance

Object typing and subtypes

Chapter 6 Introduction to Defining Classes

Some instance messages and methods

Fast Introduction to Object Oriented Programming and C++

Chapter 8: Class and Method Design

Object-Oriented Concepts and Design Principles

CS558 Programming Languages Winter 2013 Lecture 8

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Java: introduction to object-oriented features

JAVA An overview for C++ programmers

CS558 Programming Languages

Lecture 5: Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

The major elements of the object-oriented model

What is Inheritance?

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Programming Exercise 14: Inheritance and Polymorphism

Chapter 1: Object-Oriented Programming Using C++

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Instantiation of Template class

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Inheritance, Polymorphism, and Interfaces

Chapter 5. Object- Oriented Programming Part I

Overview. Elements of Programming Languages. Objects. Self-Reference

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:


CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

Wipro Technical Interview Questions

CS 2110 AEW. Machine Language. Outline. Assembly Language. High-Level Language 1/26/2012

Introduction to Inheritance

Another IS-A Relationship

Scope. Chapter Ten Modern Programming Languages 1

Transcription:

Forth Meets Smalltalk A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman 1

CONTENTS WHY FMS? NEON HERITAGE SMALLTALK HERITAGE TERMINOLOGY EXAMPLE FMS SYNTAX ACCESSING OVERRIDDEN METHODS THE OBJECT - MESSAGE CONTRACT OBJECTS AS INSTANCE VARIABLES EARLY vs LATE BINDING to SELF WHY MESSAGE NAMES ARE SPECIAL MESSAGE NAME CONFLICTS RECORDS REFERENCE IMPLEMENTATIONS LINKED LISTS DISPATCH TABLES SUMMARY 2

WHY FMS (Forth Meets Smalltalk)? Neon-like Forth object extensions have a long history of successful use in several Forths. Friendly and familiar syntax for creating classes. But there have been complaints (ordering of message-object, [ ] syntax, lack of methodless ivar access, etc.) Claimed to have inefficient (slow) dynamic binding. FMS addresses the complaints, but keeps what is liked. 3

NEON HERITAGE FMS class building syntax resembles Neon, but there are important differences. Retains Neon s ease of use in defining new classes, e.g., interface definitions are not needed. Fully supports objects-as-instance-variables. Upgrade path to Multiple Inheritance thanks to Michael Hore and PowerMops. 4

SMALLTALK HERITAGE Any message can be sent to any object (Duck Typing). No interfaces needed. Object-message syntax. Message names end with colon : 5

TERMINOLOGY Smalltalk terminology is used in this presentation. Early Binding, a message send is resolved to the correct method at compile time. Late Binding, a message send is resolved to the correct method at run time. Also called Dynamic Binding. 6

EXAMPLE FMS SYNTAX Begin a class definition Begin a method definition End the method definition :class foo :m hi:. Hello ;m ;class The class name is foo The message name is hi: The method itself End the class definition 7

Send the hi: message to the object. foo myfoo myfoo hi: Hello Instantiate an object in the dictionary of class foo. The object is named myfoo. Note that all messages sent to public objects (not objects as ivars) are late bound. Alternatively, instantiate an object in the heap. Heap> is a runtime only word. : make ( -- ^obj ) heap> foo ; make constant myfoo2 myfoo2 hi: Hello myfoo2 <free Free the heap memory when done with object myfoo2 8

Define a class whose objects will have an instance variable (ivar) of size one cell. We normally declare all ivars prior to defining the methods. Here the BYTES ivar defining primitive is used. Executing the ivar s name will return its address. :class var cell bytes data :m @: ( -- n ) data @ ;m :m!: ( n -- ) ;class data! ;m The scope of the ivar name, in this case data, is private to the class definition and any subsequent subclasses. Message names are always global in scope. 9

Define a subclass of var, named var2. Objects of class var2 inherit all data and methods from the superclass var. Objects of class var2 will also respond to the three new print messages. :class var2 <super var :m print1: data @. ;m :m print2: self @:. ;m :m print3: [self] @:. ;m ;class var2 v2 25 v2!: v2 print1: 25 v2 print2: 25 v2 print3: 25 v2 IV data @. 25 Note explicit declaration of superclass Four different ways to print the ivar value in a var2 object. What are the implications of each? 10

Method print1: will be the fastest message send because it uses in-class METHODLESS IVAR ACCESS. :m print1: data @. ;m Method print2: uses an early bound message to the pseudo ivar SELF. :m print2: self @:. ;m Method print3: uses a late bound message to the pseudo ivar SELF. The late binding is invoked by using the bracketed [SELF]. The advantage of using a late bound message send is we can more easily change the behavior of subclass methods (see next slide). The disadvantage is a late bound message send is always somewhat slower than one that is early bound. This speed difference may or may not be important to the application. Late binding to SELF is the most flexible way to design a method and is considered by some to be the preferred technique in all cases. In FMS the programmer has a choice. :m print3: [self] @:. ;m Methodless ivar access outside of a class definition will always be fastest because no message is sent (anywhere). But the downside is this trick violates the fundamental idea behind object programming. What if we later change the internal structure of the class such as changing data to be a char-length integer? v2 IV data @. 25 11

Define a subclass of var2 named var3. The @: method inherited from the superclass var has been overridden with a different method. Note that the overriding is implicit, i.e., there is no need to declare something like override because the intent is (should be) obvious. :class var3 <super var2 :m @: ( -- n ) ;class. fetch from var3 data @ ;m var3 v3 25 v3!: v3 print2: 25 Method print2: is unaffected because the @: in class var2 is early bound to SELF. v3 print3: fetch from var3 25 The late bind of @: to [SELF] in the print3: method of class var2 will use the most recently defined @: in the class hierarchy chain. Objects of class var and var2 are not affected by the redefinition of @: in class var3. Late binding to [SELF] is sort of like a context-dependent deferred definition. The context is the class. 12

ACCESSING OVERRIDDEN METHODS :class var4 <super var :m @: ( -- n ). fetch from var4 data @ ;m :m test: ( -- n ) self @:. cr super @: 1+. ;m ;class var4 v4 95 v4!: Call the @: method in this class. v4 test: fetch from var4 95 96 Call the @: method in class VAR. 13

THE OBJECT - MESSAGE CONTRACT Classic OOP theory would mandate that the only way to access an object s data or invoke its procedures is to send a message to the object. This a form of data protection. Data can only be changed via a message send. Makes it easier to avoid or track down bugs. Also allows the internal details of data formats and procedures to be changed without affecting the rest of the program. This is a form of information hiding. 14

THE OBJECT - MESSAGE CONTRACT In FMS the programmer is free to violate this contract. Why? This is Forth. Methodless ivar access is available. Ivar data and methods are available. Use with care, if used at all. 15

OBJECTS AS IVARS :class bar var x var2 y foo afoo :m!: ( x y --) y!: x! ;m We can declare as many ivars as we want. We can freely mix ivar objects and ivars created with the BYTES primitive. :m p: x @:. y print2: afoo hi: ;m ;class bar b \ instantiate an object 5 10 b!: \ send a message to b b p: 5 10 Hello \ send a message to b 16

Early vs Late Binding to SELF Defining all messages sent to SELF as late bound, ( i.e., always use [SELF] instead of SELF ) might make it easier to reuse method definitions in subclasses. This could be a benefit, but not in all cases. The downsides are: 1) Slower program execution due to many more (and unnecessary) late binds, and 2) Must be careful to avoid infinite loops when a subclass method uses a superclass method which references back to the subclass via late binding. 17

Early vs Late Binding to SELF It has been the author's experience that when an opportunity for code reuse via late binding to SELF occurs, it is a simple matter to just redefine the binding involved by changing SELF to [SELF] in the superclass method. 18

WHY MESSAGE NAMES ARE SPECIAL Sending a message is a "special" event and it should be clear when reading source code where a message send occurs. This is an opinion. In FMS we require that all message names end in colon ( <name>: ). This naming convention has been used with success in other Forths for decades. Some seem fixated on wanting to hide the use of objects thus making object code indistinguishable from Forth code not using objects. This a mistake, in the author s opinion. For example, if we define "@" to be a message then we can have code that looks like the following (next slide): 19

WHY MESSAGE NAMES ARE SPECIAL foo @ \ a normal fetch from the VARIABLE foo, or the address presented by the execution of foo. bar @ \ a message send to the OBJECT bar, or the object presented by the execution of bar. 20

MESSAGE NAME CONFLICTS FMS message names have global scope. A message name conflict with another globally scoped word is handled as you would with any name conflict in Forth. 21

RECORDS Instance variables can be declared as part of a record. Header information is then removed from an object s data. Use the REC{ }REC notation. Essentially a form of a structures package for objects. 22

REFERENCE IMPLEMENTATIONS Two example implementations of FMS are provided for reference. Not the only ways to implement FMS. Production FMS code would be optimized. Reference implementations are loosely based on Andrew McKewan s work (Object Oriented programming in ANS Forth, Forth Dimensions, March 1997). One uses linked lists to resolve messages to methods. Faster than McKewan (1997) because the n-way thread is determined at compile time. This is actually pretty fast. N could be increased. One uses dispatch tables (very fast). A small library is supplied in order to help illustrate the use of FMS. 23

LINKED LISTS ^obj SelectorID (compute mfa thread offset at compile time) : FINDM ( SelID MFA -- ^xt ) BEGIN @ DUP WHILE 2DUP CELL+ @ = IF 2 CELLS + NIP EXIT THEN REPEAT ; FINDM @ EXECUTE-METHOD : EXECUTE-METHOD ( ^obj xt -- ) [SELF] >R SWAP TO [SELF] EXECUTE R> TO [SELF] ; 24

DISPATCH TABLES ^obj SelectorID OVER CELL - @ + @ EXECUTE-METHOD : EXECUTE-METHOD ( ^obj xt -- ) [SELF] >R SWAP TO [SELF] EXECUTE R> TO [SELF] ; 25

SUMMARY FMS retains what we like from Neon. FMS improves upon any real or perceived weaknesses in Neon. 26