Skip to content

Instantly share code, notes, and snippets.

@Bike
Created November 8, 2020 21:15
Show Gist options
  • Save Bike/0af84d2a3258d6e5f6d52fbdeb3c1a01 to your computer and use it in GitHub Desktop.
Save Bike/0af84d2a3258d6e5f6d52fbdeb3c1a01 to your computer and use it in GitHub Desktop.
lexical types CL language extension

Proposal "lexical types"

Problem Description:

Lisp does not have a mechanism to bind derived types lexically analogous to macrolet. This would be occasionally useful for some forms of metaprogramming. For example, Massimiliano Ghilardi's cl-parametric-types system uses "dumb" form rewriting (like sublis) to accomplish a similar task; actual lexical type definitions would represent an improvement.

Proposal:

Add the typelet and type special operators described below.

Edit the description of macrolet to add that typelet definitions affect the macrolet definitions, as macrolet and symbol-macrolet definitions do.

Edit 3.2.3.1 "Processing of Top Level Forms" to define that subforms of typelet may be processed as top level forms, analogously to macrolet and symbol-macrolet.

Rationale:

Clearly matches the existing behaviors of macrolet etc.

Cost to Implementors:

Implementors would have to add type information to lexical environments, and implement new special operators. These new special operators, like macrolet, can be dealt with entirely in the minimal compilation process and do not require representations in target code, so they may be easier to implement than most special operators.

Cost to Users:

Little to none. These could be safely ignored.

Cost of Non-Adoption:

The few users wanting lexical types would have to continue resorting to tricks.

Benefits:

Aesthetics:

Discussion:


Special Operator TYPELET

Syntax:

typelet ((name lambda-list [[local-declaration* | local-documentation]] local-form*)*) declaration* form* => result*

Arguments and Values:

name---a symbol.

lambda-list--a deftype lambda list.

local-declaration, declaration--- declare _expression_s; not evaluated.

local-documentation---a string; not evaluated.

local-forms, forms---_implicit progn_s.

results---the values of the forms.

Description:

typelet defines local derived type specifiers, and execute forms using the local definitions. Forms are executed in order of occurrence.

The meanings of the new type specifiers are given in terms of functions which expand type specifiers into other type specifiers, which themselves will be expanded if they contain references to other derived type specifiers.

The newly defined type specifier may be referenced as a list of the form (name arg1 arg2 ...). The number of arguments must be appropriate to the lambda-list. If the new type specifier takes no arguments, or if all of its arguments are optional, the type specifier may be used as an atomic type specifier.

The argument expressions to the type specifier, arg1 ... argn, are not evaluated. Instead, these literal objects become the objects to which corresponding parameters become bound.

The body of each definition's local forms (but not the lambda-list) is implicitly enclosed in a block named name, and is evaluated as an implicit progn, returning a new type specifier.

The macro-expansion functions defined by typelet are defined in the lexical environment in which the typelet form appears. Declarations and macrolet and symbol-macrolet and typelet definitions affect the local macro definitions in a typelet, but the consequences are undefined if the local macro definitions reference any local variable or function bindings that are visible in that lexical environment.

Recursive expansion of the type specifier returned as the expansion must terminate, including the expansion of type specifiers which are nested within the expansion.

The consequences are undefined if the result of fully expanding a type specifier contains any circular structure, except within the objects referred to by member and eql type specifiers.

Documentation is attached to name as a documentation string of kind type.

The scope of the declarations between the list of local type macro definitions and the body forms in typelet does not include the bodies of the locally defined functions.

Affected By: None.

Exceptional Situations: None.

See Also:

declare, deftype, documentation

Notes:

If a typelet form is a top-level form, the body forms are also processed as top level forms. See Section 3.2.3 (File Compilation).


Special Operator TYPE

Syntax:

type type-specifier => type

Arguments and Values:

type-specifier---a type specifier.

type---If proposal "reify types" is adopted, a type. If not, a type specifier.

Description:

Resolves a type specifier in the lexical environment. The result is independent of the environment, in the sense that all type macros have been fully expanded.

Side Effects: None.

Affected By: None.

Exceptional Situations: None.

See Also:

Notes:

If proposal "reify types" is adopted, this is the function to specifier-type's fdefinition.

Note that this operator is generally required for using lexical types. For example, (typep x '(foo ...)) will ignore any lexical definition of a foo type, but (typep x (type (foo ...))) will not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment