|
From: Robert D. <rob...@gm...> - 2025-01-04 21:30:13
|
On Sat, Jan 4, 2025 at 4:07 AM Oleg Nesterov <ol...@re...> wrote:
> Nice ;) I don't understand this magic, but I will save your email,
> perhaps I will use this trick in future.
>
> However,
>
> (%i2) ccc(1);
> (%o2) <aaa(1)>
>
> Is it possible to change the code to display ccc(1) as <1> ?
Oh, the aaa(1) display is due to DIMENSION-NARY treating the 0-args or
1-arg case separately from 2 or more arguments, and just returning
foo() or foo(1), where foo is the operator, for those cases.
Here's another attempt, which makes use of DIMENSION-LIST instead.
Conveniently for us now, DIMENSION-LIST doesn't actually assume that
its argument is a list.
Note that the optional argument SEPARATOR (":" here) was introduced in
Maxima 5.44 (released circa June 2020), so the following code only
works in Maxima 5.44 or later versions.
Revised code:
(defun dimension-aaa (form result)
(dimension-list form result ":"))
(displa-def $aaa dimension-aaa)
(displa-def $bbb dimension-match "<" ">")
(defun reform-ccc (form)
(list '($bbb) (cons '($aaa) (rest form))))
(defun dimension-ccc (form result)
(let*
((reformed (reform-ccc form))
(helper (get (caar reformed) 'dimension)))
(funcall helper reformed result)))
(setf (get '$ccc 'dimension) 'dimension-ccc)
Here's what I get now:
(%i4) aaa(1);
(%o4) 1
(%i5) aaa(1, 2);
(%o5) 1:2
(%i6) aaa(1, 2, 3);
(%o6) 1:2:3
(%i7) bbb(111);
(%o7) <111>
(%i8) ccc(a);
(%o8) <a>
(%i9) ccc(a, b);
(%o9) <a:b>
(%i10) ccc(a, b, c);
(%o10) <a:b:c>
Some explanation about the code. The 2-d pretty printer (originally
devised by Richard Fateman, if I remember correctly, if not I'm sure
someone will point it out) inspects the symbol properties of the
expression operator to get the name of the function to use for
generating displayed output. You can see that by looking at the
property list. Try: `:lisp (symbol-plist '$aaa)`, likewise with $AAA
and $CCC, or any built-in operator with a special display
representation, such as $INTEGRATE.
The stuff returned by the display code amounts to a sequence of
characters and "cursor" movements (no actual cursor motion is
involved) to draw the expression on the console. You can see that by
tracing DIMENSION, DIMENSION-LIST, DIMENSION-MATCH, etc.
The pretty printing code (src/displa.lisp) is subtle but quite
capable; I have gotten interesting results out of it several times
over the years. The most involved modification was to implement
pre-subscripts and pre-superscripts, introduced in Maxima 5.44. See
declare_index_properties for more information.
best
Robert
|