From: "(typep 'nil (statisfies 'identity))
Subject: printing readably an array of type bit!
Date: 
Message-ID: <5af4dd9e-a6a6-4e39-bd5a-265f9e61d1d4@h60g2000hsg.googlegroups.com>
I like bit arrays because they are so fast in computing bit
operations, but would like to store their content. ... so I ran into
following error:

(let ((bit-array (make-array (list 3 3) :element-type 'bit
                                              :initial-contents
                                              '((0 0 0) (1 1 1) (0 0
0))))
      (*print-readably* t)
      (*print-array* t))
  (print bit-array))

Error: Unable to print #2A((0 0 0) (1 1 1) (0 0 0)) readably and
       *print-readably* is true.
[condition type: print-not-readable]

... if I remowe the  (*print-readably* t) it prints normal as expected
the array:

=> #2A((0 0 0) (1 1 1) (0 0 0))

... but this is not a (simple-array bit *), it is of type (array t).

(let ((bit-array (make-array (list 3 3)
                             :element-type 'bit
                             :initial-contents
                             '((0 0 0) (1 1 1) (0 0 0)))))
  (array-element-type (read-from-string (write-to-string bit-
array :array t))))

=> t

Then I was thinking to define my own print-object method:

(defmethod print-object ((array0 array) (stream0 stream))
  (format stream0 "#.~S"
    `(make-array ,(array-dimensions array0)
                 :element-type (array-element-type array0)
                 :initial-contents ,(loop for i from 0 below (array-
dimension array0 0)
                                        collect (loop for j from 0
below (array-dimension array0 0)
                                                    collect (bit
array0 i j))))))

.... when I evaluate the expression my implementation (Allegro 7.1)
fails gracefully like a stone!

So, what to do?
- Add a specialized method for bit arrays ...
- Write a non generic Function, and every time I print an object I've
to check if it is
 an array of type bit ... pffff. NOT NICE!
- find a way to use the #* notation of bit-vectors?

Suggestions and solutions welcome!
c.

From: Barry Margolin
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <barmar-C9DA15.21461730102008@mara100-84.onlink.net>
In article 
<····································@h60g2000hsg.googlegroups.com>,
 "\"(typep 'nil (statisfies 'identity))\"" <··············@yahoo.it> 
 wrote:

> I like bit arrays because they are so fast in computing bit
> operations, but would like to store their content. ... so I ran into
> following error:
> 
> (let ((bit-array (make-array (list 3 3) :element-type 'bit
>                                               :initial-contents
>                                               '((0 0 0) (1 1 1) (0 0
> 0))))
>       (*print-readably* t)
>       (*print-array* t))
>   (print bit-array))
> 
> Error: Unable to print #2A((0 0 0) (1 1 1) (0 0 0)) readably and
>        *print-readably* is true.
> [condition type: print-not-readable]
> 
> ... if I remowe the  (*print-readably* t) it prints normal as expected
> the array:
> 
> => #2A((0 0 0) (1 1 1) (0 0 0))
> 
> ... but this is not a (simple-array bit *), it is of type (array t).

Right.  There's no read syntax for specifying the element type of an 
array, so specialized arrays (except for special cases like strings and 
bit-vectors) are not readable.

> 
> (let ((bit-array (make-array (list 3 3)
>                              :element-type 'bit
>                              :initial-contents
>                              '((0 0 0) (1 1 1) (0 0 0)))))
>   (array-element-type (read-from-string (write-to-string bit-
> array :array t))))
> 
> => t
> 
> Then I was thinking to define my own print-object method:
> 
> (defmethod print-object ((array0 array) (stream0 stream))
>   (format stream0 "#.~S"
>     `(make-array ,(array-dimensions array0)
>                  :element-type (array-element-type array0)
>                  :initial-contents ,(loop for i from 0 below (array-
> dimension array0 0)
>                                         collect (loop for j from 0
> below (array-dimension array0 0)
>                                                     collect (bit
> array0 i j))))))

Shouldn't the J loop use (array-dimension array0 1)?  Your code will 
only work for square arrays.

And what do you expect to happen when you try to print arrays that have 
more or less than 2 dimensions?

> 
> .... when I evaluate the expression my implementation (Allegro 7.1)
> fails gracefully like a stone!

You're not allowed to define PRINT-OBJECT methods on standard classes, 
because this may conflict with the implementation's own methods.  See 
item #19 in section 11.1.2.1.2 of the CLHS.

> 
> So, what to do?
> - Add a specialized method for bit arrays ...
> - Write a non generic Function, and every time I print an object I've
> to check if it is
>  an array of type bit ... pffff. NOT NICE!
> - find a way to use the #* notation of bit-vectors?

Define your own generic function for printing your application's data.  
The method for T can simply call PRINT-OBJECT.  The method for ARRAY can 
check if it's a 2-dimensional bit array and use the above code (or you 
could define your own read-macro to go along with it), otherwise it can 
simply call PRINT-OBJECT.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: "(typep 'nil (statisfies 'identity))
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <a34796be-9160-4beb-810e-ac48c6f860b2@b2g2000prf.googlegroups.com>
Thank you for the quick reply!
- Yes, I didn't say that, it is intended to print 2 dimensional square
arrays of type bit.
- I know it is not allowed to add methods to print-object, but for
hash-talbes
  it works fine ;-).

Imagine to have vectors, lists, structures that have inside this type
of arrays and you would like to print them readably with my onw print
function . Then you have to iterate somehow over this objects... this
can become quiet triky (but possible and would also result in portable
code).

For the read-macro: is it allowed to redefine the #nA(...) notation?
If not, I've to print them first with my secialized function and that
brings me back to problem 1.

What about something like:
(defgeneric print-object ((matrix0 array) (stream0 stream))
      (if (eq 'bit (array-element-type matrix0))
         (my-print method)
      (call-next-method ....))))

c.
From: "(typep 'nil (statisfies 'identity))
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <083af91e-e516-42ea-881a-4c69adcd734c@t18g2000prt.googlegroups.com>
... here the non portable way to this:

(defmethod print-object ((array0 array) (stream0 stream))
  (if (eq 'bit (array-element-type array0))
      (format stream0 "~S"
        `(make-array ',(array-dimensions array0)
                     :element-type ',(array-element-type array0)
                     :initial-contents ',(loop
                                           with m of-type unsigned-
byte = (array-dimension array0 0)
                                           for i from 0 below m
                                            collect (loop for j from 0
below m
                                                        collect (bit
array0 i j)))))
    (call-next-method)))

... it would be interesting to see in how many implementation this
code works!
c.
From: Barry Margolin
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <barmar-BC64DE.15545031102008@mara100-84.onlink.net>
In article 
<····································@t18g2000prt.googlegroups.com>,
 "\"(typep 'nil (statisfies 'identity))\"" <··············@yahoo.it> 
 wrote:

> ... here the non portable way to this:
> 
> (defmethod print-object ((array0 array) (stream0 stream))
>   (if (eq 'bit (array-element-type array0))
>       (format stream0 "~S"
>         `(make-array ',(array-dimensions array0)
>                      :element-type ',(array-element-type array0)
>                      :initial-contents ',(loop
>                                            with m of-type unsigned-
> byte = (array-dimension array0 0)
>                                            for i from 0 below m
>                                             collect (loop for j from 0
> below m
>                                                         collect (bit
> array0 i j)))))
>     (call-next-method)))
> 
> ... it would be interesting to see in how many implementation this
> code works!
> c.

it would probably be safer if you did this as an :AROUND method rather 
than a primary method.  Otherwise, you run into trouble if the 
implementation uses this method to implement the default #nA(...) 
notation.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Barry Margolin
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <barmar-19DDD3.15580131102008@mara100-84.onlink.net>
In article 
<····································@b2g2000prf.googlegroups.com>,
 "\"(typep 'nil (statisfies 'identity))\"" <··············@yahoo.it> 
 wrote:

> Thank you for the quick reply!
> - Yes, I didn't say that, it is intended to print 2 dimensional square
> arrays of type bit.
> - I know it is not allowed to add methods to print-object, but for
> hash-talbes
>   it works fine ;-).
> 
> Imagine to have vectors, lists, structures that have inside this type
> of arrays and you would like to print them readably with my onw print
> function . Then you have to iterate somehow over this objects... this
> can become quiet triky (but possible and would also result in portable
> code).

Yes, this is a problem.  I was assuming you needed this just for some 
specific data in your application.  Do you really need the full 
generality of the Lisp printer?

Perhaps you could wrap your bit arrays in a structure or class, and 
define a PRINT-OBJECT method for that class.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Thomas F. Burdick
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <7a526767-547d-429e-96ab-e703101be64b@b2g2000prf.googlegroups.com>
On Oct 31, 8:58 pm, Barry Margolin <······@alum.mit.edu> wrote:
> In article
> <····································@b2g2000prf.googlegroups.com>,
>  "\"(typep 'nil (statisfies 'identity))\"" <··············@yahoo.it>
>
>  wrote:
> > Thank you for the quick reply!
> > - Yes, I didn't say that, it is intended to print 2 dimensional square
> > arrays of type bit.
> > - I know it is not allowed to add methods to print-object, but for
> > hash-talbes
> >   it works fine ;-).
>
> > Imagine to have vectors, lists, structures that have inside this type
> > of arrays and you would like to print them readably with my onw print
> > function . Then you have to iterate somehow over this objects... this
> > can become quiet triky (but possible and would also result in portable
> > code).
>
> Yes, this is a problem.  I was assuming you needed this just for some
> specific data in your application.  Do you really need the full
> generality of the Lisp printer?
>
> Perhaps you could wrap your bit arrays in a structure or class, and
> define a PRINT-OBJECT method for that class.

That, or maybe he wants to define his own stream class (Gray streams
may not be in the ANS, but they're de facto portable). Hard to say
since the OP is being coy about why he wants this.
From: "(typep 'nil (statisfies 'identity))
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <abfda628-393a-4cd7-88fc-8960bc260c22@s9g2000prm.googlegroups.com>
my goal is: persistance for bit arrays. when you print a bit array to
a strem and read it then back, it has become an array of :element-type
t (!!!) bit operations like bit-and do not work any more.

I was realy surprised that this happens to lisp objects, because
'print should produce objects that are in some what the same (the same
or not the same or just similar that's the question).
c.
From: Barry Margolin
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <barmar-3F3660.20311503112008@mara100-84.onlink.net>
In article 
<····································@s9g2000prm.googlegroups.com>,
 "\"(typep 'nil (statisfies 'identity))\"" <··············@yahoo.it> 
 wrote:

> my goal is: persistance for bit arrays. when you print a bit array to
> a strem and read it then back, it has become an array of :element-type
> t (!!!) bit operations like bit-and do not work any more.
> 
> I was realy surprised that this happens to lisp objects, because
> 'print should produce objects that are in some what the same (the same
> or not the same or just similar that's the question).
> c.

It's that "somewhat" that's the kicker.  Since the original and 
resulting arrays contain the same data values, they're considered 
"somewhat the same".

Specialized arrays are relatively unusual, so the standard printer and 
reader don't provide ways to represent this.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Thomas F. Burdick
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <724d9a9e-efb4-4164-9616-f625da57c45b@w39g2000prb.googlegroups.com>
On 3 nov, 10:19, "\"(typep 'nil (statisfies 'identity))\""
<··············@yahoo.it> wrote:
> my goal is: persistance for bit arrays. when you print a bit array to
> a strem and read it then back, it has become an array of :element-type
> t (!!!) bit operations like bit-and do not work any more.
>
> I was realy surprised that this happens to lisp objects, because
> 'print should produce objects that are in some what the same (the same
> or not the same or just similar that's the question).
> c.

What =/= why
From: "(typep 'nil (statisfies 'identity))
Subject: Re: printing readably an array of type bit!
Date: 
Message-ID: <57585d75-5904-40c4-a49e-1484ffc6cbdc@q30g2000prq.googlegroups.com>
>
> Perhaps you could wrap your bit arrays in a structure or class, and
> define a PRINT-OBJECT method for that class.
>
... this is a good idea! Thank you very much!
c.