Mathematica is cool for all sorts of "magic tricks", but I'm trying to get an idea on how the core term-rewriting system works. Is it possible to make a term such that the output of its evaluation can be further evaluated into something else, without additional rules being added?
1 Answers
It is not possible. Mathematica always keeps evaluating the expression for as long as any transformation rules apply. I suggest you read this tutorial.
Someone might mention Defer.
Defer[1+1] outputs something that looks like 1+1. If you select the output cell and press shift-enter, it evaluates to 2.
This seems like what you are asking for, but in reality, it is just some trickery with how things are stored in a notebook. It is specific to notebooks. It is not possible to reproduce it in command-line mode or to use it for working with partial results inside of a function.
Whenever Mathematica produces an output to be shown as a cell, it is converted into boxes which are just a representation of what is displayed in notebooks. Everything in a notebook is made of boxes. The expression you type in an input cell is made of boxes. Before sending it to the kernel for evaluation, the boxes are translated into a proper Mathematica expression. You can always look at the box form of a cell by pressing Command-Shift-E.
Now Defer does not evaluate at all. Defer[1+1] behaves exactly the same as Hold[1+1] as far as the kernel is concerned. But Defer[1+1] converts into the same boxes as 1+1. When converting back, the information that Defer was ever there is lost. Only 1+1 remains. For this to work, it is necessary to actually display the output of Defer in a notebook and trigger the conversion to boxes. If you simply do x = Defer[1+1] then FullForm[x] will show Defer[1+1].
To sum up, Defer is not about evaluation but about creating a certain kind of output cell.
- 234,956
- 30
- 623
- 1,263
HoldForm. It is not possible to return a result that would change if given as input again. – Szabolcs Oct 13 '18 at 11:10