ccl
ccl copied to clipboard
Printing 2D arrays of characters not readably doesn't print the characters.
Here is something funny with 2D array of character. When printing them with print-readably set to NIL, they print stragenly, with #<string 3, displaced> elements (instead of just a string representation #2A("foo" "bar" "baz")
that could be acceptable, or a 2D array of character representation #2A((#\f #\o #\o) (#\b #\a #\r) (#\b #\a #\z))
:
cl-user> (make-array '(3 3) :initial-contents '("foo" "bar" "baz") :element-type 'character)
#2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>)
cl-user> (make-array '(3 3) :initial-contents (mapcar (lambda (x) (coerce x 'list))'("foo" "bar" "baz")) :element-type 'character)
#2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>)
cl-user> (make-array '(3 3) :initial-element #\* :element-type 'character)
#2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>)
cl-user> *print-readably*
nil
cl-user> (setf *print-readably* t)
t
cl-user> (make-array '(3 3) :initial-element #\* :element-type 'character)
But when we try to print it with print-readably set to T, an error is signalled, and we're falling fast in the rabbit hole:
1- the error message prints the array as we expected (well, more or less, what's with those newlines in the last row?)!
2- the backtrace still shows #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>)
Attempt to print object #2A((#\* #\* #\*)
(#\* #\* #\*)
(#\*
#\*
#\*)) on stream #<string-output-stream #x30200A8FF8CD> .
[Condition of type print-not-readable]
Restarts:
0: [retry] Retry SLIME REPL evaluation request.
1: [*abort] Return to SLIME's top level.
2: [abort-break] Reset this thread
3: [abort] Kill this thread
Backtrace:
0: (ccl::print-not-readable-error #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output..
1: (ccl::%print-unreadable-object #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output..
2: (ccl::write-an-array #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output-stream #..
3: (#<ccl::standard-kernel-method print-object (t t)> #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displac..
4: (ccl::%call-next-method (nil #<ccl::standard-kernel-method print-object (t t)> . 227528341))
5: (ccl::%%standard-combined-method-dcode (#<ccl::standard-kernel-method print-object :around (t t)> #<ccl::standard-kernel..
6: (nil #<Unknown Arguments>)
7: (ccl::write-a-frob #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output-stream #x3..
8: (ccl::write-internal #<string-output-stream #x30200A8FF8CD> #2A(#<string 3, displaced> #<string 3, displaced> #<string ..
9: (ccl::write-1 #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output-stream #x30200A..
10: (prin1 #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>) #<string-output-stream #x30200A8FF8CD>..
11: (prin1-to-string #2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>))
12: ((:internal swank/backend:send swank::present-repl-results) #2A(#<string 3, displaced> #<string 3, displaced> #<string 3..
13: (ccl::map1 #<Compiled-function (:internal swank/backend:send swank::present-repl-results) (Non-Global) #x302001EB486F> ..
14: (mapc #<Compiled-function (:internal swank/backend:send swank::present-repl-results) (Non-Global) #x302001EB486F> (#2A(..
15: (swank::present-repl-results (#2A(#<string 3, displaced> #<string 3, displaced> #<string 3, displaced>)))
16: ((:internal swank-repl::repl-eval))
17: (swank-repl::track-package #<ccl:compiled-lexical-closure (:internal swank-repl::repl-eval) #x30200A8647AF>)
18: ((:internal swank-repl::repl-eval))
19: (swank::call-with-retry-restart "Retry SLIME REPL evaluation request." #<ccl:compiled-lexical-closure (:internal swank-r..
20: ((:internal swank-repl::repl-eval))
21: (#<Anonymous Function #x302001C49A3F> #<ccl:compiled-lexical-closure (:internal swank-repl::repl-eval) #x30200A86486F>)
22: (swank/backend:call-with-syntax-hooks #<ccl:compiled-lexical-closure (:internal swank-repl::repl-eval) #x30200A86486F>)
23: (swank::call-with-buffer-syntax nil #<ccl:compiled-lexical-closure (:internal swank-repl::repl-eval) #x30200A86486F>)
24: (swank-repl::repl-eval "(make-array '(3 3) :initial-element #\\* :element-type 'character)
")
25: (swank-repl:listener-eval "(make-array '(3 3) :initial-element #\\* :element-type 'character)
" :window-width nil)
26: (ccl::call-check-regs swank-repl:listener-eval "(make-array '(3 3) :initial-element #\\* :element-type 'character)
")
27: (ccl::cheap-eval (swank-repl:listener-eval "(make-array '(3 3) :initial-element #\\* :element-type 'character)
"))
28: (swank:eval-for-emacs (swank-repl:listener-eval "(make-array '(3 3) :initial-element #\\* :element-type 'character)
") "..
29: (swank::process-requests nil)
30: ((:internal swank::handle-requests))
31: ((:internal swank::handle-requests))
32: (swank/backend:call-with-debugger-hook #<Compiled-function swank:swank-debugger-hook #x302001DD235F> #<ccl:compiled-lexi..
33: (swank/backend:call-with-debugger-hook #<Compiled-function swank:swank-debugger-hook #x302001DD235F> #<ccl:compiled-lexi..
34: ((:internal swank::handle-requests))
35: (swank::call-with-bindings ((*standard-input* . #<swank/gray::slime-input-stream #x302005C6B92D>) (*standard-output* . #..
36: (swank::handle-requests #<multithreaded-connection #x302005C4526D> nil)
37: (swank-repl::repl-loop #<multithreaded-connection #x302005C4526D>)
38: ((:internal swank-repl::spawn-repl-thread))
39: (swank::call-with-bindings ((*print-pretty*) (*print-level*) (*print-length*) (*print-circle*) (*print-case* . :downcase..
40: ((:internal swank-repl::spawn-repl-thread))
41: (ccl::run-process-initial-form #<process repl-thread(1029) [Active] #x302005C5AF4D> (#<ccl:compiled-lexical-closure (:in..
42: ((:internal (ccl::%process-preset-internal (ccl:process))) #<process repl-thread(1029) [Active] #x302005C5AF4D> (#<ccl:c..
43: ((:internal ccl::thread-make-startup-function))
Similar result in the terminal, but the error message shows a more concise representation of the array:
Clozure Common Lisp Version 1.12 (v1.12-39-g6c1a9458) DarwinX8664
For more information about CCL, please see http://ccl.clozure.com.
CCL is free software. It is distributed under the terms of the Apache
Licence, Version 2.0.
? (make-array '(3 3) :initial-contents '("foo" "bar" "baz") :element-type 'character)
#2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>)
? (make-array '(3 3) :initial-contents (mapcar (lambda (x) (coerce x 'list))'("foo" "bar" "baz")) :element-type 'character)
#2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>)
? (make-array '(3 3) :initial-element #\* :element-type 'character)
#2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>)
? *print-readably*
NIL
? (setf *print-readably* t)
T
? (make-array '(3 3) :initial-element #\* :element-type 'character)
> Error: Attempt to print object #<ARRAY 3x3 type CHARACTER, simple> on stream #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> .
> While executing: CCL::PRINT-NOT-READABLE-ERROR, in process listener(1).
> Type :POP to abort, :R for a list of available restarts.
> Type :? for other options.
1 > :b
(1619418) : 0 (PRINT-NOT-READABLE-ERROR #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD>) 93
(1619438) : 1 (%PRINT-UNREADABLE-OBJECT #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> NIL NIL #<CCL:COMPILED-LEXICAL-CLOSURE (:INTERNAL CCL::WRITE-AN-ARRAY) #x173C1BF>) 133
(1619470) : 2 (WRITE-AN-ARRAY #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> 1152921504606846975) 2453
(1619510) : 3 (FUNCALL (FUNCTION #<#<CCL::STANDARD-KERNEL-METHOD PRINT-OBJECT (T T)>>) #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD>) 2341
(1619540) : 4 (%CALL-NEXT-METHOD (NIL #<CCL::STANDARD-KERNEL-METHOD PRINT-OBJECT (T T)> . 2896579)) 1037
(16195B8) : 5 (%%STANDARD-COMBINED-METHOD-DCODE (#<CCL::STANDARD-KERNEL-METHOD PRINT-OBJECT :AROUND (T T)> #<CCL::STANDARD-KERNEL-METHOD PRINT-OBJECT (T T)>) 2896579) 701
(1619630) : 6 (WRITE-A-FROB #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> 1152921504606846975 NIL) 173
(16196A8) : 7 (WRITE-INTERNAL #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) 1152921504606846975 NIL) 789
(1619708) : 8 (WRITE-1 #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> 1152921504606846975) 573
(1619730) : 9 (WRITE #2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>) :STREAM #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> :ESCAPE T :RADIX NIL :BASE 10 :CIRCLE NIL :PRETTY NIL :LEVEL NIL :LENGTH NIL :CASE :UPCASE :GENSYM T :ARRAY T :READABLY T :RIGHT-MARGIN 110 :MISER-WIDTH 40 :LINES NIL :PPRINT-DISPATCH #<PPRINT-DISPATCH-TABLE #x30200043C04D> :STRUCTURE T :SIMPLE-VECTOR NIL :SIMPLE-BIT-VECTOR NIL :STRING-LENGTH NIL) 1773
(1619AF0) : 10 (TOPLEVEL-PRINT (#2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>)) #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD>) 493
(1619B28) : 11 (READ-LOOP :INPUT-STREAM #<SYNONYM-STREAM to *TERMINAL-IO* #x30200043010D> :OUTPUT-STREAM #<SYNONYM-STREAM to *TERMINAL-IO* #x30200042FFAD> :BREAK-LEVEL 0 :PROMPT-FUNCTION #<Compiled-function (:INTERNAL CCL::READ-LOOP) (Non-Global) #x300000589C9F>) 2381
(1619D78) : 12 (RUN-READ-LOOP :BREAK-LEVEL 0) 157
(1619DA0) : 13 (TOPLEVEL-LOOP) 93
(1619DB0) : 14 (FUNCALL (FUNCTION #<(:INTERNAL (CCL:TOPLEVEL-FUNCTION (CCL::LISP-DEVELOPMENT-SYSTEM T)))>)) 109
(1619DD0) : 15 (FUNCALL (FUNCTION #<(:INTERNAL CCL::MAKE-MCL-LISTENER-PROCESS)>)) 661
(1619E68) : 16 (RUN-PROCESS-INITIAL-FORM #<TTY-LISTENER listener(1) [Active] #x30200042EF5D> (#<CCL:COMPILED-LEXICAL-CLOSURE (:INTERNAL CCL::MAKE-MCL-LISTENER-PROCESS) #x30200042EA8F>)) 669
(1619EF0) : 17 (FUNCALL (FUNCTION #<(:INTERNAL (CCL::%PROCESS-PRESET-INTERNAL (CCL:PROCESS)))>) #<TTY-LISTENER listener(1) [Active] #x30200042EF5D> (#<CCL:COMPILED-LEXICAL-CLOSURE (:INTERNAL CCL::MAKE-MCL-LISTENER-PROCESS) #x30200042EA8F>)) 581
(1619F98) : 18 (FUNCALL (FUNCTION #<(:INTERNAL CCL::THREAD-MAKE-STARTUP-FUNCTION)>)) 277
1 > (:arg ccl::object 0)
#2A(#<STRING 3, displaced> #<STRING 3, displaced> #<STRING 3, displaced>)
1 >
(make-array '(3 3) :initial-contents '("foo" "bar" "baz") :element-type 'character)
We're in UB zone because the initial contents are not of the provided type. I guess that the issue is that CCL gives results that are unexpected from the user experience perspective; this kind of printing issues certainly should not happen.
(make-array '(3 3) :initial-contents '("foo" "bar" "baz") :element-type 'character)
We're in UB zone because the initial contents are not of the provided type. I guess that the issue is that CCL gives results that are unexpected from the user experience perspective; this kind of printing issues certainly should not happen.
I don't think so. initial-contents is a sequence of sequences. It can be lists or vectors and strings are vectors of the correct element-type. IMO, it's perfectly defined and conforming. But we get the same result with a list of list of characters.
Sorry, I misread - this call should work, you are defining a 3D array.