Subject: bug#6878: bool-vectors of length 0 signal error when aref/aset the 0th element

From: MON KEY <[email-masked]>
Date: Wed, 18 Aug 2010 21:51:09 -0400

On Wed, Aug 18, 2010 at 3:36 AM, Stefan Monnier
<[email-masked]> wrote:

>> When aref/aset'ing the 0th element of a bool-vectors of length 0 i get
>> an args-out-of-range error:

> To have a 0th element, an array needs to be of length >= 1.

Sure, this is true in the generalized case for an Elisp array.

> An array of length N has elements 0..N-1, so an array of length
> 0 doesn't have any elements at all.

Maybe, but this is not what the manual has to say of the particular type of
array that is a bool-vector, again (note the "must"):

,---- (info "(elisp)Bool-Vector Type")
|
| "A "bool-vector" is a one-dimensional array of elements that must be `t'
| or `nil'."
|
`----

So why would/should Emacs let me create a bool-vectors that I can neither set
nor get without signalling an error?

I can imagine there are situations where making 0 length vanilla vector has
utility because these are "closer" to list representation w/re the _type_ of
contents they can hold and can be reasonable coerced w/ less representational
loss of data but this is not the case w/ bool-vectors because they are _limited_
by the type of data they can represenet. Coercing 0 length bool-vector returns a
bool-vector whereas coercing a like vanilla vector yields a vector e.g.:

(append (make-bool-vector 0 t) '())
;=> #&0""

(append (make-vector 0 1))
;=> []

This presents a presents a problem when one _expects_ a bool-vector to be
treated as a first class array:

(let* ((ab (make-bool-vector 0 t))
       (abeq (cons ab ab)))
  (concat (car abeq) (cdr abeq)))
;=> ""

(let* ((av (make-vector 0 0))
       (aveq (cons av av)))
(concat (car aveq) (cdr aveq)))
;=> ""

Indeed, if we are to believe the above return values the coercion results in two
return values which _are_ `eq':

(let* ((ab (make-bool-vector 0 t))
       (abeq (cons ab ab))
       (av (make-vector 0 0))
       (aveq (cons av av)))
  (eq (concat (car abeq) (cdr abeq))
      (concat (car aveq) (cdr abeq))))
;=> t

However apropos Andreas' question:
> In which way are bool vectors different from other vectors?

Andreas, if your listening this is how:

This works:

(let* ((ab (make-vector 1 116))
       (aseq (cons ab ab)))
(concat (car aseq) (cdr aseq)))
;=> "tt"

This doesn't:

(let* ((ab (make-bool-vector 1 t))
       (abeq (cons ab ab)))
  (concat (car abeq) (cdr abeq)))
;
;=> Debugger entered--Lisp error: (wrong-type-argument integerp t)

Honestly I can't find the utility of 0 length bool-vectors in a lisp system
which doesn't allow tweaking arrays at a lower level than Emacs presently
allows. Maybe bool-vectors were intended (at some point long ago)
to be (or to be eventually) made adjustable :) :

CL-USER> (setq ba (make-array 0 :element-type 'bit :adjustable t))

CL-USER> ba
#*

CL-USER> (adjustable-array-p ba)
T

CL-USER> (array-dimension ba 0)
0

CL-USER> (array-dimensions  ba)
(0)

CL-USER> (adjust-array ba 1)
#*0

CL-USER> ba
#*0

CL-USER> (bit-vector-p ba)
T

CL-USER> (bit ba 0)
0

(setf (bit ba 0) 1)
1

CL-USER> (bit ba 0)
1

CL-USER> ba
#*1

--
/s_P\