6
$\begingroup$

Mathematica v13.0:

nr:=HoldForm@Evaluate@Function[t,x@t][]

then evaluate

In[29]:= nr
During evaluation of In[29]:= Function::fpct: Too many parameters in {t} to be filled from Function[t,x[t]][].
Out[29]= Function[t,x[t]][]

Strangely, reevaluation does not reproduce the error message:

In[30]:= nr
Out[30]= Function[t,x[t]][]

although reevaluating Function[t,x[t]][] reproduces the error message, hence SetDelayed is responsible for the non reproduction of the error message and more precisely it must be caching something.

Same problem with nr:=HoldForm@Evaluate@Transpose@{{1},{1,2}}.

However, no problem with nr:=HoldForm@Evaluate[1/0] and nr:=HoldForm@Evaluate[n:=n+1;n].

What is going on? How to always obtain an error message at each evaluation?

$\endgroup$
1
  • $\begingroup$ This is cheating but produces an error every time: nr := ToExpression["Function[t,x@t][]"] $\endgroup$
    – Roman
    Feb 12, 2022 at 18:27

2 Answers 2

7
$\begingroup$

We can cause the message to appear every time by calling Update[] in the body of the definition:

ClearAll[nr]
nr := (Update[]; HoldForm@Evaluate@Function[t, x@t][])

example showing that the message occurs every call


Details

It would appear that this is one of those "special circumstances" referred to in the documentation of Update:

Update manipulates internal optimization features of the Wolfram Language. It should not need to be called except under special circumstances that rarely occur in practice.

In support of this conjecture, a call to Update will cause the message to recur:

use of Update[] to clear an internal cache

Interestingly, the internal cache does not appear to be associated with the symbol nr. Rather, it is associated with Function:

updating nr does not clear the cache

(note: Unevaluated[nr] is necessary because Update does not hold its arguments and nr has an own-value.)

We can reproduce this behaviour even in the absence of the special evaluation rules that apply to HoldForm, Evaluated and Function:

simpler example

$\endgroup$
1
  • $\begingroup$ I did not know about Update, great trick. $\endgroup$ Feb 14, 2022 at 15:38
2
$\begingroup$

This behavior can be obtained with any defined function, when the right hand side of its definition is not used. A minimal example:

ClearAll@function;
function::fpct:="function error"
function[]/;Message[function::fpct]:=whatever

then try nr:=HoldForm@Evaluate@function[];nr;nr (only one error message).

It can also be forced on any function by caching like this: nr:=(nr=HoldForm@Evaluate[1/0]);nr;nr (only one error message) as opposed to nr:=HoldForm@Evaluate[1/0];nr;nr (two error messages).

To always obtain an error message, one could try something like:

ClearAll@function
function::fpct:="function error"
function[]/;(Message[function::fpct];True):=function[]

now with nr:=HoldForm@Evaluate@function[], nr produces at least one error message at each evaluation.

Actually, the first evaluation of nr produces TWO error messages.

Independently of nr, the first evaluation of function[] produces two errors messages but the reevaluation produces only one error message.

Double error message at first evaluation can be avoided like this:

ClearAll@function
function::fpct:="function error"
firstRun=True;
function[]/;(If[Not@firstRun,Message[function::fpct],firstRun=False];True):=function[]

but this method may lead to further problems, related to the variable firstRun or the redefinition of Wolfram language proper functions like Function, Transpose.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.