如何停止在 Format/Interpretation Mathematica 结构中发生递归? [英] How do I stop recursion happening in a Format/Interpretation Mathematica construction?

查看:18
本文介绍了如何停止在 Format/Interpretation Mathematica 结构中发生递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是 Michael Pilat 在 防止加号"重新排列事物中给出的答案的延续.在那里他使用

This question follows on from the answer given by Michael Pilat in Preventing "Plus" from rearranging things. There he defined a custom + notation using

Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]]

问题在于您无法复制和粘贴输出(尽管 % 或 Out[] 仍然有效).为了解决这个问题,您应该使用 Interpretation 类型工具,它允许将表达式显示为一件事,但在作为输入提供时解释为另一件事.我对迈克尔回答的修改是

The problem with this is you can't copy and paste the output (although % or Out[] still works). To get around this you should use the Interpretation type facility which allows an expression to be displayed as one thing, but interpreted as another when supplied as input. My modification of Michael's answer is

Format[myPlus[expr__]] := Interpretation[Row[{expr}, "+"], myPlus[expr]]

这样就可以复制粘贴成功了.问题在于修改复制的表达式.您可以使用 Ctrl-Shift-I 将复制的表达式转换回 InputForm,然后更改您想要的任何内容并在任何表达式中使用 InputForm.但是如果您尝试使用 Ctrl-Shift-N 将其改回 StandardForm,那么您将输入一个递归,其中 中的第二个参数>解释反复被评估.尽管 Interpretation 具有属性 HoldAll(在正常评估期间正常工作).

This can be copied and pasted successfully. The problem lies in modifying copied expressions. You can convert a copied expression back to InputForm using Ctrl-Shift-I then change anything you want and use the InputForm in any expression. But if you try to change it back to StandardForm using Ctrl-Shift-N then you enter an recursion where the second argument in the Interpretation repeatedly gets evaluated. This is despite Interpretation having the attribute HoldAll (which works properly during normal evaluation).

通常,在定义简单符号时,我使用低级 MakeBoxes,例如

Normally, when defining simple notations I use the low-level MakeBoxes, eg

myPlus/:MakeBoxes[myPlus[expr__],fmt_]:=With[{r=Riffle[MakeBoxes/@{expr},"+"]},
   InterpretationBox[RowBox[r],myPlus[expr]]]

效果很好,所以我以前没有遇到过这个递归问题.

which works perfectly, so I have not encountered this recursion problem before.

所以我的问题(最后)是:我的 Format 类型命令出了什么问题,如何修复?或者:你如何制作一个与我的 MakeBoxes 类型命令相当的高级命令?

So my question (finally) is: What went wrong with my Format type command and how can it by fixed? Or: How do you make a high-level equivalent of my MakeBoxes type command?

推荐答案

我就这个问题咨询了一位同事,他的建议本质上是将值定义放在 MakeBoxes 上,正如你所展示的那样更好当您希望事物从输出到输入紧密集成时,而不是使用 Format.Format 并不是真正打算生成可以作为输入重复使用的输出,而只是为了格式化输出,因此在转换为 时会出现 Interpretation 的意外递归标准表格

I consulted with a colleague about this, and his recommendation was essentially that putting up-value definitions on MakeBoxes as you demonstrate is better than using Format when you want things to be tightly integrated from output back to input. Format isn't really intended to produce output that can be re-used as input, but just to format output, hence the unexpected recursion with Interpretation when converting to StandardForm, etc.

您可能会发现函数 ToBoxes 是对 MakeBoxes.

最后,这里有一个关于盒子结构的教程.

HTH!

这篇关于如何停止在 Format/Interpretation Mathematica 结构中发生递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆