Taoufik Dachraoui
1/10/2016 9:50:00 PM
On Sunday, January 10, 2016 at 4:14:05 PM UTC+1, smh wrote:
> You may not yet have grasped everything about the CL type system.
>
> On Saturday, January 9, 2016 at 11:31:06 AM UTC-8, Taoufik Dachraoui wrote:
> > I wish I can tag a cons object to be of foo type as shown below:
> >
> > (let ((a (cons 1 2)) (b (cons 2 3)))
> > (tag a 'foo)
> > (values (car a) (cdr a) (type-of a) (type-of b))
> > ->
> > 1
> > 2
> > FOO
> > CONS
>
> (type-of (cons 1 2)) can return FOO iff foo has been defined appropriately using deftype. See the "recognizable subtype" requirement on the dictionary page for type-of.
>
> Now, if the deftype were in effect, then the above type-of form _could_ return any of these:
>
> FOO ; iff an appropriate deftype is in force
> CONS
> (CONS FIXNUM)
> (CONS (MOD 37) (INTEGER 2 2))
> (CONS (EQL 1) (MEMBER 2 4 6 8))
>
> But most implementations will return simple CONS. There are good reasons.. type-of might be used in speed-critical dispatch. Usually all user code wants is CONS, so consing up a more verbose type specifier is wasteful. Finally, there is a semantic problem returning a deftype type.
>
> Suppose we have these _two_ deftypes:
>
> (deftype foo () '(cons (mod 256) (mod 256)))
> (deftype bar () '(cons (member 1 3 5 7 9 1027) (member 2 4 6 8 10)))
>
> then which should (type-of (cons 1 2)) return? The cons is an element of both types, and neither is a subset of the other.
>
> While X3J13 did tighten the restrictions on TYPE-OF, history and usual practice come into consideration. For non-complex numbers and for most other built-in types, implementations have customarily returned an atomic type symbol. For arrays (simple or non-simple) they have included the upgraded element-type and dimensions. That is what user code has come to expect. I don't remember anything in the ANS that spells this out.
I already defined a type for sparse-a as follows:
(defun sparse-a-p (a) (and (consp a) (eq :SPARSE-A (car a))))
(deftype sparse-a () '(satisfies sparse-a-p))
I use (typep obj 'sparse-a) or (sparse-a-p obj) ; (type-of (make-sparse-a)) -> CONS
so I do not use type-of; I was initially surprise to see CONS in response to (type-of (make-sparse-a)
but now I understand, thanks.
I thought of using a hash table to store all sparse-a objects but this has an unacceptable overhead
for my application.
Kind regards
Taoufik