From: David Deharbe
Subject: About type definitions.
Date: 
Message-ID: <CJKCIt.7z9@imag.fr>
First of all, I am using AKCL 1.615.

I would like to define a generic type a-list using the DEFTYPE special
form. This type should be generic because I would like the key and
datum types be parameters of my type.

If CL does not allow this, L will have to define a type for each different
type of association list of my program. Thus, I would write:

----

;;; Definition of a predicate that check that a list only contains
;;; conses cell, where the type of car and cdr are respectively
;;; keytype and datumtype.
(defun association-list-keytype-datumtype-p (l)
   (every
     #'(lambda(c)
	 (and (typep (car c) 'keytype)
	      (typep (cdr c) 'datumtype)))
     l))

;;; The required type definition?
(deftype association-list-keytype-datumtype ()
   '(and list (satisfies association-list-keytype-datumtype-p)))

----

Would this be correct (and efficient)?

Thanks for your indications,

David.
-- 
          # ·············@imag.fr # (+33) 76 63 58 78 #
          #    BP 53 ; F - 38 041 GRENOBLE CEDEX 9    #
From: Barry Margolin
Subject: Re: About type definitions.
Date: 
Message-ID: <2h47bkINNpc2@early-bird.think.com>
In article <··········@imag.fr> ·······@imag.fr (David Deharbe) writes:
>First of all, I am using AKCL 1.615.
>
>I would like to define a generic type a-list using the DEFTYPE special
>form. This type should be generic because I would like the key and
>datum types be parameters of my type.

The dpANS introduced the parametric type CONS, which looks like it could be
used to define what you want.

(deftype alist (keytype datumtype)
  `(cons (cons ,keytype ,datumtype) (or null (alist ,keytype ,datumtype))))

Unfortunately, recursive DEFTYPEs aren't permitted, so this doesn't really
work.  And it probably doesn't exist in AKCL yet.

So the only way to do what you want is with SATISFIES, and this suffers the
problem that it doesn't allow parameters.  The only solution I can think of
is to use a naming convention, and have the expansion reference a predicate
whose name is constructed:

(deftype alist (keytype datumtype)
  `(and list
        (satisfies ,(intern (format nil "ASSOCIATION-LIST-~A-~A-P"
				    ,keytype ,datumtype)
			    "ALIST-PREDICATES"))))

(defmacro define-alist-predicate (keytype datumtype)
  `(defun ,(intern (format nil "ASSOCIATION-LIST-~A-~A-P"
		           ,keytype ,datumtype)
		   "ALIST-PREDICATES") (list)
     (every #'(lambda (c)
		(and (typep (car c) ',keytype)
		     (typep (cdr c) ',datumtype))))))

>Would this be correct (and efficient)?

Since it has to scan the entire list every type you do (typep x '(alist
...)), it's not very efficient.  And I don't think any CL implementation
will be able to make optimizations based on declaring variables of this
type.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar