TransWikia.com

Why does emacs use both propertly lists and association lists?

Emacs Asked by kdb on January 17, 2021

http://www.gnu.org/software/emacs/manual/html_node/elisp/Plists-and-Alists.html gives some supposed differences between plists and alists. Basically it comes down to:

  1. Alists can be used as stacks, where values are updated by adding a cons cell, allowing to restore previous values by deleting the topmost cons cell.
  2. Plists are faster for storing information about symbols because they are typically short compared to storing everything (for all symbols) in a single alist.

The first point makes sense. While the implementation of plist-get allows using plists in such a stack-like manner, by adding new values with

(push VALUE PLIST)
(push KEY PLIST)

removing such a pair would be more involved compared to alists where one can just write

(setq ALIST (delete (assq KEY ALIST) ALIST))

though even that could be changed by defining a function for it.

The second point however seems incorrect too me, as the same advantage would apply for alists, if there was a symbol-alist function instead of symbol-plist. Additionally, in my experience plists are typically a bit slower than alists of the same size.

Hence I was wondering why emacs uses both conventions for storing key-value pairs, when using one would suffice.

Clarification Since it was raised in the comments: It seems largely a historical issue (emacs lisp was able to do both well, so people did both), but I’d still be interested in whether there was some rationale in the development of emacs to include builtin functions for handling both. Of course my view may be strongly colored by the Zen of Python (13th line) here to even raise the question.

2 Answers

Fundamentally the explanation is historical. Lisp 1 stored the definition of a function on the function symbol's plist, and stored the values of all dynamically scoped variables (the only kind) in a big alist. It could have been done in any of the other three ways.

By convention, many people treat alists as persistent (mutation-free) whereas "disembodied" plists (that is, ones not attached to a symbol) are usually mutated.

Answered by John Cowan on January 17, 2021

The Emacs Lisp Manual gives a good explanation. Here's a brief summary:

  • If you are associating info with something that is already a Lisp function or variable, plists are faster because they're usually smaller and tied directly to the symbol. But the plist of a symbol is a global namespace, so different packages might conflict.
  • If you are trying to have general purpose information, an alist works better since keys can be anything, you can use it as a stack, and because different packages can have their own alists.

Association lists (*note Association Lists::) are very similar to property lists. In contrast to association lists, the order of the pairs in the property list is not significant, since the property names must be distinct.

Property lists are better than association lists for attaching information to various Lisp function names or variables. If your program keeps all such information in one association list, it will typically need to search that entire list each time it checks for an association for a particular Lisp function name or variable, which could be slow. By contrast, if you keep the same information in the property lists of the function names or variables themselves, each search will scan only the length of one property list, which is usually short. This is why the documentation for a variable is recorded in a property named `variable-documentation'. The byte compiler likewise uses properties to record those functions needing special treatment.

However, association lists have their own advantages. Depending on your application, it may be faster to add an association to the front of an association list than to update a property. All properties for a symbol are stored in the same property list, so there is a possibility of a conflict between different uses of a property name. (For this reason, it is a good idea to choose property names that are probably unique, such as by beginning the property name with the program's usual name-prefix for variables and functions.) An association list may be used like a stack where associations are pushed on the front of the list and later discarded; this is not possible with a property list.

Answered by Alan Shutko on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP