Programming Language for Old Timers

by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.

Previous page   Table of Contents   Next page

Defun Statement

The defun statement is the second most commonly used defining statement in PLOT, hence an abbreviated name is justified. It defines a method for a function. The definition appears in the current scope, which can be local or global.

The defun statement first defines a function, unless the name is already defined in the current scope. The name must be defined as a function or an operator. Note that if the current scope is a local scope, this definition must be directly in the current scope, not inherited from an enclosing scope. defun will never add a method to a function inherited from a containing scope. Note that if the current scope is a module, the definition can be imported from another module. defun can add a method to an imported function.

Next, the defun statement creates a method and adds it to the function or operator. The value of the statement as a whole is the function or operator.

The syntax of the defun statement is as follows:

defmacro defun ?:method-head [ ?:annotations ] ?:block => ...

The following annotations are predefined for defun:

sealed This method must always be the most specific when applicable. The compiler can optimize based on the knowledge that this method cannot be overridden by a more specific method.
subsumptive This method has equivalent effect to any more specific method when both are applicable. This enables certain special cases of compiler optimization.
intrinsic(name) The compiler has special knowledge of this method under the specified name. The name is optional and defaults to the name of the function.

An example usage of subsumptive methods:

defun (x is simple-name) = (y is anything) : subsumptive
  x eq y
defun (x is anything) = (y is simple-name) : subsumptive
  x eq y
This says that = on simple-names is always eq, and nothing other than a simple-name can be = to a simple-name, without considering what other methods might be defined for =. The parentheses are unnecessary, because of operator precedence, but probably improve readability.

Previous page   Table of Contents   Next page