[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

does a typename exist

Jim Newton

10/1/2015 3:49:00 PM

Is there some way to find out whether a given s-expression names a valid type?

The built-in types are a finite site, but every class names a type and every call to DEFTYPE also is liable to define a type.

I notice that in sbcl, if I call TYPEP with an unknown type specifier, a SIMPLE-ERROR is raised.
I'd like to be able to know before calling TYPEP whether the typename is valid.



27 Answers

Kaz Kylheku

10/1/2015 4:01:00 PM

0

On 2015-10-01, Jim Newton <jimka.issy@gmail.com> wrote:
> Is there some way to find out whether a given s-expression names a valid
> type?

I do not know whether or not there is a direct way to do this.

Suppose there isn't. In that case you can still do it somewhat indirectly like
this:

(ignore-errors (subtypep <whatever> 't))

Every type is a subtype of T, including T itself. So if <whatever> is
a type, subtypep must succeeds and return t.

If <whatever> isn't a type, then ignore-errors catches the condition
and turns it into a nil return.

Jim Newton

10/1/2015 4:10:00 PM

0

could it be that (SUBTYPEP whatever t) always returns t, even if it the thing does not name a type?

(subtypep (gensym) t) ==> T ; in sbcl

Jim Newton

10/1/2015 4:16:00 PM

0

In clisp, i get a different behaviour. An error is thrown.


[1]> (subtypep (gensym) t)
[1]> (subtypep (gensym) t)

*** - SUBTYPEP: invalid type specification #:G2814
The following restarts are available:
ABORT :R1 Abort main loop
Break 1 [2]>

Marco Antoniotti

10/2/2015 6:32:00 AM

0

On Thursday, October 1, 2015 at 6:16:29 PM UTC+2, Jim Newton wrote:
> In clisp, i get a different behaviour. An error is thrown.
>
>
> [1]> (subtypep (gensym) t)
> [1]> (subtypep (gensym) t)
>
> *** - SUBTYPEP: invalid type specification #:G2814
> The following restarts are available:
> ABORT :R1 Abort main loop
> Break 1 [2]>

CCL returns T,T
LW gives out an error.

I think LW and CLISP are right and that CCL and SBCL are wrong.

I also have in many of my libraries

(defun type-specifier-p (x) (ignore-errors (subtypep x t)))

Time for a CDR?

Cheers
--
MA

Jim Newton

10/2/2015 8:11:00 AM

0

Hi Marco, of course you can write a #+sbcl version of this because sbcl seems to export SB-EXT:VALID-TYPE-SPECIFIER-P according to Douglas Katzman.

---------- Forwarded message ----------
From: Douglas Katzman
Date: Thu, Oct 1, 2015 at 6:47 PM
Subject: Re: [Sbcl-devel] should an invalid type be a subtype of t?
To: Jim Newton
Cc: SBCL Devel-list


But 12 is not a type designator. I meant "every type is a subtype of T", not "every object"

The syntax of type designators is somewhere made clear. I can't find it at the moment.

SBCL exports SB-EXT:VALID-TYPE-SPECIFIER-P to do what you want, probably.

Jim Newton

1/13/2016 1:32:00 PM

0

Hi Marco, (and anyone else)

> I also have in many of my libraries
>
> (defun type-specifier-p (x) (ignore-errors (subtypep x t)))
>
>

Have you updated your type-specifier-p to handle different implementations?
I'd love to see your latest version of this function.
Here is what I have which is far from complete.


(defun valid-type-p (type-designator)
;; TODO need to make this work for other lisps
;; current only works for sbcl
#+sbcl (SB-EXT:VALID-TYPE-SPECIFIER-P type-designator)
#+clisp (ignore-errors (subtypep type-designator t))
#-(or sbcl clisp) (error "VALID-TYEP-P not implemented for ~A" (lisp-implementation-type))
)

(assert (not (valid-type-p (gensym))))

Marco Antoniotti

1/13/2016 2:09:00 PM

0

On Wednesday, January 13, 2016 at 2:31:39 PM UTC+1, Jim Newton wrote:
> Hi Marco, (and anyone else)
>
> > I also have in many of my libraries
> >
> > (defun type-specifier-p (x) (ignore-errors (subtypep x t)))
> >
> >
>
> Have you updated your type-specifier-p to handle different implementations?
> I'd love to see your latest version of this function.
> Here is what I have which is far from complete.
>
>
> (defun valid-type-p (type-designator)
> ;; TODO need to make this work for other lisps
> ;; current only works for sbcl
> #+sbcl (SB-EXT:VALID-TYPE-SPECIFIER-P type-designator)
> #+clisp (ignore-errors (subtypep type-designator t))
> #-(or sbcl clisp) (error "VALID-TYEP-P not implemented for ~A" (lisp-implementation-type))
> )
>
> (assert (not (valid-type-p (gensym))))

Nope. I think I can first write a CDR. It will have to wait for next week though.
I am not sure how to write a portable one, given that, at least in CCL you get

? (subtypep (gensym) t)
T
T

Which, IMHO, is wrong.

Cheers
--
MA

Jim Newton

1/13/2016 2:34:00 PM

0

Can anyone expert/user of CCL answer this?

How can I extend the following function so it works in CCL?

> > (defun valid-type-p (type-designator)
> > #+sbcl (SB-EXT:VALID-TYPE-SPECIFIER-P type-designator)
> > #+clisp (ignore-errors (subtypep type-designator t))
> > #-(or sbcl clisp) (error "VALID-TYEP-P not implemented for ~A" (lisp-implementation-type))
> > )
> >
> > (assert (not (valid-type-p (gensym))))
>

taruss

1/13/2016 9:59:00 PM

0

On Wednesday, January 13, 2016 at 6:08:57 AM UTC-8, Marco Antoniotti wrote:

>... at least in CCL you get
>
> ? (subtypep (gensym) t)
> T
> T
>
> Which, IMHO, is wrong.


Hmmm. Looks like someone may have tried to do an optimization when the second
argument is T.

But it seems like the Spec may not require anything different. The Exceptional
Situations field says "None" and there isn't any text that seems to indicate
that the arguments have to be valid type specifiers.

I suppose one [not me] could argue that a symbol could name a not-yet-defined
type and therefore the (gensym) of this mythical type would be subtype of T.

Trying some more arguments, it seems that any symbol is considered a valid type
specifier. (subtypep 3 t) generates an error.

Marco Antoniotti

1/14/2016 8:24:00 AM

0

On Wednesday, January 13, 2016 at 10:58:44 PM UTC+1, tar...@google.com wrote:
> On Wednesday, January 13, 2016 at 6:08:57 AM UTC-8, Marco Antoniotti wrote:
>
> >... at least in CCL you get
> >
> > ? (subtypep (gensym) t)
> > T
> > T
> >
> > Which, IMHO, is wrong.
>
>
> Hmmm. Looks like someone may have tried to do an optimization when the second
> argument is T.
>
> But it seems like the Spec may not require anything different. The Exceptional
> Situations field says "None" and there isn't any text that seems to indicate
> that the arguments have to be valid type specifiers.
>
> I suppose one [not me] could argue that a symbol could name a not-yet-defined
> type and therefore the (gensym) of this mythical type would be subtype of T.
>
> Trying some more arguments, it seems that any symbol is considered a valid type
> specifier. (subtypep 3 t) generates an error.

I did a little more investigating and CMUCL and SBCL also exhibit the (IMHO) erroneous behavior. LW does (IMHO) TRT. I still have not reinstalled Allegro, ABCL, ECL etc on my new laptop so I cannot say anything about them.

I don't think there is much rationale to say that a symbol represents a yet-to-be defined type. Syntactically it is ok (I suppose), definitively not semantically.

Cheers
--
MA