chakravala/Grassmann.wl

MakeBoxes for Submanifold

chakravala opened this issue · 7 comments

Currently, there is an issue regarding how MakeBoxes displays Submanifold elements having Symbol coefficients.

If MakeBoxes is used on the Coefficient of the Submanifold, then for the a symbol it is printed as $CellContext`a.

V = Submanifold[MetricSignature["+++"]];
v = Submanifold[V,0];
a*v[1]+v[2]

Additionally, I would like to use InterpretationBox[Selectable -> ...] but I have not successfully implemented that yet.

Resolving my MakeBoxes issue requires figuring out how to bypass this bug (@okofish @maxitg ?):

MyFun[MyThing[x_]] := x;
MyThing /: MakeBoxes[s_MyThing, StandardForm] :=  MakeBoxes[MyFun[s], StandardForm];
MyThing[1]

When MyThing[1] is displayed, the following recursion error results:

$RecursionLimit::reclim2: Recursion depth of 4096 exceeded during evaluation of MakeBoxes[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[MyFun[<<1>>]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],<<12>>].

One possible workaround is

MyThing /: MakeBoxes[MyThing[x_], StandardForm] :=  MakeBoxes[x, StandardForm]

However, I'd prefer if I could create this by calling MyFun instead of doing it like this. Is any other way possible?

MakeBoxes is HoldAllComplete; does using ToBoxes on the RHS do what you want?

MyThing /: MakeBoxes[s_MyThing, StandardForm] := ToBoxes[MyFun[s], StandardForm]

Indeed, ToBoxes resolves this, thank you very much!

You're welcome!

Do you (e.g. @okofish) happen to know if there is a way to detect if an expression needs to be wrapped in parenthesis before being displayed as a subexpression? For example, -x*y does not need parenthesis for -x but (z-x)*y does require parenthesis for z-x. Given either -x or z-x, I would like to be able to determine if parenthesis are needed.

Alright, 09ed0a7#diff-a2c244be0060c3069d6f099d31408ae79a518d514b74163ea30eb589c747ac96R62-R67 resolves parenthesizing of Coefficient elements. Now, only remaining open questions are regarding InterpretationBox behavior.

It sounds like you solved the parentheses issue, but some ideas anyways:

  • PrecedenceForm lets you customize the automatic insertion of parentheses during typesetting by overriding the precedence value of a subexpression.
  • Prefix, Infix, and Postfix also let you customize precedence of an expression typeset in *fix notation; I think these have the same effect as PrecedenceForm.
  • The undocumented function Precedence (see ?Precedence for usage) appears to let you check the precedence value of a typeset expression or of a symbol. This could be used to manually parenthesize a subexpression based on its precedence and the precedence of the head of the surrounding expression.
  • The SyntaxForm option also does something with operator precedence but I don't really understand it.

Random note: this documentation page is a good read about precedence of built-in syntax, including the arcane variety.