From: Karol Skocik
Subject: getting a form of deftype declaration
Date: 
Message-ID: <63d74cb8-91c8-40c2-be6a-2e5a66a0dbd0@k37g2000hsf.googlegroups.com>
Hi,
  I use deftype to declare shape of user input data and such "user-
validation" type looks like this for example:

(deftype positive-integer ()
  `(and integer (satisfies positivep)))

Then I use (typep some-user-input 'positive-integer) as a test whether
the input is correct which works fine, but in case when the input is
not correct (somebody supplies value :asdf), I would like to show a
error message like:

"Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
POSITIVEP)))"

(Note that "users" are in my case common-lisp programmers (hmm, me :)
which will find such message helpful - it's part of DSL)

My problem is, that I don't know how to get a form which describes the
type. I need a function like 'type-form' which when called like:

(type-form 'positive-integer)

will result into it's definition:

(AND INTEGER (SATISFIES POSITIVEP)))

Is this possible directly in CL or do I need to wrap it in simple
macro which will store the form in itself (like documentation slot of
type or hashtable?)

Regards,
  Karol

From: Jeff
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <0e93f281-3709-4665-a7a1-c72ebeb252a6@a70g2000hsh.googlegroups.com>
You can always do (assert (predicate-p value) () "Message to print if
assertion fails").
From: Karol Skocik
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <4346a2c1-0913-4104-aed3-fbb1abb2405a@13g2000hsb.googlegroups.com>
On Apr 5, 1:04 am, Jeff <········@gmail.com> wrote:
> You can always do (assert (predicate-p value) () "Message to print if
> assertion fails").

Yes, but that way I still don't get the form of the type..

Karol
From: Pascal Bourguignon
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <87prt5av9g.fsf@thalassa.informatimago.com>
Karol Skocik <············@gmail.com> writes:

> Hi,
>   I use deftype to declare shape of user input data and such "user-
> validation" type looks like this for example:
>
> (deftype positive-integer ()
>   `(and integer (satisfies positivep)))
>
> Then I use (typep some-user-input 'positive-integer) as a test whether
> the input is correct which works fine, but in case when the input is
> not correct (somebody supplies value :asdf), I would like to show a
> error message like:
>
> "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> POSITIVEP)))"
>
> (Note that "users" are in my case common-lisp programmers (hmm, me :)
> which will find such message helpful - it's part of DSL)
>
> My problem is, that I don't know how to get a form which describes the
> type. I need a function like 'type-form' which when called like:
>
> (type-form 'positive-integer)
>
> will result into it's definition:
>
> (AND INTEGER (SATISFIES POSITIVEP)))
>
> Is this possible directly in CL or do I need to wrap it in simple
> macro which will store the form in itself (like documentation slot of
> type or hashtable?)

The later.


But isn't POSITIVE-INTEGER enough description?


Note also that for this simple type, you can define it as:

(deftype positive-integer () '(integer 0))

and you will probably get more informative an error message, such as:

debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread"
{A72B589}>: The value -1 is not of type UNSIGNED-BYTE.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Karol Skocik
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <c1e68fad-b600-4636-9eef-650a84867c25@d45g2000hsc.googlegroups.com>
On Apr 5, 4:30 am, Pascal Bourguignon <····@informatimago.com> wrote:
> Karol Skocik <············@gmail.com> writes:
> > Hi,
> >   I use deftype to declare shape of user input data and such "user-
> > validation" type looks like this for example:
>
> > (deftype positive-integer ()
> >   `(and integer (satisfies positivep)))
>
> > Then I use (typep some-user-input 'positive-integer) as a test whether
> > the input is correct which works fine, but in case when the input is
> > not correct (somebody supplies value :asdf), I would like to show a
> > error message like:
>
> > "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> > POSITIVEP)))"
>
> > (Note that "users" are in my case common-lisp programmers (hmm, me :)
> > which will find such message helpful - it's part of DSL)
>
> > My problem is, that I don't know how to get a form which describes the
> > type. I need a function like 'type-form' which when called like:
>
> > (type-form 'positive-integer)
>
> > will result into it's definition:
>
> > (AND INTEGER (SATISFIES POSITIVEP)))
>
> > Is this possible directly in CL or do I need to wrap it in simple
> > macro which will store the form in itself (like documentation slot of
> > type or hashtable?)
>
> The later.
>
> But isn't POSITIVE-INTEGER enough description?
>
> Note also that for this simple type, you can define it as:
>
> (deftype positive-integer () '(integer 0))
>
> and you will probably get more informative an error message, such as:
>
> debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread"
> {A72B589}>: The value -1 is not of type UNSIGNED-BYTE.
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.

The problem is that I do the checks in macros, and I don't want to do
(error ....) in macro because that will result in compilation error
without better information what went wrong... I want to test it and
emit form `(error "some slot somewhere is not of type ~a" '<some-
deftype-form-here>)

But I decided that since the users of that macros are just 2 CL
programmers, I will be spartan in error messages :)

Karol
From: Alan Crowe
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <861w5kcxuk.fsf@cawtech.freeserve.co.uk>
Karol Skocik <············@gmail.com> writes:

> Hi,
>   I use deftype to declare shape of user input data and such "user-
> validation" type looks like this for example:
> 
> (deftype positive-integer ()
>   `(and integer (satisfies positivep)))
> 
> Then I use (typep some-user-input 'positive-integer) as a test whether
> the input is correct which works fine, but in case when the input is
> not correct (somebody supplies value :asdf), I would like to show a
> error message like:
> 
> "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> POSITIVEP)))"
> 
> (Note that "users" are in my case common-lisp programmers (hmm, me :)
> which will find such message helpful - it's part of DSL)
> 

A possible compromise use the argument to deftype

CL-USER> (deftype integer-and (predicate)
           `(and integer (satisfies ,predicate)))
INTEGER-AND
CL-USER> (let ((x -4))
           (check-type x (integer-and plusp))
           (sqrt (/ x)))
Type a form to be evaluated:
16
0.25

Alan Crowe
Edinburgh
Scotland
From: Karol Skocik
Subject: Re: getting a form of deftype declaration
Date: 
Message-ID: <a502dfc5-c306-46c2-abd2-e587e802b925@a1g2000hsb.googlegroups.com>
On Apr 5, 2:03 pm, Alan Crowe <····@cawtech.freeserve.co.uk> wrote:
> Karol Skocik <············@gmail.com> writes:
> > Hi,
> >   I use deftype to declare shape of user input data and such "user-
> > validation" type looks like this for example:
>
> > (deftype positive-integer ()
> >   `(and integer (satisfies positivep)))
>
> > Then I use (typep some-user-input 'positive-integer) as a test whether
> > the input is correct which works fine, but in case when the input is
> > not correct (somebody supplies value :asdf), I would like to show a
> > error message like:
>
> > "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> > POSITIVEP)))"
>
> > (Note that "users" are in my case common-lisp programmers (hmm, me :)
> > which will find such message helpful - it's part of DSL)
>
> A possible compromise use the argument to deftype
>
> CL-USER> (deftype integer-and (predicate)
>            `(and integer (satisfies ,predicate)))
> INTEGER-AND
> CL-USER> (let ((x -4))
>            (check-type x (integer-and plusp))
>            (sqrt (/ x)))
> Type a form to be evaluated:
> 16
> 0.25
>
> Alan Crowe
> Edinburgh
> Scotland

Thats nice trick, thanks!
Karol