Advice (programming)

Last updated

In aspect and functional programming, advice describes a class of functions which modify other functions when the latter are run; it is a certain function, method or procedure that is to be applied at a given join point of a program.

Contents

Use

The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot or should not be readily modified or extended. For instance, the Emacspeak Emacs-addon makes extensive use of advice: it must modify thousands of existing Emacs modules and functions such that it can produce audio output for the blind corresponding to the visual presentation, but it would be infeasible to copy all of them and redefine them to produce audio output in addition to their normal outputs; so, the Emacspeak programmers define advice functions which run before and after.

For a simple Emacs example: suppose after a user corrected a mis-spelled word using the Emacs ispell module, they wanted to re-spellcheck the entire buffer. ispell-word offers no such functionality, even if the corrected word appears frequently in the buffer. The user could track down the definition of ispell-word, copy it into their personal Emacs files, and add the desired functionality there, but this is tedious and, worse, brittle (the user's version is now out of sync with the core Emacs implementation, if it even works without further refactoring). What the user wants is quite simple — just to run another command any time ispell-word runs. Using advice, it can be done as simply as this:

(advice-add#'ispell-word:after#'flyspell-buffer)

While this example is obviously trivial, the strength of advice, especially when compared to similar facilities such as Python decorators and Java annotations, lies in the fact that not only do the advised functions / methods not need to be designed to accept advice, but also the advice themselves need not be designed to be usable as advice - they're just normal functions. The availability of evaluation throughout the lifetime of a piece of code (cf. code staging) in Lisp allows advice to be inlined automatically into any other code in a variety of ways. Any piece of code can be advised to carry out any other computation before, after, around, or instead of its original definition.

Readability

Advice has the potential to introduce confusion, as a piece of advice applied to a function is not apparent to a user who tracks down the function's source definition to learn about it. In such cases, advice acts almost like a COMEFROM, a joke facility added to INTERCAL to spoof the spaghettification attendant to the extensive use of GOTOs. In practice, however, such issues rarely present themselves. Upstream developers and maintainers of Lisp packages and modules never use advice, since there is no advantage to be gained by advising functions when their original source definitions can be freely rewritten to include the desired features. Advice is only useful in that it enables downstream users to subsequently modify default behaviour in a way that does not require propagation of such modifications into the core implementation's source definition.

Implementations

A form of advices were part of C with Classes in the late 1970s and early 1980s, namely functions called call and return defined in a class, which were called before (respectively, after) member functions of the class. However, these were dropped from C++. [1]

Advices are part of the Common Lisp Object System (CLOS), as :before, :after, and :around methods, which are combined with the primary method under "standard method combination". [2]

Common Lisp implementations provide advice functionality (in addition to the standard method combination for CLOS) as extensions. LispWorks [3] supports advising functions, macros and CLOS methods.

EmacsLisp added advice-related code in version 19.28, 1994.

History

The following is taken from a discussion at the mailing list aosd-discuss. Pascal Costanza contributed the following:

The term "advice" goes back to the term advising as introduced by Warren Teitelman in his PhD thesis in 1966. Here is a quote from Chapter 3 of his thesis:

Advising is the basic innovation in the model, and in the PILOT system. Advising consists of inserting new procedures at any or all of the entry or exit points to a particular procedure (or class of procedures). The procedures inserted are called "advice procedures" or simply "advice".
Since each piece of advice is itself a procedure, it has its own entries and exits. In particular, this means that the execution of advice can cause the procedure that it modifies to be bypassed completely, e.g., by specifying as an exit from the advice one of the exits from the original procedure; or the advice may change essential variables and continue with the computation so that the original procedure is executed, but with modified variables. Finally, the advice may not alter the execution or affect the original procedure at all, e.g., it may merely perform some additional computation such as printing a message or recording history. Since advice can be conditional, the decision as to what is to be done can depend on the results of the computation up to that point.
The principal advantage of advising is that the user need not be concerned about the details of the actual changes in his program, nor the internal representation of advice. He can treat the procedure to be advised _as a unit_, a single block, and make changes to it without concern for the particulars of this block. This may be contrasted with editing in which the programmer must be cognizant of the internal structure of the procedure.

"Advising" found its way into BBN Lisp and later into Xerox PARC's Interlisp.

It also found its way to Flavors, the first object-oriented extension to Lisp developed at MIT. They were subsumed under the notion of method combination. [4] [lower-alpha 1]

Since method combination and macros are closely related, it's also interesting to note that the first macro system was described in 1963, three years before Warren Teitelman's PhD thesis. [5] [lower-alpha 2]

See also

Notes

Gregor Kiczales comments the above as follows:

  1. Advice appeared separately from Flavors in Maclisp and the Lisp Machine. You could advise any function, just like in Interlisp at the time. The before/after ontology appeared separately in Flavors methods.
  2. Method combination and macros were only marginally related until much later, in New Flavors and CLOS, when a macro-like mechanism was provided to allow people to define their own rules for combining methods. Prior to that the rules governing combination of before/after methods and so-called whoppers methods (around) was fixed, and the compiler just generated the code for that. There were things called wrappers, which had macro-like behavior, but I forget when they came around. Traipsing through the various versions of MacLisp and Lispm manual to get this part of the history exactly right could be interesting. Or it could be that Howard Cannon or David Moon or someone could actually remember it all exactly.

Related Research Articles

<span class="mw-page-title-main">Common Lisp</span> Programming language standard

Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ANSI INCITS 226-1994 (S2018). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived from the ANSI Common Lisp standard.

<span class="mw-page-title-main">Emacs Lisp</span> Dialect of Lisp used as the primary implementation and extension language for Emacs

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs. It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter. Emacs Lisp is also termed Elisp, although there are also older, unrelated Lisp dialects with that name.

<span class="mw-page-title-main">Lisp (programming language)</span> Programming language family

Lisp is a family of programming languages with a long history and a distinctive, fully parenthesized prefix notation. Originally specified in 1960, it is the third-oldest high-level programming language still in common use, after Fortran and COBOL. Lisp has changed since its early days, and many dialects have existed over its history. Today, the best-known general-purpose Lisp dialects are Common Lisp, Scheme, Racket, and Clojure.

<span class="mw-page-title-main">Macro (computer science)</span> Rule for substituting a set input with a set output

In computer programming, a macro is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion. The input and output may be a sequence of lexical tokens or characters, or a syntax tree. Character macros are supported in software applications to make it easy to invoke common command sequences. Token and tree macros are supported in some programming languages to enable code reuse or to extend the language, sometimes for domain-specific languages.

<span class="mw-page-title-main">Text editor</span> Computer software used to edit plain text documents

A text editor is a type of computer program that edits plain text. Such programs are sometimes known as "notepad" software. Text editors are provided with operating systems and software development packages, and can be used to change files such as configuration files, documentation files and programming language source code.

<span class="mw-page-title-main">Design by contract</span> Approach for designing software

Design by contract (DbC), also known as contract programming, programming by contract and design-by-contract programming, is an approach for designing software.

<span class="mw-page-title-main">Genera (operating system)</span> Symbolics operating system based on Lisp

Genera is a commercial operating system and integrated development environment for Lisp machines created by Symbolics. It is essentially a fork of an earlier operating system originating on the Massachusetts Institute of Technology (MIT) AI Lab's Lisp machines which Symbolics had used in common with Lisp Machines, Inc. (LMI), and Texas Instruments (TI). Genera was also sold by Symbolics as Open Genera, which runs Genera on computers based on a Digital Equipment Corporation (DEC) Alpha processor using Tru64 UNIX. In 2021 a new version was released as Portable Genera which runs on Tru64 UNIX on Alpha, Linux on x86-64 and Arm64 Linux, and macOS on x86-64 and Arm64. It is released and licensed as proprietary software.

In computer programming, the scope of a name binding is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name.

<span class="mw-page-title-main">Common Lisp Object System</span>

The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object systems such as MIT Flavors and CommonLoops, although it is more general than either. Originally proposed as an add-on, CLOS was adopted as part of the ANSI standard for Common Lisp and has been adapted into other Lisp dialects such as EuLisp or Emacs Lisp.

In object-oriented programming languages, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".

Flavors, an early object-oriented extension to Lisp developed by Howard Cannon at the MIT Artificial Intelligence Laboratory for the Lisp machine and its programming language Lisp Machine Lisp, was the first programming language to include mixins. Symbolics used it for its Lisp machines, and eventually developed it into New Flavors; both the original and new Flavors were message passing OO models. It was hugely influential in the development of the Common Lisp Object System (CLOS).

<span class="mw-page-title-main">GNU Guile</span> Extension Language

GNU Ubiquitous Intelligent Language for Extensions is the preferred extension language system for the GNU Project and features an implementation of the programming language Scheme. Its first version was released in 1993. In addition to large parts of Scheme standards, Guile Scheme includes modularized extensions for many different programming tasks.

DWIM computer systems attempt to anticipate what users intend to do, correcting trivial errors automatically rather than blindly executing users' explicit but potentially incorrect input.

Emacspeak is a free computer application, a speech interface, and an audio desktop. It employs Emacs, Emacs Lisp, and Tcl. Developed principally by T. V. Raman, it was first released in April 1995. It is portable to all POSIX-compatible OSs. It is tightly integrated with Emacs, allowing it to render intelligible and useful content rather than parsing the graphics ; its default voice synthesizer can be replaced with other software synthesizers when a server module is installed. Emacspeak is one of the most popular speech interfaces for Linux, bundled with most major distributions. In 2014, Raman wrote an article describing how the software's design was impacted by shifts in computer technology and its general usage over 20 years.

CommonLoops is an early programming language which extended Common Lisp to include Object-oriented programming functionality and is a dynamic object system which differs from the OOP facilities found in static languages such as C++ or Java. Like New Flavors, CommonLoops supported multiple inheritance, generic functions and method combination. CommonLoops also supported multi-methods and made use of metaobjects. CommonLoops and New Flavors were the primary ancestors of CLOS. CommonLoops was supported by a portable implementation known as Portable CommonLoops (PCL) which ran on all Common Lisp implementations of the day.

Warren Teitelman was an American computer scientist known for his work on programming environments and the invention and first implementation of concepts including Undo / Redo, spelling correction, advising, online help, and DWIM (Do What I Mean).

<span class="mw-page-title-main">GNU Emacs</span> GNU version of the Emacs text editor

GNU Emacs is a free software text editor. It was created by GNU Project founder Richard Stallman, based on the Emacs editor developed for Unix operating systems. GNU Emacs has been a central component of the GNU project and a flagship project of the free software movement. Its tag line is "the extensible self-documenting text editor."

Emacs, originally named EMACS, is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, self-documenting, real-time display editor". Development of the first Emacs began in the mid-1970s, and work on GNU Emacs, directly descended from the original, is ongoing; its latest version is 29.2, released January 2024.

<span class="mw-page-title-main">Alice K. Hartley</span> American computer scientist (1937–2017)

Alice Hartley (1937–2017) was an American computer scientist and business woman. Hartley worked on several dialects of Lisp, implementing multiple parts of Interlisp, maintaining Macintosh Common Lisp, and developing concepts in computer science and programming language design still in use today.

References

  1. The Design and Evolution of C++, p. 57
  2. "A Brief Guide to CLOS". Archived from the original on 2015-05-06. Retrieved 2015-04-27.
  3. LispWorks 7 User Guide and Reference Manual, The Advice Facility
  4. See, for example, AIM-602 at https://web.archive.org/web/20060913001624/http://www.ai.mit.edu/research/publications/browse/0600browse.shtml
  5. See AIM-57 at https://web.archive.org/web/20060913001624/http://www.ai.mit.edu/research/publications/browse/0000browse.shtml