FREE ELECTRONIC LIBRARY - Dissertations, online materials

Pages:   || 2 |

«1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001|Structure and Interpretation of Computer ...»

-- [ Page 1 ] --



Department of Electrical Engineering and Computer Science

6.001|Structure and Interpretation of Computer Programs

Sample assignment

Object-oriented programming

Reading: Section 4.1

Languages for Object-Oriented Programming

This problem set is probably the most di cult one of the semester, but paradoxically, the one

that asks you to write the least amount of code, and for which you should have to spend the least time in lab, provided that you prepare before you come to lab. Instead of asking you to do a lot of implementation, we are asking you to assume the role of language designer, and to think about and discuss some issues in the design of languages for object-oriented programming. Note especially that there is a signi cant part of this problem set to be completed after you have nished in the lab.

This problem set has been designed so that the interpreter you will be dealing with is an extension of the metacircular evaluator in chapter 4 of the book. The implementation below is described with reference to the programs in the book. In order to understand what is going on, it will be necessary to work through section 4.1 before starting on this assignment.

Although Object-Oriented programming has become very popular, the design of languages to sup- port Object-Oriented programming is still an area of active research. In this problem set, you will be dealing with issues that are not well-understood, and around which there are major disagree- ments among language designers. The questions in this problem set will not ask you to supply \right answers." Instead, they will ask you to make reasonable design choices, and to be able to defend these choices. We hope you will appreciate that, once you have come to grips with the notion of an interpreter, you are in a position to address major issues in language design, even issues that are at the forefront of current research.1 Tutorial exercise 1: Do exercise 4.2 of the notes. Don't actually go to lab to implement this.

Just be able to explain precisely what procedures need to be modi ed, what new procedures need to be written, and what the code must do.

This problem set was developed by Hal Abelson, Greg McLaren, and David LaMacchia. It draws on a Scheme 1 implementation of Oaklisp by McLaren and a Scheme implementation of Dylan by Jim Miller. The organization of the generic function code follows the presentation of the Common Lisp Object System (CLOS) in The Art of the Metaobject Protocol, by Gregor Kiczales, Jim des Rivieres, and Dan Bobrow (MIT Press, 1991).

6.001, Sample assignment|Object-oriented programming 2

1. Issues in object-oriented language design We've already seen two di erent approaches to implementing generic operations. One is datadirected programming, which relies on a table to dispatch based on the types of arguments. The second method, message-passing, represents objects as procedures with local state. As we saw in problem set 5, these objects can be arranged in complex inheritance relationships, such as \a troll is a kind of person."

One drawback with both of these approaches is that they make a distinction between generic operations and ordinary procedures, or between message-passing objects and ordinary data. This makes it awkward, for example, to extend an ordinary procedure so that it also works as a generic operation on new types of data. For instance, we might like to extend the addition operator + so that it can add two vectors, rather than having to de ne a separate vector-add procedure.

Recent experiments with object-oriented languages have attempted to integrate objects into the core of the language, rather than building an object system on top of the language. The idea is that everything in the language is an object, and all procedures are generic operations. Two such languages, both based upon Scheme, are Oaklisp, developed in 1986 by Kevin Lang and Barak Pearlmutter at CMU, and DylanTM (Dynamic Language), currently under development at the Apple Research Center in Cambridge. The language we will implement in this problem set is called MIT TOOL (Tiny Object Oriented Language). It is essentially a (very) simpli ed version of Dylan, designed to make the implementation an easy extension of the metacircular evaluator of chapter 4.

1.1 Classes, instances, and generic functions The framework we'll be using in TOOL (which is the same as in many object-oriented systems) includes basically the same ideas as we've already seen, although with di erent terminology. An object's behavior is de ned by its class|the object is said to be an instance of the class. All instances of a class have identical behavior, except for information held in a set of speci ed slots, which provides the local state for the instance. Following Dylan, we'll use the convention of naming classes with names that are enclosed in angle brackets, for example account or number.2 The define-class special form creates a new kind of class. You specify the name of the class, the class's superclass, and the names for the slots. In TOOL, every class has a superclass, whose behavior (and slots) it inherits. There is a prede ned class called object that is the most general kind of object. Every TOOL class has object as an ancestor. Once you have de ned a class, you use the special form make to create instances of it. Make takes the class as argument, together with a list that speci es values for the slots. The order in which the slots and values are listed does not matter, since each slot is identi ed by name. For example, we can specify that a \cat" is a kind of object that has a size and a breed, and then create an instance of cat. Note the use of the get-slot procedure to obtain the value in a designated slot.

Keep in mind that this use of brackets is a naming convention only|like naming predicates with names that end 2

–  –  –

TOOL== (define garfield (make cat (size 6) (breed 'weird))) *undefined* as in Scheme, define returns the undefined value TOOL== (get-slot garfield 'breed) weird

Procedures in TOOL are all generic-functions, de ned with the special form make-generic-function:

TOOL== (define-generic-function 4-legged?) (defined generic function: 4-legged?) You can think of a newly de ned generic function as an empty table to be lled in with methods.

You use define-method to specify methods for a generic function that determine its behavior on various classes.

TOOL== (define-method 4-legged? ((x cat)) true) (added method to generic function: 4-legged?)

–  –  –

TOOL== (4-legged? garfield) #t TOOL== (4-legged? 'Hal) who-knows?

The list in define-method following the generic function name is called the list of specializers for the method. This is like an argument list, except that it also speci es the class of each argument.

In the rst example above, we de ne a method for 4-legged? that takes one argument named x, where x is a member of the class cat. In the second example, we de ne another method for 4-legged? that takes one argument named x, where x is a member of the class object. Now 4-legged? will return true if the argument is a cat, and will return who-knows? if the argument is an object. Notice that garfield is an object as well as a cat (because object is the superclass of cat). Yet, when we call 4-legged? with garfield as an input, TOOL uses the method for cat, and not the method for object. In general, TOOL uses the most speci c method that applies to the inputs.3 In a similar way, we can de ne a new generic function say and give it a method for cats (and

subclasses of cats):

See the code (below) for a de nition of \most speci c method." This is one of the things that language designers 3

–  –  –

TOOL== (define-method say ((cat cat) (stuff object)) (print 'meow:) print is TOOL's procedure for printing things (print stuff)) (added method to generic function: say) TOOL== (define-class house-cat cat address) (defined class: house-cat) TOOL== (define fluffy note that a house cat is a cat, and therefore (make house-cat has slots for breed and size, as well (size 'tiny))) as for address TOOL== (get-slot fluffy 'breed) *undefined* we never initialized fluffy's breed TOOL== (say garfield '(feed me))


(feed me) TOOL== (say fluffy '(feed me))


(feed me) TOOL== (say 'hal '(feed me)) No method found -- APPLY-GENERIC-FUNCTION In the nal example, TOOL gives an error message when we apply say to the symbol hal. This is because hal is a symbol (not a cat) and there is no say method de ned for symbols.

We can go on to de ne a subclass of cat:

TOOL== (define-class show-cat cat awards) (defined class: show-cat) TOOL== (define-method say ((cat show-cat) (stuff object)) (print stuff) (print '(I am beautiful))) (added method to generic function: say)

–  –  –

TOOL== (say cornelius-silverspoon-the-Third '(feed me)) (feed me) (i am beautiful) TOOL== (define-method say ((cat cat) (stuff number)) (print '(cats never discuss numbers))) (added method to generic function: say) TOOL== (say fluffy 37) (cats never discuss numbers) As the nal example illustrates, TOOL picks the appropriate method for a generic function by examining the classes of all the arguments to which the function is applied. This di ers from the 6.001, Sample assignment|Object-oriented programming 5 message-passing model, where the dispatch is done by a single object.

Notice also that TOOL knows that 37 is a member of the class number. In TOOL, every data object is a member of some class. The classes number, symbol, list, and procedure are prede ned, with object as their superclass. Also, every procedure is a generic procedure, to which you can add new methods. The following generic procedures are prede ned, each initially

with a single method as indicated by the specializer:

+ (number number)

- (number number) * (number number) / (number number) = (number number) (number number) (number number) sqrt (number) cons (object object) append (list list) car (list) cdr (list) null? (object) print (object) get-slot (object symbol) set-slot! (object symbol object)

–  –  –

Multiplying a number times a vector, or a vector times a number, should scale the vector by the number. Adding a vector plus a number is not de ned. Also de ne a generic function length, such that the length of a vector is its length and the length of a number is its absolute value.

2. The TOOL Interpreter A complete listing of the TOOL interpreter is appended to this problem set. This section leads you through the most important parts, describing how they di er from the Scheme evaluator in chapter 4.

–  –  –

New data structures A class is represented by a data structure that contains the class name, a list of slots for that class, and a list of all the ancestors of the class. For instance, in our cat example above, we would have a class with the name house-cat, slots (address size breed), and superclasses (cat object). Note that the slot names include all the slots for that class (i.e., including the slots for the superclass). Similarly, the list of ancestors of a class includes the superclass and all of its ancestors.

A generic function is a data structure that contains the name of the function and a list of the methods de ned for that function. Each method is a pair|the specializers and the resulting procedure to use. The specializers are a list of classes to which the arguments must belong in order for the method to be applicable. The procedure is represented as an ordinary Scheme procedure.

Omitting lambda takes away our ability to have unnamed procedures, as we do in Scheme. You might want to 4 think about how to add such a feature to TOOL.

6.001, Sample assignment|Object-oriented programming 7 An instance is a structure that contains the class of the instance and the list of values for the slots.

See the attached code for details of the selectors and constructors for these data structures.

–  –  –

is handled by the following procedure:

(define (eval-generic-function-definition exp env) (let ((name (generic-function-definition-name exp))) (let ((val (make-generic-function name))) (define-variable! name val env) (list 'defined 'generic 'function: name)))) This procedure extracts the name portion of the expression and calls make-generic-function to create a new generic function. Then it binds name to the new generic function in the given environment. The value returned is a message to the user, which will be printed by the read-evalprint loop.

Eval-define-method handles the special form

–  –  –

for example (define-method say ((cat cat) (stuff number)) (print '(cats never discuss numbers))) In general here, generic-function is the generic function to which the method will be added, paramsand-classes is a list of parameters for this method and the classes to which they must belong, and body is a procedure body, just as for an ordinary Scheme procedure.5 The syntax procedures for this form include appropriate procedures to select out these pieces (see the code).

Eval-define-method rst nds the generic function. Notice that the generic-function piece of the expression must be evaluated to obtain the actual generic function. Eval-define-method disassembles the list of params-and-classes into separate lists of parameters and classes. The parameters, the body, and the environment are combined to form a procedure, just as in Scheme. The classes become the specializers for this method. Finally, the method is installed into the generic function.

The dot before the word \body" signi es that we can put more than one expression in the body|just as with 5

–  –  –

Tutorial exercise 3: Eval-define-method calls paramlist-element-class in order to nd the class for each parameter. Without looking at the attached code, predict whether paramlist-element-class should call tool-eval. Now look at the code and see if you were right. Give a careful explanation of why tool-eval is (or is not) called, and what di erence this makes.

–  –  –

Pages:   || 2 |

Similar works:

«Topic Significance Ranking of LDA Generative Models Loulwah AlSumait1 Daniel Barbar´1 James Gentle2 Carlotta Domeniconi1 a 1 Department of Computer Science, George Mason University, Fairfax VA 22030, USA 2 Department of Computational and Data Sciences, George Mason University, Fairfax VA 22030, USA Abstract. Topic models, like Latent Dirichlet Allocation (LDA), have been recently used to automatically generate text corpora topics, and to subdivide the corpus words among those topics. However,...»

«D O C U M E N T D E T R AVA I L LE TRAVAIL : NORME ET SIGNIFICATION YOLANDE BENARROSH N° 04 octobre 2000 «LE DESCARTES I» 29, PROMENADE MICHEL SIMON 93166 NOISY-LE-GRAND CEDEX TÉL. 01 45 92 68 00 FAX 01 49 31 02 44 MÉL. cee@cee.enpc.fr http://www.cee-recherche.fr Le travail : norme et signification YOLANDE BENARROSH Centre d'études de l'emploi et Centre d'études et de recherches sur les qualifications DOCUMENT DE TRAVAIL N° 04 octobre 2000 Ce texte a été soumis au comité éditorial...»

«PUBLISHED UNITED STATES COURT OF APPEALS FOR THE FOURTH CIRCUIT No. 12-4486 UNITED STATES OF AMERICA, Plaintiff Appellee, v.JAMAAL ANTONIO ROBERTSON, Defendant Appellant. Appeal from the United States District Court for the Middle District of North Carolina, at Greensboro. Thomas D. Schroeder, District Judge; Catherine C. Eagles, District Judge. (1:11-cr00296-CCE-1) Argued: September 20, 2013 Decided: December 3, 2013 Before GREGORY and DUNCAN, Circuit Judges, and Samuel G. WILSON, United...»

«-WARNINGS AND PRECAUTIONS HIGHLIGHTS OF PRESCRIBING INFORMATION • Serious These highlights do not include all the information needed to use and potentially fatal cardiovascular thrombotic events, myocardial PENNSAID® Topical Solution safely and effectively. See full prescribing infarction, and stroke can occur with NSAID treatment. Use the lowest information for PENNSAID Topical Solution. effective dose of PENNSAID Topical Solution in patients with known CV disease or risk factors for CV...»

«Gary Gutting FOUCAULT A Very Short Introduction 1 3 Great Clarendon Street, Oxford o x 2 6 d p Oxford University Press is a department of the University of Oxford. It furthers the University’s objective of excellence in research, scholarship, and education by publishing worldwide in Oxford New York Auckland Cape Town Dar es Salaam Hong Kong Karachi Kuala Lumpur Madrid Melbourne Mexico City Nairobi New Delhi Shanghai Taipei Toronto With offices in Argentina Austria Brazil Chile Czech Republic...»

«33 GEORGE AND HELEN WAITE PAPASHVILY: THE FIRST DAY The First Day * * GEORGE AND HELEN WAITE PAPASHVILY George Papashvily and his American wife collaborated on Anything Can Happen, a book recounting George's experiences as,an immigrant from Georgia, one of the republics of the former Soviet Union, to the United States shortly after World War I (1914-1918). AT FIVE IN THE MORNING THE ENGINES STOPPED, AND AFTER THIRTY-SEVEN days the boat was quiet. We were in America. I got up and stepped over...»

«SUBURBAN REVISIONS A Thesis Presented to The Academic Faculty By Alyssa Shank Durden In Partial Fulfillment Of the Requirements for the Degree Master of Architecture I Georgia Institute of Technology August, 2005 SUBURBAN REVISIONS Approved by: Richard Dagenhart, Advisor College of Architecture Michael Dobbins College of Architecture Michael Gamble College of Architecture Date Approved: May 16, 2005 ii to my grandmother, Mary Ellen Shank, who inspired me to write and encouraged me to draw since...»

«XR Flight Operations Manual Version 2.7 Publication Date: 20-Aug-2016 Vessel Versions: XR5 1.10 / XR1 1.12 / XR2 1.7 Copyright 2006-2016 Douglas Beachy. All Rights Reserved. This software is freeware and may not be sold. Web: http://www.alteaaerospace.com Email: mailto:dougb@alteaaerospace.com Orbiter Forum: dbeachy1 (http://orbiter-forum.com) XR Flight Operations Manual Version 2.7 1 Copyright 2006-2016 Douglas Beachy. All Rights Reserved. Table of Contents DG-XR1 Development Team XR5 Vanguard...»

«Juniper Automates Service Delivery with NFV and Cloud CPE Publication Date: 25 Nov 2015 | Product code: TE0006-001145 David Krozier Juniper Automates Service Delivery with NFV and Cloud CPE Ovum view Summary On November 3, 2015, Juniper announced its Cloud CPE solution. Cloud CPE is a networking framework based on NFV and cloud technology that delivers virtualized network and security services on demand from the cloud to the customer edge. With Cloud CPE service providers can select either a...»

«HIGHLIGHTS OF PRESCRIBING INFORMATION -WARNINGS AND PRECAUTIONS -­  Serious and potentially fatal cardiovascular thrombotic events, These highlights do not include all the information needed to use ® PENNSAID safely and effectively. See full prescribing myocardial infarction, and stroke can occur with NSAID treatment. information for PENNSAID. Use the lowest effective dose of PENNSAID in patients with known CV disease or risk factors for CV disease. (5.1) PENNSAID (diclofenac sodium...»

«when co-administered with other serotonergic agents (including triptans, HIGHLIGHTS OF PRESCRIBING INFORMATION tricyclic antidepressants, fentanyl, lithium, tramadol, tryptophan, These highlights do not include all the information needed to use Savella buspirone and St. John’s Wort). If such symptoms occur, discontinue safely and effectively. See full prescribing information for Savella. Savella and initiate supportive treatment. If concomitant use of Savella with other serotonergic drugs is...»

«FRACTURES OF THE ANTERIOR FOSSA OF THE SKULL Lecture delivered at the Royal College of Surgeons of England on 22nd April, 1952 by Wylie McKissock, O.B.E., F.R.C.S. Surgeon, National Hospital, Queen Square, London FAR TOO MUCH weight has beenattached in the past to the presence of a fracture of the skull. A mere linear fracture of the vault or base is of no importance in assessing the degree of temporary or permanent damage to the individual concerned. Only when such a fracture involves other...»

<<  HOME   |    CONTACTS
2016 www.dissertation.xlibx.info - Dissertations, online materials

Materials of this site are available for review, all rights belong to their respective owners.
If you do not agree with the fact that your material is placed on this site, please, email us, we will within 1-2 business days delete him.