Group Abstract Group Abstract

Message Boards Message Boards

1
|
3.2K Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Why is ForAll HoldAll?

Posted 9 years ago

What is the reasoning behind making ForAll hold its arguments? I find this surprising and it causes behaviour that I found confusing. There are no other functions that I am aware of that deal with symbolic computation and hold their arguments.

The behaviour is documented:

The value of x in ForAll[x,expr] is taken to be localized, as in Block.

Here are some examples which I found confusing before realizing the HoldAll property:

In[94]:= Clear[x, z]

In[95]:= ForAll[x, x > -1, Abs[x] == x]
Out[95]= ForAll[x, x > -1, Abs[x] == x]

In[96]:= z = x
Out[96]= x

In[97]:= ForAll[z, x > -1, Abs[x] == x]
Out[97]= x > -1 \[Implies] Abs[x] == x

In[98]:= x = 1
Out[98]= 1

In[99]:= ForAll[x, x > -1, Abs[x] == x]
Out[99]= ForAll[x, x > -1, Abs[x] == x]

In[100]:= ForAll[z, x > -1, Abs[x] == x]
Out[100]= True

The two confusing points are that:

  1. Even with z=x, ForAll[z, ...] behaved differently from ForAll[x, ...]. Of course this is because of the HoldAll. This bit me when I did something like ForAll[Variables[expr], expr > 0].

  2. Now that we know that ForAll is HoldAll, we may assume that it is safe to use it with variables that have a value. Not so, because ForAll may evaluate to Implies, which is not HoldAll, like the majority of symbolic processing functions.

So is there anything special about ForAll (or Exists) that make it a good idea to have the HoldAll attribute, and wouldn't apply to functions such as Reduce, Solve, Integrate, etc.?

POSTED BY: Szabolcs Horvát
2 Replies

These are also HoldAll:

Sum
Product
And (Or,Nor,Nand)
Exists

For And (Or,Nor,Nand) this is because of lazy evaluation purposes. I'm not sure why ForAll has it unfortunately. From your example it is indeed sometimes a bit 'confusing' (for lack of a better word). I'm curious...

POSTED BY: Sander Huisman

That's right, but these are neither purely mathematical in nature (they are programming constructs), nor strictly symbolic.

And and friends must be HoldAll so they can short circuit. Most languages have a short circuiting and, so I'd consider this a critical feature that allows for simpler, shorter code.

Sum and Product are hybrids between mathematical/symbolic functions and programming constructs. Sum[1/k^2, {k, 1, Infinity}] is clearly a symbolic use, but these are also used as programming constructs equivalent to Table, but summing/multiplying instead of producing a list. Programming constructs must be HoldAll so that code like this can work: Sum[With[{x = k*k}, x*x], {k, 1, 10}]. Personally I am not too fond of this mixing of symbolic/mathematical and programming functionality. I avoid programming uses of Sum because I am never fully confident that symbolic processing won't kick and and won't slow down my program (though I don't recall it ever happening in practice).

ForAll is most definitely not a programming construct. It is used to represent a mathematical statement.

I did mention Exists as the peer of ForAll, so my question applies to both.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard