This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

[REL] DEFSTAR - painless type declaration library

3 posts · 1275 views

Hi,
Since comp.lang.lisp seems to be slowly turning into a shoe bazaar, I thought I would announce this library here. Hopefully some people will find it useful.

DEFSTAR is a collection of macros that can be used as replacements for the forms DEFUN, DEFMETHOD, DEFGENERIC, DEFVAR, DEFPARAMETER, LABELS, FLET and LAMBDA. The names are the same as the forms they replace, with a star at the end, eg DEFUN*.

Repository: http://bitbucket.org/eeeickythump/defstar/

Main features are:
  • Easy in-line type declaration within lambda lists (no more separate DECLARE statements)
  • Easy declaration of the return type of a function -- also within the definition of the function
  • Assertions that check the type, value, or anything else about arguments prior to entering the function body, and prior to returning from the function
  • Replacements for DEFUN, DEFMETHOD, DEFGENERIC, LAMBDA, FLET, LABELS, DEFVAR, DEFPARAMETER
  • Supports &key, &optional, &rest and &aux arguments
  • Supports specialised lambda lists (defmethod)
  • Supports assertions and typechecking for functions that return multiple values.
Ridiculously trivial example:
;; A function that takes two reals as arguments, and returns a real.
(defun* (sum -> real) ((a real) (b real))
   (+ a b))
Macroexpands to:
(PROGN (DECLAIM (FTYPE (FUNCTION (REAL REAL) REAL) SUM))
       (DEFUN SUM (A B)
         (DECLARE (TYPE REAL B) (TYPE REAL A))
         (THE REAL (+ A B))))
A long while ago I used a library called TYPEDVAR-COMMON-LISP, written I believe by Bruno Haible, which did something similar to DEFSTAR, but did so by replacing the "CL" package, defining reader macros for square brackets, and code-walking through all toplevel forms. I found Bruno's library made it much more convenient and less error-prone to declare types, and therefore made me do it more often, thereby catching more bugs earlier. However, using a replacement for CL eventually became more trouble than it was worth as interoperability with other libraries, especially ones that code-walked such as screamer, became very difficult.

DEFSTAR does the same as TYPEDVAR-COMMON-LISP (in fact the assertions facility means it does more), but works solely through use of different forms: DEFUN*, LAMBDA*, etc. No reader macros and no code-walking. No dependencies other than ASDF (optional). It should play well with all other CL libraries. Just
(use-package :defstar) 
and you should be away.

DEFSTAR has mainly been tested in Clozure CL, and a bit on SBCL. All macros expand to completely uncontroversial CL so in theory it should be portable across all conformant implementations.

See http://bitbucket.org/eeeickythump/defstar/wiki/ for more details, examples and documentation.

Re: [REL] DEFSTAR - painless type declaration library

You are right.