[REL] DEFSTAR - painless type declaration library

Discussion of Common Lisp
Post Reply
Posts: 9
Joined: Sun Jan 03, 2010 2:04 am

[REL] DEFSTAR - painless type declaration library

Post by eeeickythump » Fri Feb 05, 2010 8:03 pm

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
  • 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

       (DEFUN SUM (A B)
         (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.

Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan

Re: [REL] DEFSTAR - painless type declaration library

Post by dmitry_vk » Fri Feb 05, 2010 11:58 pm

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)"?

Posts: 9
Joined: Sun Jan 03, 2010 2:04 am

Re: [REL] DEFSTAR - painless type declaration library

Post by eeeickythump » Sat Feb 06, 2010 12:09 am

You are right.

Post Reply