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


Definition Statements

This is a summary of the standard definition statements in PLOT. Their detailed syntax is given in later sections.

def defines a value. It specifies a name, optionally a type, an = or := sign, and an expression. The name becomes defined to the value of the expression. Using = creates a fixed definition. Using := creates an assignable definition. All definitions other than assignable value definitions are fixed.

The left-hand side can also be an invocation of a constructor that has a corresponding destructuring macro.

Annotations can be inserted after the = or := sign.

defun defines a method. It specifies a function name, a parameter list in parentheses, optionally is and the result type, and a body. A method with the specified parameters and body is added to the function that is the definition of the name. If a definition of the name does not already exist in the current scope (not inherited from an enclosing scope; imported from another module is okay), a new function definition is created. The parenthesized parameter list can be followed by := and one more parameter to define a setter function.

A call to a prefix or infix operator or operator macro can be used in place of the function name and parameter list, so in fact defun is followed by a prototype function call with stylized arguments that specify the names and types of the method parameters, and then the body of the method.

Annotations can be inserted before the body.

When re-executing a method definition after editing it to change its parameter list, it is the responsibility of the development environment to distinguish between adding a new method and replacing a method while changing its parameters.

defmacro defines a macro, which is a syntax extension. It specifies a name, one or more syntax patterns, and for each pattern an expression that returns the expansion of the macro.
defsyntax defines a syntactic type, with one or more patterns for parsing it. This can be invoked from other syntax patterns.
defparser defines one or more syntax patterns that can be invoked as a kind of subroutine from other syntax patterns.
defoperator defines an operator, which can be invoked with prefix or infix syntax. It specifies the name of the operator, some attributes such as its precedence, and optionally macros to parse the prefix and/or infix forms of invocations of the operator.
defmodule defines a module, specifying its name, exports, and from which other modules it imports definitions.
defclass defines a class of objects. It specifies a constructor and initializer parameter list, inheritance from another class and from zero or more protocols, and a set of slots which have values in each instance of the class.

Annotations can be inserted after the inheritance specification and after each slot specification.

defprotocol defines a protocol type and a set of related method requirements. A method requirement asserts that the function will always have a method for a specific set of argument types and the result of that method will always be a member of a specified type. A protocol is a type whose semantics come from the methods and method requirements that have parameters of that type.
require specifies a method requirement. This does not define any name.

All definition statements work in the global scope or in a local scope. If the name being defined is a name-in-context whose context is a module, the definition statement must be in the global scope and defines a global definition in the specified module. Otherwise, a definition statement in the global scope defines a global definition in the current module, while a definition statement in a local scope defines a local definition in that scope. Local defmacro and defoperator work because a known definition is recorded immediately before parsing the rest of the block. Local defmodule, defclass, and defprotocol are permitted but are probably useless.

Examples:

def pi = 3.14159

def [x1, y1] = transform(x, y)

def state := #Alabama

defun factorial(n) if n < 2 then 1 else n * factorial(n - 1)

defmacro exch(?a, ?b) =>
  `block
     def temp = ?a
     ?a := ?b
     ?b := temp`

defclass point(x, y)
  x = x
  y = y

;; The above can be abbreviated to
defclass point
  x
  y


Previous page   Table of Contents   Next page