[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

type expansion

Jim Newton

11/13/2015 10:55:00 AM

What is the correct way to expand a type designator?
I believe the internals of the compiler (sbcl in this case) have ways of doing this,
but for debug purposes I'd like to look at how my type (via a deftype) expands given
different argument lists.

Also what is the correct terminology to use when describing it for documentation purposes?
For example, I may want to say something like the following, but I know it is not really correct.

The type T42 when given an argument X, (T42 X) expands to the type designator
(T41 X string) which in turn expands to (and string (satisfies X))

1 Answer

Pascal J. Bourguignon

11/13/2015 12:08:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> What is the correct way to expand a type designator?
> I believe the internals of the compiler (sbcl in this case) have ways of doing this,
> but for debug purposes I'd like to look at how my type (via a deftype) expands given
> different argument lists.

There's a very big hint in the clhs deftype page.

> Also what is the correct terminology to use when describing it for documentation purposes?
> For example, I may want to say something like the following, but I know it is not really correct.
>
> The type T42 when given an argument X, (T42 X) expands to the type designator
> (T41 X string) which in turn expands to (and string (satisfies X))

Did you see it?

It's "deftype lambda list".


The glossary says:

deftype lambda list n. a lambda list that is like a macro lambda list
except that the default value for unsupplied optional parameters and
keyword parameters is the symbol * (rather than nil). See Section 3.4.8
(Deftype Lambda Lists).

I've you've ever seen a non trivial deftype definition, you might have
noticed that we often use backquote there.

So basically:

(shadow 'deftype)
(defmacro deftype (name (&rest lambda-list) &body body)
`(defmacro ,name ,(replace-default lambda-list) ,@body))

with:

(eval-when (:compile-toplevel :load-toplevel :execute)
(defun replace-default (lambda-list)
(if (intersection lambda-list-keywords lambda-list)
(let ((doit nil))
(mapcar (lambda (item)
(if doit
(cond
((member item lambda-list-keywords)
item)
((consp item)
(ecase (length item)
((1) (list (first item) ''*))
((2 3) (if (null (second item))
(list* (first item) ''* (cddr item))
item))))
(t
(list item ''*)))
(case item
((&optional &key) (setf doit t) item)
(otherwise item))))
lambda-list))
(mapcar (lambda (item)
(if (consp item)
(replace-default item)
item))
lambda-list))))

(shadow 'deftype)

(defmacro deftype (name (&rest lambda-list) &body body)
`(defmacro ,name ,(replace-default lambda-list) ,@body))


(deftype simfun (x) `(integer ,(- x) ,x))
(deftype funny (a &optional (b nil bp))
(if bp
`(or (simfun ,a) (string ,b))
`(cons integer string)))

(macroexpand-1 '(funny 5 10))
(or (simfun 5) (string 10))
t

Of course, you cannot really use those macros as lisp code, since
they're not CL, but CL type. For example, OR doesn't expand right:

(swank:swank-macroexpand-all "(funny 5 10)")
"(let ((#1=#:g8256 (integer -5 5))) (if #1# #1# (string 10)))"

But you could also shadow the other type specifiers like OR AND NOT etc,
and give our deftype definitions for all the standard types, so that you
would be able to expand all types using swank-macroexpand-all.

The alternative os to define a parallel "type expansion" mechanism,
quite similar to defmacro and macroexpand, with deftype registering a
typefunction instead of a macrofunction.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk