Page 1 of 1

[REL] DEFSTAR - painless type declaration library

Posted: Fri Feb 05, 2010 8:03 pm
by eeeickythump
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:

Code: Select all

;; A function that takes two reals as arguments, and returns a real.
(defun* (sum -> real) ((a real) (b real))
   (+ a b))
Macroexpands to:

Code: Select all

(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

Code: Select all

(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

Posted: Fri Feb 05, 2010 11:58 pm
by dmitry_vk
Seems useful.
Minor note: is example on http://bitbucket.org/eeeickythump/defstar/wiki/Home right? Shouldn't "((test (function (t))) nil)" be "((test (or null (function (t)))) nil)"?

Re: [REL] DEFSTAR - painless type declaration library

Posted: Sat Feb 06, 2010 12:09 am
by eeeickythump
You are right.