Revise Metaobject Protocol Essay
authorclinton <clinton@unknownlamer.org>
Fri, 26 Sep 2008 19:25:53 +0000 (19:25 +0000)
committerclinton <clinton@unknownlamer.org>
Fri, 26 Sep 2008 19:25:53 +0000 (19:25 +0000)
Fixed a number of style issues, clarified some points, and moved
examples to their proper sections. All in all this is much more
readable now.

Metaobject Protocols.muse

index 550a8b9..ebed29f 100644 (file)
@@ -24,8 +24,7 @@ significantly more powerful as it encourages complete encapsulation
 through its use of classes primarily for method specialization rather
 than for state storage.
 
-
-*** Classes for scratch data and types
+*** Classes for Scratch Data and Types
 
 In CLOS classes store data in slots (which are the same as data
 members). Encapsulation is not provided; any bit of code can use
@@ -33,7 +32,7 @@ members). Encapsulation is not provided; any bit of code can use
 first, but encapsulation is of questionable importance as the slots
 are meant only to be used by the protocol defined around the class.
 
-Classes are defined with defclass
+Classes are defined with =defclass=
 
 <src lang="lisp">
 (defclass name (superclasses ...)
@@ -48,21 +47,22 @@ Classes are defined with defclass
   ((bar :accessor bar-of :initform (list 1 2 3))))
 </src>
 
-Slot defintions have several option; the above example shows only the
+Slot defintions have several options; the above example shows only the
 =:accessor= and =:initform= options which are the most commonly
 used. =:accessor= generates an accessor for the slot (e.g. if you have
-an instance of =example= you can =(setf (foo-of some-example-instance) 'some-value)= to set and =(foo-of some-example-instance)= to access the
+an instance of =example= you can =(setf (foo-of some-example-instance)
+'some-value)= to set and =(foo-of some-example-instance)= to access the
 value). =:initform= provides a default initial value for the slot as a
-symbolic expression to be evaluated when an instance is created.
+symbolic expression to be evaluated when an instance is created in the
+lexical environment of the class definition.
 
-*** Generics with methods that implement protocols
+*** Generics with Methods that Implement Protocols
 
 Generics are like normal functions in Lisp, but they only provide a
 lambda list (parameter list). Methods are added to the generic which
-specialize on the types of their parameters, and provide the actual
-implementation. This allows for rich layered protocols to be developed
-which can enable selective modification of individual facets with
-minimal code.
+specialize on the types of their parameters and provide an
+implementation. This allows writing rich layered protocols which can
+enable selective modification of individual facets with minimal code.
 
 <src lang="lisp">
 (defgeneric generic (parameters ...)
@@ -89,45 +89,47 @@ reasons outside of the scope of this presentation.
 * Limitations of Default Language Behavior
 
 The behavior of a language is a compromise between many competing
-issues that attempts to be as generally useful as possible, and most
-applications will have no issue with the default behavior. There are,
-however, certain applications that could be cleanly written with minor
-modifications to the behavior of the language, but would be impossible
-or quite difficult to write otherwise.
+issues that attempts to be as generally useful as possible so that
+*most* applications will have no issue with the default behavior. There
+are, however, certain applications that could be cleanly written with
+minor modifications to the behavior of the language, but would be
+impossible or quite difficult to write otherwise.
 
 ** Slot Storage
 
 Most languages choose to preallocate storage for all of the slots of
-an instance. Imagine a contact database that stored information about
-people as slots of the class. There may be dozens of slots, but often
-many of them will be left blank. If slot storage is preallocated much
-memory will be wasted and the system may not be able to fit into the
-memory of the hardware it must run on (perhaps for financial reasons,
-huge datasets, etc.).
+an instance. Now imagine a contact database that stores information
+about people in slots of a class. There may be dozens of slots, but
+often many of them will be left blank. If slot storage is preallocated
+much memory will be wasted and the database may not be able to fit
+into the memory of the hardware it must run on (perhaps for financial
+reasons, huge datasets, etc.).
 
 To save memory the author of the contact database must implement his
 own system to store properties and allocate them lazily. This
 represents a fair bit of effort, and would implement a system that
-differed from the existing slot system of classes only in the method
-of data storage.
+differed from the existing slot system of classes only regarding slot
+storage.
 
-It would be useful if there were a way to customize instance
-allocation. The customizations would be minor and require overriding
+It would be useful if there were a way to customize slot allocation in
+instances. The customizations would be minor and require overriding
 only the initial allocation behavior and the behavior of the first
 assignment to the slot. It is a a trivial problem in a language that
-allows customization of these.
+allows customization of these behaviors.
 
 ** Design Patterns
 
 Design Patterns are generalized versions of common patterns found in
 programs. Many of them are merely methods to get around deficiencies
 in the language, and can be quite messy to implement in some
-languages.
+languages. Ideally a pattern would be subsumed by the language, but
+real world contraints require language standards to remain fairly
+static.
 
 * Metasoftware
 
 Some types of programs could be written easily if the language were
-customizable, but are nearly impossible to write when it is not.
+customizable but are nearly impossible to write when it is not.
 
 ** Runtime Generated Classes
 
@@ -135,153 +137,52 @@ Say you wanted to write a video game where players could create their
 own objects, attach behaviors to the objects, and perhaps mix
 different objects together to create new ones. When you abstract the
 problem this looks just like an object system!  Wouldn't it be nice if
-your program could create new objects and methods on the fly portably?
+your program could create new classes and methods on the fly portably?
 
 ** Object Inspection
 
-Imagine if you were developing a complicated program with many
-different objects that interacted in fairly complex ways. A tool to
-inspect the structure of objects while debugging would be quite
-useful, but in a traditional language would be impossible to implement
-portably. This could force you to purchase a certain compiler
-implementation which provided one, and even then would more than
-likely not be customizable.
+Imagine you were developing a complicated program with many different
+objects that interacted in fairly complex ways. A tool to inspect the
+structure of objects while debugging would be quite useful, but in a
+traditional language would be impossible to implement portably. This
+could force you to purchase a certain compiler implementation which
+provided an inspector, and even then would likely not be customizable.
 
 This problem can be generalized to apply to most debugging tools; it
 would be useful to write such tools portably because users of the
 *language* and not the *compiler* need to debug software. Sharing
 infrastructure would result in better tools (more developers), and
-save man-years of wasted effort that comes with having to rewrite
-non-portable functionality from scratch multiple times.
+save the man-years of wasted effort that comes with having to rewrite
+unportable tools from scratch multiple times.
 
 * Metaobject Protocols
 
 ** Limited/Generalized Internals of the Implementation
 
-A Metaobject protocol is a generalized and limited subset of the
-underlying implementation of the language. It is generalized and
-limited in scope to allow for multiple implementation strategies;
-this, along with careful design, is essential because programming
-language research is ever advancing and new techniques for creating
-more reliable and faster implementations are still being discovered.
+A Metaobject Protocol (MOP) is a generalized and limited subset of the
+underlying language implementation. It is limited to allow multiple
+implementation strategies; this, along with careful design, is
+essential because programming language research is ever advancing and
+new techniques for creating more reliable and faster implementations
+are still being discovered.
 
 This subset of the implementation is exported as a set of methods on
-metaobjects. Thus the system is implemented in itself. The system can
-then be customized using the extension and overriding features of the
-system.
+metaobjects. Thus the language is implemented in itself. The system
+can then be customized using the extension and overriding features of
+the language itself.
 
 ** Classes of MOPs
 
 *** Reflective
 
-A reflective MOP provides a functional/procedural interface to
-information about the system. It exposes class relationships, the
-methods are attached to a generic, etc. A reflective MOP often
-provides some functionality for creating new classes at runtime.
+A reflective MOP provides an interface to information *about* the
+running system. It exposes class relationships, the methods attached
+to a generic, etc. A reflective MOP often provides some functionality
+for creating new classes at runtime. Smalltalk was one of the first
+languages to expose a reflective MOP.
 
 **** Example: Object Inspector
 
-**** Example: Runtime Generated Classes and Methods
-
-*** Intercessory
-
-Intercessory MOPs allow the user to customize language behavior by
-implementing methods which override certain aspects of the language
-behavior. This class of MOPs are what make MOPs especially
-powerful. No longer must a problem be restructured to fit the
-implementation language; the underyling language can be reshaped to
-fit the task at hand, and obfuscation of the intended structure of the
-application can be avoided.
-
-**** Example: Lazily Allocated Slots
-
-**** Example: Observer Design Pattern
-
-** Violation of Encapsulation?
-
-A MOP may seem like a violation of encapsulation by revealing some
-implementation details, but in reality a well designed protocol does
-not reveal anything which was not already exposed. Implementation
-decisions affect users, and some of these details do leak through to
-higher levels (e.g. the memory layout of slots). Implicit in the
-protocol specification are these implementation details, and the MOP
-merely makes this limited subset available for customization.
-
-A MOP makes it possible to customize certain implementation decisions
-that do not **radically** alter the behavior of the base language. The
-conceptual vocabulary of the system retains its meaning, and so code
-written in one dialect can interact with code written in another
-without knowing that they speak different ones.
-
-* MOP Design Principles
-
-** Layered Protocol
-
-A layered protocol design is good for both meta and normal object
-protocols, and enables a combinatorial explosion of customizations to
-the protocol.
-
-*** Top level **must** call lower level functions
-
-The top level methods of a layered metaobject protocol are required to
-call certain methods to perform some tasks. This both makes it easier
-to customize the top level methods (which perform very broad tasks) by
-providing some pieces of them for the programmer, and allows more
-customization to be done by opening up the replacement of lower level
-functions as a way to alter a small detail of the high level behavior.
-
-*** Lower level methods are easier to customize
-
-The lower level methods of a MOP are limited in scope and can be
-implemented easily. Often the changes to language behavior that are
-wanted are very small, and having methods that perform simple tasks
-which are often customized reduces the effort required to extend the
-system.
-
-** Functional Where Possible
-
-Functional protocols are preferred for MOPs (and object protocol in
-general). Functional protocols open up several optimizations for the
-implementation without burdening the user of the protocol.
-
-*** Memoization
-
-Memoization is the process of saving the results of a function call
-for future use. This avoids expensive recomputation of values which
-have not changed (recall that a true function will always return the
-same result when given the same arguments).
-
-A functional MOP can be optimized easily by exploiting this property
-to memoize the return values of calls to expensive operations. A MOP
-must be be very fast to avoid making programs unusably slow, and
-memoization is able to give an appreciable speedup in many cases
-without an insignificant burden on memory usage.
-
-**** Constant Shared Return Values
-
-Disallowing the modification of values returned by protocol methods
-allows the implementation to return large data structures by reference
-to avoid expensive copying without having to do expensive data
-integrity checks.
-
-*** Cleaner Code
-
-** Procedural Only Where Neccesary
-
-Some operations like method invocation are inheretly stateful and so
-must use a procedural protocol. There is no benefit to be gained from
-using a functional protocol, and indeed an attempt would result in
-obtuse code that severely restricted the implementor. Do note that
-only a very small part of method invocation is stateful (the actual
-call), and most of it can be implemented functionally (e.g. computing
-the discriminating function).
-
-* Examples
-
-** Object Inspector
-
-A primitive portable object inspector is presented here.
-
 <src lang="lisp">
 (defgeneric example-inspect (instance)
   (:documentation "Simple object inspector using CLOS MOP"))
@@ -316,7 +217,21 @@ A primitive portable object inspector is presented here.
           (inspect-loop slots instance inspector)))))
 </src>
 
-** Observer Design Pattern
+**** Example: Runtime Generated Classes and Methods
+
+*** Intercessory
+
+Intercessory MOPs allow the user to customize language behavior by
+implementing methods which override certain aspects of the language
+behavior. This class of MOPs are what make MOPs especially
+powerful. No longer must a problem be restructured to fit the
+implementation language; the underyling language can be reshaped to
+fit the task at hand, and obfuscation of the intended structure of the
+application can be avoided.
+
+**** Example: Lazily Allocated Slots
+
+**** Example: Observer Design Pattern
 
 A simple implementation of the observer pattern is under 100 lines,
 and the user level code requires only a single line of code to make
@@ -427,7 +342,86 @@ details to the program.
                       observers))))))
 </src>
 
+
+** Violation of Encapsulation?
+
+A MOP may seem like a violation of encapsulation by revealing some
+implementation details, but in reality a well designed protocol does
+not reveal anything which was not already exposed. Implementation
+decisions affect users, and some of these details do leak through to
+higher levels (e.g. the memory layout of slots). Implicit in the
+protocol specification are these implementation details, and the MOP
+merely makes this limited subset available for customization.
+
+A MOP makes it possible to customize certain implementation decisions
+that do not **radically** alter the behavior of the base language. The
+conceptual vocabulary of the system retains its meaning, and so code
+written in one dialect can interact with code written in another
+without knowing that they speak different ones.
+
+* MOP Design Principles
+
+** Layered Protocol
+
+A layered protocol design is good for both meta and normal object
+protocols, and enables a combinatorial explosion of customizations to
+the protocol.
+
+*** Top Level **Must** Call Lower Level Methods
+
+The top level methods of a layered protocol are required to call
+certain lower level methods to perform some tasks. This both makes it
+easier to customize the top level methods (which perform very broad
+tasks) by providing some pieces of implementation for the programmer,
+and enables more customization by opening up the replacement of lower
+level functions as a way to alter a small detail of the high level
+behavior.
+
+*** Lower Level Methods are Easier to Customize
+
+The lower level methods of a MOP are limited in scope and can be
+implemented easily. Often the desired changes to language behavior are
+minor, and having methods that perform simple tasks which are often
+customized reduces the effort required to extend the system.
+
+** Functional Where Possible
+
+Functional protocols are preferred for MOPs (and object protocols in
+general). Functional protocols open up several optimizations for the
+implementation without burdening the user of the protocol.
+
+*** Memoization
+
+Memoization is the process of saving the results of a function call
+for future use. This avoids expensive recomputation of values which
+have not changed (recall that a true function will always return the
+same result when given the same arguments).
+
+A functional MOP can be optimized easily by exploiting this property
+to memoize the return values of calls to expensive operations. A MOP
+must be be very fast to avoid making programs unusably slow, and
+memoization is able to give an appreciable speedup in many cases
+without a significant burden on memory usage.
+
+*** Constant Shared Return Values
+
+Disallowing modification of values returned by protocol methods allows
+the implementation to return large data structures by reference to
+avoid expensive copying without having to do expensive data integrity
+checks or copying.
+
+** Procedural Only Where Neccesary
+
+Some operations like method invocation are inheretly stateful and so
+must use a procedural protocol. There is no benefit to be gained from
+using a functional protocol, and indeed an attempt would result in
+obtuse code that severely restricted the implementian. Do note that
+only a very small part of method invocation is stateful (the actual
+call), and most of it can be implemented functionally (e.g. computing
+the discriminating function).
+
 ** Real World
+
 *** [[http://common-lisp.net/project/ucw/][UCW]] and [[http://common-lisp.net/project/bese/arnesi.html][Arnesi]]
 
 Arnesi uses the CLOS MOP to implement methods which are transparantly
@@ -448,14 +442,15 @@ implementing relational slots).
 *** [[http://common-lisp.net/project/elephant/][Elephant]]
 
 Elephant uses the CLOS MOP to transparantly store any class to disk
-and handle paging between the disk store and memory efficiently and
-with no user intervention.
+and handle paging between the disk store and memory efficiently
+without user intervention.
 
 * Sources & Further Reading
 
 ** Sources
 
 *** The Art of the Metaobject Protocol
+
 **** Kiczales, Gregor et al. MIT Press 1991
 
 Highly recommended reading even if you plan to never implement a MOP